blob: 471b2c09095cec3d726e18b4c8d1bf44e8378a0b [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),
Emilian Peev71c73a22017-03-21 16:35:51 +000077 mListener(NULL),
78 mVendorTagId(CAMERA_METADATA_INVALID_VENDOR_ID)
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080079{
80 ATRACE_CALL();
81 camera3_callback_ops::notify = &sNotify;
82 camera3_callback_ops::process_capture_result = &sProcessCaptureResult;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -080083 ALOGV("%s: Created device for camera %s", __FUNCTION__, mId.string());
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080084}
85
86Camera3Device::~Camera3Device()
87{
88 ATRACE_CALL();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -080089 ALOGV("%s: Tearing down for camera id %s", __FUNCTION__, mId.string());
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080090 disconnect();
91}
92
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -080093const String8& Camera3Device::getId() const {
Igor Murashkin71381052013-03-04 14:53:08 -080094 return mId;
95}
96
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -080097status_t Camera3Device::initialize(sp<CameraProviderManager> manager) {
98 ATRACE_CALL();
99 Mutex::Autolock il(mInterfaceLock);
100 Mutex::Autolock l(mLock);
101
102 ALOGV("%s: Initializing HIDL device for camera %s", __FUNCTION__, mId.string());
103 if (mStatus != STATUS_UNINITIALIZED) {
104 CLOGE("Already initialized!");
105 return INVALID_OPERATION;
106 }
107 if (manager == nullptr) return INVALID_OPERATION;
108
109 sp<ICameraDeviceSession> session;
110 ATRACE_BEGIN("CameraHal::openSession");
Steven Moreland5ff9c912017-03-09 23:13:00 -0800111 status_t res = manager->openSession(mId.string(), this,
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800112 /*out*/ &session);
113 ATRACE_END();
114 if (res != OK) {
115 SET_ERR_L("Could not open camera session: %s (%d)", strerror(-res), res);
116 return res;
117 }
118
Steven Moreland5ff9c912017-03-09 23:13:00 -0800119 res = manager->getCameraCharacteristics(mId.string(), &mDeviceInfo);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800120 if (res != OK) {
121 SET_ERR_L("Could not retrive camera characteristics: %s (%d)", strerror(-res), res);
122 session->close();
123 return res;
124 }
Yin-Chia Yeh52778d42016-12-22 18:20:43 -0800125
Yifan Hongf79b5542017-04-11 14:44:25 -0700126 std::shared_ptr<RequestMetadataQueue> queue;
Yifan Honga640c5a2017-04-12 16:30:31 -0700127 auto requestQueueRet = session->getCaptureRequestMetadataQueue(
128 [&queue](const auto& descriptor) {
129 queue = std::make_shared<RequestMetadataQueue>(descriptor);
130 if (!queue->isValid() || queue->availableToWrite() <= 0) {
131 ALOGE("HAL returns empty request metadata fmq, not use it");
132 queue = nullptr;
133 // don't use the queue onwards.
134 }
135 });
136 if (!requestQueueRet.isOk()) {
137 ALOGE("Transaction error when getting request metadata fmq: %s, not use it",
138 requestQueueRet.description().c_str());
Eino-Ville Talvalac5cbb872017-04-27 12:48:33 -0700139 return DEAD_OBJECT;
Yifan Hongf79b5542017-04-11 14:44:25 -0700140 }
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -0700141
142 std::unique_ptr<ResultMetadataQueue>& resQueue = mResultMetadataQueue;
Yifan Honga640c5a2017-04-12 16:30:31 -0700143 auto resultQueueRet = session->getCaptureResultMetadataQueue(
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -0700144 [&resQueue](const auto& descriptor) {
145 resQueue = std::make_unique<ResultMetadataQueue>(descriptor);
146 if (!resQueue->isValid() || resQueue->availableToWrite() <= 0) {
Yifan Honga640c5a2017-04-12 16:30:31 -0700147 ALOGE("HAL returns empty result metadata fmq, not use it");
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -0700148 resQueue = nullptr;
149 // Don't use the resQueue onwards.
Yifan Honga640c5a2017-04-12 16:30:31 -0700150 }
151 });
152 if (!resultQueueRet.isOk()) {
153 ALOGE("Transaction error when getting result metadata queue from camera session: %s",
154 resultQueueRet.description().c_str());
Eino-Ville Talvalac5cbb872017-04-27 12:48:33 -0700155 return DEAD_OBJECT;
Yifan Honga640c5a2017-04-12 16:30:31 -0700156 }
Yifan Hongf79b5542017-04-11 14:44:25 -0700157
Yin-Chia Yehdb1e8642017-07-14 15:19:30 -0700158 mInterface = new HalInterface(session, queue);
Emilian Peev71c73a22017-03-21 16:35:51 +0000159 std::string providerType;
160 mVendorTagId = manager->getProviderTagIdLocked(mId.string());
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800161
162 return initializeCommonLocked();
163}
164
165status_t Camera3Device::initializeCommonLocked() {
166
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700167 /** Start up status tracker thread */
168 mStatusTracker = new StatusTracker(this);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800169 status_t res = mStatusTracker->run(String8::format("C3Dev-%s-Status", mId.string()).string());
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700170 if (res != OK) {
171 SET_ERR_L("Unable to start status tracking thread: %s (%d)",
172 strerror(-res), res);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800173 mInterface->close();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700174 mStatusTracker.clear();
175 return res;
176 }
177
Eino-Ville Talvala24b366e2016-07-21 12:53:07 -0700178 /** Register in-flight map to the status tracker */
179 mInFlightStatusId = mStatusTracker->addComponent();
180
Zhijun He125684a2015-12-26 15:07:30 -0800181 /** Create buffer manager */
182 mBufferManager = new Camera3BufferManager();
183
Emilian Peev71c73a22017-03-21 16:35:51 +0000184 mTagMonitor.initialize(mVendorTagId);
185
Chien-Yu Chenab5135b2015-06-30 11:20:58 -0700186 /** Start up request queue thread */
Yin-Chia Yehdb1e8642017-07-14 15:19:30 -0700187 mRequestThread = new RequestThread(this, mStatusTracker, mInterface);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800188 res = mRequestThread->run(String8::format("C3Dev-%s-ReqQueue", mId.string()).string());
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800189 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700190 SET_ERR_L("Unable to start request queue thread: %s (%d)",
191 strerror(-res), res);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800192 mInterface->close();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800193 mRequestThread.clear();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800194 return res;
195 }
196
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700197 mPreparerThread = new PreparerThread();
198
Ruben Brunk183f0562015-08-12 12:55:02 -0700199 internalUpdateStatusLocked(STATUS_UNCONFIGURED);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800200 mNextStreamId = 0;
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -0700201 mDummyStreamId = NO_STREAM;
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -0700202 mNeedConfig = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700203 mPauseStateNotify = false;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800204
Shuzhen Wangc28dccc2016-02-11 23:48:46 -0800205 // Measure the clock domain offset between camera and video/hw_composer
206 camera_metadata_entry timestampSource =
207 mDeviceInfo.find(ANDROID_SENSOR_INFO_TIMESTAMP_SOURCE);
208 if (timestampSource.count > 0 && timestampSource.data.u8[0] ==
209 ANDROID_SENSOR_INFO_TIMESTAMP_SOURCE_REALTIME) {
210 mTimestampOffset = getMonoToBoottimeOffset();
211 }
212
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -0700213 // Will the HAL be sending in early partial result metadata?
Emilian Peev08dd2452017-04-06 16:55:14 +0100214 camera_metadata_entry partialResultsCount =
215 mDeviceInfo.find(ANDROID_REQUEST_PARTIAL_RESULT_COUNT);
216 if (partialResultsCount.count > 0) {
217 mNumPartialResults = partialResultsCount.data.i32[0];
218 mUsePartialResult = (mNumPartialResults > 1);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -0700219 }
220
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700221 camera_metadata_entry configs =
222 mDeviceInfo.find(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS);
223 for (uint32_t i = 0; i < configs.count; i += 4) {
224 if (configs.data.i32[i] == HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED &&
225 configs.data.i32[i + 3] ==
226 ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_INPUT) {
227 mSupportedOpaqueInputSizes.add(Size(configs.data.i32[i + 1],
228 configs.data.i32[i + 2]));
229 }
230 }
231
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800232 return OK;
233}
234
235status_t Camera3Device::disconnect() {
236 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700237 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800238
Yin-Chia Yehe1c80632016-08-08 14:48:05 -0700239 ALOGI("%s: E", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800240
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700241 status_t res = OK;
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -0700242 std::vector<wp<Camera3StreamInterface>> streams;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700243 {
244 Mutex::Autolock l(mLock);
245 if (mStatus == STATUS_UNINITIALIZED) return res;
246
247 if (mStatus == STATUS_ACTIVE ||
248 (mStatus == STATUS_ERROR && mRequestThread != NULL)) {
249 res = mRequestThread->clearRepeatingRequests();
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700250 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700251 SET_ERR_L("Can't stop streaming");
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700252 // Continue to close device even in case of error
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700253 } else {
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -0700254 nsecs_t maxExpectedDuration = getExpectedInFlightDurationLocked();
255 res = waitUntilStateThenRelock(/*active*/ false, maxExpectedDuration);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700256 if (res != OK) {
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -0700257 SET_ERR_L("Timeout waiting for HAL to drain (% " PRIi64 " ns)",
258 maxExpectedDuration);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700259 // Continue to close device even in case of error
260 }
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700261 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800262 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800263
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700264 if (mStatus == STATUS_ERROR) {
265 CLOGE("Shutting down in an error state");
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700266 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700267
268 if (mStatusTracker != NULL) {
269 mStatusTracker->requestExit();
270 }
271
272 if (mRequestThread != NULL) {
273 mRequestThread->requestExit();
274 }
275
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -0700276 streams.reserve(mOutputStreams.size() + (mInputStream != nullptr ? 1 : 0));
277 for (size_t i = 0; i < mOutputStreams.size(); i++) {
278 streams.push_back(mOutputStreams[i]);
279 }
280 if (mInputStream != nullptr) {
281 streams.push_back(mInputStream);
282 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700283 }
284
285 // Joining done without holding mLock, otherwise deadlocks may ensue
286 // as the threads try to access parent state
287 if (mRequestThread != NULL && mStatus != STATUS_ERROR) {
288 // HAL may be in a bad state, so waiting for request thread
289 // (which may be stuck in the HAL processCaptureRequest call)
290 // could be dangerous.
291 mRequestThread->join();
292 }
293
294 if (mStatusTracker != NULL) {
295 mStatusTracker->join();
296 }
297
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800298 HalInterface* interface;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700299 {
300 Mutex::Autolock l(mLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800301 mRequestThread.clear();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700302 mStatusTracker.clear();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800303 interface = mInterface.get();
Eino-Ville Talvalaefff1c42015-08-28 16:27:27 -0700304 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800305
Eino-Ville Talvalaefff1c42015-08-28 16:27:27 -0700306 // Call close without internal mutex held, as the HAL close may need to
307 // wait on assorted callbacks,etc, to complete before it can return.
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800308 interface->close();
Eino-Ville Talvalaefff1c42015-08-28 16:27:27 -0700309
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -0700310 flushInflightRequests();
311
Eino-Ville Talvalaefff1c42015-08-28 16:27:27 -0700312 {
313 Mutex::Autolock l(mLock);
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -0700314 mExpectedInflightDuration = 0;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800315 mInterface->clear();
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -0700316 mOutputStreams.clear();
317 mInputStream.clear();
Yin-Chia Yeh5090c732017-07-20 16:05:29 -0700318 mDeletedStreams.clear();
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -0700319 mBufferManager.clear();
Ruben Brunk183f0562015-08-12 12:55:02 -0700320 internalUpdateStatusLocked(STATUS_UNINITIALIZED);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700321 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800322
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -0700323 for (auto& weakStream : streams) {
324 sp<Camera3StreamInterface> stream = weakStream.promote();
325 if (stream != nullptr) {
326 ALOGE("%s: Stream %d leaked! strong reference (%d)!",
327 __FUNCTION__, stream->getId(), stream->getStrongCount() - 1);
328 }
329 }
330
Yin-Chia Yehe1c80632016-08-08 14:48:05 -0700331 ALOGI("%s: X", __FUNCTION__);
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700332 return res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800333}
334
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700335// For dumping/debugging only -
336// try to acquire a lock a few times, eventually give up to proceed with
337// debug/dump operations
338bool Camera3Device::tryLockSpinRightRound(Mutex& lock) {
339 bool gotLock = false;
340 for (size_t i = 0; i < kDumpLockAttempts; ++i) {
341 if (lock.tryLock() == NO_ERROR) {
342 gotLock = true;
343 break;
344 } else {
345 usleep(kDumpSleepDuration);
346 }
347 }
348 return gotLock;
349}
350
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700351Camera3Device::Size Camera3Device::getMaxJpegResolution() const {
352 int32_t maxJpegWidth = 0, maxJpegHeight = 0;
Emilian Peev08dd2452017-04-06 16:55:14 +0100353 const int STREAM_CONFIGURATION_SIZE = 4;
354 const int STREAM_FORMAT_OFFSET = 0;
355 const int STREAM_WIDTH_OFFSET = 1;
356 const int STREAM_HEIGHT_OFFSET = 2;
357 const int STREAM_IS_INPUT_OFFSET = 3;
358 camera_metadata_ro_entry_t availableStreamConfigs =
359 mDeviceInfo.find(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS);
360 if (availableStreamConfigs.count == 0 ||
361 availableStreamConfigs.count % STREAM_CONFIGURATION_SIZE != 0) {
362 return Size(0, 0);
363 }
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700364
Emilian Peev08dd2452017-04-06 16:55:14 +0100365 // Get max jpeg size (area-wise).
366 for (size_t i=0; i < availableStreamConfigs.count; i+= STREAM_CONFIGURATION_SIZE) {
367 int32_t format = availableStreamConfigs.data.i32[i + STREAM_FORMAT_OFFSET];
368 int32_t width = availableStreamConfigs.data.i32[i + STREAM_WIDTH_OFFSET];
369 int32_t height = availableStreamConfigs.data.i32[i + STREAM_HEIGHT_OFFSET];
370 int32_t isInput = availableStreamConfigs.data.i32[i + STREAM_IS_INPUT_OFFSET];
371 if (isInput == ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_OUTPUT
372 && format == HAL_PIXEL_FORMAT_BLOB &&
373 (width * height > maxJpegWidth * maxJpegHeight)) {
374 maxJpegWidth = width;
375 maxJpegHeight = height;
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700376 }
377 }
Emilian Peev08dd2452017-04-06 16:55:14 +0100378
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700379 return Size(maxJpegWidth, maxJpegHeight);
380}
381
Shuzhen Wangc28dccc2016-02-11 23:48:46 -0800382nsecs_t Camera3Device::getMonoToBoottimeOffset() {
383 // try three times to get the clock offset, choose the one
384 // with the minimum gap in measurements.
385 const int tries = 3;
386 nsecs_t bestGap, measured;
387 for (int i = 0; i < tries; ++i) {
388 const nsecs_t tmono = systemTime(SYSTEM_TIME_MONOTONIC);
389 const nsecs_t tbase = systemTime(SYSTEM_TIME_BOOTTIME);
390 const nsecs_t tmono2 = systemTime(SYSTEM_TIME_MONOTONIC);
391 const nsecs_t gap = tmono2 - tmono;
392 if (i == 0 || gap < bestGap) {
393 bestGap = gap;
394 measured = tbase - ((tmono + tmono2) >> 1);
395 }
396 }
397 return measured;
398}
399
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800400hardware::graphics::common::V1_0::PixelFormat Camera3Device::mapToPixelFormat(
401 int frameworkFormat) {
402 return (hardware::graphics::common::V1_0::PixelFormat) frameworkFormat;
403}
404
405DataspaceFlags Camera3Device::mapToHidlDataspace(
406 android_dataspace dataSpace) {
407 return dataSpace;
408}
409
Chia-I Wu67a0c0e2017-04-06 13:37:01 -0700410BufferUsageFlags Camera3Device::mapToConsumerUsage(
Yin-Chia Yeh47cf8e62017-04-04 13:00:03 -0700411 uint32_t usage) {
412 return usage;
413}
414
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800415StreamRotation Camera3Device::mapToStreamRotation(camera3_stream_rotation_t rotation) {
416 switch (rotation) {
417 case CAMERA3_STREAM_ROTATION_0:
418 return StreamRotation::ROTATION_0;
419 case CAMERA3_STREAM_ROTATION_90:
420 return StreamRotation::ROTATION_90;
421 case CAMERA3_STREAM_ROTATION_180:
422 return StreamRotation::ROTATION_180;
423 case CAMERA3_STREAM_ROTATION_270:
424 return StreamRotation::ROTATION_270;
425 }
426 ALOGE("%s: Unknown stream rotation %d", __FUNCTION__, rotation);
427 return StreamRotation::ROTATION_0;
428}
429
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -0800430status_t Camera3Device::mapToStreamConfigurationMode(
431 camera3_stream_configuration_mode_t operationMode, StreamConfigurationMode *mode) {
432 if (mode == nullptr) return BAD_VALUE;
433 if (operationMode < CAMERA3_VENDOR_STREAM_CONFIGURATION_MODE_START) {
434 switch(operationMode) {
435 case CAMERA3_STREAM_CONFIGURATION_NORMAL_MODE:
436 *mode = StreamConfigurationMode::NORMAL_MODE;
437 break;
438 case CAMERA3_STREAM_CONFIGURATION_CONSTRAINED_HIGH_SPEED_MODE:
439 *mode = StreamConfigurationMode::CONSTRAINED_HIGH_SPEED_MODE;
440 break;
441 default:
442 ALOGE("%s: Unknown stream configuration mode %d", __FUNCTION__, operationMode);
443 return BAD_VALUE;
444 }
445 } else {
446 *mode = static_cast<StreamConfigurationMode>(operationMode);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800447 }
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -0800448 return OK;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800449}
450
451camera3_buffer_status_t Camera3Device::mapHidlBufferStatus(BufferStatus status) {
452 switch (status) {
453 case BufferStatus::OK: return CAMERA3_BUFFER_STATUS_OK;
454 case BufferStatus::ERROR: return CAMERA3_BUFFER_STATUS_ERROR;
455 }
456 return CAMERA3_BUFFER_STATUS_ERROR;
457}
458
459int Camera3Device::mapToFrameworkFormat(
460 hardware::graphics::common::V1_0::PixelFormat pixelFormat) {
461 return static_cast<uint32_t>(pixelFormat);
462}
463
Yin-Chia Yeh47cf8e62017-04-04 13:00:03 -0700464uint32_t Camera3Device::mapConsumerToFrameworkUsage(
Chia-I Wu67a0c0e2017-04-06 13:37:01 -0700465 BufferUsageFlags usage) {
Yin-Chia Yeh47cf8e62017-04-04 13:00:03 -0700466 return usage;
467}
468
469uint32_t Camera3Device::mapProducerToFrameworkUsage(
Chia-I Wu67a0c0e2017-04-06 13:37:01 -0700470 BufferUsageFlags usage) {
Yin-Chia Yeh47cf8e62017-04-04 13:00:03 -0700471 return usage;
472}
473
Zhijun Hef7da0962014-04-24 13:27:56 -0700474ssize_t Camera3Device::getJpegBufferSize(uint32_t width, uint32_t height) const {
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700475 // Get max jpeg size (area-wise).
476 Size maxJpegResolution = getMaxJpegResolution();
477 if (maxJpegResolution.width == 0) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800478 ALOGE("%s: Camera %s: Can't find valid available jpeg sizes in static metadata!",
479 __FUNCTION__, mId.string());
Zhijun Hef7da0962014-04-24 13:27:56 -0700480 return BAD_VALUE;
481 }
482
Zhijun Hef7da0962014-04-24 13:27:56 -0700483 // Get max jpeg buffer size
484 ssize_t maxJpegBufferSize = 0;
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700485 camera_metadata_ro_entry jpegBufMaxSize = mDeviceInfo.find(ANDROID_JPEG_MAX_SIZE);
486 if (jpegBufMaxSize.count == 0) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800487 ALOGE("%s: Camera %s: Can't find maximum JPEG size in static metadata!", __FUNCTION__,
488 mId.string());
Zhijun Hef7da0962014-04-24 13:27:56 -0700489 return BAD_VALUE;
490 }
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700491 maxJpegBufferSize = jpegBufMaxSize.data.i32[0];
Yin-Chia Yeh0c4e56d2015-01-09 15:21:27 -0800492 assert(kMinJpegBufferSize < maxJpegBufferSize);
Zhijun Hef7da0962014-04-24 13:27:56 -0700493
494 // Calculate final jpeg buffer size for the given resolution.
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700495 float scaleFactor = ((float) (width * height)) /
496 (maxJpegResolution.width * maxJpegResolution.height);
Yin-Chia Yeh0c4e56d2015-01-09 15:21:27 -0800497 ssize_t jpegBufferSize = scaleFactor * (maxJpegBufferSize - kMinJpegBufferSize) +
498 kMinJpegBufferSize;
Zhijun Hef7da0962014-04-24 13:27:56 -0700499 if (jpegBufferSize > maxJpegBufferSize) {
500 jpegBufferSize = maxJpegBufferSize;
Zhijun Hef7da0962014-04-24 13:27:56 -0700501 }
502
503 return jpegBufferSize;
504}
505
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700506ssize_t Camera3Device::getPointCloudBufferSize() const {
507 const int FLOATS_PER_POINT=4;
508 camera_metadata_ro_entry maxPointCount = mDeviceInfo.find(ANDROID_DEPTH_MAX_DEPTH_SAMPLES);
509 if (maxPointCount.count == 0) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800510 ALOGE("%s: Camera %s: Can't find maximum depth point cloud size in static metadata!",
511 __FUNCTION__, mId.string());
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700512 return BAD_VALUE;
513 }
514 ssize_t maxBytesForPointCloud = sizeof(android_depth_points) +
515 maxPointCount.data.i32[0] * sizeof(float) * FLOATS_PER_POINT;
516 return maxBytesForPointCloud;
517}
518
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800519ssize_t Camera3Device::getRawOpaqueBufferSize(int32_t width, int32_t height) const {
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800520 const int PER_CONFIGURATION_SIZE = 3;
521 const int WIDTH_OFFSET = 0;
522 const int HEIGHT_OFFSET = 1;
523 const int SIZE_OFFSET = 2;
524 camera_metadata_ro_entry rawOpaqueSizes =
525 mDeviceInfo.find(ANDROID_SENSOR_OPAQUE_RAW_SIZE);
Aurimas Liutikasbc57b122016-02-16 09:59:16 -0800526 size_t count = rawOpaqueSizes.count;
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800527 if (count == 0 || (count % PER_CONFIGURATION_SIZE)) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800528 ALOGE("%s: Camera %s: bad opaque RAW size static metadata length(%zu)!",
529 __FUNCTION__, mId.string(), count);
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800530 return BAD_VALUE;
531 }
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700532
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800533 for (size_t i = 0; i < count; i += PER_CONFIGURATION_SIZE) {
534 if (width == rawOpaqueSizes.data.i32[i + WIDTH_OFFSET] &&
535 height == rawOpaqueSizes.data.i32[i + HEIGHT_OFFSET]) {
536 return rawOpaqueSizes.data.i32[i + SIZE_OFFSET];
537 }
538 }
539
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800540 ALOGE("%s: Camera %s: cannot find size for %dx%d opaque RAW image!",
541 __FUNCTION__, mId.string(), width, height);
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800542 return BAD_VALUE;
543}
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700544
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800545status_t Camera3Device::dump(int fd, const Vector<String16> &args) {
546 ATRACE_CALL();
547 (void)args;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700548
549 // Try to lock, but continue in case of failure (to avoid blocking in
550 // deadlocks)
551 bool gotInterfaceLock = tryLockSpinRightRound(mInterfaceLock);
552 bool gotLock = tryLockSpinRightRound(mLock);
553
554 ALOGW_IF(!gotInterfaceLock,
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800555 "Camera %s: %s: Unable to lock interface lock, proceeding anyway",
556 mId.string(), __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700557 ALOGW_IF(!gotLock,
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800558 "Camera %s: %s: Unable to lock main lock, proceeding anyway",
559 mId.string(), __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700560
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800561 bool dumpTemplates = false;
Eino-Ville Talvala4d453832016-07-15 11:56:53 -0700562
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800563 String16 templatesOption("-t");
Eino-Ville Talvala4d453832016-07-15 11:56:53 -0700564 String16 monitorOption("-m");
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800565 int n = args.size();
566 for (int i = 0; i < n; i++) {
567 if (args[i] == templatesOption) {
568 dumpTemplates = true;
569 }
Eino-Ville Talvala4d453832016-07-15 11:56:53 -0700570 if (args[i] == monitorOption) {
571 if (i + 1 < n) {
572 String8 monitorTags = String8(args[i + 1]);
573 if (monitorTags == "off") {
574 mTagMonitor.disableMonitoring();
575 } else {
576 mTagMonitor.parseTagsToMonitor(monitorTags);
577 }
578 } else {
579 mTagMonitor.disableMonitoring();
580 }
581 }
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800582 }
583
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800584 String8 lines;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800585
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800586 const char *status =
587 mStatus == STATUS_ERROR ? "ERROR" :
588 mStatus == STATUS_UNINITIALIZED ? "UNINITIALIZED" :
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700589 mStatus == STATUS_UNCONFIGURED ? "UNCONFIGURED" :
590 mStatus == STATUS_CONFIGURED ? "CONFIGURED" :
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800591 mStatus == STATUS_ACTIVE ? "ACTIVE" :
592 "Unknown";
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700593
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800594 lines.appendFormat(" Device status: %s\n", status);
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700595 if (mStatus == STATUS_ERROR) {
596 lines.appendFormat(" Error cause: %s\n", mErrorCause.string());
597 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800598 lines.appendFormat(" Stream configuration:\n");
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -0800599 const char *mode =
600 mOperatingMode == static_cast<int>(StreamConfigurationMode::NORMAL_MODE) ? "NORMAL" :
601 mOperatingMode == static_cast<int>(
602 StreamConfigurationMode::CONSTRAINED_HIGH_SPEED_MODE) ? "CONSTRAINED_HIGH_SPEED" :
603 "CUSTOM";
604 lines.appendFormat(" Operation mode: %s (%d) \n", mode, mOperatingMode);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800605
606 if (mInputStream != NULL) {
607 write(fd, lines.string(), lines.size());
608 mInputStream->dump(fd, args);
609 } else {
610 lines.appendFormat(" No input stream.\n");
611 write(fd, lines.string(), lines.size());
612 }
613 for (size_t i = 0; i < mOutputStreams.size(); i++) {
614 mOutputStreams[i]->dump(fd,args);
615 }
616
Zhijun He431503c2016-03-07 17:30:16 -0800617 if (mBufferManager != NULL) {
618 lines = String8(" Camera3 Buffer Manager:\n");
619 write(fd, lines.string(), lines.size());
620 mBufferManager->dump(fd, args);
621 }
Zhijun He125684a2015-12-26 15:07:30 -0800622
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700623 lines = String8(" In-flight requests:\n");
624 if (mInFlightMap.size() == 0) {
625 lines.append(" None\n");
626 } else {
627 for (size_t i = 0; i < mInFlightMap.size(); i++) {
628 InFlightRequest r = mInFlightMap.valueAt(i);
Colin Crosse5729fa2014-03-21 15:04:25 -0700629 lines.appendFormat(" Frame %d | Timestamp: %" PRId64 ", metadata"
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700630 " arrived: %s, buffers left: %d\n", mInFlightMap.keyAt(i),
Chien-Yu Chen43e69a62014-11-25 16:38:33 -0800631 r.shutterTimestamp, r.haveResultMetadata ? "true" : "false",
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700632 r.numBuffersLeft);
633 }
634 }
635 write(fd, lines.string(), lines.size());
636
Shuzhen Wang686f6442017-06-20 16:16:04 -0700637 if (mRequestThread != NULL) {
638 mRequestThread->dumpCaptureRequestLatency(fd,
639 " ProcessCaptureRequest latency histogram:");
640 }
641
Igor Murashkin1e479c02013-09-06 16:55:14 -0700642 {
643 lines = String8(" Last request sent:\n");
644 write(fd, lines.string(), lines.size());
645
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700646 CameraMetadata lastRequest = getLatestRequestLocked();
Igor Murashkin1e479c02013-09-06 16:55:14 -0700647 lastRequest.dump(fd, /*verbosity*/2, /*indentation*/6);
648 }
649
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800650 if (dumpTemplates) {
651 const char *templateNames[] = {
652 "TEMPLATE_PREVIEW",
653 "TEMPLATE_STILL_CAPTURE",
654 "TEMPLATE_VIDEO_RECORD",
655 "TEMPLATE_VIDEO_SNAPSHOT",
656 "TEMPLATE_ZERO_SHUTTER_LAG",
657 "TEMPLATE_MANUAL"
658 };
659
660 for (int i = 1; i < CAMERA3_TEMPLATE_COUNT; i++) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800661 camera_metadata_t *templateRequest = nullptr;
662 mInterface->constructDefaultRequestSettings(
663 (camera3_request_template_t) i, &templateRequest);
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800664 lines = String8::format(" HAL Request %s:\n", templateNames[i-1]);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800665 if (templateRequest == nullptr) {
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800666 lines.append(" Not supported\n");
667 write(fd, lines.string(), lines.size());
668 } else {
669 write(fd, lines.string(), lines.size());
670 dump_indented_camera_metadata(templateRequest,
671 fd, /*verbosity*/2, /*indentation*/8);
672 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800673 free_camera_metadata(templateRequest);
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800674 }
675 }
676
Eino-Ville Talvala4d453832016-07-15 11:56:53 -0700677 mTagMonitor.dumpMonitoredMetadata(fd);
678
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800679 if (mInterface->valid()) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -0800680 lines = String8(" HAL device dump:\n");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800681 write(fd, lines.string(), lines.size());
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800682 mInterface->dump(fd);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800683 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800684
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700685 if (gotLock) mLock.unlock();
686 if (gotInterfaceLock) mInterfaceLock.unlock();
687
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800688 return OK;
689}
690
691const CameraMetadata& Camera3Device::info() const {
692 ALOGVV("%s: E", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800693 if (CC_UNLIKELY(mStatus == STATUS_UNINITIALIZED ||
694 mStatus == STATUS_ERROR)) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700695 ALOGW("%s: Access to static info %s!", __FUNCTION__,
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800696 mStatus == STATUS_ERROR ?
697 "when in error state" : "before init");
698 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800699 return mDeviceInfo;
700}
701
Jianing Wei90e59c92014-03-12 18:29:36 -0700702status_t Camera3Device::checkStatusOkToCaptureLocked() {
703 switch (mStatus) {
704 case STATUS_ERROR:
705 CLOGE("Device has encountered a serious error");
706 return INVALID_OPERATION;
707 case STATUS_UNINITIALIZED:
708 CLOGE("Device not initialized");
709 return INVALID_OPERATION;
710 case STATUS_UNCONFIGURED:
711 case STATUS_CONFIGURED:
712 case STATUS_ACTIVE:
713 // OK
714 break;
715 default:
716 SET_ERR_L("Unexpected status: %d", mStatus);
717 return INVALID_OPERATION;
718 }
719 return OK;
720}
721
722status_t Camera3Device::convertMetadataListToRequestListLocked(
Shuzhen Wang0129d522016-10-30 22:43:41 -0700723 const List<const CameraMetadata> &metadataList,
724 const std::list<const SurfaceMap> &surfaceMaps,
725 bool repeating,
Shuzhen Wang9d066012016-09-30 11:30:20 -0700726 RequestList *requestList) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700727 if (requestList == NULL) {
728 CLOGE("requestList cannot be NULL.");
729 return BAD_VALUE;
730 }
731
Jianing Weicb0652e2014-03-12 18:29:36 -0700732 int32_t burstId = 0;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700733 List<const CameraMetadata>::const_iterator metadataIt = metadataList.begin();
734 std::list<const SurfaceMap>::const_iterator surfaceMapIt = surfaceMaps.begin();
735 for (; metadataIt != metadataList.end() && surfaceMapIt != surfaceMaps.end();
736 ++metadataIt, ++surfaceMapIt) {
737 sp<CaptureRequest> newRequest = setUpRequestLocked(*metadataIt, *surfaceMapIt);
Jianing Wei90e59c92014-03-12 18:29:36 -0700738 if (newRequest == 0) {
739 CLOGE("Can't create capture request");
740 return BAD_VALUE;
741 }
Jianing Weicb0652e2014-03-12 18:29:36 -0700742
Shuzhen Wang9d066012016-09-30 11:30:20 -0700743 newRequest->mRepeating = repeating;
744
Jianing Weicb0652e2014-03-12 18:29:36 -0700745 // Setup burst Id and request Id
746 newRequest->mResultExtras.burstId = burstId++;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700747 if (metadataIt->exists(ANDROID_REQUEST_ID)) {
748 if (metadataIt->find(ANDROID_REQUEST_ID).count == 0) {
Jianing Weicb0652e2014-03-12 18:29:36 -0700749 CLOGE("RequestID entry exists; but must not be empty in metadata");
750 return BAD_VALUE;
751 }
Shuzhen Wang0129d522016-10-30 22:43:41 -0700752 newRequest->mResultExtras.requestId = metadataIt->find(ANDROID_REQUEST_ID).data.i32[0];
Jianing Weicb0652e2014-03-12 18:29:36 -0700753 } else {
754 CLOGE("RequestID does not exist in metadata");
755 return BAD_VALUE;
756 }
757
Jianing Wei90e59c92014-03-12 18:29:36 -0700758 requestList->push_back(newRequest);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700759
760 ALOGV("%s: requestId = %" PRId32, __FUNCTION__, newRequest->mResultExtras.requestId);
Jianing Wei90e59c92014-03-12 18:29:36 -0700761 }
Shuzhen Wang0129d522016-10-30 22:43:41 -0700762 if (metadataIt != metadataList.end() || surfaceMapIt != surfaceMaps.end()) {
763 ALOGE("%s: metadataList and surfaceMaps are not the same size!", __FUNCTION__);
764 return BAD_VALUE;
765 }
Chien-Yu Chen85a64552015-08-28 15:46:12 -0700766
767 // Setup batch size if this is a high speed video recording request.
768 if (mIsConstrainedHighSpeedConfiguration && requestList->size() > 0) {
769 auto firstRequest = requestList->begin();
770 for (auto& outputStream : (*firstRequest)->mOutputStreams) {
771 if (outputStream->isVideoStream()) {
772 (*firstRequest)->mBatchSize = requestList->size();
773 break;
774 }
775 }
776 }
777
Jianing Wei90e59c92014-03-12 18:29:36 -0700778 return OK;
779}
780
Jianing Weicb0652e2014-03-12 18:29:36 -0700781status_t Camera3Device::capture(CameraMetadata &request, int64_t* /*lastFrameNumber*/) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800782 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800783
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700784 List<const CameraMetadata> requests;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700785 std::list<const SurfaceMap> surfaceMaps;
786 convertToRequestList(requests, surfaceMaps, request);
787
788 return captureList(requests, surfaceMaps, /*lastFrameNumber*/NULL);
789}
790
791void Camera3Device::convertToRequestList(List<const CameraMetadata>& requests,
792 std::list<const SurfaceMap>& surfaceMaps,
793 const CameraMetadata& request) {
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700794 requests.push_back(request);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700795
796 SurfaceMap surfaceMap;
797 camera_metadata_ro_entry streams = request.find(ANDROID_REQUEST_OUTPUT_STREAMS);
798 // With no surface list passed in, stream and surface will have 1-to-1
799 // mapping. So the surface index is 0 for each stream in the surfaceMap.
800 for (size_t i = 0; i < streams.count; i++) {
801 surfaceMap[streams.data.i32[i]].push_back(0);
802 }
803 surfaceMaps.push_back(surfaceMap);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800804}
805
Jianing Wei90e59c92014-03-12 18:29:36 -0700806status_t Camera3Device::submitRequestsHelper(
Shuzhen Wang0129d522016-10-30 22:43:41 -0700807 const List<const CameraMetadata> &requests,
808 const std::list<const SurfaceMap> &surfaceMaps,
809 bool repeating,
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700810 /*out*/
811 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700812 ATRACE_CALL();
813 Mutex::Autolock il(mInterfaceLock);
814 Mutex::Autolock l(mLock);
815
816 status_t res = checkStatusOkToCaptureLocked();
817 if (res != OK) {
818 // error logged by previous call
819 return res;
820 }
821
822 RequestList requestList;
823
Shuzhen Wang0129d522016-10-30 22:43:41 -0700824 res = convertMetadataListToRequestListLocked(requests, surfaceMaps,
825 repeating, /*out*/&requestList);
Jianing Wei90e59c92014-03-12 18:29:36 -0700826 if (res != OK) {
827 // error logged by previous call
828 return res;
829 }
830
831 if (repeating) {
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700832 res = mRequestThread->setRepeatingRequests(requestList, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700833 } else {
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700834 res = mRequestThread->queueRequestList(requestList, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700835 }
836
837 if (res == OK) {
838 waitUntilStateThenRelock(/*active*/true, kActiveTimeout);
839 if (res != OK) {
840 SET_ERR_L("Can't transition to active in %f seconds!",
841 kActiveTimeout/1e9);
842 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800843 ALOGV("Camera %s: Capture request %" PRId32 " enqueued", mId.string(),
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700844 (*(requestList.begin()))->mResultExtras.requestId);
Jianing Wei90e59c92014-03-12 18:29:36 -0700845 } else {
846 CLOGE("Cannot queue request. Impossible.");
847 return BAD_VALUE;
848 }
849
850 return res;
851}
852
Yifan Honga640c5a2017-04-12 16:30:31 -0700853// Only one processCaptureResult should be called at a time, so
854// the locks won't block. The locks are present here simply to enforce this.
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800855hardware::Return<void> Camera3Device::processCaptureResult(
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -0800856 const hardware::hidl_vec<
857 hardware::camera::device::V3_2::CaptureResult>& results) {
Yin-Chia Yeh657c1872017-07-18 18:09:57 -0700858 // Ideally we should grab mLock, but that can lead to deadlock, and
859 // it's not super important to get up to date value of mStatus for this
860 // warning print, hence skipping the lock here
861 if (mStatus == STATUS_ERROR) {
862 // Per API contract, HAL should act as closed after device error
863 // But mStatus can be set to error by framework as well, so just log
864 // a warning here.
865 ALOGW("%s: received capture result in error state.", __FUNCTION__);
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -0700866 }
Yifan Honga640c5a2017-04-12 16:30:31 -0700867
868 if (mProcessCaptureResultLock.tryLock() != OK) {
869 // This should never happen; it indicates a wrong client implementation
870 // that doesn't follow the contract. But, we can be tolerant here.
871 ALOGE("%s: callback overlapped! waiting 1s...",
872 __FUNCTION__);
873 if (mProcessCaptureResultLock.timedLock(1000000000 /* 1s */) != OK) {
874 ALOGE("%s: cannot acquire lock in 1s, dropping results",
875 __FUNCTION__);
876 // really don't know what to do, so bail out.
877 return hardware::Void();
878 }
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -0800879 }
Yifan Honga640c5a2017-04-12 16:30:31 -0700880 for (const auto& result : results) {
881 processOneCaptureResultLocked(result);
882 }
883 mProcessCaptureResultLock.unlock();
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -0800884 return hardware::Void();
885}
886
Yifan Honga640c5a2017-04-12 16:30:31 -0700887void Camera3Device::processOneCaptureResultLocked(
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -0800888 const hardware::camera::device::V3_2::CaptureResult& result) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800889 camera3_capture_result r;
890 status_t res;
891 r.frame_number = result.frameNumber;
Yifan Honga640c5a2017-04-12 16:30:31 -0700892
893 hardware::camera::device::V3_2::CameraMetadata resultMetadata;
894 if (result.fmqResultSize > 0) {
895 resultMetadata.resize(result.fmqResultSize);
896 if (mResultMetadataQueue == nullptr) {
897 return; // logged in initialize()
898 }
899 if (!mResultMetadataQueue->read(resultMetadata.data(), result.fmqResultSize)) {
900 ALOGE("%s: Frame %d: Cannot read camera metadata from fmq, size = %" PRIu64,
901 __FUNCTION__, result.frameNumber, result.fmqResultSize);
902 return;
903 }
904 } else {
905 resultMetadata.setToExternal(const_cast<uint8_t *>(result.result.data()),
906 result.result.size());
907 }
908
909 if (resultMetadata.size() != 0) {
910 r.result = reinterpret_cast<const camera_metadata_t*>(resultMetadata.data());
911 size_t expected_metadata_size = resultMetadata.size();
Yin-Chia Yeh52778d42016-12-22 18:20:43 -0800912 if ((res = validate_camera_metadata_structure(r.result, &expected_metadata_size)) != OK) {
913 ALOGE("%s: Frame %d: Invalid camera metadata received by camera service from HAL: %s (%d)",
914 __FUNCTION__, result.frameNumber, strerror(-res), res);
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -0800915 return;
Yin-Chia Yeh52778d42016-12-22 18:20:43 -0800916 }
917 } else {
918 r.result = nullptr;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800919 }
920
921 std::vector<camera3_stream_buffer_t> outputBuffers(result.outputBuffers.size());
922 std::vector<buffer_handle_t> outputBufferHandles(result.outputBuffers.size());
923 for (size_t i = 0; i < result.outputBuffers.size(); i++) {
924 auto& bDst = outputBuffers[i];
925 const StreamBuffer &bSrc = result.outputBuffers[i];
926
927 ssize_t idx = mOutputStreams.indexOfKey(bSrc.streamId);
Emilian Peevbe3d40c2017-03-27 13:03:10 +0100928 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800929 ALOGE("%s: Frame %d: Buffer %zu: Invalid output stream id %d",
930 __FUNCTION__, result.frameNumber, i, bSrc.streamId);
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -0800931 return;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800932 }
933 bDst.stream = mOutputStreams.valueAt(idx)->asHalStream();
934
935 buffer_handle_t *buffer;
Yin-Chia Yehf4650602017-01-10 13:13:39 -0800936 res = mInterface->popInflightBuffer(result.frameNumber, bSrc.streamId, &buffer);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800937 if (res != OK) {
938 ALOGE("%s: Frame %d: Buffer %zu: No in-flight buffer for stream %d",
939 __FUNCTION__, result.frameNumber, i, bSrc.streamId);
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -0800940 return;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800941 }
942 bDst.buffer = buffer;
943 bDst.status = mapHidlBufferStatus(bSrc.status);
944 bDst.acquire_fence = -1;
945 if (bSrc.releaseFence == nullptr) {
946 bDst.release_fence = -1;
947 } else if (bSrc.releaseFence->numFds == 1) {
948 bDst.release_fence = dup(bSrc.releaseFence->data[0]);
949 } else {
950 ALOGE("%s: Frame %d: Invalid release fence for buffer %zu, fd count is %d, not 1",
951 __FUNCTION__, result.frameNumber, i, bSrc.releaseFence->numFds);
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -0800952 return;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800953 }
954 }
955 r.num_output_buffers = outputBuffers.size();
956 r.output_buffers = outputBuffers.data();
957
958 camera3_stream_buffer_t inputBuffer;
Yin-Chia Yeh52778d42016-12-22 18:20:43 -0800959 if (result.inputBuffer.streamId == -1) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800960 r.input_buffer = nullptr;
961 } else {
962 if (mInputStream->getId() != result.inputBuffer.streamId) {
963 ALOGE("%s: Frame %d: Invalid input stream id %d", __FUNCTION__,
964 result.frameNumber, result.inputBuffer.streamId);
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -0800965 return;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800966 }
967 inputBuffer.stream = mInputStream->asHalStream();
968 buffer_handle_t *buffer;
969 res = mInterface->popInflightBuffer(result.frameNumber, result.inputBuffer.streamId,
970 &buffer);
971 if (res != OK) {
972 ALOGE("%s: Frame %d: Input buffer: No in-flight buffer for stream %d",
973 __FUNCTION__, result.frameNumber, result.inputBuffer.streamId);
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -0800974 return;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800975 }
976 inputBuffer.buffer = buffer;
977 inputBuffer.status = mapHidlBufferStatus(result.inputBuffer.status);
978 inputBuffer.acquire_fence = -1;
979 if (result.inputBuffer.releaseFence == nullptr) {
980 inputBuffer.release_fence = -1;
981 } else if (result.inputBuffer.releaseFence->numFds == 1) {
982 inputBuffer.release_fence = dup(result.inputBuffer.releaseFence->data[0]);
983 } else {
984 ALOGE("%s: Frame %d: Invalid release fence for input buffer, fd count is %d, not 1",
985 __FUNCTION__, result.frameNumber, result.inputBuffer.releaseFence->numFds);
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -0800986 return;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800987 }
988 r.input_buffer = &inputBuffer;
989 }
990
991 r.partial_result = result.partialResult;
992
993 processCaptureResult(&r);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800994}
995
996hardware::Return<void> Camera3Device::notify(
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -0800997 const hardware::hidl_vec<hardware::camera::device::V3_2::NotifyMsg>& msgs) {
Yin-Chia Yeh657c1872017-07-18 18:09:57 -0700998 // Ideally we should grab mLock, but that can lead to deadlock, and
999 // it's not super important to get up to date value of mStatus for this
1000 // warning print, hence skipping the lock here
1001 if (mStatus == STATUS_ERROR) {
1002 // Per API contract, HAL should act as closed after device error
1003 // But mStatus can be set to error by framework as well, so just log
1004 // a warning here.
1005 ALOGW("%s: received notify message in error state.", __FUNCTION__);
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -07001006 }
Yin-Chia Yeh657c1872017-07-18 18:09:57 -07001007
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08001008 for (const auto& msg : msgs) {
1009 notify(msg);
1010 }
1011 return hardware::Void();
1012}
1013
1014void Camera3Device::notify(
1015 const hardware::camera::device::V3_2::NotifyMsg& msg) {
1016
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001017 camera3_notify_msg m;
1018 switch (msg.type) {
1019 case MsgType::ERROR:
1020 m.type = CAMERA3_MSG_ERROR;
1021 m.message.error.frame_number = msg.msg.error.frameNumber;
1022 if (msg.msg.error.errorStreamId >= 0) {
1023 ssize_t idx = mOutputStreams.indexOfKey(msg.msg.error.errorStreamId);
Emilian Peevbe3d40c2017-03-27 13:03:10 +01001024 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001025 ALOGE("%s: Frame %d: Invalid error stream id %d",
1026 __FUNCTION__, m.message.error.frame_number, msg.msg.error.errorStreamId);
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08001027 return;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001028 }
1029 m.message.error.error_stream = mOutputStreams.valueAt(idx)->asHalStream();
1030 } else {
1031 m.message.error.error_stream = nullptr;
1032 }
1033 switch (msg.msg.error.errorCode) {
1034 case ErrorCode::ERROR_DEVICE:
1035 m.message.error.error_code = CAMERA3_MSG_ERROR_DEVICE;
1036 break;
1037 case ErrorCode::ERROR_REQUEST:
1038 m.message.error.error_code = CAMERA3_MSG_ERROR_REQUEST;
1039 break;
1040 case ErrorCode::ERROR_RESULT:
1041 m.message.error.error_code = CAMERA3_MSG_ERROR_RESULT;
1042 break;
1043 case ErrorCode::ERROR_BUFFER:
1044 m.message.error.error_code = CAMERA3_MSG_ERROR_BUFFER;
1045 break;
1046 }
1047 break;
1048 case MsgType::SHUTTER:
1049 m.type = CAMERA3_MSG_SHUTTER;
1050 m.message.shutter.frame_number = msg.msg.shutter.frameNumber;
1051 m.message.shutter.timestamp = msg.msg.shutter.timestamp;
1052 break;
1053 }
1054 notify(&m);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001055}
1056
Jianing Weicb0652e2014-03-12 18:29:36 -07001057status_t Camera3Device::captureList(const List<const CameraMetadata> &requests,
Shuzhen Wang0129d522016-10-30 22:43:41 -07001058 const std::list<const SurfaceMap> &surfaceMaps,
Jianing Weicb0652e2014-03-12 18:29:36 -07001059 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -07001060 ATRACE_CALL();
1061
Shuzhen Wang0129d522016-10-30 22:43:41 -07001062 return submitRequestsHelper(requests, surfaceMaps, /*repeating*/false, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -07001063}
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001064
Jianing Weicb0652e2014-03-12 18:29:36 -07001065status_t Camera3Device::setStreamingRequest(const CameraMetadata &request,
1066 int64_t* /*lastFrameNumber*/) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001067 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001068
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07001069 List<const CameraMetadata> requests;
Shuzhen Wang0129d522016-10-30 22:43:41 -07001070 std::list<const SurfaceMap> surfaceMaps;
1071 convertToRequestList(requests, surfaceMaps, request);
1072
1073 return setStreamingRequestList(requests, /*surfaceMap*/surfaceMaps,
1074 /*lastFrameNumber*/NULL);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001075}
1076
Jianing Weicb0652e2014-03-12 18:29:36 -07001077status_t Camera3Device::setStreamingRequestList(const List<const CameraMetadata> &requests,
Shuzhen Wang0129d522016-10-30 22:43:41 -07001078 const std::list<const SurfaceMap> &surfaceMaps,
Jianing Weicb0652e2014-03-12 18:29:36 -07001079 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -07001080 ATRACE_CALL();
1081
Shuzhen Wang0129d522016-10-30 22:43:41 -07001082 return submitRequestsHelper(requests, surfaceMaps, /*repeating*/true, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -07001083}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001084
1085sp<Camera3Device::CaptureRequest> Camera3Device::setUpRequestLocked(
Shuzhen Wang0129d522016-10-30 22:43:41 -07001086 const CameraMetadata &request, const SurfaceMap &surfaceMap) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001087 status_t res;
1088
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001089 if (mStatus == STATUS_UNCONFIGURED || mNeedConfig) {
Eino-Ville Talvalae7091aa2017-03-07 15:23:06 -08001090 // This point should only be reached via API1 (API2 must explicitly call configureStreams)
1091 // so unilaterally select normal operating mode.
1092 res = configureStreamsLocked(CAMERA3_STREAM_CONFIGURATION_NORMAL_MODE);
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07001093 // Stream configuration failed. Client might try other configuraitons.
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001094 if (res != OK) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07001095 CLOGE("Can't set up streams: %s (%d)", strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001096 return NULL;
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07001097 } else if (mStatus == STATUS_UNCONFIGURED) {
1098 // Stream configuration successfully configure to empty stream configuration.
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001099 CLOGE("No streams configured");
1100 return NULL;
1101 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001102 }
1103
Shuzhen Wang0129d522016-10-30 22:43:41 -07001104 sp<CaptureRequest> newRequest = createCaptureRequest(request, surfaceMap);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001105 return newRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001106}
1107
Jianing Weicb0652e2014-03-12 18:29:36 -07001108status_t Camera3Device::clearStreamingRequest(int64_t *lastFrameNumber) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001109 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001110 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001111 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001112
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001113 switch (mStatus) {
1114 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001115 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001116 return INVALID_OPERATION;
1117 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001118 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001119 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001120 case STATUS_UNCONFIGURED:
1121 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001122 case STATUS_ACTIVE:
1123 // OK
1124 break;
1125 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001126 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001127 return INVALID_OPERATION;
1128 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001129 ALOGV("Camera %s: Clearing repeating request", mId.string());
Jianing Weicb0652e2014-03-12 18:29:36 -07001130
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07001131 return mRequestThread->clearRepeatingRequests(lastFrameNumber);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001132}
1133
1134status_t Camera3Device::waitUntilRequestReceived(int32_t requestId, nsecs_t timeout) {
1135 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001136 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001137
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001138 return mRequestThread->waitUntilRequestProcessed(requestId, timeout);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001139}
1140
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001141status_t Camera3Device::createInputStream(
1142 uint32_t width, uint32_t height, int format, int *id) {
1143 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001144 Mutex::Autolock il(mInterfaceLock);
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001145 Mutex::Autolock l(mLock);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001146 ALOGV("Camera %s: Creating new input stream %d: %d x %d, format %d",
1147 mId.string(), mNextStreamId, width, height, format);
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001148
1149 status_t res;
1150 bool wasActive = false;
1151
1152 switch (mStatus) {
1153 case STATUS_ERROR:
1154 ALOGE("%s: Device has encountered a serious error", __FUNCTION__);
1155 return INVALID_OPERATION;
1156 case STATUS_UNINITIALIZED:
1157 ALOGE("%s: Device not initialized", __FUNCTION__);
1158 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001159 case STATUS_UNCONFIGURED:
1160 case STATUS_CONFIGURED:
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001161 // OK
1162 break;
1163 case STATUS_ACTIVE:
1164 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001165 res = internalPauseAndWaitLocked();
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001166 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001167 SET_ERR_L("Can't pause captures to reconfigure streams!");
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001168 return res;
1169 }
1170 wasActive = true;
1171 break;
1172 default:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001173 SET_ERR_L("%s: Unexpected status: %d", mStatus);
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001174 return INVALID_OPERATION;
1175 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001176 assert(mStatus != STATUS_ACTIVE);
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001177
1178 if (mInputStream != 0) {
1179 ALOGE("%s: Cannot create more than 1 input stream", __FUNCTION__);
1180 return INVALID_OPERATION;
1181 }
1182
1183 sp<Camera3InputStream> newStream = new Camera3InputStream(mNextStreamId,
1184 width, height, format);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001185 newStream->setStatusTracker(mStatusTracker);
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001186
1187 mInputStream = newStream;
1188
1189 *id = mNextStreamId++;
1190
1191 // Continue captures if active at start
1192 if (wasActive) {
1193 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
Eino-Ville Talvalae7091aa2017-03-07 15:23:06 -08001194 // Reuse current operating mode for new stream config
1195 res = configureStreamsLocked(mOperatingMode);
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001196 if (res != OK) {
1197 ALOGE("%s: Can't reconfigure device for new stream %d: %s (%d)",
1198 __FUNCTION__, mNextStreamId, strerror(-res), res);
1199 return res;
1200 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001201 internalResumeLocked();
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001202 }
1203
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001204 ALOGV("Camera %s: Created input stream", mId.string());
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001205 return OK;
1206}
1207
Eino-Ville Talvala727d1722015-06-09 13:44:19 -07001208status_t Camera3Device::createStream(sp<Surface> consumer,
Shuzhen Wang0129d522016-10-30 22:43:41 -07001209 uint32_t width, uint32_t height, int format,
1210 android_dataspace dataSpace, camera3_stream_rotation_t rotation, int *id,
Shuzhen Wang758c2152017-01-10 18:26:18 -08001211 int streamSetId, bool isShared, uint32_t consumerUsage) {
Shuzhen Wang0129d522016-10-30 22:43:41 -07001212 ATRACE_CALL();
1213
1214 if (consumer == nullptr) {
1215 ALOGE("%s: consumer must not be null", __FUNCTION__);
1216 return BAD_VALUE;
1217 }
1218
1219 std::vector<sp<Surface>> consumers;
1220 consumers.push_back(consumer);
1221
1222 return createStream(consumers, /*hasDeferredConsumer*/ false, width, height,
Shuzhen Wang758c2152017-01-10 18:26:18 -08001223 format, dataSpace, rotation, id, streamSetId, isShared, consumerUsage);
Shuzhen Wang0129d522016-10-30 22:43:41 -07001224}
1225
1226status_t Camera3Device::createStream(const std::vector<sp<Surface>>& consumers,
1227 bool hasDeferredConsumer, uint32_t width, uint32_t height, int format,
1228 android_dataspace dataSpace, camera3_stream_rotation_t rotation, int *id,
Shuzhen Wang758c2152017-01-10 18:26:18 -08001229 int streamSetId, bool isShared, uint32_t consumerUsage) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001230 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001231 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001232 Mutex::Autolock l(mLock);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001233 ALOGV("Camera %s: Creating new stream %d: %d x %d, format %d, dataspace %d rotation %d"
Shuzhen Wang758c2152017-01-10 18:26:18 -08001234 " consumer usage 0x%x, isShared %d", mId.string(), mNextStreamId, width, height, format,
1235 dataSpace, rotation, consumerUsage, isShared);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001236
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001237 status_t res;
1238 bool wasActive = false;
1239
1240 switch (mStatus) {
1241 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001242 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001243 return INVALID_OPERATION;
1244 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001245 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001246 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001247 case STATUS_UNCONFIGURED:
1248 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001249 // OK
1250 break;
1251 case STATUS_ACTIVE:
1252 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001253 res = internalPauseAndWaitLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001254 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001255 SET_ERR_L("Can't pause captures to reconfigure streams!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001256 return res;
1257 }
1258 wasActive = true;
1259 break;
1260 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001261 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001262 return INVALID_OPERATION;
1263 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001264 assert(mStatus != STATUS_ACTIVE);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001265
1266 sp<Camera3OutputStream> newStream;
Zhijun He5d677d12016-05-29 16:52:39 -07001267
Shuzhen Wang0129d522016-10-30 22:43:41 -07001268 if (consumers.size() == 0 && !hasDeferredConsumer) {
1269 ALOGE("%s: Number of consumers cannot be smaller than 1", __FUNCTION__);
1270 return BAD_VALUE;
1271 }
Zhijun He5d677d12016-05-29 16:52:39 -07001272
Shuzhen Wang0129d522016-10-30 22:43:41 -07001273 if (hasDeferredConsumer && format != HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED) {
Zhijun He5d677d12016-05-29 16:52:39 -07001274 ALOGE("Deferred consumer stream creation only support IMPLEMENTATION_DEFINED format");
1275 return BAD_VALUE;
1276 }
1277
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001278 if (format == HAL_PIXEL_FORMAT_BLOB) {
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -07001279 ssize_t blobBufferSize;
1280 if (dataSpace != HAL_DATASPACE_DEPTH) {
1281 blobBufferSize = getJpegBufferSize(width, height);
1282 if (blobBufferSize <= 0) {
1283 SET_ERR_L("Invalid jpeg buffer size %zd", blobBufferSize);
1284 return BAD_VALUE;
1285 }
1286 } else {
1287 blobBufferSize = getPointCloudBufferSize();
1288 if (blobBufferSize <= 0) {
1289 SET_ERR_L("Invalid point cloud buffer size %zd", blobBufferSize);
1290 return BAD_VALUE;
1291 }
Zhijun Hef7da0962014-04-24 13:27:56 -07001292 }
Shuzhen Wang0129d522016-10-30 22:43:41 -07001293 newStream = new Camera3OutputStream(mNextStreamId, consumers[0],
Shuzhen Wangc28dccc2016-02-11 23:48:46 -08001294 width, height, blobBufferSize, format, dataSpace, rotation,
1295 mTimestampOffset, streamSetId);
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -08001296 } else if (format == HAL_PIXEL_FORMAT_RAW_OPAQUE) {
1297 ssize_t rawOpaqueBufferSize = getRawOpaqueBufferSize(width, height);
1298 if (rawOpaqueBufferSize <= 0) {
1299 SET_ERR_L("Invalid RAW opaque buffer size %zd", rawOpaqueBufferSize);
1300 return BAD_VALUE;
1301 }
Shuzhen Wang0129d522016-10-30 22:43:41 -07001302 newStream = new Camera3OutputStream(mNextStreamId, consumers[0],
Shuzhen Wangc28dccc2016-02-11 23:48:46 -08001303 width, height, rawOpaqueBufferSize, format, dataSpace, rotation,
1304 mTimestampOffset, streamSetId);
Shuzhen Wang758c2152017-01-10 18:26:18 -08001305 } else if (isShared) {
1306 newStream = new Camera3SharedOutputStream(mNextStreamId, consumers,
1307 width, height, format, consumerUsage, dataSpace, rotation,
1308 mTimestampOffset, streamSetId);
Shuzhen Wang0129d522016-10-30 22:43:41 -07001309 } else if (consumers.size() == 0 && hasDeferredConsumer) {
Zhijun He5d677d12016-05-29 16:52:39 -07001310 newStream = new Camera3OutputStream(mNextStreamId,
1311 width, height, format, consumerUsage, dataSpace, rotation,
1312 mTimestampOffset, streamSetId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001313 } else {
Shuzhen Wang0129d522016-10-30 22:43:41 -07001314 newStream = new Camera3OutputStream(mNextStreamId, consumers[0],
Shuzhen Wangc28dccc2016-02-11 23:48:46 -08001315 width, height, format, dataSpace, rotation,
1316 mTimestampOffset, streamSetId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001317 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001318 newStream->setStatusTracker(mStatusTracker);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001319
Emilian Peev08dd2452017-04-06 16:55:14 +01001320 newStream->setBufferManager(mBufferManager);
Zhijun He125684a2015-12-26 15:07:30 -08001321
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001322 res = mOutputStreams.add(mNextStreamId, newStream);
1323 if (res < 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001324 SET_ERR_L("Can't add new stream to set: %s (%d)", strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001325 return res;
1326 }
1327
1328 *id = mNextStreamId++;
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001329 mNeedConfig = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001330
1331 // Continue captures if active at start
1332 if (wasActive) {
1333 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
Eino-Ville Talvalae7091aa2017-03-07 15:23:06 -08001334 // Reuse current operating mode for new stream config
1335 res = configureStreamsLocked(mOperatingMode);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001336 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001337 CLOGE("Can't reconfigure device for new stream %d: %s (%d)",
1338 mNextStreamId, strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001339 return res;
1340 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001341 internalResumeLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001342 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001343 ALOGV("Camera %s: Created new stream", mId.string());
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001344 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001345}
1346
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001347status_t Camera3Device::getStreamInfo(int id,
Eino-Ville Talvalad46a6b92015-05-14 17:26:24 -07001348 uint32_t *width, uint32_t *height,
1349 uint32_t *format, android_dataspace *dataSpace) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001350 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001351 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001352 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001353
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001354 switch (mStatus) {
1355 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001356 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001357 return INVALID_OPERATION;
1358 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001359 CLOGE("Device not initialized!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001360 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001361 case STATUS_UNCONFIGURED:
1362 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001363 case STATUS_ACTIVE:
1364 // OK
1365 break;
1366 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001367 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001368 return INVALID_OPERATION;
1369 }
1370
1371 ssize_t idx = mOutputStreams.indexOfKey(id);
1372 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001373 CLOGE("Stream %d is unknown", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001374 return idx;
1375 }
1376
1377 if (width) *width = mOutputStreams[idx]->getWidth();
1378 if (height) *height = mOutputStreams[idx]->getHeight();
1379 if (format) *format = mOutputStreams[idx]->getFormat();
Eino-Ville Talvalad46a6b92015-05-14 17:26:24 -07001380 if (dataSpace) *dataSpace = mOutputStreams[idx]->getDataSpace();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001381 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001382}
1383
1384status_t Camera3Device::setStreamTransform(int id,
1385 int transform) {
1386 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001387 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001388 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001389
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001390 switch (mStatus) {
1391 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001392 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001393 return INVALID_OPERATION;
1394 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001395 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001396 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001397 case STATUS_UNCONFIGURED:
1398 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001399 case STATUS_ACTIVE:
1400 // OK
1401 break;
1402 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001403 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001404 return INVALID_OPERATION;
1405 }
1406
1407 ssize_t idx = mOutputStreams.indexOfKey(id);
1408 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001409 CLOGE("Stream %d does not exist",
1410 id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001411 return BAD_VALUE;
1412 }
1413
1414 return mOutputStreams.editValueAt(idx)->setTransform(transform);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001415}
1416
1417status_t Camera3Device::deleteStream(int id) {
1418 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001419 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001420 Mutex::Autolock l(mLock);
1421 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001422
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001423 ALOGV("%s: Camera %s: Deleting stream %d", __FUNCTION__, mId.string(), id);
Igor Murashkine2172be2013-05-28 15:31:39 -07001424
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001425 // CameraDevice semantics require device to already be idle before
1426 // deleteStream is called, unlike for createStream.
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001427 if (mStatus == STATUS_ACTIVE) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001428 ALOGV("%s: Camera %s: Device not idle", __FUNCTION__, mId.string());
Igor Murashkin52827132013-05-13 14:53:44 -07001429 return -EBUSY;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001430 }
1431
Yin-Chia Yeh5090c732017-07-20 16:05:29 -07001432 if (mStatus == STATUS_ERROR) {
1433 ALOGW("%s: Camera %s: deleteStream not allowed in ERROR state",
1434 __FUNCTION__, mId.string());
1435 return -EBUSY;
1436 }
1437
Igor Murashkin2fba5842013-04-22 14:03:54 -07001438 sp<Camera3StreamInterface> deletedStream;
Zhijun He5f446352014-01-22 09:49:33 -08001439 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001440 if (mInputStream != NULL && id == mInputStream->getId()) {
1441 deletedStream = mInputStream;
1442 mInputStream.clear();
1443 } else {
Zhijun He5f446352014-01-22 09:49:33 -08001444 if (outputStreamIdx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001445 CLOGE("Stream %d does not exist", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001446 return BAD_VALUE;
1447 }
Zhijun He5f446352014-01-22 09:49:33 -08001448 }
1449
1450 // Delete output stream or the output part of a bi-directional stream.
1451 if (outputStreamIdx != NAME_NOT_FOUND) {
1452 deletedStream = mOutputStreams.editValueAt(outputStreamIdx);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001453 mOutputStreams.removeItem(id);
1454 }
1455
1456 // Free up the stream endpoint so that it can be used by some other stream
1457 res = deletedStream->disconnect();
1458 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001459 SET_ERR_L("Can't disconnect deleted stream %d", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001460 // fall through since we want to still list the stream as deleted.
1461 }
1462 mDeletedStreams.add(deletedStream);
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001463 mNeedConfig = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001464
1465 return res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001466}
1467
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -08001468status_t Camera3Device::configureStreams(int operatingMode) {
Igor Murashkine2d167e2014-08-19 16:19:59 -07001469 ATRACE_CALL();
1470 ALOGV("%s: E", __FUNCTION__);
1471
1472 Mutex::Autolock il(mInterfaceLock);
1473 Mutex::Autolock l(mLock);
Chien-Yu Chen17338fc2015-06-18 16:30:12 -07001474
Eino-Ville Talvalae7091aa2017-03-07 15:23:06 -08001475 return configureStreamsLocked(operatingMode);
Igor Murashkine2d167e2014-08-19 16:19:59 -07001476}
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001477
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001478status_t Camera3Device::getInputBufferProducer(
1479 sp<IGraphicBufferProducer> *producer) {
1480 Mutex::Autolock il(mInterfaceLock);
1481 Mutex::Autolock l(mLock);
1482
1483 if (producer == NULL) {
1484 return BAD_VALUE;
1485 } else if (mInputStream == NULL) {
1486 return INVALID_OPERATION;
1487 }
1488
1489 return mInputStream->getInputBufferProducer(producer);
1490}
1491
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001492status_t Camera3Device::createDefaultRequest(int templateId,
1493 CameraMetadata *request) {
1494 ATRACE_CALL();
Alex Rayfe7e0c62013-05-30 00:12:13 -07001495 ALOGV("%s: for template %d", __FUNCTION__, templateId);
Chien-Yu Chen9cd14022016-03-09 12:21:01 -08001496
1497 if (templateId <= 0 || templateId >= CAMERA3_TEMPLATE_COUNT) {
1498 android_errorWriteWithInfoLog(CameraService::SN_EVENT_LOG_ID, "26866110",
1499 IPCThreadState::self()->getCallingUid(), nullptr, 0);
1500 return BAD_VALUE;
1501 }
1502
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001503 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001504 Mutex::Autolock l(mLock);
1505
1506 switch (mStatus) {
1507 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001508 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001509 return INVALID_OPERATION;
1510 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001511 CLOGE("Device is not initialized!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001512 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001513 case STATUS_UNCONFIGURED:
1514 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001515 case STATUS_ACTIVE:
1516 // OK
1517 break;
1518 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001519 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001520 return INVALID_OPERATION;
1521 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001522
Zhijun Hea1530f12014-09-14 12:44:20 -07001523 if (!mRequestTemplateCache[templateId].isEmpty()) {
1524 *request = mRequestTemplateCache[templateId];
1525 return OK;
1526 }
1527
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001528 camera_metadata_t *rawRequest;
1529 status_t res = mInterface->constructDefaultRequestSettings(
1530 (camera3_request_template_t) templateId, &rawRequest);
1531 if (res == BAD_VALUE) {
Yin-Chia Yeh0336d362015-04-14 12:34:22 -07001532 ALOGI("%s: template %d is not supported on this camera device",
1533 __FUNCTION__, templateId);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001534 return res;
1535 } else if (res != OK) {
1536 CLOGE("Unable to construct request template %d: %s (%d)",
1537 templateId, strerror(-res), res);
1538 return res;
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001539 }
Yin-Chia Yeh4c060992016-04-11 17:40:12 -07001540
Emilian Peev71c73a22017-03-21 16:35:51 +00001541 set_camera_metadata_vendor_id(rawRequest, mVendorTagId);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001542 mRequestTemplateCache[templateId].acquire(rawRequest);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001543
Yin-Chia Yeh4c060992016-04-11 17:40:12 -07001544 *request = mRequestTemplateCache[templateId];
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001545 return OK;
1546}
1547
1548status_t Camera3Device::waitUntilDrained() {
1549 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001550 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001551 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001552
Zhijun He69a37482014-03-23 18:44:49 -07001553 return waitUntilDrainedLocked();
1554}
1555
1556status_t Camera3Device::waitUntilDrainedLocked() {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001557 switch (mStatus) {
1558 case STATUS_UNINITIALIZED:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001559 case STATUS_UNCONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001560 ALOGV("%s: Already idle", __FUNCTION__);
1561 return OK;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001562 case STATUS_CONFIGURED:
1563 // To avoid race conditions, check with tracker to be sure
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001564 case STATUS_ERROR:
1565 case STATUS_ACTIVE:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001566 // Need to verify shut down
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001567 break;
1568 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001569 SET_ERR_L("Unexpected status: %d",mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001570 return INVALID_OPERATION;
1571 }
1572
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07001573 nsecs_t maxExpectedDuration = getExpectedInFlightDurationLocked();
1574
1575 ALOGV("%s: Camera %s: Waiting until idle (%" PRIi64 "ns)", __FUNCTION__, mId.string(),
1576 maxExpectedDuration);
1577 status_t res = waitUntilStateThenRelock(/*active*/ false, maxExpectedDuration);
Eino-Ville Talvala9c8a0912014-09-14 14:52:19 -07001578 if (res != OK) {
1579 SET_ERR_L("Error waiting for HAL to drain: %s (%d)", strerror(-res),
1580 res);
1581 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001582 return res;
1583}
1584
Ruben Brunk183f0562015-08-12 12:55:02 -07001585
1586void Camera3Device::internalUpdateStatusLocked(Status status) {
1587 mStatus = status;
1588 mRecentStatusUpdates.add(mStatus);
1589 mStatusChanged.broadcast();
1590}
1591
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001592// Pause to reconfigure
1593status_t Camera3Device::internalPauseAndWaitLocked() {
1594 mRequestThread->setPaused(true);
1595 mPauseStateNotify = true;
1596
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07001597 nsecs_t maxExpectedDuration = getExpectedInFlightDurationLocked();
1598 ALOGV("%s: Camera %s: Internal wait until idle (% " PRIi64 " ns)", __FUNCTION__, mId.string(),
1599 maxExpectedDuration);
1600 status_t res = waitUntilStateThenRelock(/*active*/ false, maxExpectedDuration);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001601 if (res != OK) {
1602 SET_ERR_L("Can't idle device in %f seconds!",
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07001603 maxExpectedDuration/1e9);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001604 }
1605
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001606 return res;
1607}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001608
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001609// Resume after internalPauseAndWaitLocked
1610status_t Camera3Device::internalResumeLocked() {
1611 status_t res;
1612
1613 mRequestThread->setPaused(false);
1614
1615 res = waitUntilStateThenRelock(/*active*/ true, kActiveTimeout);
1616 if (res != OK) {
1617 SET_ERR_L("Can't transition to active in %f seconds!",
1618 kActiveTimeout/1e9);
1619 }
1620 mPauseStateNotify = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001621 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001622}
1623
Ruben Brunk183f0562015-08-12 12:55:02 -07001624status_t Camera3Device::waitUntilStateThenRelock(bool active, nsecs_t timeout) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001625 status_t res = OK;
Ruben Brunk183f0562015-08-12 12:55:02 -07001626
1627 size_t startIndex = 0;
1628 if (mStatusWaiters == 0) {
1629 // Clear the list of recent statuses if there are no existing threads waiting on updates to
1630 // this status list
1631 mRecentStatusUpdates.clear();
1632 } else {
1633 // If other threads are waiting on updates to this status list, set the position of the
1634 // first element that this list will check rather than clearing the list.
1635 startIndex = mRecentStatusUpdates.size();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001636 }
1637
Ruben Brunk183f0562015-08-12 12:55:02 -07001638 mStatusWaiters++;
1639
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001640 bool stateSeen = false;
1641 do {
Ruben Brunk183f0562015-08-12 12:55:02 -07001642 if (active == (mStatus == STATUS_ACTIVE)) {
1643 // Desired state is current
1644 break;
1645 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001646
1647 res = mStatusChanged.waitRelative(mLock, timeout);
1648 if (res != OK) break;
1649
Ruben Brunk183f0562015-08-12 12:55:02 -07001650 // This is impossible, but if not, could result in subtle deadlocks and invalid state
1651 // transitions.
1652 LOG_ALWAYS_FATAL_IF(startIndex > mRecentStatusUpdates.size(),
1653 "%s: Skipping status updates in Camera3Device, may result in deadlock.",
1654 __FUNCTION__);
1655
1656 // Encountered desired state since we began waiting
1657 for (size_t i = startIndex; i < mRecentStatusUpdates.size(); i++) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001658 if (active == (mRecentStatusUpdates[i] == STATUS_ACTIVE) ) {
1659 stateSeen = true;
1660 break;
1661 }
1662 }
1663 } while (!stateSeen);
1664
Ruben Brunk183f0562015-08-12 12:55:02 -07001665 mStatusWaiters--;
1666
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001667 return res;
1668}
1669
1670
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07001671status_t Camera3Device::setNotifyCallback(wp<NotificationListener> listener) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001672 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001673 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001674
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001675 if (listener != NULL && mListener != NULL) {
1676 ALOGW("%s: Replacing old callback listener", __FUNCTION__);
1677 }
1678 mListener = listener;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001679 mRequestThread->setNotificationListener(listener);
1680 mPreparerThread->setNotificationListener(listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001681
1682 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001683}
1684
Eino-Ville Talvala46910bd2013-07-18 19:15:17 -07001685bool Camera3Device::willNotify3A() {
1686 return false;
1687}
1688
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001689status_t Camera3Device::waitForNextFrame(nsecs_t timeout) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001690 status_t res;
1691 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001692
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001693 while (mResultQueue.empty()) {
1694 res = mResultSignal.waitRelative(mOutputLock, timeout);
1695 if (res == TIMED_OUT) {
1696 return res;
1697 } else if (res != OK) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001698 ALOGW("%s: Camera %s: No frame in %" PRId64 " ns: %s (%d)",
1699 __FUNCTION__, mId.string(), timeout, strerror(-res), res);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001700 return res;
1701 }
1702 }
1703 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001704}
1705
Jianing Weicb0652e2014-03-12 18:29:36 -07001706status_t Camera3Device::getNextResult(CaptureResult *frame) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001707 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001708 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001709
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001710 if (mResultQueue.empty()) {
1711 return NOT_ENOUGH_DATA;
1712 }
1713
Jianing Weicb0652e2014-03-12 18:29:36 -07001714 if (frame == NULL) {
1715 ALOGE("%s: argument cannot be NULL", __FUNCTION__);
1716 return BAD_VALUE;
1717 }
1718
1719 CaptureResult &result = *(mResultQueue.begin());
1720 frame->mResultExtras = result.mResultExtras;
1721 frame->mMetadata.acquire(result.mMetadata);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001722 mResultQueue.erase(mResultQueue.begin());
1723
1724 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001725}
1726
1727status_t Camera3Device::triggerAutofocus(uint32_t id) {
1728 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001729 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001730
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001731 ALOGV("%s: Triggering autofocus, id %d", __FUNCTION__, id);
1732 // Mix-in this trigger into the next request and only the next request.
1733 RequestTrigger trigger[] = {
1734 {
1735 ANDROID_CONTROL_AF_TRIGGER,
1736 ANDROID_CONTROL_AF_TRIGGER_START
1737 },
1738 {
1739 ANDROID_CONTROL_AF_TRIGGER_ID,
1740 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001741 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001742 };
1743
1744 return mRequestThread->queueTrigger(trigger,
1745 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001746}
1747
1748status_t Camera3Device::triggerCancelAutofocus(uint32_t id) {
1749 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001750 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001751
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001752 ALOGV("%s: Triggering cancel autofocus, id %d", __FUNCTION__, id);
1753 // Mix-in this trigger into the next request and only the next request.
1754 RequestTrigger trigger[] = {
1755 {
1756 ANDROID_CONTROL_AF_TRIGGER,
1757 ANDROID_CONTROL_AF_TRIGGER_CANCEL
1758 },
1759 {
1760 ANDROID_CONTROL_AF_TRIGGER_ID,
1761 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001762 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001763 };
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001764
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001765 return mRequestThread->queueTrigger(trigger,
1766 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001767}
1768
1769status_t Camera3Device::triggerPrecaptureMetering(uint32_t id) {
1770 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001771 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001772
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001773 ALOGV("%s: Triggering precapture metering, id %d", __FUNCTION__, id);
1774 // Mix-in this trigger into the next request and only the next request.
1775 RequestTrigger trigger[] = {
1776 {
1777 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER,
1778 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_START
1779 },
1780 {
1781 ANDROID_CONTROL_AE_PRECAPTURE_ID,
1782 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001783 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001784 };
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001785
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001786 return mRequestThread->queueTrigger(trigger,
1787 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001788}
1789
Jianing Weicb0652e2014-03-12 18:29:36 -07001790status_t Camera3Device::flush(int64_t *frameNumber) {
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001791 ATRACE_CALL();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001792 ALOGV("%s: Camera %s: Flushing all requests", __FUNCTION__, mId.string());
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001793 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001794
Zhijun He7ef20392014-04-21 16:04:17 -07001795 {
1796 Mutex::Autolock l(mLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07001797 mRequestThread->clear(/*out*/frameNumber);
Zhijun He7ef20392014-04-21 16:04:17 -07001798 }
1799
Emilian Peev08dd2452017-04-06 16:55:14 +01001800 return mRequestThread->flush();
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001801}
1802
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001803status_t Camera3Device::prepare(int streamId) {
Ruben Brunkc78ac262015-08-13 17:58:46 -07001804 return prepare(camera3::Camera3StreamInterface::ALLOCATE_PIPELINE_MAX, streamId);
1805}
1806
1807status_t Camera3Device::prepare(int maxCount, int streamId) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001808 ATRACE_CALL();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001809 ALOGV("%s: Camera %s: Preparing stream %d", __FUNCTION__, mId.string(), streamId);
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001810 Mutex::Autolock il(mInterfaceLock);
1811 Mutex::Autolock l(mLock);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001812
1813 sp<Camera3StreamInterface> stream;
1814 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(streamId);
1815 if (outputStreamIdx == NAME_NOT_FOUND) {
1816 CLOGE("Stream %d does not exist", streamId);
1817 return BAD_VALUE;
1818 }
1819
1820 stream = mOutputStreams.editValueAt(outputStreamIdx);
1821
1822 if (stream->isUnpreparable() || stream->hasOutstandingBuffers() ) {
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001823 CLOGE("Stream %d has already been a request target", streamId);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001824 return BAD_VALUE;
1825 }
1826
1827 if (mRequestThread->isStreamPending(stream)) {
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001828 CLOGE("Stream %d is already a target in a pending request", streamId);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001829 return BAD_VALUE;
1830 }
1831
Ruben Brunkc78ac262015-08-13 17:58:46 -07001832 return mPreparerThread->prepare(maxCount, stream);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001833}
1834
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001835status_t Camera3Device::tearDown(int streamId) {
1836 ATRACE_CALL();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001837 ALOGV("%s: Camera %s: Tearing down stream %d", __FUNCTION__, mId.string(), streamId);
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001838 Mutex::Autolock il(mInterfaceLock);
1839 Mutex::Autolock l(mLock);
1840
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001841 sp<Camera3StreamInterface> stream;
1842 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(streamId);
1843 if (outputStreamIdx == NAME_NOT_FOUND) {
1844 CLOGE("Stream %d does not exist", streamId);
1845 return BAD_VALUE;
1846 }
1847
1848 stream = mOutputStreams.editValueAt(outputStreamIdx);
1849
1850 if (stream->hasOutstandingBuffers() || mRequestThread->isStreamPending(stream)) {
1851 CLOGE("Stream %d is a target of a in-progress request", streamId);
1852 return BAD_VALUE;
1853 }
1854
1855 return stream->tearDown();
1856}
1857
Shuzhen Wangb0fdc1e2016-03-20 23:21:39 -07001858status_t Camera3Device::addBufferListenerForStream(int streamId,
1859 wp<Camera3StreamBufferListener> listener) {
1860 ATRACE_CALL();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001861 ALOGV("%s: Camera %s: Adding buffer listener for stream %d", __FUNCTION__, mId.string(), streamId);
Shuzhen Wangb0fdc1e2016-03-20 23:21:39 -07001862 Mutex::Autolock il(mInterfaceLock);
1863 Mutex::Autolock l(mLock);
1864
1865 sp<Camera3StreamInterface> stream;
1866 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(streamId);
1867 if (outputStreamIdx == NAME_NOT_FOUND) {
1868 CLOGE("Stream %d does not exist", streamId);
1869 return BAD_VALUE;
1870 }
1871
1872 stream = mOutputStreams.editValueAt(outputStreamIdx);
1873 stream->addBufferListener(listener);
1874
1875 return OK;
1876}
1877
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001878/**
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001879 * Methods called by subclasses
1880 */
1881
1882void Camera3Device::notifyStatus(bool idle) {
1883 {
1884 // Need mLock to safely update state and synchronize to current
1885 // state of methods in flight.
1886 Mutex::Autolock l(mLock);
1887 // We can get various system-idle notices from the status tracker
1888 // while starting up. Only care about them if we've actually sent
1889 // in some requests recently.
1890 if (mStatus != STATUS_ACTIVE && mStatus != STATUS_CONFIGURED) {
1891 return;
1892 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001893 ALOGV("%s: Camera %s: Now %s", __FUNCTION__, mId.string(),
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001894 idle ? "idle" : "active");
Ruben Brunk183f0562015-08-12 12:55:02 -07001895 internalUpdateStatusLocked(idle ? STATUS_CONFIGURED : STATUS_ACTIVE);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001896
1897 // Skip notifying listener if we're doing some user-transparent
1898 // state changes
1899 if (mPauseStateNotify) return;
1900 }
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07001901
1902 sp<NotificationListener> listener;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001903 {
1904 Mutex::Autolock l(mOutputLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07001905 listener = mListener.promote();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001906 }
1907 if (idle && listener != NULL) {
1908 listener->notifyIdle();
1909 }
1910}
1911
Shuzhen Wang758c2152017-01-10 18:26:18 -08001912status_t Camera3Device::setConsumerSurfaces(int streamId,
1913 const std::vector<sp<Surface>>& consumers) {
Zhijun He5d677d12016-05-29 16:52:39 -07001914 ATRACE_CALL();
Shuzhen Wang758c2152017-01-10 18:26:18 -08001915 ALOGV("%s: Camera %s: set consumer surface for stream %d",
1916 __FUNCTION__, mId.string(), streamId);
Zhijun He5d677d12016-05-29 16:52:39 -07001917 Mutex::Autolock il(mInterfaceLock);
1918 Mutex::Autolock l(mLock);
1919
Shuzhen Wang758c2152017-01-10 18:26:18 -08001920 if (consumers.size() == 0) {
1921 CLOGE("No consumer is passed!");
Zhijun He5d677d12016-05-29 16:52:39 -07001922 return BAD_VALUE;
1923 }
1924
1925 ssize_t idx = mOutputStreams.indexOfKey(streamId);
1926 if (idx == NAME_NOT_FOUND) {
1927 CLOGE("Stream %d is unknown", streamId);
1928 return idx;
1929 }
1930 sp<Camera3OutputStreamInterface> stream = mOutputStreams[idx];
Shuzhen Wang758c2152017-01-10 18:26:18 -08001931 status_t res = stream->setConsumers(consumers);
Zhijun He5d677d12016-05-29 16:52:39 -07001932 if (res != OK) {
1933 CLOGE("Stream %d set consumer failed (error %d %s) ", streamId, res, strerror(-res));
1934 return res;
1935 }
1936
Shuzhen Wang0129d522016-10-30 22:43:41 -07001937 if (stream->isConsumerConfigurationDeferred()) {
1938 if (!stream->isConfiguring()) {
1939 CLOGE("Stream %d was already fully configured.", streamId);
1940 return INVALID_OPERATION;
1941 }
Zhijun He5d677d12016-05-29 16:52:39 -07001942
Shuzhen Wang0129d522016-10-30 22:43:41 -07001943 res = stream->finishConfiguration();
1944 if (res != OK) {
1945 SET_ERR_L("Can't finish configuring output stream %d: %s (%d)",
1946 stream->getId(), strerror(-res), res);
1947 return res;
1948 }
Zhijun He5d677d12016-05-29 16:52:39 -07001949 }
1950
1951 return OK;
1952}
1953
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001954/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001955 * Camera3Device private methods
1956 */
1957
1958sp<Camera3Device::CaptureRequest> Camera3Device::createCaptureRequest(
Shuzhen Wang0129d522016-10-30 22:43:41 -07001959 const CameraMetadata &request, const SurfaceMap &surfaceMap) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001960 ATRACE_CALL();
1961 status_t res;
1962
1963 sp<CaptureRequest> newRequest = new CaptureRequest;
1964 newRequest->mSettings = request;
1965
1966 camera_metadata_entry_t inputStreams =
1967 newRequest->mSettings.find(ANDROID_REQUEST_INPUT_STREAMS);
1968 if (inputStreams.count > 0) {
1969 if (mInputStream == NULL ||
Zhijun Hed1d64672013-09-06 15:00:01 -07001970 mInputStream->getId() != inputStreams.data.i32[0]) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001971 CLOGE("Request references unknown input stream %d",
1972 inputStreams.data.u8[0]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001973 return NULL;
1974 }
1975 // Lazy completion of stream configuration (allocation/registration)
1976 // on first use
1977 if (mInputStream->isConfiguring()) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001978 res = mInputStream->finishConfiguration();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001979 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001980 SET_ERR_L("Unable to finish configuring input stream %d:"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001981 " %s (%d)",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001982 mInputStream->getId(), strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001983 return NULL;
1984 }
1985 }
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001986 // Check if stream is being prepared
1987 if (mInputStream->isPreparing()) {
1988 CLOGE("Request references an input stream that's being prepared!");
1989 return NULL;
1990 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001991
1992 newRequest->mInputStream = mInputStream;
1993 newRequest->mSettings.erase(ANDROID_REQUEST_INPUT_STREAMS);
1994 }
1995
1996 camera_metadata_entry_t streams =
1997 newRequest->mSettings.find(ANDROID_REQUEST_OUTPUT_STREAMS);
1998 if (streams.count == 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001999 CLOGE("Zero output streams specified!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002000 return NULL;
2001 }
2002
2003 for (size_t i = 0; i < streams.count; i++) {
Zhijun Hed1d64672013-09-06 15:00:01 -07002004 int idx = mOutputStreams.indexOfKey(streams.data.i32[i]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002005 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002006 CLOGE("Request references unknown stream %d",
2007 streams.data.u8[i]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002008 return NULL;
2009 }
Igor Murashkin2fba5842013-04-22 14:03:54 -07002010 sp<Camera3OutputStreamInterface> stream =
2011 mOutputStreams.editValueAt(idx);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002012
Zhijun He5d677d12016-05-29 16:52:39 -07002013 // It is illegal to include a deferred consumer output stream into a request
Shuzhen Wang0129d522016-10-30 22:43:41 -07002014 auto iter = surfaceMap.find(streams.data.i32[i]);
2015 if (iter != surfaceMap.end()) {
2016 const std::vector<size_t>& surfaces = iter->second;
2017 for (const auto& surface : surfaces) {
2018 if (stream->isConsumerConfigurationDeferred(surface)) {
2019 CLOGE("Stream %d surface %zu hasn't finished configuration yet "
2020 "due to deferred consumer", stream->getId(), surface);
2021 return NULL;
2022 }
2023 }
2024 newRequest->mOutputSurfaces[i] = surfaces;
Zhijun He5d677d12016-05-29 16:52:39 -07002025 }
2026
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002027 // Lazy completion of stream configuration (allocation/registration)
2028 // on first use
2029 if (stream->isConfiguring()) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002030 res = stream->finishConfiguration();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002031 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002032 SET_ERR_L("Unable to finish configuring stream %d: %s (%d)",
2033 stream->getId(), strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002034 return NULL;
2035 }
2036 }
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002037 // Check if stream is being prepared
2038 if (stream->isPreparing()) {
2039 CLOGE("Request references an output stream that's being prepared!");
2040 return NULL;
2041 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002042
2043 newRequest->mOutputStreams.push(stream);
2044 }
2045 newRequest->mSettings.erase(ANDROID_REQUEST_OUTPUT_STREAMS);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07002046 newRequest->mBatchSize = 1;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002047
2048 return newRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002049}
2050
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002051bool Camera3Device::isOpaqueInputSizeSupported(uint32_t width, uint32_t height) {
2052 for (uint32_t i = 0; i < mSupportedOpaqueInputSizes.size(); i++) {
2053 Size size = mSupportedOpaqueInputSizes[i];
2054 if (size.width == width && size.height == height) {
2055 return true;
2056 }
2057 }
2058
2059 return false;
2060}
2061
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002062void Camera3Device::cancelStreamsConfigurationLocked() {
2063 int res = OK;
2064 if (mInputStream != NULL && mInputStream->isConfiguring()) {
2065 res = mInputStream->cancelConfiguration();
2066 if (res != OK) {
2067 CLOGE("Can't cancel configuring input stream %d: %s (%d)",
2068 mInputStream->getId(), strerror(-res), res);
2069 }
2070 }
2071
2072 for (size_t i = 0; i < mOutputStreams.size(); i++) {
2073 sp<Camera3OutputStreamInterface> outputStream = mOutputStreams.editValueAt(i);
2074 if (outputStream->isConfiguring()) {
2075 res = outputStream->cancelConfiguration();
2076 if (res != OK) {
2077 CLOGE("Can't cancel configuring output stream %d: %s (%d)",
2078 outputStream->getId(), strerror(-res), res);
2079 }
2080 }
2081 }
2082
2083 // Return state to that at start of call, so that future configures
2084 // properly clean things up
2085 internalUpdateStatusLocked(STATUS_UNCONFIGURED);
2086 mNeedConfig = true;
2087}
2088
Eino-Ville Talvalae7091aa2017-03-07 15:23:06 -08002089status_t Camera3Device::configureStreamsLocked(int operatingMode) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002090 ATRACE_CALL();
2091 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002092
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002093 if (mStatus != STATUS_UNCONFIGURED && mStatus != STATUS_CONFIGURED) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002094 CLOGE("Not idle");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002095 return INVALID_OPERATION;
2096 }
2097
Eino-Ville Talvalae7091aa2017-03-07 15:23:06 -08002098 if (operatingMode < 0) {
2099 CLOGE("Invalid operating mode: %d", operatingMode);
2100 return BAD_VALUE;
2101 }
2102
2103 bool isConstrainedHighSpeed =
2104 static_cast<int>(StreamConfigurationMode::CONSTRAINED_HIGH_SPEED_MODE) ==
2105 operatingMode;
2106
2107 if (mOperatingMode != operatingMode) {
2108 mNeedConfig = true;
2109 mIsConstrainedHighSpeedConfiguration = isConstrainedHighSpeed;
2110 mOperatingMode = operatingMode;
2111 }
2112
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07002113 if (!mNeedConfig) {
2114 ALOGV("%s: Skipping config, no stream changes", __FUNCTION__);
2115 return OK;
2116 }
2117
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002118 // Workaround for device HALv3.2 or older spec bug - zero streams requires
2119 // adding a dummy stream instead.
2120 // TODO: Bug: 17321404 for fixing the HAL spec and removing this workaround.
2121 if (mOutputStreams.size() == 0) {
2122 addDummyStreamLocked();
2123 } else {
2124 tryRemoveDummyStreamLocked();
2125 }
2126
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002127 // Start configuring the streams
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002128 ALOGV("%s: Camera %s: Starting stream configuration", __FUNCTION__, mId.string());
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002129
2130 camera3_stream_configuration config;
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -08002131 config.operation_mode = mOperatingMode;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002132 config.num_streams = (mInputStream != NULL) + mOutputStreams.size();
2133
2134 Vector<camera3_stream_t*> streams;
2135 streams.setCapacity(config.num_streams);
2136
2137 if (mInputStream != NULL) {
2138 camera3_stream_t *inputStream;
2139 inputStream = mInputStream->startConfiguration();
2140 if (inputStream == NULL) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002141 CLOGE("Can't start input stream configuration");
2142 cancelStreamsConfigurationLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002143 return INVALID_OPERATION;
2144 }
2145 streams.add(inputStream);
2146 }
2147
2148 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Igor Murashkin2fba5842013-04-22 14:03:54 -07002149
2150 // Don't configure bidi streams twice, nor add them twice to the list
2151 if (mOutputStreams[i].get() ==
2152 static_cast<Camera3StreamInterface*>(mInputStream.get())) {
2153
2154 config.num_streams--;
2155 continue;
2156 }
2157
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002158 camera3_stream_t *outputStream;
2159 outputStream = mOutputStreams.editValueAt(i)->startConfiguration();
2160 if (outputStream == NULL) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002161 CLOGE("Can't start output stream configuration");
2162 cancelStreamsConfigurationLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002163 return INVALID_OPERATION;
2164 }
2165 streams.add(outputStream);
2166 }
2167
2168 config.streams = streams.editArray();
2169
2170 // Do the HAL configuration; will potentially touch stream
2171 // max_buffers, usage, priv fields.
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002172
2173 res = mInterface->configureStreams(&config);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002174
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002175 if (res == BAD_VALUE) {
2176 // HAL rejected this set of streams as unsupported, clean up config
2177 // attempt and return to unconfigured state
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002178 CLOGE("Set of requested inputs/outputs not supported by HAL");
2179 cancelStreamsConfigurationLocked();
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002180 return BAD_VALUE;
2181 } else if (res != OK) {
2182 // Some other kind of error from configure_streams - this is not
2183 // expected
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002184 SET_ERR_L("Unable to configure streams with HAL: %s (%d)",
2185 strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002186 return res;
2187 }
2188
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002189 // Finish all stream configuration immediately.
2190 // TODO: Try to relax this later back to lazy completion, which should be
2191 // faster
2192
Igor Murashkin073f8572013-05-02 14:59:28 -07002193 if (mInputStream != NULL && mInputStream->isConfiguring()) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002194 res = mInputStream->finishConfiguration();
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002195 if (res != OK) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002196 CLOGE("Can't finish configuring input stream %d: %s (%d)",
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002197 mInputStream->getId(), strerror(-res), res);
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002198 cancelStreamsConfigurationLocked();
2199 return BAD_VALUE;
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002200 }
2201 }
2202
2203 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Igor Murashkin073f8572013-05-02 14:59:28 -07002204 sp<Camera3OutputStreamInterface> outputStream =
2205 mOutputStreams.editValueAt(i);
Zhijun He5d677d12016-05-29 16:52:39 -07002206 if (outputStream->isConfiguring() && !outputStream->isConsumerConfigurationDeferred()) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002207 res = outputStream->finishConfiguration();
Igor Murashkin073f8572013-05-02 14:59:28 -07002208 if (res != OK) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002209 CLOGE("Can't finish configuring output stream %d: %s (%d)",
Igor Murashkin073f8572013-05-02 14:59:28 -07002210 outputStream->getId(), strerror(-res), res);
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002211 cancelStreamsConfigurationLocked();
2212 return BAD_VALUE;
Igor Murashkin073f8572013-05-02 14:59:28 -07002213 }
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002214 }
2215 }
2216
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002217 // Request thread needs to know to avoid using repeat-last-settings protocol
2218 // across configure_streams() calls
Chien-Yu Chenc66969b2016-05-19 16:37:51 -07002219 mRequestThread->configurationComplete(mIsConstrainedHighSpeedConfiguration);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002220
Zhijun He90f7c372016-08-16 16:19:43 -07002221 char value[PROPERTY_VALUE_MAX];
2222 property_get("camera.fifo.disable", value, "0");
2223 int32_t disableFifo = atoi(value);
2224 if (disableFifo != 1) {
2225 // Boost priority of request thread to SCHED_FIFO.
2226 pid_t requestThreadTid = mRequestThread->getTid();
2227 res = requestPriority(getpid(), requestThreadTid,
Mikhail Naganov83f04272017-02-07 10:45:09 -08002228 kRequestThreadPriority, /*isForApp*/ false, /*asynchronous*/ false);
Zhijun He90f7c372016-08-16 16:19:43 -07002229 if (res != OK) {
2230 ALOGW("Can't set realtime priority for request processing thread: %s (%d)",
2231 strerror(-res), res);
2232 } else {
2233 ALOGD("Set real time priority for request queue thread (tid %d)", requestThreadTid);
2234 }
Eino-Ville Talvalaf99498e2015-09-25 16:52:55 -07002235 }
2236
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002237 // Update device state
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002238
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07002239 mNeedConfig = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002240
Ruben Brunk183f0562015-08-12 12:55:02 -07002241 internalUpdateStatusLocked((mDummyStreamId == NO_STREAM) ?
2242 STATUS_CONFIGURED : STATUS_UNCONFIGURED);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002243
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002244 ALOGV("%s: Camera %s: Stream configuration complete", __FUNCTION__, mId.string());
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002245
Zhijun He0a210512014-07-24 13:45:15 -07002246 // tear down the deleted streams after configure streams.
2247 mDeletedStreams.clear();
2248
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002249 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002250}
2251
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002252status_t Camera3Device::addDummyStreamLocked() {
2253 ATRACE_CALL();
2254 status_t res;
2255
2256 if (mDummyStreamId != NO_STREAM) {
2257 // Should never be adding a second dummy stream when one is already
2258 // active
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002259 SET_ERR_L("%s: Camera %s: A dummy stream already exists!",
2260 __FUNCTION__, mId.string());
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002261 return INVALID_OPERATION;
2262 }
2263
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002264 ALOGV("%s: Camera %s: Adding a dummy stream", __FUNCTION__, mId.string());
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002265
2266 sp<Camera3OutputStreamInterface> dummyStream =
2267 new Camera3DummyStream(mNextStreamId);
2268
2269 res = mOutputStreams.add(mNextStreamId, dummyStream);
2270 if (res < 0) {
2271 SET_ERR_L("Can't add dummy stream to set: %s (%d)", strerror(-res), res);
2272 return res;
2273 }
2274
2275 mDummyStreamId = mNextStreamId;
2276 mNextStreamId++;
2277
2278 return OK;
2279}
2280
2281status_t Camera3Device::tryRemoveDummyStreamLocked() {
2282 ATRACE_CALL();
2283 status_t res;
2284
2285 if (mDummyStreamId == NO_STREAM) return OK;
2286 if (mOutputStreams.size() == 1) return OK;
2287
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002288 ALOGV("%s: Camera %s: Removing the dummy stream", __FUNCTION__, mId.string());
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002289
2290 // Ok, have a dummy stream and there's at least one other output stream,
2291 // so remove the dummy
2292
2293 sp<Camera3StreamInterface> deletedStream;
2294 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(mDummyStreamId);
2295 if (outputStreamIdx == NAME_NOT_FOUND) {
2296 SET_ERR_L("Dummy stream %d does not appear to exist", mDummyStreamId);
2297 return INVALID_OPERATION;
2298 }
2299
2300 deletedStream = mOutputStreams.editValueAt(outputStreamIdx);
2301 mOutputStreams.removeItemsAt(outputStreamIdx);
2302
2303 // Free up the stream endpoint so that it can be used by some other stream
2304 res = deletedStream->disconnect();
2305 if (res != OK) {
2306 SET_ERR_L("Can't disconnect deleted dummy stream %d", mDummyStreamId);
2307 // fall through since we want to still list the stream as deleted.
2308 }
2309 mDeletedStreams.add(deletedStream);
2310 mDummyStreamId = NO_STREAM;
2311
2312 return res;
2313}
2314
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002315void Camera3Device::setErrorState(const char *fmt, ...) {
2316 Mutex::Autolock l(mLock);
2317 va_list args;
2318 va_start(args, fmt);
2319
2320 setErrorStateLockedV(fmt, args);
2321
2322 va_end(args);
2323}
2324
2325void Camera3Device::setErrorStateV(const char *fmt, va_list args) {
2326 Mutex::Autolock l(mLock);
2327 setErrorStateLockedV(fmt, args);
2328}
2329
2330void Camera3Device::setErrorStateLocked(const char *fmt, ...) {
2331 va_list args;
2332 va_start(args, fmt);
2333
2334 setErrorStateLockedV(fmt, args);
2335
2336 va_end(args);
2337}
2338
2339void Camera3Device::setErrorStateLockedV(const char *fmt, va_list args) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002340 // Print out all error messages to log
2341 String8 errorCause = String8::formatV(fmt, args);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002342 ALOGE("Camera %s: %s", mId.string(), errorCause.string());
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002343
2344 // But only do error state transition steps for the first error
Zhijun Heb05eeae2013-06-06 13:51:22 -07002345 if (mStatus == STATUS_ERROR || mStatus == STATUS_UNINITIALIZED) return;
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002346
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002347 mErrorCause = errorCause;
2348
2349 mRequestThread->setPaused(true);
Ruben Brunk183f0562015-08-12 12:55:02 -07002350 internalUpdateStatusLocked(STATUS_ERROR);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002351
2352 // Notify upstream about a device error
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07002353 sp<NotificationListener> listener = mListener.promote();
2354 if (listener != NULL) {
2355 listener->notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002356 CaptureResultExtras());
2357 }
2358
2359 // Save stack trace. View by dumping it later.
2360 CameraTraces::saveTrace();
2361 // TODO: consider adding errorCause and client pid/procname
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002362}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002363
2364/**
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002365 * In-flight request management
2366 */
2367
Jianing Weicb0652e2014-03-12 18:29:36 -07002368status_t Camera3Device::registerInFlight(uint32_t frameNumber,
Chien-Yu Chend196d612015-06-22 19:49:01 -07002369 int32_t numBuffers, CaptureResultExtras resultExtras, bool hasInput,
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07002370 bool hasAppCallback, nsecs_t maxExpectedDuration) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002371 ATRACE_CALL();
2372 Mutex::Autolock l(mInFlightLock);
2373
2374 ssize_t res;
Chien-Yu Chend196d612015-06-22 19:49:01 -07002375 res = mInFlightMap.add(frameNumber, InFlightRequest(numBuffers, resultExtras, hasInput,
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07002376 hasAppCallback, maxExpectedDuration));
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002377 if (res < 0) return res;
2378
Eino-Ville Talvala24b366e2016-07-21 12:53:07 -07002379 if (mInFlightMap.size() == 1) {
Yin-Chia Yeh38dfde52017-06-27 17:13:33 -07002380 // hold mLock to prevent race with disconnect
2381 Mutex::Autolock l(mLock);
2382 if (mStatusTracker != nullptr) {
2383 mStatusTracker->markComponentActive(mInFlightStatusId);
2384 }
Eino-Ville Talvala24b366e2016-07-21 12:53:07 -07002385 }
2386
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07002387 Mutex::Autolock ml(mLock);
2388 mExpectedInflightDuration += maxExpectedDuration;
2389
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002390 return OK;
2391}
2392
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002393void Camera3Device::returnOutputBuffers(
2394 const camera3_stream_buffer_t *outputBuffers, size_t numBuffers,
2395 nsecs_t timestamp) {
2396 for (size_t i = 0; i < numBuffers; i++)
2397 {
2398 Camera3Stream *stream = Camera3Stream::cast(outputBuffers[i].stream);
2399 status_t res = stream->returnBuffer(outputBuffers[i], timestamp);
2400 // Note: stream may be deallocated at this point, if this buffer was
2401 // the last reference to it.
2402 if (res != OK) {
2403 ALOGE("Can't return buffer to its stream: %s (%d)",
2404 strerror(-res), res);
2405 }
2406 }
2407}
2408
Shuzhen Wangcadb3302016-11-04 14:17:56 -07002409void Camera3Device::removeInFlightMapEntryLocked(int idx) {
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07002410 nsecs_t duration = mInFlightMap.valueAt(idx).maxExpectedDuration;
Shuzhen Wangcadb3302016-11-04 14:17:56 -07002411 mInFlightMap.removeItemsAt(idx, 1);
2412
2413 // Indicate idle inFlightMap to the status tracker
2414 if (mInFlightMap.size() == 0) {
Yin-Chia Yeh38dfde52017-06-27 17:13:33 -07002415 // hold mLock to prevent race with disconnect
2416 Mutex::Autolock l(mLock);
2417 if (mStatusTracker != nullptr) {
2418 mStatusTracker->markComponentIdle(mInFlightStatusId, Fence::NO_FENCE);
2419 }
Shuzhen Wangcadb3302016-11-04 14:17:56 -07002420 }
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07002421 Mutex::Autolock l(mLock);
2422 mExpectedInflightDuration -= duration;
Shuzhen Wangcadb3302016-11-04 14:17:56 -07002423}
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002424
2425void Camera3Device::removeInFlightRequestIfReadyLocked(int idx) {
2426
2427 const InFlightRequest &request = mInFlightMap.valueAt(idx);
2428 const uint32_t frameNumber = mInFlightMap.keyAt(idx);
2429
2430 nsecs_t sensorTimestamp = request.sensorTimestamp;
2431 nsecs_t shutterTimestamp = request.shutterTimestamp;
2432
Emilian Peevc6747862017-06-12 15:57:26 +01002433 bool skipResultMetadata = false;
2434 if (request.requestStatus != OK) {
2435 switch (request.requestStatus) {
2436 case CAMERA3_MSG_ERROR_DEVICE:
2437 case CAMERA3_MSG_ERROR_REQUEST:
2438 case CAMERA3_MSG_ERROR_RESULT:
2439 skipResultMetadata = true;
2440 break;
2441 case CAMERA3_MSG_ERROR_BUFFER:
2442 //Result metadata should return in this case.
2443 skipResultMetadata = false;
2444 break;
2445 default:
2446 SET_ERR("Unknown error message: %d", request.requestStatus);
2447 skipResultMetadata = false;
2448 break;
2449 }
2450 }
2451
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002452 // Check if it's okay to remove the request from InFlightMap:
2453 // In the case of a successful request:
2454 // all input and output buffers, all result metadata, shutter callback
2455 // arrived.
2456 // In the case of a unsuccessful request:
2457 // all input and output buffers arrived.
2458 if (request.numBuffersLeft == 0 &&
Emilian Peevc6747862017-06-12 15:57:26 +01002459 (skipResultMetadata ||
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002460 (request.haveResultMetadata && shutterTimestamp != 0))) {
2461 ATRACE_ASYNC_END("frame capture", frameNumber);
2462
Shuzhen Wang403044a2017-02-26 23:29:04 -08002463 // Sanity check - if sensor timestamp matches shutter timestamp in the
2464 // case of request having callback.
2465 if (request.hasCallback && request.requestStatus == OK &&
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002466 sensorTimestamp != shutterTimestamp) {
2467 SET_ERR("sensor timestamp (%" PRId64
2468 ") for frame %d doesn't match shutter timestamp (%" PRId64 ")",
2469 sensorTimestamp, frameNumber, shutterTimestamp);
2470 }
2471
2472 // for an unsuccessful request, it may have pending output buffers to
2473 // return.
2474 assert(request.requestStatus != OK ||
2475 request.pendingOutputBuffers.size() == 0);
2476 returnOutputBuffers(request.pendingOutputBuffers.array(),
2477 request.pendingOutputBuffers.size(), 0);
2478
Shuzhen Wangcadb3302016-11-04 14:17:56 -07002479 removeInFlightMapEntryLocked(idx);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002480 ALOGVV("%s: removed frame %d from InFlightMap", __FUNCTION__, frameNumber);
2481 }
2482
2483 // Sanity check - if we have too many in-flight frames, something has
2484 // likely gone wrong
Chien-Yu Chenc96ac8d2015-08-12 16:46:24 -07002485 if (!mIsConstrainedHighSpeedConfiguration && mInFlightMap.size() > kInFlightWarnLimit) {
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002486 CLOGE("In-flight list too large: %zu", mInFlightMap.size());
Chien-Yu Chenc96ac8d2015-08-12 16:46:24 -07002487 } else if (mIsConstrainedHighSpeedConfiguration && mInFlightMap.size() >
2488 kInFlightWarnLimitHighSpeed) {
2489 CLOGE("In-flight list too large for high speed configuration: %zu",
2490 mInFlightMap.size());
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002491 }
2492}
2493
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -07002494void Camera3Device::flushInflightRequests() {
2495 { // First return buffers cached in mInFlightMap
2496 Mutex::Autolock l(mInFlightLock);
2497 for (size_t idx = 0; idx < mInFlightMap.size(); idx++) {
2498 const InFlightRequest &request = mInFlightMap.valueAt(idx);
2499 returnOutputBuffers(request.pendingOutputBuffers.array(),
2500 request.pendingOutputBuffers.size(), 0);
2501 }
2502 mInFlightMap.clear();
2503 }
2504
2505 // Then return all inflight buffers not returned by HAL
2506 std::vector<std::pair<int32_t, int32_t>> inflightKeys;
2507 mInterface->getInflightBufferKeys(&inflightKeys);
2508
2509 int32_t inputStreamId = (mInputStream != nullptr) ? mInputStream->getId() : -1;
2510 for (auto& pair : inflightKeys) {
2511 int32_t frameNumber = pair.first;
2512 int32_t streamId = pair.second;
2513 buffer_handle_t* buffer;
2514 status_t res = mInterface->popInflightBuffer(frameNumber, streamId, &buffer);
2515 if (res != OK) {
2516 ALOGE("%s: Frame %d: No in-flight buffer for stream %d",
2517 __FUNCTION__, frameNumber, streamId);
2518 continue;
2519 }
2520
2521 camera3_stream_buffer_t streamBuffer;
2522 streamBuffer.buffer = buffer;
2523 streamBuffer.status = CAMERA3_BUFFER_STATUS_ERROR;
2524 streamBuffer.acquire_fence = -1;
2525 streamBuffer.release_fence = -1;
Yin-Chia Yeh5090c732017-07-20 16:05:29 -07002526
2527 // First check if the buffer belongs to deleted stream
2528 bool streamDeleted = false;
2529 for (auto& stream : mDeletedStreams) {
2530 if (streamId == stream->getId()) {
2531 streamDeleted = true;
2532 // Return buffer to deleted stream
2533 camera3_stream* halStream = stream->asHalStream();
2534 streamBuffer.stream = halStream;
2535 switch (halStream->stream_type) {
2536 case CAMERA3_STREAM_OUTPUT:
2537 res = stream->returnBuffer(streamBuffer, /*timestamp*/ 0);
2538 if (res != OK) {
2539 ALOGE("%s: Can't return output buffer for frame %d to"
2540 " stream %d: %s (%d)", __FUNCTION__,
2541 frameNumber, streamId, strerror(-res), res);
2542 }
2543 break;
2544 case CAMERA3_STREAM_INPUT:
2545 res = stream->returnInputBuffer(streamBuffer);
2546 if (res != OK) {
2547 ALOGE("%s: Can't return input buffer for frame %d to"
2548 " stream %d: %s (%d)", __FUNCTION__,
2549 frameNumber, streamId, strerror(-res), res);
2550 }
2551 break;
2552 default: // Bi-direcitonal stream is deprecated
2553 ALOGE("%s: stream %d has unknown stream type %d",
2554 __FUNCTION__, streamId, halStream->stream_type);
2555 break;
2556 }
2557 break;
2558 }
2559 }
2560 if (streamDeleted) {
2561 continue;
2562 }
2563
2564 // Then check against configured streams
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -07002565 if (streamId == inputStreamId) {
2566 streamBuffer.stream = mInputStream->asHalStream();
2567 res = mInputStream->returnInputBuffer(streamBuffer);
2568 if (res != OK) {
2569 ALOGE("%s: Can't return input buffer for frame %d to"
Yin-Chia Yeh5090c732017-07-20 16:05:29 -07002570 " stream %d: %s (%d)", __FUNCTION__,
2571 frameNumber, streamId, strerror(-res), res);
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -07002572 }
2573 } else {
Yin-Chia Yeh5090c732017-07-20 16:05:29 -07002574 ssize_t idx = mOutputStreams.indexOfKey(streamId);
2575 if (idx == NAME_NOT_FOUND) {
2576 ALOGE("%s: Output stream id %d not found!", __FUNCTION__, streamId);
2577 continue;
2578 }
2579 streamBuffer.stream = mOutputStreams.valueAt(idx)->asHalStream();
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -07002580 returnOutputBuffers(&streamBuffer, /*size*/1, /*timestamp*/ 0);
2581 }
2582 }
2583}
2584
Emilian Peev7e25e5e2017-04-07 15:48:49 +01002585void Camera3Device::insertResultLocked(CaptureResult *result,
2586 uint32_t frameNumber) {
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002587 if (result == nullptr) return;
2588
Emilian Peev71c73a22017-03-21 16:35:51 +00002589 camera_metadata_t *meta = const_cast<camera_metadata_t *>(
2590 result->mMetadata.getAndLock());
2591 set_camera_metadata_vendor_id(meta, mVendorTagId);
2592 result->mMetadata.unlock(meta);
2593
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002594 if (result->mMetadata.update(ANDROID_REQUEST_FRAME_COUNT,
2595 (int32_t*)&frameNumber, 1) != OK) {
2596 SET_ERR("Failed to set frame number %d in metadata", frameNumber);
2597 return;
2598 }
2599
2600 if (result->mMetadata.update(ANDROID_REQUEST_ID, &result->mResultExtras.requestId, 1) != OK) {
2601 SET_ERR("Failed to set request ID in metadata for frame %d", frameNumber);
2602 return;
2603 }
2604
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002605 // Valid result, insert into queue
2606 List<CaptureResult>::iterator queuedResult =
2607 mResultQueue.insert(mResultQueue.end(), CaptureResult(*result));
2608 ALOGVV("%s: result requestId = %" PRId32 ", frameNumber = %" PRId64
2609 ", burstId = %" PRId32, __FUNCTION__,
2610 queuedResult->mResultExtras.requestId,
2611 queuedResult->mResultExtras.frameNumber,
2612 queuedResult->mResultExtras.burstId);
2613
2614 mResultSignal.signal();
2615}
2616
2617
2618void Camera3Device::sendPartialCaptureResult(const camera_metadata_t * partialResult,
Emilian Peev7e25e5e2017-04-07 15:48:49 +01002619 const CaptureResultExtras &resultExtras, uint32_t frameNumber) {
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002620 Mutex::Autolock l(mOutputLock);
2621
2622 CaptureResult captureResult;
2623 captureResult.mResultExtras = resultExtras;
2624 captureResult.mMetadata = partialResult;
2625
Emilian Peev7e25e5e2017-04-07 15:48:49 +01002626 insertResultLocked(&captureResult, frameNumber);
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002627}
2628
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002629
2630void Camera3Device::sendCaptureResult(CameraMetadata &pendingMetadata,
2631 CaptureResultExtras &resultExtras,
2632 CameraMetadata &collectedPartialResult,
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002633 uint32_t frameNumber,
Emilian Peev7e25e5e2017-04-07 15:48:49 +01002634 bool reprocess) {
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002635 if (pendingMetadata.isEmpty())
2636 return;
2637
2638 Mutex::Autolock l(mOutputLock);
2639
2640 // TODO: need to track errors for tighter bounds on expected frame number
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002641 if (reprocess) {
2642 if (frameNumber < mNextReprocessResultFrameNumber) {
2643 SET_ERR("Out-of-order reprocess capture result metadata submitted! "
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002644 "(got frame number %d, expecting %d)",
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002645 frameNumber, mNextReprocessResultFrameNumber);
2646 return;
2647 }
2648 mNextReprocessResultFrameNumber = frameNumber + 1;
2649 } else {
2650 if (frameNumber < mNextResultFrameNumber) {
2651 SET_ERR("Out-of-order capture result metadata submitted! "
2652 "(got frame number %d, expecting %d)",
2653 frameNumber, mNextResultFrameNumber);
2654 return;
2655 }
2656 mNextResultFrameNumber = frameNumber + 1;
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002657 }
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002658
2659 CaptureResult captureResult;
2660 captureResult.mResultExtras = resultExtras;
2661 captureResult.mMetadata = pendingMetadata;
2662
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002663 // Append any previous partials to form a complete result
2664 if (mUsePartialResult && !collectedPartialResult.isEmpty()) {
2665 captureResult.mMetadata.append(collectedPartialResult);
2666 }
2667
2668 captureResult.mMetadata.sort();
2669
2670 // Check that there's a timestamp in the result metadata
Eino-Ville Talvala4d453832016-07-15 11:56:53 -07002671 camera_metadata_entry timestamp = captureResult.mMetadata.find(ANDROID_SENSOR_TIMESTAMP);
2672 if (timestamp.count == 0) {
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002673 SET_ERR("No timestamp provided by HAL for frame %d!",
2674 frameNumber);
2675 return;
2676 }
2677
Eino-Ville Talvala4d453832016-07-15 11:56:53 -07002678 mTagMonitor.monitorMetadata(TagMonitor::RESULT,
2679 frameNumber, timestamp.data.i64[0], captureResult.mMetadata);
2680
Emilian Peev7e25e5e2017-04-07 15:48:49 +01002681 insertResultLocked(&captureResult, frameNumber);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002682}
2683
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002684/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002685 * Camera HAL device callback methods
2686 */
2687
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002688void Camera3Device::processCaptureResult(const camera3_capture_result *result) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002689 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002690
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002691 status_t res;
2692
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002693 uint32_t frameNumber = result->frame_number;
Zhijun Hef0d962a2014-06-30 10:24:11 -07002694 if (result->result == NULL && result->num_output_buffers == 0 &&
2695 result->input_buffer == NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002696 SET_ERR("No result data provided by HAL for frame %d",
2697 frameNumber);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002698 return;
2699 }
Zhijun He204e3292014-07-14 17:09:23 -07002700
Zhijun He204e3292014-07-14 17:09:23 -07002701 if (!mUsePartialResult &&
Zhijun He204e3292014-07-14 17:09:23 -07002702 result->result != NULL &&
2703 result->partial_result != 1) {
2704 SET_ERR("Result is malformed for frame %d: partial_result %u must be 1"
2705 " if partial result is not supported",
2706 frameNumber, result->partial_result);
2707 return;
2708 }
2709
2710 bool isPartialResult = false;
2711 CameraMetadata collectedPartialResult;
Jianing Weicb0652e2014-03-12 18:29:36 -07002712 CaptureResultExtras resultExtras;
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002713 bool hasInputBufferInRequest = false;
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002714
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002715 // Get shutter timestamp and resultExtras from list of in-flight requests,
2716 // where it was added by the shutter notification for this frame. If the
2717 // shutter timestamp isn't received yet, append the output buffers to the
2718 // in-flight request and they will be returned when the shutter timestamp
2719 // arrives. Update the in-flight status and remove the in-flight entry if
2720 // all result data and shutter timestamp have been received.
2721 nsecs_t shutterTimestamp = 0;
2722
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002723 {
2724 Mutex::Autolock l(mInFlightLock);
2725 ssize_t idx = mInFlightMap.indexOfKey(frameNumber);
2726 if (idx == NAME_NOT_FOUND) {
2727 SET_ERR("Unknown frame number for capture result: %d",
2728 frameNumber);
2729 return;
2730 }
2731 InFlightRequest &request = mInFlightMap.editValueAt(idx);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002732 ALOGVV("%s: got InFlightRequest requestId = %" PRId32
2733 ", frameNumber = %" PRId64 ", burstId = %" PRId32
Shuzhen Wang4a472662017-02-26 23:29:04 -08002734 ", partialResultCount = %d, hasCallback = %d",
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002735 __FUNCTION__, request.resultExtras.requestId,
2736 request.resultExtras.frameNumber, request.resultExtras.burstId,
Shuzhen Wang4a472662017-02-26 23:29:04 -08002737 result->partial_result, request.hasCallback);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002738 // Always update the partial count to the latest one if it's not 0
2739 // (buffers only). When framework aggregates adjacent partial results
2740 // into one, the latest partial count will be used.
2741 if (result->partial_result != 0)
2742 request.resultExtras.partialResultCount = result->partial_result;
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002743
2744 // Check if this result carries only partial metadata
Zhijun He204e3292014-07-14 17:09:23 -07002745 if (mUsePartialResult && result->result != NULL) {
Emilian Peev08dd2452017-04-06 16:55:14 +01002746 if (result->partial_result > mNumPartialResults || result->partial_result < 1) {
2747 SET_ERR("Result is malformed for frame %d: partial_result %u must be in"
2748 " the range of [1, %d] when metadata is included in the result",
2749 frameNumber, result->partial_result, mNumPartialResults);
2750 return;
2751 }
2752 isPartialResult = (result->partial_result < mNumPartialResults);
2753 if (isPartialResult) {
2754 request.collectedPartialResult.append(result->result);
Zhijun He204e3292014-07-14 17:09:23 -07002755 }
2756
Shuzhen Wang4a472662017-02-26 23:29:04 -08002757 if (isPartialResult && request.hasCallback) {
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002758 // Send partial capture result
Emilian Peev7e25e5e2017-04-07 15:48:49 +01002759 sendPartialCaptureResult(result->result, request.resultExtras,
2760 frameNumber);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002761 }
2762 }
2763
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002764 shutterTimestamp = request.shutterTimestamp;
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002765 hasInputBufferInRequest = request.hasInputBuffer;
Jianing Weicb0652e2014-03-12 18:29:36 -07002766
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002767 // Did we get the (final) result metadata for this capture?
Zhijun He204e3292014-07-14 17:09:23 -07002768 if (result->result != NULL && !isPartialResult) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002769 if (request.haveResultMetadata) {
2770 SET_ERR("Called multiple times with metadata for frame %d",
2771 frameNumber);
2772 return;
2773 }
Zhijun He204e3292014-07-14 17:09:23 -07002774 if (mUsePartialResult &&
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002775 !request.collectedPartialResult.isEmpty()) {
Zhijun He204e3292014-07-14 17:09:23 -07002776 collectedPartialResult.acquire(
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002777 request.collectedPartialResult);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002778 }
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002779 request.haveResultMetadata = true;
2780 }
2781
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002782 uint32_t numBuffersReturned = result->num_output_buffers;
2783 if (result->input_buffer != NULL) {
2784 if (hasInputBufferInRequest) {
2785 numBuffersReturned += 1;
2786 } else {
2787 ALOGW("%s: Input buffer should be NULL if there is no input"
2788 " buffer sent in the request",
2789 __FUNCTION__);
2790 }
2791 }
2792 request.numBuffersLeft -= numBuffersReturned;
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002793 if (request.numBuffersLeft < 0) {
2794 SET_ERR("Too many buffers returned for frame %d",
2795 frameNumber);
2796 return;
2797 }
2798
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002799 camera_metadata_ro_entry_t entry;
2800 res = find_camera_metadata_ro_entry(result->result,
2801 ANDROID_SENSOR_TIMESTAMP, &entry);
2802 if (res == OK && entry.count == 1) {
2803 request.sensorTimestamp = entry.data.i64[0];
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002804 }
2805
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002806 // If shutter event isn't received yet, append the output buffers to
2807 // the in-flight request. Otherwise, return the output buffers to
2808 // streams.
2809 if (shutterTimestamp == 0) {
2810 request.pendingOutputBuffers.appendArray(result->output_buffers,
2811 result->num_output_buffers);
Igor Murashkind2c90692013-04-02 12:32:32 -07002812 } else {
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002813 returnOutputBuffers(result->output_buffers,
2814 result->num_output_buffers, shutterTimestamp);
Igor Murashkind2c90692013-04-02 12:32:32 -07002815 }
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002816
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002817 if (result->result != NULL && !isPartialResult) {
2818 if (shutterTimestamp == 0) {
2819 request.pendingMetadata = result->result;
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002820 request.collectedPartialResult = collectedPartialResult;
Shuzhen Wang4a472662017-02-26 23:29:04 -08002821 } else if (request.hasCallback) {
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002822 CameraMetadata metadata;
2823 metadata = result->result;
2824 sendCaptureResult(metadata, request.resultExtras,
Emilian Peev7e25e5e2017-04-07 15:48:49 +01002825 collectedPartialResult, frameNumber,
2826 hasInputBufferInRequest);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002827 }
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002828 }
2829
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002830 removeInFlightRequestIfReadyLocked(idx);
2831 } // scope for mInFlightLock
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002832
Zhijun Hef0d962a2014-06-30 10:24:11 -07002833 if (result->input_buffer != NULL) {
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002834 if (hasInputBufferInRequest) {
2835 Camera3Stream *stream =
2836 Camera3Stream::cast(result->input_buffer->stream);
2837 res = stream->returnInputBuffer(*(result->input_buffer));
2838 // Note: stream may be deallocated at this point, if this buffer was the
2839 // last reference to it.
2840 if (res != OK) {
2841 ALOGE("%s: RequestThread: Can't return input buffer for frame %d to"
2842 " its stream:%s (%d)", __FUNCTION__,
2843 frameNumber, strerror(-res), res);
Zhijun He0ea8fa42014-07-07 17:05:38 -07002844 }
2845 } else {
2846 ALOGW("%s: Input buffer should be NULL if there is no input"
2847 " buffer sent in the request, skipping input buffer return.",
2848 __FUNCTION__);
Zhijun Hef0d962a2014-06-30 10:24:11 -07002849 }
2850 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002851}
2852
2853void Camera3Device::notify(const camera3_notify_msg *msg) {
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07002854 ATRACE_CALL();
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07002855 sp<NotificationListener> listener;
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002856 {
2857 Mutex::Autolock l(mOutputLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07002858 listener = mListener.promote();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002859 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002860
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002861 if (msg == NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002862 SET_ERR("HAL sent NULL notify message!");
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002863 return;
2864 }
2865
2866 switch (msg->type) {
2867 case CAMERA3_MSG_ERROR: {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002868 notifyError(msg->message.error, listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002869 break;
2870 }
2871 case CAMERA3_MSG_SHUTTER: {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002872 notifyShutter(msg->message.shutter, listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002873 break;
2874 }
2875 default:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002876 SET_ERR("Unknown notify message from HAL: %d",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002877 msg->type);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002878 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002879}
2880
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002881void Camera3Device::notifyError(const camera3_error_msg_t &msg,
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07002882 sp<NotificationListener> listener) {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002883
2884 // Map camera HAL error codes to ICameraDeviceCallback error codes
2885 // Index into this with the HAL error code
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002886 static const int32_t halErrorMap[CAMERA3_MSG_NUM_ERRORS] = {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002887 // 0 = Unused error code
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002888 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_INVALID_ERROR,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002889 // 1 = CAMERA3_MSG_ERROR_DEVICE
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002890 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002891 // 2 = CAMERA3_MSG_ERROR_REQUEST
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002892 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002893 // 3 = CAMERA3_MSG_ERROR_RESULT
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002894 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_RESULT,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002895 // 4 = CAMERA3_MSG_ERROR_BUFFER
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002896 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_BUFFER
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002897 };
2898
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002899 int32_t errorCode =
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002900 ((msg.error_code >= 0) &&
2901 (msg.error_code < CAMERA3_MSG_NUM_ERRORS)) ?
2902 halErrorMap[msg.error_code] :
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002903 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_INVALID_ERROR;
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002904
2905 int streamId = 0;
2906 if (msg.error_stream != NULL) {
2907 Camera3Stream *stream =
2908 Camera3Stream::cast(msg.error_stream);
2909 streamId = stream->getId();
2910 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002911 ALOGV("Camera %s: %s: HAL error, frame %d, stream %d: %d",
2912 mId.string(), __FUNCTION__, msg.frame_number,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002913 streamId, msg.error_code);
2914
2915 CaptureResultExtras resultExtras;
2916 switch (errorCode) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002917 case hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE:
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002918 // SET_ERR calls notifyError
2919 SET_ERR("Camera HAL reported serious device error");
2920 break;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002921 case hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST:
2922 case hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_RESULT:
2923 case hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_BUFFER:
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002924 {
2925 Mutex::Autolock l(mInFlightLock);
2926 ssize_t idx = mInFlightMap.indexOfKey(msg.frame_number);
2927 if (idx >= 0) {
2928 InFlightRequest &r = mInFlightMap.editValueAt(idx);
2929 r.requestStatus = msg.error_code;
2930 resultExtras = r.resultExtras;
Emilian Peevba0fac32017-03-30 09:05:34 +01002931 if (hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_RESULT ==
2932 errorCode) {
2933 // In case of missing result check whether the buffers
2934 // returned. If they returned, then remove inflight
2935 // request.
2936 removeInFlightRequestIfReadyLocked(idx);
2937 }
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002938 } else {
2939 resultExtras.frameNumber = msg.frame_number;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002940 ALOGE("Camera %s: %s: cannot find in-flight request on "
2941 "frame %" PRId64 " error", mId.string(), __FUNCTION__,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002942 resultExtras.frameNumber);
2943 }
2944 }
Eino-Ville Talvalae95bb632016-03-06 19:55:44 -08002945 resultExtras.errorStreamId = streamId;
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002946 if (listener != NULL) {
2947 listener->notifyError(errorCode, resultExtras);
2948 } else {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002949 ALOGE("Camera %s: %s: no listener available", mId.string(), __FUNCTION__);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002950 }
2951 break;
2952 default:
2953 // SET_ERR calls notifyError
2954 SET_ERR("Unknown error message from HAL: %d", msg.error_code);
2955 break;
2956 }
2957}
2958
2959void Camera3Device::notifyShutter(const camera3_shutter_msg_t &msg,
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07002960 sp<NotificationListener> listener) {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002961 ssize_t idx;
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002962
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002963 // Set timestamp for the request in the in-flight tracking
2964 // and get the request ID to send upstream
2965 {
2966 Mutex::Autolock l(mInFlightLock);
2967 idx = mInFlightMap.indexOfKey(msg.frame_number);
2968 if (idx >= 0) {
2969 InFlightRequest &r = mInFlightMap.editValueAt(idx);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002970
Chien-Yu Chen3df11ce2015-09-30 14:13:30 -07002971 // Verify ordering of shutter notifications
2972 {
2973 Mutex::Autolock l(mOutputLock);
2974 // TODO: need to track errors for tighter bounds on expected frame number.
2975 if (r.hasInputBuffer) {
2976 if (msg.frame_number < mNextReprocessShutterFrameNumber) {
2977 SET_ERR("Shutter notification out-of-order. Expected "
2978 "notification for frame %d, got frame %d",
2979 mNextReprocessShutterFrameNumber, msg.frame_number);
2980 return;
2981 }
2982 mNextReprocessShutterFrameNumber = msg.frame_number + 1;
2983 } else {
2984 if (msg.frame_number < mNextShutterFrameNumber) {
2985 SET_ERR("Shutter notification out-of-order. Expected "
2986 "notification for frame %d, got frame %d",
2987 mNextShutterFrameNumber, msg.frame_number);
2988 return;
2989 }
2990 mNextShutterFrameNumber = msg.frame_number + 1;
2991 }
2992 }
2993
Shuzhen Wang4a472662017-02-26 23:29:04 -08002994 r.shutterTimestamp = msg.timestamp;
2995 if (r.hasCallback) {
2996 ALOGVV("Camera %s: %s: Shutter fired for frame %d (id %d) at %" PRId64,
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002997 mId.string(), __FUNCTION__,
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002998 msg.frame_number, r.resultExtras.requestId, msg.timestamp);
Shuzhen Wang4a472662017-02-26 23:29:04 -08002999 // Call listener, if any
3000 if (listener != NULL) {
3001 listener->notifyShutter(r.resultExtras, msg.timestamp);
3002 }
3003 // send pending result and buffers
3004 sendCaptureResult(r.pendingMetadata, r.resultExtras,
3005 r.collectedPartialResult, msg.frame_number,
Emilian Peev7e25e5e2017-04-07 15:48:49 +01003006 r.hasInputBuffer);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003007 }
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003008 returnOutputBuffers(r.pendingOutputBuffers.array(),
3009 r.pendingOutputBuffers.size(), r.shutterTimestamp);
3010 r.pendingOutputBuffers.clear();
3011
3012 removeInFlightRequestIfReadyLocked(idx);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003013 }
3014 }
3015 if (idx < 0) {
3016 SET_ERR("Shutter notification for non-existent frame number %d",
3017 msg.frame_number);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003018 }
3019}
3020
3021
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003022CameraMetadata Camera3Device::getLatestRequestLocked() {
Igor Murashkin1e479c02013-09-06 16:55:14 -07003023 ALOGV("%s", __FUNCTION__);
3024
Igor Murashkin1e479c02013-09-06 16:55:14 -07003025 CameraMetadata retVal;
3026
3027 if (mRequestThread != NULL) {
3028 retVal = mRequestThread->getLatestRequest();
3029 }
3030
Igor Murashkin1e479c02013-09-06 16:55:14 -07003031 return retVal;
3032}
3033
Jianing Weicb0652e2014-03-12 18:29:36 -07003034
Eino-Ville Talvala4d453832016-07-15 11:56:53 -07003035void Camera3Device::monitorMetadata(TagMonitor::eventSource source,
3036 int64_t frameNumber, nsecs_t timestamp, const CameraMetadata& metadata) {
3037 mTagMonitor.monitorMetadata(source, frameNumber, timestamp, metadata);
3038}
3039
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08003040/**
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003041 * HalInterface inner class methods
3042 */
3043
Yifan Hongf79b5542017-04-11 14:44:25 -07003044Camera3Device::HalInterface::HalInterface(
3045 sp<ICameraDeviceSession> &session,
3046 std::shared_ptr<RequestMetadataQueue> queue) :
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003047 mHal3Device(nullptr),
Yifan Hongf79b5542017-04-11 14:44:25 -07003048 mHidlSession(session),
3049 mRequestMetadataQueue(queue) {}
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003050
3051Camera3Device::HalInterface::HalInterface() :
3052 mHal3Device(nullptr) {}
3053
3054Camera3Device::HalInterface::HalInterface(const HalInterface& other) :
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003055 mHal3Device(other.mHal3Device),
Yifan Hongf79b5542017-04-11 14:44:25 -07003056 mHidlSession(other.mHidlSession),
3057 mRequestMetadataQueue(other.mRequestMetadataQueue) {}
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003058
3059bool Camera3Device::HalInterface::valid() {
3060 return (mHal3Device != nullptr) || (mHidlSession != nullptr);
3061}
3062
3063void Camera3Device::HalInterface::clear() {
3064 mHal3Device = nullptr;
3065 mHidlSession.clear();
3066}
3067
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003068bool Camera3Device::HalInterface::supportBatchRequest() {
3069 return mHidlSession != nullptr;
3070}
3071
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003072status_t Camera3Device::HalInterface::constructDefaultRequestSettings(
3073 camera3_request_template_t templateId,
3074 /*out*/ camera_metadata_t **requestTemplate) {
3075 ATRACE_NAME("CameraHal::constructDefaultRequestSettings");
3076 if (!valid()) return INVALID_OPERATION;
3077 status_t res = OK;
3078
3079 if (mHal3Device != nullptr) {
3080 const camera_metadata *r;
3081 r = mHal3Device->ops->construct_default_request_settings(
3082 mHal3Device, templateId);
3083 if (r == nullptr) return BAD_VALUE;
3084 *requestTemplate = clone_camera_metadata(r);
3085 if (requestTemplate == nullptr) {
3086 ALOGE("%s: Unable to clone camera metadata received from HAL",
3087 __FUNCTION__);
3088 return INVALID_OPERATION;
3089 }
3090 } else {
3091 common::V1_0::Status status;
3092 RequestTemplate id;
3093 switch (templateId) {
3094 case CAMERA3_TEMPLATE_PREVIEW:
3095 id = RequestTemplate::PREVIEW;
3096 break;
3097 case CAMERA3_TEMPLATE_STILL_CAPTURE:
3098 id = RequestTemplate::STILL_CAPTURE;
3099 break;
3100 case CAMERA3_TEMPLATE_VIDEO_RECORD:
3101 id = RequestTemplate::VIDEO_RECORD;
3102 break;
3103 case CAMERA3_TEMPLATE_VIDEO_SNAPSHOT:
3104 id = RequestTemplate::VIDEO_SNAPSHOT;
3105 break;
3106 case CAMERA3_TEMPLATE_ZERO_SHUTTER_LAG:
3107 id = RequestTemplate::ZERO_SHUTTER_LAG;
3108 break;
3109 case CAMERA3_TEMPLATE_MANUAL:
3110 id = RequestTemplate::MANUAL;
3111 break;
3112 default:
3113 // Unknown template ID
3114 return BAD_VALUE;
3115 }
Eino-Ville Talvalac5cbb872017-04-27 12:48:33 -07003116 auto err = mHidlSession->constructDefaultRequestSettings(id,
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003117 [&status, &requestTemplate]
3118 (common::V1_0::Status s, const device::V3_2::CameraMetadata& request) {
3119 status = s;
3120 if (status == common::V1_0::Status::OK) {
3121 const camera_metadata *r =
3122 reinterpret_cast<const camera_metadata_t*>(request.data());
3123 size_t expectedSize = request.size();
3124 int ret = validate_camera_metadata_structure(r, &expectedSize);
Yin-Chia Yeh238ef5f2017-04-18 15:01:15 -07003125 if (ret == OK || ret == CAMERA_METADATA_VALIDATION_SHIFTED) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003126 *requestTemplate = clone_camera_metadata(r);
3127 if (*requestTemplate == nullptr) {
3128 ALOGE("%s: Unable to clone camera metadata received from HAL",
3129 __FUNCTION__);
3130 status = common::V1_0::Status::INTERNAL_ERROR;
3131 }
3132 } else {
3133 ALOGE("%s: Malformed camera metadata received from HAL", __FUNCTION__);
3134 status = common::V1_0::Status::INTERNAL_ERROR;
3135 }
3136 }
3137 });
Eino-Ville Talvalac5cbb872017-04-27 12:48:33 -07003138 if (!err.isOk()) {
3139 ALOGE("%s: Transaction error: %s", __FUNCTION__, err.description().c_str());
3140 res = DEAD_OBJECT;
3141 } else {
3142 res = CameraProviderManager::mapToStatusT(status);
3143 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003144 }
3145 return res;
3146}
3147
3148status_t Camera3Device::HalInterface::configureStreams(camera3_stream_configuration *config) {
3149 ATRACE_NAME("CameraHal::configureStreams");
3150 if (!valid()) return INVALID_OPERATION;
3151 status_t res = OK;
3152
3153 if (mHal3Device != nullptr) {
3154 res = mHal3Device->ops->configure_streams(mHal3Device, config);
3155 } else {
3156 // Convert stream config to HIDL
Yin-Chia Yeh77327052017-01-09 18:23:07 -08003157 std::set<int> activeStreams;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003158 StreamConfiguration requestedConfiguration;
3159 requestedConfiguration.streams.resize(config->num_streams);
3160 for (size_t i = 0; i < config->num_streams; i++) {
3161 Stream &dst = requestedConfiguration.streams[i];
3162 camera3_stream_t *src = config->streams[i];
3163
Yin-Chia Yehbe83fa72017-03-30 13:35:36 -07003164 Camera3Stream* cam3stream = Camera3Stream::cast(src);
3165 cam3stream->setBufferFreedListener(this);
3166 int streamId = cam3stream->getId();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003167 StreamType streamType;
3168 switch (src->stream_type) {
3169 case CAMERA3_STREAM_OUTPUT:
3170 streamType = StreamType::OUTPUT;
3171 break;
3172 case CAMERA3_STREAM_INPUT:
3173 streamType = StreamType::INPUT;
3174 break;
3175 default:
3176 ALOGE("%s: Stream %d: Unsupported stream type %d",
3177 __FUNCTION__, streamId, config->streams[i]->stream_type);
3178 return BAD_VALUE;
3179 }
3180 dst.id = streamId;
3181 dst.streamType = streamType;
3182 dst.width = src->width;
3183 dst.height = src->height;
3184 dst.format = mapToPixelFormat(src->format);
Yin-Chia Yeh47cf8e62017-04-04 13:00:03 -07003185 dst.usage = mapToConsumerUsage(src->usage);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003186 dst.dataSpace = mapToHidlDataspace(src->data_space);
3187 dst.rotation = mapToStreamRotation((camera3_stream_rotation_t) src->rotation);
Yin-Chia Yeh77327052017-01-09 18:23:07 -08003188
3189 activeStreams.insert(streamId);
3190 // Create Buffer ID map if necessary
3191 if (mBufferIdMaps.count(streamId) == 0) {
3192 mBufferIdMaps.emplace(streamId, BufferIdMap{});
3193 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003194 }
Yin-Chia Yeh77327052017-01-09 18:23:07 -08003195 // remove BufferIdMap for deleted streams
3196 for(auto it = mBufferIdMaps.begin(); it != mBufferIdMaps.end();) {
3197 int streamId = it->first;
3198 bool active = activeStreams.count(streamId) > 0;
3199 if (!active) {
3200 it = mBufferIdMaps.erase(it);
3201 } else {
3202 ++it;
3203 }
3204 }
3205
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -08003206 res = mapToStreamConfigurationMode(
3207 (camera3_stream_configuration_mode_t) config->operation_mode,
3208 /*out*/ &requestedConfiguration.operationMode);
3209 if (res != OK) {
3210 return res;
3211 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003212
3213 // Invoke configureStreams
3214
3215 HalStreamConfiguration finalConfiguration;
3216 common::V1_0::Status status;
Eino-Ville Talvalac5cbb872017-04-27 12:48:33 -07003217 auto err = mHidlSession->configureStreams(requestedConfiguration,
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003218 [&status, &finalConfiguration]
3219 (common::V1_0::Status s, const HalStreamConfiguration& halConfiguration) {
3220 finalConfiguration = halConfiguration;
3221 status = s;
3222 });
Eino-Ville Talvalac5cbb872017-04-27 12:48:33 -07003223 if (!err.isOk()) {
3224 ALOGE("%s: Transaction error: %s", __FUNCTION__, err.description().c_str());
3225 return DEAD_OBJECT;
3226 }
3227
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003228 if (status != common::V1_0::Status::OK ) {
3229 return CameraProviderManager::mapToStatusT(status);
3230 }
3231
3232 // And convert output stream configuration from HIDL
3233
3234 for (size_t i = 0; i < config->num_streams; i++) {
3235 camera3_stream_t *dst = config->streams[i];
3236 int streamId = Camera3Stream::cast(dst)->getId();
3237
3238 // Start scan at i, with the assumption that the stream order matches
3239 size_t realIdx = i;
3240 bool found = false;
3241 for (size_t idx = 0; idx < finalConfiguration.streams.size(); idx++) {
3242 if (finalConfiguration.streams[realIdx].id == streamId) {
3243 found = true;
3244 break;
3245 }
3246 realIdx = (realIdx >= finalConfiguration.streams.size()) ? 0 : realIdx + 1;
3247 }
3248 if (!found) {
3249 ALOGE("%s: Stream %d not found in stream configuration response from HAL",
3250 __FUNCTION__, streamId);
3251 return INVALID_OPERATION;
3252 }
3253 HalStream &src = finalConfiguration.streams[realIdx];
3254
3255 int overrideFormat = mapToFrameworkFormat(src.overrideFormat);
3256 if (dst->format != HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED) {
3257 if (dst->format != overrideFormat) {
3258 ALOGE("%s: Stream %d: Format override not allowed for format 0x%x", __FUNCTION__,
3259 streamId, dst->format);
3260 }
3261 } else {
3262 // Override allowed with IMPLEMENTATION_DEFINED
3263 dst->format = overrideFormat;
3264 }
3265
3266 if (dst->stream_type == CAMERA3_STREAM_INPUT) {
3267 if (src.producerUsage != 0) {
3268 ALOGE("%s: Stream %d: INPUT streams must have 0 for producer usage",
3269 __FUNCTION__, streamId);
3270 return INVALID_OPERATION;
3271 }
Yin-Chia Yeh47cf8e62017-04-04 13:00:03 -07003272 dst->usage = mapConsumerToFrameworkUsage(src.consumerUsage);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003273 } else {
3274 // OUTPUT
3275 if (src.consumerUsage != 0) {
3276 ALOGE("%s: Stream %d: OUTPUT streams must have 0 for consumer usage",
3277 __FUNCTION__, streamId);
3278 return INVALID_OPERATION;
3279 }
Yin-Chia Yeh47cf8e62017-04-04 13:00:03 -07003280 dst->usage = mapProducerToFrameworkUsage(src.producerUsage);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003281 }
3282 dst->max_buffers = src.maxBuffers;
3283 }
3284 }
3285 return res;
3286}
3287
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003288void Camera3Device::HalInterface::wrapAsHidlRequest(camera3_capture_request_t* request,
3289 /*out*/device::V3_2::CaptureRequest* captureRequest,
3290 /*out*/std::vector<native_handle_t*>* handlesCreated) {
3291
3292 if (captureRequest == nullptr || handlesCreated == nullptr) {
3293 ALOGE("%s: captureRequest (%p) and handlesCreated (%p) must not be null",
3294 __FUNCTION__, captureRequest, handlesCreated);
3295 return;
3296 }
3297
3298 captureRequest->frameNumber = request->frame_number;
Yifan Hongf79b5542017-04-11 14:44:25 -07003299
3300 captureRequest->fmqSettingsSize = 0;
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003301
3302 {
3303 std::lock_guard<std::mutex> lock(mInflightLock);
3304 if (request->input_buffer != nullptr) {
3305 int32_t streamId = Camera3Stream::cast(request->input_buffer->stream)->getId();
3306 buffer_handle_t buf = *(request->input_buffer->buffer);
3307 auto pair = getBufferId(buf, streamId);
3308 bool isNewBuffer = pair.first;
3309 uint64_t bufferId = pair.second;
3310 captureRequest->inputBuffer.streamId = streamId;
3311 captureRequest->inputBuffer.bufferId = bufferId;
3312 captureRequest->inputBuffer.buffer = (isNewBuffer) ? buf : nullptr;
3313 captureRequest->inputBuffer.status = BufferStatus::OK;
3314 native_handle_t *acquireFence = nullptr;
3315 if (request->input_buffer->acquire_fence != -1) {
3316 acquireFence = native_handle_create(1,0);
3317 acquireFence->data[0] = request->input_buffer->acquire_fence;
3318 handlesCreated->push_back(acquireFence);
3319 }
3320 captureRequest->inputBuffer.acquireFence = acquireFence;
3321 captureRequest->inputBuffer.releaseFence = nullptr;
3322
3323 pushInflightBufferLocked(captureRequest->frameNumber, streamId,
3324 request->input_buffer->buffer,
3325 request->input_buffer->acquire_fence);
3326 } else {
3327 captureRequest->inputBuffer.streamId = -1;
3328 captureRequest->inputBuffer.bufferId = BUFFER_ID_NO_BUFFER;
3329 }
3330
3331 captureRequest->outputBuffers.resize(request->num_output_buffers);
3332 for (size_t i = 0; i < request->num_output_buffers; i++) {
3333 const camera3_stream_buffer_t *src = request->output_buffers + i;
3334 StreamBuffer &dst = captureRequest->outputBuffers[i];
3335 int32_t streamId = Camera3Stream::cast(src->stream)->getId();
3336 buffer_handle_t buf = *(src->buffer);
3337 auto pair = getBufferId(buf, streamId);
3338 bool isNewBuffer = pair.first;
3339 dst.streamId = streamId;
3340 dst.bufferId = pair.second;
3341 dst.buffer = isNewBuffer ? buf : nullptr;
3342 dst.status = BufferStatus::OK;
3343 native_handle_t *acquireFence = nullptr;
3344 if (src->acquire_fence != -1) {
3345 acquireFence = native_handle_create(1,0);
3346 acquireFence->data[0] = src->acquire_fence;
3347 handlesCreated->push_back(acquireFence);
3348 }
3349 dst.acquireFence = acquireFence;
3350 dst.releaseFence = nullptr;
3351
3352 pushInflightBufferLocked(captureRequest->frameNumber, streamId,
3353 src->buffer, src->acquire_fence);
3354 }
3355 }
3356}
3357
3358status_t Camera3Device::HalInterface::processBatchCaptureRequests(
3359 std::vector<camera3_capture_request_t*>& requests,/*out*/uint32_t* numRequestProcessed) {
3360 ATRACE_NAME("CameraHal::processBatchCaptureRequests");
3361 if (!valid()) return INVALID_OPERATION;
3362
3363 hardware::hidl_vec<device::V3_2::CaptureRequest> captureRequests;
3364 size_t batchSize = requests.size();
3365 captureRequests.resize(batchSize);
3366 std::vector<native_handle_t*> handlesCreated;
3367
3368 for (size_t i = 0; i < batchSize; i++) {
3369 wrapAsHidlRequest(requests[i], /*out*/&captureRequests[i], /*out*/&handlesCreated);
3370 }
3371
Yin-Chia Yehbe83fa72017-03-30 13:35:36 -07003372 std::vector<device::V3_2::BufferCache> cachesToRemove;
3373 {
3374 std::lock_guard<std::mutex> lock(mBufferIdMapLock);
3375 for (auto& pair : mFreedBuffers) {
3376 // The stream might have been removed since onBufferFreed
3377 if (mBufferIdMaps.find(pair.first) != mBufferIdMaps.end()) {
3378 cachesToRemove.push_back({pair.first, pair.second});
3379 }
3380 }
3381 mFreedBuffers.clear();
3382 }
3383
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003384 common::V1_0::Status status = common::V1_0::Status::INTERNAL_ERROR;
3385 *numRequestProcessed = 0;
Yifan Hongf79b5542017-04-11 14:44:25 -07003386
3387 // Write metadata to FMQ.
3388 for (size_t i = 0; i < batchSize; i++) {
3389 camera3_capture_request_t* request = requests[i];
3390 device::V3_2::CaptureRequest* captureRequest = &captureRequests[i];
3391
3392 if (request->settings != nullptr) {
3393 size_t settingsSize = get_camera_metadata_size(request->settings);
3394 if (mRequestMetadataQueue != nullptr && mRequestMetadataQueue->write(
3395 reinterpret_cast<const uint8_t*>(request->settings), settingsSize)) {
3396 captureRequest->settings.resize(0);
3397 captureRequest->fmqSettingsSize = settingsSize;
3398 } else {
3399 if (mRequestMetadataQueue != nullptr) {
3400 ALOGW("%s: couldn't utilize fmq, fallback to hwbinder", __FUNCTION__);
3401 }
3402 captureRequest->settings.setToExternal(
3403 reinterpret_cast<uint8_t*>(const_cast<camera_metadata_t*>(request->settings)),
3404 get_camera_metadata_size(request->settings));
3405 captureRequest->fmqSettingsSize = 0u;
3406 }
3407 } else {
3408 // A null request settings maps to a size-0 CameraMetadata
3409 captureRequest->settings.resize(0);
3410 captureRequest->fmqSettingsSize = 0u;
3411 }
3412 }
Eino-Ville Talvalac5cbb872017-04-27 12:48:33 -07003413 auto err = mHidlSession->processCaptureRequest(captureRequests, cachesToRemove,
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003414 [&status, &numRequestProcessed] (auto s, uint32_t n) {
3415 status = s;
3416 *numRequestProcessed = n;
3417 });
Eino-Ville Talvalac5cbb872017-04-27 12:48:33 -07003418 if (!err.isOk()) {
3419 ALOGE("%s: Transaction error: %s", __FUNCTION__, err.description().c_str());
3420 return DEAD_OBJECT;
3421 }
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003422 if (status == common::V1_0::Status::OK && *numRequestProcessed != batchSize) {
3423 ALOGE("%s: processCaptureRequest returns OK but processed %d/%zu requests",
3424 __FUNCTION__, *numRequestProcessed, batchSize);
3425 status = common::V1_0::Status::INTERNAL_ERROR;
3426 }
3427
3428 for (auto& handle : handlesCreated) {
3429 native_handle_delete(handle);
3430 }
3431 return CameraProviderManager::mapToStatusT(status);
3432}
3433
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003434status_t Camera3Device::HalInterface::processCaptureRequest(
3435 camera3_capture_request_t *request) {
3436 ATRACE_NAME("CameraHal::processCaptureRequest");
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003437 if (!valid()) return INVALID_OPERATION;
3438 status_t res = OK;
3439
3440 if (mHal3Device != nullptr) {
3441 res = mHal3Device->ops->process_capture_request(mHal3Device, request);
3442 } else {
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003443 uint32_t numRequestProcessed = 0;
3444 std::vector<camera3_capture_request_t*> requests(1);
3445 requests[0] = request;
3446 res = processBatchCaptureRequests(requests, &numRequestProcessed);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003447 }
3448 return res;
3449}
3450
3451status_t Camera3Device::HalInterface::flush() {
3452 ATRACE_NAME("CameraHal::flush");
3453 if (!valid()) return INVALID_OPERATION;
3454 status_t res = OK;
3455
3456 if (mHal3Device != nullptr) {
3457 res = mHal3Device->ops->flush(mHal3Device);
3458 } else {
Eino-Ville Talvalac5cbb872017-04-27 12:48:33 -07003459 auto err = mHidlSession->flush();
3460 if (!err.isOk()) {
3461 ALOGE("%s: Transaction error: %s", __FUNCTION__, err.description().c_str());
3462 res = DEAD_OBJECT;
3463 } else {
3464 res = CameraProviderManager::mapToStatusT(err);
3465 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003466 }
3467 return res;
3468}
3469
3470status_t Camera3Device::HalInterface::dump(int fd) {
3471 ATRACE_NAME("CameraHal::dump");
3472 if (!valid()) return INVALID_OPERATION;
3473 status_t res = OK;
3474
3475 if (mHal3Device != nullptr) {
3476 mHal3Device->ops->dump(mHal3Device, fd);
3477 } else {
3478 // Handled by CameraProviderManager::dump
3479 }
3480 return res;
3481}
3482
3483status_t Camera3Device::HalInterface::close() {
3484 ATRACE_NAME("CameraHal::close()");
3485 if (!valid()) return INVALID_OPERATION;
3486 status_t res = OK;
3487
3488 if (mHal3Device != nullptr) {
3489 mHal3Device->common.close(&mHal3Device->common);
3490 } else {
Eino-Ville Talvalac5cbb872017-04-27 12:48:33 -07003491 auto err = mHidlSession->close();
3492 // Interface will be dead shortly anyway, so don't log errors
3493 if (!err.isOk()) {
3494 res = DEAD_OBJECT;
3495 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003496 }
3497 return res;
3498}
3499
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -07003500void Camera3Device::HalInterface::getInflightBufferKeys(
3501 std::vector<std::pair<int32_t, int32_t>>* out) {
3502 std::lock_guard<std::mutex> lock(mInflightLock);
3503 out->clear();
3504 out->reserve(mInflightBufferMap.size());
3505 for (auto& pair : mInflightBufferMap) {
3506 uint64_t key = pair.first;
3507 int32_t streamId = key & 0xFFFFFFFF;
3508 int32_t frameNumber = (key >> 32) & 0xFFFFFFFF;
3509 out->push_back(std::make_pair(frameNumber, streamId));
3510 }
3511 return;
3512}
3513
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003514status_t Camera3Device::HalInterface::pushInflightBufferLocked(
Yin-Chia Yehf4650602017-01-10 13:13:39 -08003515 int32_t frameNumber, int32_t streamId, buffer_handle_t *buffer, int acquireFence) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003516 uint64_t key = static_cast<uint64_t>(frameNumber) << 32 | static_cast<uint64_t>(streamId);
Yin-Chia Yehf4650602017-01-10 13:13:39 -08003517 auto pair = std::make_pair(buffer, acquireFence);
3518 mInflightBufferMap[key] = pair;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003519 return OK;
3520}
3521
3522status_t Camera3Device::HalInterface::popInflightBuffer(
Yin-Chia Yehf4650602017-01-10 13:13:39 -08003523 int32_t frameNumber, int32_t streamId,
3524 /*out*/ buffer_handle_t **buffer) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003525 std::lock_guard<std::mutex> lock(mInflightLock);
3526
3527 uint64_t key = static_cast<uint64_t>(frameNumber) << 32 | static_cast<uint64_t>(streamId);
3528 auto it = mInflightBufferMap.find(key);
3529 if (it == mInflightBufferMap.end()) return NAME_NOT_FOUND;
Yin-Chia Yehf4650602017-01-10 13:13:39 -08003530 auto pair = it->second;
3531 *buffer = pair.first;
3532 int acquireFence = pair.second;
3533 if (acquireFence > 0) {
3534 ::close(acquireFence);
3535 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003536 mInflightBufferMap.erase(it);
3537 return OK;
3538}
3539
Yin-Chia Yeh77327052017-01-09 18:23:07 -08003540std::pair<bool, uint64_t> Camera3Device::HalInterface::getBufferId(
3541 const buffer_handle_t& buf, int streamId) {
3542 std::lock_guard<std::mutex> lock(mBufferIdMapLock);
3543
3544 BufferIdMap& bIdMap = mBufferIdMaps.at(streamId);
3545 auto it = bIdMap.find(buf);
3546 if (it == bIdMap.end()) {
3547 bIdMap[buf] = mNextBufferId++;
Yin-Chia Yehbe83fa72017-03-30 13:35:36 -07003548 ALOGV("stream %d now have %zu buffer caches, buf %p",
3549 streamId, bIdMap.size(), buf);
Yin-Chia Yeh77327052017-01-09 18:23:07 -08003550 return std::make_pair(true, mNextBufferId - 1);
3551 } else {
3552 return std::make_pair(false, it->second);
3553 }
3554}
3555
Yin-Chia Yehbe83fa72017-03-30 13:35:36 -07003556void Camera3Device::HalInterface::onBufferFreed(
3557 int streamId, const native_handle_t* handle) {
3558 std::lock_guard<std::mutex> lock(mBufferIdMapLock);
3559 uint64_t bufferId = BUFFER_ID_NO_BUFFER;
3560 auto mapIt = mBufferIdMaps.find(streamId);
3561 if (mapIt == mBufferIdMaps.end()) {
3562 // streamId might be from a deleted stream here
3563 ALOGI("%s: stream %d has been removed",
3564 __FUNCTION__, streamId);
3565 return;
3566 }
3567 BufferIdMap& bIdMap = mapIt->second;
3568 auto it = bIdMap.find(handle);
3569 if (it == bIdMap.end()) {
3570 ALOGW("%s: cannot find buffer %p in stream %d",
3571 __FUNCTION__, handle, streamId);
3572 return;
3573 } else {
3574 bufferId = it->second;
3575 bIdMap.erase(it);
3576 ALOGV("%s: stream %d now have %zu buffer caches after removing buf %p",
3577 __FUNCTION__, streamId, bIdMap.size(), handle);
3578 }
3579 mFreedBuffers.push_back(std::make_pair(streamId, bufferId));
3580}
3581
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003582/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003583 * RequestThread inner class methods
3584 */
3585
3586Camera3Device::RequestThread::RequestThread(wp<Camera3Device> parent,
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003587 sp<StatusTracker> statusTracker,
Yin-Chia Yehdb1e8642017-07-14 15:19:30 -07003588 sp<HalInterface> interface) :
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003589 Thread(/*canCallJava*/false),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003590 mParent(parent),
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003591 mStatusTracker(statusTracker),
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003592 mInterface(interface),
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -07003593 mListener(nullptr),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003594 mId(getId(parent)),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003595 mReconfigured(false),
3596 mDoPause(false),
3597 mPaused(true),
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003598 mFrameNumber(0),
Jianing Weicb0652e2014-03-12 18:29:36 -07003599 mLatestRequestId(NAME_NOT_FOUND),
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07003600 mCurrentAfTriggerId(0),
3601 mCurrentPreCaptureTriggerId(0),
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003602 mRepeatingLastFrameNumber(
3603 hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES),
Shuzhen Wang686f6442017-06-20 16:16:04 -07003604 mPrepareVideoStream(false),
3605 mRequestLatency(kRequestLatencyBinSize) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003606 mStatusId = statusTracker->addComponent();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003607}
3608
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003609Camera3Device::RequestThread::~RequestThread() {}
3610
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003611void Camera3Device::RequestThread::setNotificationListener(
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003612 wp<NotificationListener> listener) {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003613 Mutex::Autolock l(mRequestLock);
3614 mListener = listener;
3615}
3616
Chien-Yu Chenc66969b2016-05-19 16:37:51 -07003617void Camera3Device::RequestThread::configurationComplete(bool isConstrainedHighSpeed) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003618 Mutex::Autolock l(mRequestLock);
3619 mReconfigured = true;
Chien-Yu Chenc66969b2016-05-19 16:37:51 -07003620 // Prepare video stream for high speed recording.
3621 mPrepareVideoStream = isConstrainedHighSpeed;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003622}
3623
Jianing Wei90e59c92014-03-12 18:29:36 -07003624status_t Camera3Device::RequestThread::queueRequestList(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003625 List<sp<CaptureRequest> > &requests,
3626 /*out*/
3627 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -07003628 Mutex::Autolock l(mRequestLock);
3629 for (List<sp<CaptureRequest> >::iterator it = requests.begin(); it != requests.end();
3630 ++it) {
3631 mRequestQueue.push_back(*it);
3632 }
3633
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003634 if (lastFrameNumber != NULL) {
3635 *lastFrameNumber = mFrameNumber + mRequestQueue.size() - 1;
3636 ALOGV("%s: requestId %d, mFrameNumber %" PRId32 ", lastFrameNumber %" PRId64 ".",
3637 __FUNCTION__, (*(requests.begin()))->mResultExtras.requestId, mFrameNumber,
3638 *lastFrameNumber);
3639 }
Jianing Weicb0652e2014-03-12 18:29:36 -07003640
Jianing Wei90e59c92014-03-12 18:29:36 -07003641 unpauseForNewRequests();
3642
3643 return OK;
3644}
3645
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003646
3647status_t Camera3Device::RequestThread::queueTrigger(
3648 RequestTrigger trigger[],
3649 size_t count) {
3650
3651 Mutex::Autolock l(mTriggerMutex);
3652 status_t ret;
3653
3654 for (size_t i = 0; i < count; ++i) {
3655 ret = queueTriggerLocked(trigger[i]);
3656
3657 if (ret != OK) {
3658 return ret;
3659 }
3660 }
3661
3662 return OK;
3663}
3664
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003665const String8& Camera3Device::RequestThread::getId(const wp<Camera3Device> &device) {
3666 static String8 deadId("<DeadDevice>");
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003667 sp<Camera3Device> d = device.promote();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003668 if (d != nullptr) return d->mId;
3669 return deadId;
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003670}
3671
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003672status_t Camera3Device::RequestThread::queueTriggerLocked(
3673 RequestTrigger trigger) {
3674
3675 uint32_t tag = trigger.metadataTag;
3676 ssize_t index = mTriggerMap.indexOfKey(tag);
3677
3678 switch (trigger.getTagType()) {
3679 case TYPE_BYTE:
3680 // fall-through
3681 case TYPE_INT32:
3682 break;
3683 default:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003684 ALOGE("%s: Type not supported: 0x%x", __FUNCTION__,
3685 trigger.getTagType());
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003686 return INVALID_OPERATION;
3687 }
3688
3689 /**
3690 * Collect only the latest trigger, since we only have 1 field
3691 * in the request settings per trigger tag, and can't send more than 1
3692 * trigger per request.
3693 */
3694 if (index != NAME_NOT_FOUND) {
3695 mTriggerMap.editValueAt(index) = trigger;
3696 } else {
3697 mTriggerMap.add(tag, trigger);
3698 }
3699
3700 return OK;
3701}
3702
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003703status_t Camera3Device::RequestThread::setRepeatingRequests(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003704 const RequestList &requests,
3705 /*out*/
3706 int64_t *lastFrameNumber) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003707 Mutex::Autolock l(mRequestLock);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003708 if (lastFrameNumber != NULL) {
3709 *lastFrameNumber = mRepeatingLastFrameNumber;
3710 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003711 mRepeatingRequests.clear();
3712 mRepeatingRequests.insert(mRepeatingRequests.begin(),
3713 requests.begin(), requests.end());
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07003714
3715 unpauseForNewRequests();
3716
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003717 mRepeatingLastFrameNumber = hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003718 return OK;
3719}
3720
Chih-Hung Hsieh8b0b9712016-08-09 14:25:53 -07003721bool Camera3Device::RequestThread::isRepeatingRequestLocked(const sp<CaptureRequest>& requestIn) {
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07003722 if (mRepeatingRequests.empty()) {
3723 return false;
3724 }
3725 int32_t requestId = requestIn->mResultExtras.requestId;
3726 const RequestList &repeatRequests = mRepeatingRequests;
3727 // All repeating requests are guaranteed to have same id so only check first quest
3728 const sp<CaptureRequest> firstRequest = *repeatRequests.begin();
3729 return (firstRequest->mResultExtras.requestId == requestId);
3730}
3731
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003732status_t Camera3Device::RequestThread::clearRepeatingRequests(/*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003733 Mutex::Autolock l(mRequestLock);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07003734 return clearRepeatingRequestsLocked(lastFrameNumber);
3735
3736}
3737
3738status_t Camera3Device::RequestThread::clearRepeatingRequestsLocked(/*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003739 mRepeatingRequests.clear();
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003740 if (lastFrameNumber != NULL) {
3741 *lastFrameNumber = mRepeatingLastFrameNumber;
3742 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003743 mRepeatingLastFrameNumber = hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003744 return OK;
3745}
3746
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003747status_t Camera3Device::RequestThread::clear(
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003748 /*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07003749 Mutex::Autolock l(mRequestLock);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003750 ALOGV("RequestThread::%s:", __FUNCTION__);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003751
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07003752 mRepeatingRequests.clear();
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07003753
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003754 // Send errors for all requests pending in the request queue, including
3755 // pending repeating requests
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003756 sp<NotificationListener> listener = mListener.promote();
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003757 if (listener != NULL) {
3758 for (RequestList::iterator it = mRequestQueue.begin();
3759 it != mRequestQueue.end(); ++it) {
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07003760 // Abort the input buffers for reprocess requests.
3761 if ((*it)->mInputStream != NULL) {
3762 camera3_stream_buffer_t inputBuffer;
Eino-Ville Talvalaba435252017-06-21 16:07:25 -07003763 status_t res = (*it)->mInputStream->getInputBuffer(&inputBuffer,
3764 /*respectHalLimit*/ false);
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07003765 if (res != OK) {
3766 ALOGW("%s: %d: couldn't get input buffer while clearing the request "
3767 "list: %s (%d)", __FUNCTION__, __LINE__, strerror(-res), res);
3768 } else {
3769 res = (*it)->mInputStream->returnInputBuffer(inputBuffer);
3770 if (res != OK) {
3771 ALOGE("%s: %d: couldn't return input buffer while clearing the request "
3772 "list: %s (%d)", __FUNCTION__, __LINE__, strerror(-res), res);
3773 }
3774 }
3775 }
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003776 // Set the frame number this request would have had, if it
3777 // had been submitted; this frame number will not be reused.
3778 // The requestId and burstId fields were set when the request was
3779 // submitted originally (in convertMetadataListToRequestListLocked)
3780 (*it)->mResultExtras.frameNumber = mFrameNumber++;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003781 listener->notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003782 (*it)->mResultExtras);
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07003783 }
3784 }
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07003785 mRequestQueue.clear();
Jinguang Dongb26e7a02016-11-14 16:04:02 +08003786
3787 Mutex::Autolock al(mTriggerMutex);
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07003788 mTriggerMap.clear();
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003789 if (lastFrameNumber != NULL) {
3790 *lastFrameNumber = mRepeatingLastFrameNumber;
3791 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003792 mRepeatingLastFrameNumber = hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07003793 return OK;
3794}
3795
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003796status_t Camera3Device::RequestThread::flush() {
3797 ATRACE_CALL();
3798 Mutex::Autolock l(mFlushLock);
3799
Emilian Peev08dd2452017-04-06 16:55:14 +01003800 return mInterface->flush();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003801}
3802
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003803void Camera3Device::RequestThread::setPaused(bool paused) {
3804 Mutex::Autolock l(mPauseLock);
3805 mDoPause = paused;
3806 mDoPauseSignal.signal();
3807}
3808
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003809status_t Camera3Device::RequestThread::waitUntilRequestProcessed(
3810 int32_t requestId, nsecs_t timeout) {
3811 Mutex::Autolock l(mLatestRequestMutex);
3812 status_t res;
3813 while (mLatestRequestId != requestId) {
3814 nsecs_t startTime = systemTime();
3815
3816 res = mLatestRequestSignal.waitRelative(mLatestRequestMutex, timeout);
3817 if (res != OK) return res;
3818
3819 timeout -= (systemTime() - startTime);
3820 }
3821
3822 return OK;
3823}
3824
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003825void Camera3Device::RequestThread::requestExit() {
3826 // Call parent to set up shutdown
3827 Thread::requestExit();
3828 // The exit from any possible waits
3829 mDoPauseSignal.signal();
3830 mRequestSignal.signal();
Shuzhen Wang686f6442017-06-20 16:16:04 -07003831
3832 mRequestLatency.log("ProcessCaptureRequest latency histogram");
3833 mRequestLatency.reset();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003834}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003835
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07003836void Camera3Device::RequestThread::checkAndStopRepeatingRequest() {
Yin-Chia Yeh473fad92016-05-23 15:54:41 -07003837 bool surfaceAbandoned = false;
3838 int64_t lastFrameNumber = 0;
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003839 sp<NotificationListener> listener;
Yin-Chia Yeh473fad92016-05-23 15:54:41 -07003840 {
3841 Mutex::Autolock l(mRequestLock);
3842 // Check all streams needed by repeating requests are still valid. Otherwise, stop
3843 // repeating requests.
3844 for (const auto& request : mRepeatingRequests) {
3845 for (const auto& s : request->mOutputStreams) {
3846 if (s->isAbandoned()) {
3847 surfaceAbandoned = true;
3848 clearRepeatingRequestsLocked(&lastFrameNumber);
3849 break;
3850 }
3851 }
3852 if (surfaceAbandoned) {
3853 break;
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07003854 }
3855 }
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003856 listener = mListener.promote();
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07003857 }
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003858
3859 if (listener != NULL && surfaceAbandoned) {
3860 listener->notifyRepeatingRequestError(lastFrameNumber);
Yin-Chia Yeh473fad92016-05-23 15:54:41 -07003861 }
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07003862}
3863
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003864bool Camera3Device::RequestThread::sendRequestsBatch() {
3865 status_t res;
3866 size_t batchSize = mNextRequests.size();
3867 std::vector<camera3_capture_request_t*> requests(batchSize);
3868 uint32_t numRequestProcessed = 0;
3869 for (size_t i = 0; i < batchSize; i++) {
3870 requests[i] = &mNextRequests.editItemAt(i).halRequest;
3871 }
3872
3873 ATRACE_ASYNC_BEGIN("batch frame capture", mNextRequests[0].halRequest.frame_number);
3874 res = mInterface->processBatchCaptureRequests(requests, &numRequestProcessed);
3875
3876 bool triggerRemoveFailed = false;
3877 NextRequest& triggerFailedRequest = mNextRequests.editItemAt(0);
3878 for (size_t i = 0; i < numRequestProcessed; i++) {
3879 NextRequest& nextRequest = mNextRequests.editItemAt(i);
3880 nextRequest.submitted = true;
3881
3882
3883 // Update the latest request sent to HAL
3884 if (nextRequest.halRequest.settings != NULL) { // Don't update if they were unchanged
3885 Mutex::Autolock al(mLatestRequestMutex);
3886
3887 camera_metadata_t* cloned = clone_camera_metadata(nextRequest.halRequest.settings);
3888 mLatestRequest.acquire(cloned);
3889
3890 sp<Camera3Device> parent = mParent.promote();
3891 if (parent != NULL) {
3892 parent->monitorMetadata(TagMonitor::REQUEST,
3893 nextRequest.halRequest.frame_number,
3894 0, mLatestRequest);
3895 }
3896 }
3897
3898 if (nextRequest.halRequest.settings != NULL) {
3899 nextRequest.captureRequest->mSettings.unlock(nextRequest.halRequest.settings);
3900 }
3901
3902 if (!triggerRemoveFailed) {
3903 // Remove any previously queued triggers (after unlock)
3904 status_t removeTriggerRes = removeTriggers(mPrevRequest);
3905 if (removeTriggerRes != OK) {
3906 triggerRemoveFailed = true;
3907 triggerFailedRequest = nextRequest;
3908 }
3909 }
3910 }
3911
3912 if (triggerRemoveFailed) {
3913 SET_ERR("RequestThread: Unable to remove triggers "
3914 "(capture request %d, HAL device: %s (%d)",
3915 triggerFailedRequest.halRequest.frame_number, strerror(-res), res);
3916 cleanUpFailedRequests(/*sendRequestError*/ false);
3917 return false;
3918 }
3919
3920 if (res != OK) {
3921 // Should only get a failure here for malformed requests or device-level
3922 // errors, so consider all errors fatal. Bad metadata failures should
3923 // come through notify.
3924 SET_ERR("RequestThread: Unable to submit capture request %d to HAL device: %s (%d)",
3925 mNextRequests[numRequestProcessed].halRequest.frame_number,
3926 strerror(-res), res);
3927 cleanUpFailedRequests(/*sendRequestError*/ false);
3928 return false;
3929 }
3930 return true;
3931}
3932
3933bool Camera3Device::RequestThread::sendRequestsOneByOne() {
3934 status_t res;
3935
3936 for (auto& nextRequest : mNextRequests) {
3937 // Submit request and block until ready for next one
3938 ATRACE_ASYNC_BEGIN("frame capture", nextRequest.halRequest.frame_number);
3939 res = mInterface->processCaptureRequest(&nextRequest.halRequest);
3940
3941 if (res != OK) {
3942 // Should only get a failure here for malformed requests or device-level
3943 // errors, so consider all errors fatal. Bad metadata failures should
3944 // come through notify.
3945 SET_ERR("RequestThread: Unable to submit capture request %d to HAL"
3946 " device: %s (%d)", nextRequest.halRequest.frame_number, strerror(-res),
3947 res);
3948 cleanUpFailedRequests(/*sendRequestError*/ false);
3949 return false;
3950 }
3951
3952 // Mark that the request has be submitted successfully.
3953 nextRequest.submitted = true;
3954
3955 // Update the latest request sent to HAL
3956 if (nextRequest.halRequest.settings != NULL) { // Don't update if they were unchanged
3957 Mutex::Autolock al(mLatestRequestMutex);
3958
3959 camera_metadata_t* cloned = clone_camera_metadata(nextRequest.halRequest.settings);
3960 mLatestRequest.acquire(cloned);
3961
3962 sp<Camera3Device> parent = mParent.promote();
3963 if (parent != NULL) {
3964 parent->monitorMetadata(TagMonitor::REQUEST, nextRequest.halRequest.frame_number,
3965 0, mLatestRequest);
3966 }
3967 }
3968
3969 if (nextRequest.halRequest.settings != NULL) {
3970 nextRequest.captureRequest->mSettings.unlock(nextRequest.halRequest.settings);
3971 }
3972
3973 // Remove any previously queued triggers (after unlock)
3974 res = removeTriggers(mPrevRequest);
3975 if (res != OK) {
3976 SET_ERR("RequestThread: Unable to remove triggers "
3977 "(capture request %d, HAL device: %s (%d)",
3978 nextRequest.halRequest.frame_number, strerror(-res), res);
3979 cleanUpFailedRequests(/*sendRequestError*/ false);
3980 return false;
3981 }
3982 }
3983 return true;
3984}
3985
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07003986nsecs_t Camera3Device::RequestThread::calculateMaxExpectedDuration(const camera_metadata_t *request) {
3987 nsecs_t maxExpectedDuration = kDefaultExpectedDuration;
3988 camera_metadata_ro_entry_t e = camera_metadata_ro_entry_t();
3989 find_camera_metadata_ro_entry(request,
3990 ANDROID_CONTROL_AE_MODE,
3991 &e);
3992 if (e.count == 0) return maxExpectedDuration;
3993
3994 switch (e.data.u8[0]) {
3995 case ANDROID_CONTROL_AE_MODE_OFF:
3996 find_camera_metadata_ro_entry(request,
3997 ANDROID_SENSOR_EXPOSURE_TIME,
3998 &e);
3999 if (e.count > 0) {
4000 maxExpectedDuration = e.data.i64[0];
4001 }
4002 find_camera_metadata_ro_entry(request,
4003 ANDROID_SENSOR_FRAME_DURATION,
4004 &e);
4005 if (e.count > 0) {
4006 maxExpectedDuration = std::max(e.data.i64[0], maxExpectedDuration);
4007 }
4008 break;
4009 default:
4010 find_camera_metadata_ro_entry(request,
4011 ANDROID_CONTROL_AE_TARGET_FPS_RANGE,
4012 &e);
4013 if (e.count > 1) {
4014 maxExpectedDuration = 1e9 / e.data.u8[0];
4015 }
4016 break;
4017 }
4018
4019 return maxExpectedDuration;
4020}
4021
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004022bool Camera3Device::RequestThread::threadLoop() {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004023 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004024 status_t res;
4025
4026 // Handle paused state.
4027 if (waitIfPaused()) {
4028 return true;
4029 }
4030
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004031 // Wait for the next batch of requests.
4032 waitForNextRequestBatch();
4033 if (mNextRequests.size() == 0) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004034 return true;
4035 }
4036
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004037 // Get the latest request ID, if any
4038 int latestRequestId;
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004039 camera_metadata_entry_t requestIdEntry = mNextRequests[mNextRequests.size() - 1].
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004040 captureRequest->mSettings.find(ANDROID_REQUEST_ID);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004041 if (requestIdEntry.count > 0) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004042 latestRequestId = requestIdEntry.data.i32[0];
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004043 } else {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004044 ALOGW("%s: Did not have android.request.id set in the request.", __FUNCTION__);
4045 latestRequestId = NAME_NOT_FOUND;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004046 }
4047
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004048 // Prepare a batch of HAL requests and output buffers.
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004049 res = prepareHalRequests();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004050 if (res == TIMED_OUT) {
4051 // Not a fatal error if getting output buffers time out.
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004052 cleanUpFailedRequests(/*sendRequestError*/ true);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07004053 // Check if any stream is abandoned.
4054 checkAndStopRepeatingRequest();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004055 return true;
4056 } else if (res != OK) {
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004057 cleanUpFailedRequests(/*sendRequestError*/ false);
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07004058 return false;
4059 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004060
Zhijun Hecc27e112013-10-03 16:12:43 -07004061 // Inform waitUntilRequestProcessed thread of a new request ID
4062 {
4063 Mutex::Autolock al(mLatestRequestMutex);
4064
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004065 mLatestRequestId = latestRequestId;
Zhijun Hecc27e112013-10-03 16:12:43 -07004066 mLatestRequestSignal.signal();
4067 }
4068
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004069 // Submit a batch of requests to HAL.
4070 // Use flush lock only when submitting multilple requests in a batch.
4071 // TODO: The problem with flush lock is flush() will be blocked by process_capture_request()
4072 // which may take a long time to finish so synchronizing flush() and
4073 // process_capture_request() defeats the purpose of cancelling requests ASAP with flush().
4074 // For now, only synchronize for high speed recording and we should figure something out for
4075 // removing the synchronization.
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004076 bool useFlushLock = mNextRequests.size() > 1;
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07004077
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004078 if (useFlushLock) {
4079 mFlushLock.lock();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004080 }
4081
Zhijun Hef0645c12016-08-02 00:58:11 -07004082 ALOGVV("%s: %d: submitting %zu requests in a batch.", __FUNCTION__, __LINE__,
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004083 mNextRequests.size());
Igor Murashkin1e479c02013-09-06 16:55:14 -07004084
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08004085 bool submitRequestSuccess = false;
Shuzhen Wang686f6442017-06-20 16:16:04 -07004086 nsecs_t tRequestStart = systemTime(SYSTEM_TIME_MONOTONIC);
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08004087 if (mInterface->supportBatchRequest()) {
4088 submitRequestSuccess = sendRequestsBatch();
4089 } else {
4090 submitRequestSuccess = sendRequestsOneByOne();
Igor Murashkin1e479c02013-09-06 16:55:14 -07004091 }
Shuzhen Wang686f6442017-06-20 16:16:04 -07004092 nsecs_t tRequestEnd = systemTime(SYSTEM_TIME_MONOTONIC);
4093 mRequestLatency.add(tRequestStart, tRequestEnd);
Igor Murashkin1e479c02013-09-06 16:55:14 -07004094
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004095 if (useFlushLock) {
4096 mFlushLock.unlock();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004097 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004098
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07004099 // Unset as current request
4100 {
4101 Mutex::Autolock l(mRequestLock);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004102 mNextRequests.clear();
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07004103 }
4104
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08004105 return submitRequestSuccess;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004106}
4107
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004108status_t Camera3Device::RequestThread::prepareHalRequests() {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004109 ATRACE_CALL();
4110
Shuzhen Wang4a472662017-02-26 23:29:04 -08004111 for (size_t i = 0; i < mNextRequests.size(); i++) {
4112 auto& nextRequest = mNextRequests.editItemAt(i);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004113 sp<CaptureRequest> captureRequest = nextRequest.captureRequest;
4114 camera3_capture_request_t* halRequest = &nextRequest.halRequest;
4115 Vector<camera3_stream_buffer_t>* outputBuffers = &nextRequest.outputBuffers;
4116
4117 // Prepare a request to HAL
4118 halRequest->frame_number = captureRequest->mResultExtras.frameNumber;
4119
4120 // Insert any queued triggers (before metadata is locked)
4121 status_t res = insertTriggers(captureRequest);
4122
4123 if (res < 0) {
4124 SET_ERR("RequestThread: Unable to insert triggers "
4125 "(capture request %d, HAL device: %s (%d)",
4126 halRequest->frame_number, strerror(-res), res);
4127 return INVALID_OPERATION;
4128 }
4129 int triggerCount = res;
4130 bool triggersMixedIn = (triggerCount > 0 || mPrevTriggers > 0);
4131 mPrevTriggers = triggerCount;
4132
4133 // If the request is the same as last, or we had triggers last time
4134 if (mPrevRequest != captureRequest || triggersMixedIn) {
4135 /**
4136 * HAL workaround:
4137 * Insert a dummy trigger ID if a trigger is set but no trigger ID is
4138 */
4139 res = addDummyTriggerIds(captureRequest);
4140 if (res != OK) {
4141 SET_ERR("RequestThread: Unable to insert dummy trigger IDs "
4142 "(capture request %d, HAL device: %s (%d)",
4143 halRequest->frame_number, strerror(-res), res);
4144 return INVALID_OPERATION;
4145 }
4146
4147 /**
4148 * The request should be presorted so accesses in HAL
4149 * are O(logn). Sidenote, sorting a sorted metadata is nop.
4150 */
4151 captureRequest->mSettings.sort();
4152 halRequest->settings = captureRequest->mSettings.getAndLock();
4153 mPrevRequest = captureRequest;
4154 ALOGVV("%s: Request settings are NEW", __FUNCTION__);
4155
4156 IF_ALOGV() {
4157 camera_metadata_ro_entry_t e = camera_metadata_ro_entry_t();
4158 find_camera_metadata_ro_entry(
4159 halRequest->settings,
4160 ANDROID_CONTROL_AF_TRIGGER,
4161 &e
4162 );
4163 if (e.count > 0) {
4164 ALOGV("%s: Request (frame num %d) had AF trigger 0x%x",
4165 __FUNCTION__,
4166 halRequest->frame_number,
4167 e.data.u8[0]);
4168 }
4169 }
4170 } else {
4171 // leave request.settings NULL to indicate 'reuse latest given'
4172 ALOGVV("%s: Request settings are REUSED",
4173 __FUNCTION__);
4174 }
4175
4176 uint32_t totalNumBuffers = 0;
4177
4178 // Fill in buffers
4179 if (captureRequest->mInputStream != NULL) {
4180 halRequest->input_buffer = &captureRequest->mInputBuffer;
4181 totalNumBuffers += 1;
4182 } else {
4183 halRequest->input_buffer = NULL;
4184 }
4185
4186 outputBuffers->insertAt(camera3_stream_buffer_t(), 0,
4187 captureRequest->mOutputStreams.size());
4188 halRequest->output_buffers = outputBuffers->array();
Shuzhen Wang4a472662017-02-26 23:29:04 -08004189 for (size_t j = 0; j < captureRequest->mOutputStreams.size(); j++) {
4190 sp<Camera3OutputStreamInterface> outputStream = captureRequest->mOutputStreams.editItemAt(j);
Chien-Yu Chenc66969b2016-05-19 16:37:51 -07004191
4192 // Prepare video buffers for high speed recording on the first video request.
4193 if (mPrepareVideoStream && outputStream->isVideoStream()) {
4194 // Only try to prepare video stream on the first video request.
4195 mPrepareVideoStream = false;
4196
4197 res = outputStream->startPrepare(Camera3StreamInterface::ALLOCATE_PIPELINE_MAX);
4198 while (res == NOT_ENOUGH_DATA) {
4199 res = outputStream->prepareNextBuffer();
4200 }
4201 if (res != OK) {
4202 ALOGW("%s: Preparing video buffers for high speed failed: %s (%d)",
4203 __FUNCTION__, strerror(-res), res);
4204 outputStream->cancelPrepare();
4205 }
4206 }
4207
Shuzhen Wang4a472662017-02-26 23:29:04 -08004208 res = outputStream->getBuffer(&outputBuffers->editItemAt(j),
4209 captureRequest->mOutputSurfaces[j]);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004210 if (res != OK) {
4211 // Can't get output buffer from gralloc queue - this could be due to
4212 // abandoned queue or other consumer misbehavior, so not a fatal
4213 // error
4214 ALOGE("RequestThread: Can't get output buffer, skipping request:"
4215 " %s (%d)", strerror(-res), res);
4216
4217 return TIMED_OUT;
4218 }
4219 halRequest->num_output_buffers++;
Shuzhen Wang0129d522016-10-30 22:43:41 -07004220
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004221 }
4222 totalNumBuffers += halRequest->num_output_buffers;
4223
4224 // Log request in the in-flight queue
4225 sp<Camera3Device> parent = mParent.promote();
4226 if (parent == NULL) {
4227 // Should not happen, and nowhere to send errors to, so just log it
4228 CLOGE("RequestThread: Parent is gone");
4229 return INVALID_OPERATION;
4230 }
Shuzhen Wang4a472662017-02-26 23:29:04 -08004231
4232 // If this request list is for constrained high speed recording (not
4233 // preview), and the current request is not the last one in the batch,
4234 // do not send callback to the app.
4235 bool hasCallback = true;
4236 if (mNextRequests[0].captureRequest->mBatchSize > 1 && i != mNextRequests.size()-1) {
4237 hasCallback = false;
4238 }
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004239 res = parent->registerInFlight(halRequest->frame_number,
4240 totalNumBuffers, captureRequest->mResultExtras,
4241 /*hasInput*/halRequest->input_buffer != NULL,
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07004242 hasCallback,
4243 calculateMaxExpectedDuration(halRequest->settings));
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004244 ALOGVV("%s: registered in flight requestId = %" PRId32 ", frameNumber = %" PRId64
4245 ", burstId = %" PRId32 ".",
4246 __FUNCTION__,
4247 captureRequest->mResultExtras.requestId, captureRequest->mResultExtras.frameNumber,
4248 captureRequest->mResultExtras.burstId);
4249 if (res != OK) {
4250 SET_ERR("RequestThread: Unable to register new in-flight request:"
4251 " %s (%d)", strerror(-res), res);
4252 return INVALID_OPERATION;
4253 }
4254 }
4255
4256 return OK;
4257}
4258
Igor Murashkin1e479c02013-09-06 16:55:14 -07004259CameraMetadata Camera3Device::RequestThread::getLatestRequest() const {
4260 Mutex::Autolock al(mLatestRequestMutex);
4261
4262 ALOGV("RequestThread::%s", __FUNCTION__);
4263
4264 return mLatestRequest;
4265}
4266
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004267bool Camera3Device::RequestThread::isStreamPending(
4268 sp<Camera3StreamInterface>& stream) {
4269 Mutex::Autolock l(mRequestLock);
4270
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004271 for (const auto& nextRequest : mNextRequests) {
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004272 if (!nextRequest.submitted) {
4273 for (const auto& s : nextRequest.captureRequest->mOutputStreams) {
4274 if (stream == s) return true;
4275 }
4276 if (stream == nextRequest.captureRequest->mInputStream) return true;
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07004277 }
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07004278 }
4279
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004280 for (const auto& request : mRequestQueue) {
4281 for (const auto& s : request->mOutputStreams) {
4282 if (stream == s) return true;
4283 }
4284 if (stream == request->mInputStream) return true;
4285 }
4286
4287 for (const auto& request : mRepeatingRequests) {
4288 for (const auto& s : request->mOutputStreams) {
4289 if (stream == s) return true;
4290 }
4291 if (stream == request->mInputStream) return true;
4292 }
4293
4294 return false;
4295}
Jianing Weicb0652e2014-03-12 18:29:36 -07004296
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07004297nsecs_t Camera3Device::getExpectedInFlightDurationLocked() {
4298 return mExpectedInflightDuration > kMinInflightDuration ?
4299 mExpectedInflightDuration : kMinInflightDuration;
4300}
4301
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004302void Camera3Device::RequestThread::cleanUpFailedRequests(bool sendRequestError) {
4303 if (mNextRequests.empty()) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004304 return;
4305 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004306
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004307 for (auto& nextRequest : mNextRequests) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004308 // Skip the ones that have been submitted successfully.
4309 if (nextRequest.submitted) {
4310 continue;
4311 }
4312
4313 sp<CaptureRequest> captureRequest = nextRequest.captureRequest;
4314 camera3_capture_request_t* halRequest = &nextRequest.halRequest;
4315 Vector<camera3_stream_buffer_t>* outputBuffers = &nextRequest.outputBuffers;
4316
4317 if (halRequest->settings != NULL) {
4318 captureRequest->mSettings.unlock(halRequest->settings);
4319 }
4320
4321 if (captureRequest->mInputStream != NULL) {
4322 captureRequest->mInputBuffer.status = CAMERA3_BUFFER_STATUS_ERROR;
4323 captureRequest->mInputStream->returnInputBuffer(captureRequest->mInputBuffer);
4324 }
4325
4326 for (size_t i = 0; i < halRequest->num_output_buffers; i++) {
Emilian Peevc58cf4c2017-05-11 17:23:41 +01004327 //Buffers that failed processing could still have
4328 //valid acquire fence.
4329 int acquireFence = (*outputBuffers)[i].acquire_fence;
4330 if (0 <= acquireFence) {
4331 close(acquireFence);
4332 outputBuffers->editItemAt(i).acquire_fence = -1;
4333 }
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004334 outputBuffers->editItemAt(i).status = CAMERA3_BUFFER_STATUS_ERROR;
4335 captureRequest->mOutputStreams.editItemAt(i)->returnBuffer((*outputBuffers)[i], 0);
4336 }
4337
4338 if (sendRequestError) {
4339 Mutex::Autolock l(mRequestLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004340 sp<NotificationListener> listener = mListener.promote();
4341 if (listener != NULL) {
4342 listener->notifyError(
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08004343 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004344 captureRequest->mResultExtras);
4345 }
4346 }
Shuzhen Wangcadb3302016-11-04 14:17:56 -07004347
4348 // Remove yet-to-be submitted inflight request from inflightMap
4349 {
4350 sp<Camera3Device> parent = mParent.promote();
4351 if (parent != NULL) {
4352 Mutex::Autolock l(parent->mInFlightLock);
4353 ssize_t idx = parent->mInFlightMap.indexOfKey(captureRequest->mResultExtras.frameNumber);
4354 if (idx >= 0) {
4355 ALOGV("%s: Remove inflight request from queue: frameNumber %" PRId64,
4356 __FUNCTION__, captureRequest->mResultExtras.frameNumber);
4357 parent->removeInFlightMapEntryLocked(idx);
4358 }
4359 }
4360 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004361 }
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07004362
4363 Mutex::Autolock l(mRequestLock);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004364 mNextRequests.clear();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004365}
4366
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004367void Camera3Device::RequestThread::waitForNextRequestBatch() {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004368 // Optimized a bit for the simple steady-state case (single repeating
4369 // request), to avoid putting that request in the queue temporarily.
4370 Mutex::Autolock l(mRequestLock);
4371
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004372 assert(mNextRequests.empty());
4373
4374 NextRequest nextRequest;
4375 nextRequest.captureRequest = waitForNextRequestLocked();
4376 if (nextRequest.captureRequest == nullptr) {
4377 return;
4378 }
4379
4380 nextRequest.halRequest = camera3_capture_request_t();
4381 nextRequest.submitted = false;
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004382 mNextRequests.add(nextRequest);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004383
4384 // Wait for additional requests
4385 const size_t batchSize = nextRequest.captureRequest->mBatchSize;
4386
4387 for (size_t i = 1; i < batchSize; i++) {
4388 NextRequest additionalRequest;
4389 additionalRequest.captureRequest = waitForNextRequestLocked();
4390 if (additionalRequest.captureRequest == nullptr) {
4391 break;
4392 }
4393
4394 additionalRequest.halRequest = camera3_capture_request_t();
4395 additionalRequest.submitted = false;
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004396 mNextRequests.add(additionalRequest);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004397 }
4398
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004399 if (mNextRequests.size() < batchSize) {
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08004400 ALOGE("RequestThread: only get %zu out of %zu requests. Skipping requests.",
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004401 mNextRequests.size(), batchSize);
4402 cleanUpFailedRequests(/*sendRequestError*/true);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004403 }
4404
4405 return;
4406}
4407
4408sp<Camera3Device::CaptureRequest>
4409 Camera3Device::RequestThread::waitForNextRequestLocked() {
4410 status_t res;
4411 sp<CaptureRequest> nextRequest;
4412
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004413 while (mRequestQueue.empty()) {
4414 if (!mRepeatingRequests.empty()) {
4415 // Always atomically enqueue all requests in a repeating request
4416 // list. Guarantees a complete in-sequence set of captures to
4417 // application.
4418 const RequestList &requests = mRepeatingRequests;
4419 RequestList::const_iterator firstRequest =
4420 requests.begin();
4421 nextRequest = *firstRequest;
4422 mRequestQueue.insert(mRequestQueue.end(),
4423 ++firstRequest,
4424 requests.end());
4425 // No need to wait any longer
Jianing Weicb0652e2014-03-12 18:29:36 -07004426
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07004427 mRepeatingLastFrameNumber = mFrameNumber + requests.size() - 1;
Jianing Weicb0652e2014-03-12 18:29:36 -07004428
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004429 break;
4430 }
4431
4432 res = mRequestSignal.waitRelative(mRequestLock, kRequestTimeout);
4433
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004434 if ((mRequestQueue.empty() && mRepeatingRequests.empty()) ||
4435 exitPending()) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004436 Mutex::Autolock pl(mPauseLock);
4437 if (mPaused == false) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004438 ALOGV("%s: RequestThread: Going idle", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004439 mPaused = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004440 // Let the tracker know
4441 sp<StatusTracker> statusTracker = mStatusTracker.promote();
4442 if (statusTracker != 0) {
4443 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
4444 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004445 }
4446 // Stop waiting for now and let thread management happen
4447 return NULL;
4448 }
4449 }
4450
4451 if (nextRequest == NULL) {
4452 // Don't have a repeating request already in hand, so queue
4453 // must have an entry now.
4454 RequestList::iterator firstRequest =
4455 mRequestQueue.begin();
4456 nextRequest = *firstRequest;
4457 mRequestQueue.erase(firstRequest);
Shuzhen Wang9d066012016-09-30 11:30:20 -07004458 if (mRequestQueue.empty() && !nextRequest->mRepeating) {
4459 sp<NotificationListener> listener = mListener.promote();
4460 if (listener != NULL) {
4461 listener->notifyRequestQueueEmpty();
4462 }
4463 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004464 }
4465
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07004466 // In case we've been unpaused by setPaused clearing mDoPause, need to
4467 // update internal pause state (capture/setRepeatingRequest unpause
4468 // directly).
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004469 Mutex::Autolock pl(mPauseLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004470 if (mPaused) {
4471 ALOGV("%s: RequestThread: Unpaused", __FUNCTION__);
4472 sp<StatusTracker> statusTracker = mStatusTracker.promote();
4473 if (statusTracker != 0) {
4474 statusTracker->markComponentActive(mStatusId);
4475 }
4476 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004477 mPaused = false;
4478
4479 // Check if we've reconfigured since last time, and reset the preview
4480 // request if so. Can't use 'NULL request == repeat' across configure calls.
4481 if (mReconfigured) {
4482 mPrevRequest.clear();
4483 mReconfigured = false;
4484 }
4485
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07004486 if (nextRequest != NULL) {
4487 nextRequest->mResultExtras.frameNumber = mFrameNumber++;
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07004488 nextRequest->mResultExtras.afTriggerId = mCurrentAfTriggerId;
4489 nextRequest->mResultExtras.precaptureTriggerId = mCurrentPreCaptureTriggerId;
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07004490
4491 // Since RequestThread::clear() removes buffers from the input stream,
4492 // get the right buffer here before unlocking mRequestLock
4493 if (nextRequest->mInputStream != NULL) {
4494 res = nextRequest->mInputStream->getInputBuffer(&nextRequest->mInputBuffer);
4495 if (res != OK) {
4496 // Can't get input buffer from gralloc queue - this could be due to
4497 // disconnected queue or other producer misbehavior, so not a fatal
4498 // error
4499 ALOGE("%s: Can't get input buffer, skipping request:"
4500 " %s (%d)", __FUNCTION__, strerror(-res), res);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004501
4502 sp<NotificationListener> listener = mListener.promote();
4503 if (listener != NULL) {
4504 listener->notifyError(
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08004505 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07004506 nextRequest->mResultExtras);
4507 }
4508 return NULL;
4509 }
4510 }
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07004511 }
Chien-Yu Chend196d612015-06-22 19:49:01 -07004512
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004513 return nextRequest;
4514}
4515
4516bool Camera3Device::RequestThread::waitIfPaused() {
4517 status_t res;
4518 Mutex::Autolock l(mPauseLock);
4519 while (mDoPause) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004520 if (mPaused == false) {
4521 mPaused = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004522 ALOGV("%s: RequestThread: Paused", __FUNCTION__);
4523 // Let the tracker know
4524 sp<StatusTracker> statusTracker = mStatusTracker.promote();
4525 if (statusTracker != 0) {
4526 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
4527 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004528 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004529
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004530 res = mDoPauseSignal.waitRelative(mPauseLock, kRequestTimeout);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004531 if (res == TIMED_OUT || exitPending()) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004532 return true;
4533 }
4534 }
4535 // We don't set mPaused to false here, because waitForNextRequest needs
4536 // to further manage the paused state in case of starvation.
4537 return false;
4538}
4539
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07004540void Camera3Device::RequestThread::unpauseForNewRequests() {
4541 // With work to do, mark thread as unpaused.
4542 // If paused by request (setPaused), don't resume, to avoid
4543 // extra signaling/waiting overhead to waitUntilPaused
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004544 mRequestSignal.signal();
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07004545 Mutex::Autolock p(mPauseLock);
4546 if (!mDoPause) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004547 ALOGV("%s: RequestThread: Going active", __FUNCTION__);
4548 if (mPaused) {
4549 sp<StatusTracker> statusTracker = mStatusTracker.promote();
4550 if (statusTracker != 0) {
4551 statusTracker->markComponentActive(mStatusId);
4552 }
4553 }
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07004554 mPaused = false;
4555 }
4556}
4557
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07004558void Camera3Device::RequestThread::setErrorState(const char *fmt, ...) {
4559 sp<Camera3Device> parent = mParent.promote();
4560 if (parent != NULL) {
4561 va_list args;
4562 va_start(args, fmt);
4563
4564 parent->setErrorStateV(fmt, args);
4565
4566 va_end(args);
4567 }
4568}
4569
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004570status_t Camera3Device::RequestThread::insertTriggers(
4571 const sp<CaptureRequest> &request) {
4572
4573 Mutex::Autolock al(mTriggerMutex);
4574
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07004575 sp<Camera3Device> parent = mParent.promote();
4576 if (parent == NULL) {
4577 CLOGE("RequestThread: Parent is gone");
4578 return DEAD_OBJECT;
4579 }
4580
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004581 CameraMetadata &metadata = request->mSettings;
4582 size_t count = mTriggerMap.size();
4583
4584 for (size_t i = 0; i < count; ++i) {
4585 RequestTrigger trigger = mTriggerMap.valueAt(i);
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004586 uint32_t tag = trigger.metadataTag;
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07004587
4588 if (tag == ANDROID_CONTROL_AF_TRIGGER_ID || tag == ANDROID_CONTROL_AE_PRECAPTURE_ID) {
4589 bool isAeTrigger = (trigger.metadataTag == ANDROID_CONTROL_AE_PRECAPTURE_ID);
4590 uint32_t triggerId = static_cast<uint32_t>(trigger.entryValue);
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07004591 if (isAeTrigger) {
4592 request->mResultExtras.precaptureTriggerId = triggerId;
4593 mCurrentPreCaptureTriggerId = triggerId;
4594 } else {
4595 request->mResultExtras.afTriggerId = triggerId;
4596 mCurrentAfTriggerId = triggerId;
4597 }
Emilian Peev7e25e5e2017-04-07 15:48:49 +01004598 continue;
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07004599 }
4600
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004601 camera_metadata_entry entry = metadata.find(tag);
4602
4603 if (entry.count > 0) {
4604 /**
4605 * Already has an entry for this trigger in the request.
4606 * Rewrite it with our requested trigger value.
4607 */
4608 RequestTrigger oldTrigger = trigger;
4609
4610 oldTrigger.entryValue = entry.data.u8[0];
4611
4612 mTriggerReplacedMap.add(tag, oldTrigger);
4613 } else {
4614 /**
4615 * More typical, no trigger entry, so we just add it
4616 */
4617 mTriggerRemovedMap.add(tag, trigger);
4618 }
4619
4620 status_t res;
4621
4622 switch (trigger.getTagType()) {
4623 case TYPE_BYTE: {
4624 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
4625 res = metadata.update(tag,
4626 &entryValue,
4627 /*count*/1);
4628 break;
4629 }
4630 case TYPE_INT32:
4631 res = metadata.update(tag,
4632 &trigger.entryValue,
4633 /*count*/1);
4634 break;
4635 default:
4636 ALOGE("%s: Type not supported: 0x%x",
4637 __FUNCTION__,
4638 trigger.getTagType());
4639 return INVALID_OPERATION;
4640 }
4641
4642 if (res != OK) {
4643 ALOGE("%s: Failed to update request metadata with trigger tag %s"
4644 ", value %d", __FUNCTION__, trigger.getTagName(),
4645 trigger.entryValue);
4646 return res;
4647 }
4648
4649 ALOGV("%s: Mixed in trigger %s, value %d", __FUNCTION__,
4650 trigger.getTagName(),
4651 trigger.entryValue);
4652 }
4653
4654 mTriggerMap.clear();
4655
4656 return count;
4657}
4658
4659status_t Camera3Device::RequestThread::removeTriggers(
4660 const sp<CaptureRequest> &request) {
4661 Mutex::Autolock al(mTriggerMutex);
4662
4663 CameraMetadata &metadata = request->mSettings;
4664
4665 /**
4666 * Replace all old entries with their old values.
4667 */
4668 for (size_t i = 0; i < mTriggerReplacedMap.size(); ++i) {
4669 RequestTrigger trigger = mTriggerReplacedMap.valueAt(i);
4670
4671 status_t res;
4672
4673 uint32_t tag = trigger.metadataTag;
4674 switch (trigger.getTagType()) {
4675 case TYPE_BYTE: {
4676 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
4677 res = metadata.update(tag,
4678 &entryValue,
4679 /*count*/1);
4680 break;
4681 }
4682 case TYPE_INT32:
4683 res = metadata.update(tag,
4684 &trigger.entryValue,
4685 /*count*/1);
4686 break;
4687 default:
4688 ALOGE("%s: Type not supported: 0x%x",
4689 __FUNCTION__,
4690 trigger.getTagType());
4691 return INVALID_OPERATION;
4692 }
4693
4694 if (res != OK) {
4695 ALOGE("%s: Failed to restore request metadata with trigger tag %s"
4696 ", trigger value %d", __FUNCTION__,
4697 trigger.getTagName(), trigger.entryValue);
4698 return res;
4699 }
4700 }
4701 mTriggerReplacedMap.clear();
4702
4703 /**
4704 * Remove all new entries.
4705 */
4706 for (size_t i = 0; i < mTriggerRemovedMap.size(); ++i) {
4707 RequestTrigger trigger = mTriggerRemovedMap.valueAt(i);
4708 status_t res = metadata.erase(trigger.metadataTag);
4709
4710 if (res != OK) {
4711 ALOGE("%s: Failed to erase metadata with trigger tag %s"
4712 ", trigger value %d", __FUNCTION__,
4713 trigger.getTagName(), trigger.entryValue);
4714 return res;
4715 }
4716 }
4717 mTriggerRemovedMap.clear();
4718
4719 return OK;
4720}
4721
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07004722status_t Camera3Device::RequestThread::addDummyTriggerIds(
4723 const sp<CaptureRequest> &request) {
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08004724 // Trigger ID 0 had special meaning in the HAL2 spec, so avoid it here
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07004725 static const int32_t dummyTriggerId = 1;
4726 status_t res;
4727
4728 CameraMetadata &metadata = request->mSettings;
4729
4730 // If AF trigger is active, insert a dummy AF trigger ID if none already
4731 // exists
4732 camera_metadata_entry afTrigger = metadata.find(ANDROID_CONTROL_AF_TRIGGER);
4733 camera_metadata_entry afId = metadata.find(ANDROID_CONTROL_AF_TRIGGER_ID);
4734 if (afTrigger.count > 0 &&
4735 afTrigger.data.u8[0] != ANDROID_CONTROL_AF_TRIGGER_IDLE &&
4736 afId.count == 0) {
4737 res = metadata.update(ANDROID_CONTROL_AF_TRIGGER_ID, &dummyTriggerId, 1);
4738 if (res != OK) return res;
4739 }
4740
4741 // If AE precapture trigger is active, insert a dummy precapture trigger ID
4742 // if none already exists
4743 camera_metadata_entry pcTrigger =
4744 metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER);
4745 camera_metadata_entry pcId = metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_ID);
4746 if (pcTrigger.count > 0 &&
4747 pcTrigger.data.u8[0] != ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_IDLE &&
4748 pcId.count == 0) {
4749 res = metadata.update(ANDROID_CONTROL_AE_PRECAPTURE_ID,
4750 &dummyTriggerId, 1);
4751 if (res != OK) return res;
4752 }
4753
4754 return OK;
4755}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004756
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004757/**
4758 * PreparerThread inner class methods
4759 */
4760
4761Camera3Device::PreparerThread::PreparerThread() :
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -07004762 Thread(/*canCallJava*/false), mListener(nullptr),
4763 mActive(false), mCancelNow(false) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004764}
4765
4766Camera3Device::PreparerThread::~PreparerThread() {
4767 Thread::requestExitAndWait();
4768 if (mCurrentStream != nullptr) {
4769 mCurrentStream->cancelPrepare();
4770 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
4771 mCurrentStream.clear();
4772 }
4773 clear();
4774}
4775
Ruben Brunkc78ac262015-08-13 17:58:46 -07004776status_t Camera3Device::PreparerThread::prepare(int maxCount, sp<Camera3StreamInterface>& stream) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004777 status_t res;
4778
4779 Mutex::Autolock l(mLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004780 sp<NotificationListener> listener = mListener.promote();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004781
Ruben Brunkc78ac262015-08-13 17:58:46 -07004782 res = stream->startPrepare(maxCount);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004783 if (res == OK) {
4784 // No preparation needed, fire listener right off
4785 ALOGV("%s: Stream %d already prepared", __FUNCTION__, stream->getId());
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004786 if (listener != NULL) {
4787 listener->notifyPrepared(stream->getId());
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004788 }
4789 return OK;
4790 } else if (res != NOT_ENOUGH_DATA) {
4791 return res;
4792 }
4793
4794 // Need to prepare, start up thread if necessary
4795 if (!mActive) {
4796 // mRunning will change to false before the thread fully shuts down, so wait to be sure it
4797 // isn't running
4798 Thread::requestExitAndWait();
4799 res = Thread::run("C3PrepThread", PRIORITY_BACKGROUND);
4800 if (res != OK) {
4801 ALOGE("%s: Unable to start preparer stream: %d (%s)", __FUNCTION__, res, strerror(-res));
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004802 if (listener != NULL) {
4803 listener->notifyPrepared(stream->getId());
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004804 }
4805 return res;
4806 }
4807 mCancelNow = false;
4808 mActive = true;
4809 ALOGV("%s: Preparer stream started", __FUNCTION__);
4810 }
4811
4812 // queue up the work
4813 mPendingStreams.push_back(stream);
4814 ALOGV("%s: Stream %d queued for preparing", __FUNCTION__, stream->getId());
4815
4816 return OK;
4817}
4818
4819status_t Camera3Device::PreparerThread::clear() {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004820 Mutex::Autolock l(mLock);
4821
4822 for (const auto& stream : mPendingStreams) {
4823 stream->cancelPrepare();
4824 }
4825 mPendingStreams.clear();
4826 mCancelNow = true;
4827
4828 return OK;
4829}
4830
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004831void Camera3Device::PreparerThread::setNotificationListener(wp<NotificationListener> listener) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004832 Mutex::Autolock l(mLock);
4833 mListener = listener;
4834}
4835
4836bool Camera3Device::PreparerThread::threadLoop() {
4837 status_t res;
4838 {
4839 Mutex::Autolock l(mLock);
4840 if (mCurrentStream == nullptr) {
4841 // End thread if done with work
4842 if (mPendingStreams.empty()) {
4843 ALOGV("%s: Preparer stream out of work", __FUNCTION__);
4844 // threadLoop _must not_ re-acquire mLock after it sets mActive to false; would
4845 // cause deadlock with prepare()'s requestExitAndWait triggered by !mActive.
4846 mActive = false;
4847 return false;
4848 }
4849
4850 // Get next stream to prepare
4851 auto it = mPendingStreams.begin();
4852 mCurrentStream = *it;
4853 mPendingStreams.erase(it);
4854 ATRACE_ASYNC_BEGIN("stream prepare", mCurrentStream->getId());
4855 ALOGV("%s: Preparing stream %d", __FUNCTION__, mCurrentStream->getId());
4856 } else if (mCancelNow) {
4857 mCurrentStream->cancelPrepare();
4858 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
4859 ALOGV("%s: Cancelling stream %d prepare", __FUNCTION__, mCurrentStream->getId());
4860 mCurrentStream.clear();
4861 mCancelNow = false;
4862 return true;
4863 }
4864 }
4865
4866 res = mCurrentStream->prepareNextBuffer();
4867 if (res == NOT_ENOUGH_DATA) return true;
4868 if (res != OK) {
4869 // Something bad happened; try to recover by cancelling prepare and
4870 // signalling listener anyway
4871 ALOGE("%s: Stream %d returned error %d (%s) during prepare", __FUNCTION__,
4872 mCurrentStream->getId(), res, strerror(-res));
4873 mCurrentStream->cancelPrepare();
4874 }
4875
4876 // This stream has finished, notify listener
4877 Mutex::Autolock l(mLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004878 sp<NotificationListener> listener = mListener.promote();
4879 if (listener != NULL) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004880 ALOGV("%s: Stream %d prepare done, signaling listener", __FUNCTION__,
4881 mCurrentStream->getId());
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004882 listener->notifyPrepared(mCurrentStream->getId());
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004883 }
4884
4885 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
4886 mCurrentStream.clear();
4887
4888 return true;
4889}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004890
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004891/**
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08004892 * Static callback forwarding methods from HAL to instance
4893 */
4894
4895void Camera3Device::sProcessCaptureResult(const camera3_callback_ops *cb,
4896 const camera3_capture_result *result) {
4897 Camera3Device *d =
4898 const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb));
Chien-Yu Chend196d612015-06-22 19:49:01 -07004899
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08004900 d->processCaptureResult(result);
4901}
4902
4903void Camera3Device::sNotify(const camera3_callback_ops *cb,
4904 const camera3_notify_msg *msg) {
4905 Camera3Device *d =
4906 const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb));
4907 d->notify(msg);
4908}
4909
4910}; // namespace android