blob: 68703507df51c73c861b2408d4e37f13ce977bb3 [file] [log] [blame]
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -08001/*
Shuzhen Wangc28189a2017-11-27 23:05:10 -08002 * Copyright (C) 2013-2018 The Android Open Source Project
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -08003 *
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-Stream"
18#define ATRACE_TAG ATRACE_TAG_CAMERA
19//#define LOG_NDEBUG 0
20
21#include <utils/Log.h>
22#include <utils/Trace.h>
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -070023#include "device3/Camera3Stream.h"
24#include "device3/StatusTracker.h"
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -080025
Igor Murashkin13d315e2014-04-03 18:09:04 -070026#include <cutils/properties.h>
27
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -080028namespace android {
29
30namespace camera3 {
31
32Camera3Stream::~Camera3Stream() {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -070033 sp<StatusTracker> statusTracker = mStatusTracker.promote();
34 if (statusTracker != 0 && mStatusId != StatusTracker::NO_STATUS_ID) {
35 statusTracker->removeComponent(mStatusId);
36 }
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -080037}
38
39Camera3Stream* Camera3Stream::cast(camera3_stream *stream) {
40 return static_cast<Camera3Stream*>(stream);
41}
42
43const Camera3Stream* Camera3Stream::cast(const camera3_stream *stream) {
44 return static_cast<const Camera3Stream*>(stream);
45}
46
47Camera3Stream::Camera3Stream(int id,
48 camera3_stream_type type,
Eino-Ville Talvala3d82c0d2015-02-23 15:19:19 -080049 uint32_t width, uint32_t height, size_t maxSize, int format,
Shuzhen Wangc28189a2017-11-27 23:05:10 -080050 android_dataspace dataSpace, camera3_stream_rotation_t rotation,
51 const String8& physicalCameraId, int setId) :
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -080052 camera3_stream(),
53 mId(id),
Zhijun He125684a2015-12-26 15:07:30 -080054 mSetId(setId),
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -080055 mName(String8::format("Camera3Stream[%d]", id)),
56 mMaxSize(maxSize),
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -070057 mState(STATE_CONSTRUCTED),
Ruben Brunkc78ac262015-08-13 17:58:46 -070058 mStatusId(StatusTracker::NO_STATUS_ID),
Zhijun He5d677d12016-05-29 16:52:39 -070059 mStreamUnpreparable(true),
Emilian Peev050f5dc2017-05-18 14:43:56 +010060 mUsage(0),
Eino-Ville Talvala02bf0322016-02-18 12:41:10 -080061 mOldUsage(0),
62 mOldMaxBuffers(0),
Zhijun He125684a2015-12-26 15:07:30 -080063 mPrepared(false),
Shuzhen Wangb3a0fb52018-09-13 17:24:08 -070064 mPrepareBlockRequest(true),
Zhijun He125684a2015-12-26 15:07:30 -080065 mPreparedBufferIdx(0),
Shuzhen Wang686f6442017-06-20 16:16:04 -070066 mLastMaxCount(Camera3StreamInterface::ALLOCATE_PIPELINE_MAX),
Emilian Peev710c1422017-08-30 11:19:38 +010067 mBufferLimitLatency(kBufferLimitLatencyBinSize),
68 mFormatOverridden(false),
Shuzhen Wangc28189a2017-11-27 23:05:10 -080069 mOriginalFormat(-1),
70 mPhysicalCameraId(physicalCameraId) {
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -080071
72 camera3_stream::stream_type = type;
73 camera3_stream::width = width;
74 camera3_stream::height = height;
75 camera3_stream::format = format;
Eino-Ville Talvala3d82c0d2015-02-23 15:19:19 -080076 camera3_stream::data_space = dataSpace;
Yin-Chia Yehb97babb2015-03-12 13:42:44 -070077 camera3_stream::rotation = rotation;
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -080078 camera3_stream::max_buffers = 0;
79 camera3_stream::priv = NULL;
Shuzhen Wangc28189a2017-11-27 23:05:10 -080080 camera3_stream::physical_camera_id = mPhysicalCameraId.string();
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -080081
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -080082 if ((format == HAL_PIXEL_FORMAT_BLOB || format == HAL_PIXEL_FORMAT_RAW_OPAQUE) &&
83 maxSize == 0) {
84 ALOGE("%s: BLOB or RAW_OPAQUE format with size == 0", __FUNCTION__);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -080085 mState = STATE_ERROR;
86 }
87}
88
89int Camera3Stream::getId() const {
90 return mId;
91}
92
Zhijun He125684a2015-12-26 15:07:30 -080093int Camera3Stream::getStreamSetId() const {
94 return mSetId;
95}
96
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -080097uint32_t Camera3Stream::getWidth() const {
98 return camera3_stream::width;
99}
100
101uint32_t Camera3Stream::getHeight() const {
102 return camera3_stream::height;
103}
104
105int Camera3Stream::getFormat() const {
106 return camera3_stream::format;
107}
108
Eino-Ville Talvala3d82c0d2015-02-23 15:19:19 -0800109android_dataspace Camera3Stream::getDataSpace() const {
110 return camera3_stream::data_space;
111}
112
Emilian Peev050f5dc2017-05-18 14:43:56 +0100113uint64_t Camera3Stream::getUsage() const {
114 return mUsage;
115}
116
117void Camera3Stream::setUsage(uint64_t usage) {
118 mUsage = usage;
119}
120
Emilian Peev710c1422017-08-30 11:19:38 +0100121void Camera3Stream::setFormatOverride(bool formatOverridden) {
122 mFormatOverridden = formatOverridden;
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -0700123 if (formatOverridden) mOriginalFormat = camera3_stream::format;
Emilian Peev710c1422017-08-30 11:19:38 +0100124}
125
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -0700126bool Camera3Stream::isFormatOverridden() const {
Emilian Peev710c1422017-08-30 11:19:38 +0100127 return mFormatOverridden;
128}
129
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -0700130int Camera3Stream::getOriginalFormat() const {
131 return mOriginalFormat;
Emilian Peev710c1422017-08-30 11:19:38 +0100132}
133
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -0700134void Camera3Stream::setDataSpaceOverride(bool dataSpaceOverridden) {
135 mDataSpaceOverridden = dataSpaceOverridden;
136 if (dataSpaceOverridden) mOriginalDataSpace = camera3_stream::data_space;
137}
138
139bool Camera3Stream::isDataSpaceOverridden() const {
140 return mDataSpaceOverridden;
141}
142
143android_dataspace Camera3Stream::getOriginalDataSpace() const {
144 return mOriginalDataSpace;
Emilian Peev710c1422017-08-30 11:19:38 +0100145}
146
Shuzhen Wang5c22c152017-12-31 17:12:25 -0800147const String8& Camera3Stream::physicalCameraId() const {
148 return mPhysicalCameraId;
149}
150
Emilian Peevac3ce6c2017-12-12 15:27:02 +0000151status_t Camera3Stream::forceToIdle() {
152 ATRACE_CALL();
153 Mutex::Autolock l(mLock);
154 status_t res;
155
156 switch (mState) {
157 case STATE_ERROR:
158 case STATE_CONSTRUCTED:
159 case STATE_IN_CONFIG:
160 case STATE_PREPARING:
161 case STATE_IN_RECONFIG:
162 ALOGE("%s: Invalid state: %d", __FUNCTION__, mState);
163 res = NO_INIT;
164 break;
165 case STATE_CONFIGURED:
166 if (hasOutstandingBuffersLocked()) {
167 sp<StatusTracker> statusTracker = mStatusTracker.promote();
168 if (statusTracker != 0) {
169 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
170 }
171 }
172
173 mState = STATE_IN_IDLE;
174 res = OK;
175
176 break;
177 default:
178 ALOGE("%s: Unknown state %d", __FUNCTION__, mState);
179 res = NO_INIT;
180 }
181
182 return res;
183}
184
185status_t Camera3Stream::restoreConfiguredState() {
186 ATRACE_CALL();
187 Mutex::Autolock l(mLock);
188 status_t res;
189
190 switch (mState) {
191 case STATE_ERROR:
192 case STATE_CONSTRUCTED:
193 case STATE_IN_CONFIG:
194 case STATE_PREPARING:
195 case STATE_IN_RECONFIG:
196 case STATE_CONFIGURED:
197 ALOGE("%s: Invalid state: %d", __FUNCTION__, mState);
198 res = NO_INIT;
199 break;
200 case STATE_IN_IDLE:
201 if (hasOutstandingBuffersLocked()) {
202 sp<StatusTracker> statusTracker = mStatusTracker.promote();
203 if (statusTracker != 0) {
204 statusTracker->markComponentActive(mStatusId);
205 }
206 }
207
208 mState = STATE_CONFIGURED;
209 res = OK;
210
211 break;
212 default:
213 ALOGE("%s: Unknown state %d", __FUNCTION__, mState);
214 res = NO_INIT;
215 }
216
217 return res;
218}
219
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800220camera3_stream* Camera3Stream::startConfiguration() {
Eino-Ville Talvalab2f5b192013-07-30 14:36:03 -0700221 ATRACE_CALL();
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800222 Mutex::Autolock l(mLock);
Eino-Ville Talvalab2f5b192013-07-30 14:36:03 -0700223 status_t res;
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800224
225 switch (mState) {
226 case STATE_ERROR:
227 ALOGE("%s: In error state", __FUNCTION__);
228 return NULL;
229 case STATE_CONSTRUCTED:
Emilian Peevac3ce6c2017-12-12 15:27:02 +0000230 case STATE_IN_IDLE:
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800231 // OK
232 break;
233 case STATE_IN_CONFIG:
234 case STATE_IN_RECONFIG:
235 // Can start config again with no trouble; but don't redo
Eino-Ville Talvala02bf0322016-02-18 12:41:10 -0800236 // mOldUsage/mOldMaxBuffers
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800237 return this;
238 case STATE_CONFIGURED:
Chien-Yu Chen90746f42015-04-15 13:50:13 -0700239 if (hasOutstandingBuffersLocked()) {
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800240 ALOGE("%s: Cannot configure stream; has outstanding buffers",
241 __FUNCTION__);
242 return NULL;
243 }
244 break;
245 default:
246 ALOGE("%s: Unknown state %d", __FUNCTION__, mState);
247 return NULL;
248 }
249
Emilian Peev050f5dc2017-05-18 14:43:56 +0100250 mOldUsage = mUsage;
Eino-Ville Talvala02bf0322016-02-18 12:41:10 -0800251 mOldMaxBuffers = camera3_stream::max_buffers;
Eino-Ville Talvalab2f5b192013-07-30 14:36:03 -0700252
Emilian Peev050f5dc2017-05-18 14:43:56 +0100253 res = getEndpointUsage(&mUsage);
Eino-Ville Talvalab2f5b192013-07-30 14:36:03 -0700254 if (res != OK) {
255 ALOGE("%s: Cannot query consumer endpoint usage!",
256 __FUNCTION__);
257 return NULL;
258 }
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800259
Emilian Peevac3ce6c2017-12-12 15:27:02 +0000260 if (mState == STATE_IN_IDLE) {
261 // Skip configuration.
262 return this;
263 }
264
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700265 // Stop tracking if currently doing so
266 if (mStatusId != StatusTracker::NO_STATUS_ID) {
267 sp<StatusTracker> statusTracker = mStatusTracker.promote();
268 if (statusTracker != 0) {
269 statusTracker->removeComponent(mStatusId);
270 }
271 mStatusId = StatusTracker::NO_STATUS_ID;
272 }
273
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800274 if (mState == STATE_CONSTRUCTED) {
275 mState = STATE_IN_CONFIG;
276 } else { // mState == STATE_CONFIGURED
Igor Murashkin13d315e2014-04-03 18:09:04 -0700277 LOG_ALWAYS_FATAL_IF(mState != STATE_CONFIGURED, "Invalid state: 0x%x", mState);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800278 mState = STATE_IN_RECONFIG;
279 }
280
281 return this;
282}
283
284bool Camera3Stream::isConfiguring() const {
285 Mutex::Autolock l(mLock);
286 return (mState == STATE_IN_CONFIG) || (mState == STATE_IN_RECONFIG);
287}
288
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800289status_t Camera3Stream::finishConfiguration() {
Eino-Ville Talvalab2f5b192013-07-30 14:36:03 -0700290 ATRACE_CALL();
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800291 Mutex::Autolock l(mLock);
292 switch (mState) {
293 case STATE_ERROR:
294 ALOGE("%s: In error state", __FUNCTION__);
295 return INVALID_OPERATION;
296 case STATE_IN_CONFIG:
297 case STATE_IN_RECONFIG:
298 // OK
299 break;
300 case STATE_CONSTRUCTED:
301 case STATE_CONFIGURED:
302 ALOGE("%s: Cannot finish configuration that hasn't been started",
303 __FUNCTION__);
304 return INVALID_OPERATION;
Emilian Peevac3ce6c2017-12-12 15:27:02 +0000305 case STATE_IN_IDLE:
306 //Skip configuration in this state
307 return OK;
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800308 default:
309 ALOGE("%s: Unknown state", __FUNCTION__);
310 return INVALID_OPERATION;
311 }
312
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700313 // Register for idle tracking
314 sp<StatusTracker> statusTracker = mStatusTracker.promote();
315 if (statusTracker != 0) {
316 mStatusId = statusTracker->addComponent();
317 }
318
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800319 // Check if the stream configuration is unchanged, and skip reallocation if
320 // so. As documented in hardware/camera3.h:configure_streams().
321 if (mState == STATE_IN_RECONFIG &&
Emilian Peev050f5dc2017-05-18 14:43:56 +0100322 mOldUsage == mUsage &&
Eino-Ville Talvala02bf0322016-02-18 12:41:10 -0800323 mOldMaxBuffers == camera3_stream::max_buffers) {
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800324 mState = STATE_CONFIGURED;
325 return OK;
326 }
327
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700328 // Reset prepared state, since buffer config has changed, and existing
329 // allocations are no longer valid
330 mPrepared = false;
Shuzhen Wangb3a0fb52018-09-13 17:24:08 -0700331 mPrepareBlockRequest = true;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700332 mStreamUnpreparable = false;
333
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800334 status_t res;
335 res = configureQueueLocked();
336 if (res != OK) {
337 ALOGE("%s: Unable to configure stream %d queue: %s (%d)",
338 __FUNCTION__, mId, strerror(-res), res);
339 mState = STATE_ERROR;
340 return res;
341 }
342
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800343 mState = STATE_CONFIGURED;
344
345 return res;
346}
347
Eino-Ville Talvala17543512014-08-06 14:32:02 -0700348status_t Camera3Stream::cancelConfiguration() {
349 ATRACE_CALL();
350 Mutex::Autolock l(mLock);
351 switch (mState) {
352 case STATE_ERROR:
353 ALOGE("%s: In error state", __FUNCTION__);
354 return INVALID_OPERATION;
355 case STATE_IN_CONFIG:
356 case STATE_IN_RECONFIG:
Emilian Peevac3ce6c2017-12-12 15:27:02 +0000357 case STATE_IN_IDLE:
Eino-Ville Talvala17543512014-08-06 14:32:02 -0700358 // OK
359 break;
360 case STATE_CONSTRUCTED:
361 case STATE_CONFIGURED:
362 ALOGE("%s: Cannot cancel configuration that hasn't been started",
363 __FUNCTION__);
364 return INVALID_OPERATION;
365 default:
366 ALOGE("%s: Unknown state", __FUNCTION__);
367 return INVALID_OPERATION;
368 }
369
Emilian Peev050f5dc2017-05-18 14:43:56 +0100370 mUsage = mOldUsage;
Eino-Ville Talvala02bf0322016-02-18 12:41:10 -0800371 camera3_stream::max_buffers = mOldMaxBuffers;
Eino-Ville Talvala17543512014-08-06 14:32:02 -0700372
Emilian Peevac3ce6c2017-12-12 15:27:02 +0000373 mState = ((mState == STATE_IN_RECONFIG) || (mState == STATE_IN_IDLE)) ? STATE_CONFIGURED :
374 STATE_CONSTRUCTED;
375
Eino-Ville Talvala17543512014-08-06 14:32:02 -0700376 return OK;
377}
378
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700379bool Camera3Stream::isUnpreparable() {
380 ATRACE_CALL();
381
382 Mutex::Autolock l(mLock);
383 return mStreamUnpreparable;
384}
385
Shuzhen Wangb3a0fb52018-09-13 17:24:08 -0700386status_t Camera3Stream::startPrepare(int maxCount, bool blockRequest) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700387 ATRACE_CALL();
388
389 Mutex::Autolock l(mLock);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700390
Ruben Brunkc78ac262015-08-13 17:58:46 -0700391 if (maxCount < 0) {
392 ALOGE("%s: Stream %d: Can't prepare stream if max buffer count (%d) is < 0",
393 __FUNCTION__, mId, maxCount);
394 return BAD_VALUE;
395 }
396
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700397 // This function should be only called when the stream is configured already.
398 if (mState != STATE_CONFIGURED) {
399 ALOGE("%s: Stream %d: Can't prepare stream if stream is not in CONFIGURED "
400 "state %d", __FUNCTION__, mId, mState);
401 return INVALID_OPERATION;
402 }
403
404 // This function can't be called if the stream has already received filled
405 // buffers
406 if (mStreamUnpreparable) {
407 ALOGE("%s: Stream %d: Can't prepare stream that's already in use",
408 __FUNCTION__, mId);
409 return INVALID_OPERATION;
410 }
411
412 if (getHandoutOutputBufferCountLocked() > 0) {
413 ALOGE("%s: Stream %d: Can't prepare stream that has outstanding buffers",
414 __FUNCTION__, mId);
415 return INVALID_OPERATION;
416 }
417
Ruben Brunkc78ac262015-08-13 17:58:46 -0700418 size_t pipelineMax = getBufferCountLocked();
419 size_t clampedCount = (pipelineMax < static_cast<size_t>(maxCount)) ?
420 pipelineMax : static_cast<size_t>(maxCount);
421 size_t bufferCount = (maxCount == Camera3StreamInterface::ALLOCATE_PIPELINE_MAX) ?
422 pipelineMax : clampedCount;
423
424 mPrepared = bufferCount <= mLastMaxCount;
Shuzhen Wangb3a0fb52018-09-13 17:24:08 -0700425 mPrepareBlockRequest = blockRequest;
Ruben Brunkc78ac262015-08-13 17:58:46 -0700426
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700427 if (mPrepared) return OK;
428
Ruben Brunkc78ac262015-08-13 17:58:46 -0700429 mLastMaxCount = bufferCount;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700430
431 mPreparedBuffers.insertAt(camera3_stream_buffer_t(), /*index*/0, bufferCount);
432 mPreparedBufferIdx = 0;
433
434 mState = STATE_PREPARING;
435
436 return NOT_ENOUGH_DATA;
437}
438
Shuzhen Wangb3a0fb52018-09-13 17:24:08 -0700439bool Camera3Stream::isBlockedByPrepare() const {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700440 Mutex::Autolock l(mLock);
Shuzhen Wangb3a0fb52018-09-13 17:24:08 -0700441 return mState == STATE_PREPARING && mPrepareBlockRequest;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700442}
443
Chien-Yu Chene8c535e2016-04-14 12:18:26 -0700444bool Camera3Stream::isAbandoned() const {
445 Mutex::Autolock l(mLock);
446 return mState == STATE_ABANDONED;
447}
448
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700449status_t Camera3Stream::prepareNextBuffer() {
450 ATRACE_CALL();
451
452 Mutex::Autolock l(mLock);
453 status_t res = OK;
454
455 // This function should be only called when the stream is preparing
456 if (mState != STATE_PREPARING) {
457 ALOGE("%s: Stream %d: Can't prepare buffer if stream is not in PREPARING "
458 "state %d", __FUNCTION__, mId, mState);
459 return INVALID_OPERATION;
460 }
461
462 // Get next buffer - this may allocate, and take a while for large buffers
463 res = getBufferLocked( &mPreparedBuffers.editItemAt(mPreparedBufferIdx) );
464 if (res != OK) {
Eino-Ville Talvala02bf0322016-02-18 12:41:10 -0800465 ALOGE("%s: Stream %d: Unable to allocate buffer %zu during preparation",
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700466 __FUNCTION__, mId, mPreparedBufferIdx);
467 return NO_INIT;
468 }
469
470 mPreparedBufferIdx++;
471
472 // Check if we still have buffers left to allocate
473 if (mPreparedBufferIdx < mPreparedBuffers.size()) {
474 return NOT_ENOUGH_DATA;
475 }
476
477 // Done with prepare - mark stream as such, and return all buffers
478 // via cancelPrepare
479 mPrepared = true;
480
481 return cancelPrepareLocked();
482}
483
484status_t Camera3Stream::cancelPrepare() {
485 ATRACE_CALL();
486
487 Mutex::Autolock l(mLock);
488
489 return cancelPrepareLocked();
490}
491
492status_t Camera3Stream::cancelPrepareLocked() {
493 status_t res = OK;
494
495 // This function should be only called when the stream is mid-preparing.
496 if (mState != STATE_PREPARING) {
497 ALOGE("%s: Stream %d: Can't cancel prepare stream if stream is not in "
498 "PREPARING state %d", __FUNCTION__, mId, mState);
499 return INVALID_OPERATION;
500 }
501
502 // Return all valid buffers to stream, in ERROR state to indicate
503 // they weren't filled.
504 for (size_t i = 0; i < mPreparedBufferIdx; i++) {
505 mPreparedBuffers.editItemAt(i).release_fence = -1;
506 mPreparedBuffers.editItemAt(i).status = CAMERA3_BUFFER_STATUS_ERROR;
507 returnBufferLocked(mPreparedBuffers[i], 0);
508 }
509 mPreparedBuffers.clear();
510 mPreparedBufferIdx = 0;
511
512 mState = STATE_CONFIGURED;
513
514 return res;
515}
516
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -0700517status_t Camera3Stream::tearDown() {
518 ATRACE_CALL();
519 Mutex::Autolock l(mLock);
520
521 status_t res = OK;
522
523 // This function should be only called when the stream is configured.
524 if (mState != STATE_CONFIGURED) {
525 ALOGE("%s: Stream %d: Can't tear down stream if stream is not in "
526 "CONFIGURED state %d", __FUNCTION__, mId, mState);
527 return INVALID_OPERATION;
528 }
529
530 // If any buffers have been handed to the HAL, the stream cannot be torn down.
531 if (getHandoutOutputBufferCountLocked() > 0) {
532 ALOGE("%s: Stream %d: Can't tear down a stream that has outstanding buffers",
533 __FUNCTION__, mId);
534 return INVALID_OPERATION;
535 }
536
537 // Free buffers by disconnecting and then reconnecting to the buffer queue
538 // Only unused buffers will be dropped immediately; buffers that have been filled
539 // and are waiting to be acquired by the consumer and buffers that are currently
540 // acquired will be freed once they are released by the consumer.
541
542 res = disconnectLocked();
543 if (res != OK) {
544 if (res == -ENOTCONN) {
545 // queue has been disconnected, nothing left to do, so exit with success
546 return OK;
547 }
548 ALOGE("%s: Stream %d: Unable to disconnect to tear down buffers: %s (%d)",
549 __FUNCTION__, mId, strerror(-res), res);
550 return res;
551 }
552
553 mState = STATE_IN_CONFIG;
554
555 res = configureQueueLocked();
556 if (res != OK) {
557 ALOGE("%s: Unable to configure stream %d queue: %s (%d)",
558 __FUNCTION__, mId, strerror(-res), res);
559 mState = STATE_ERROR;
560 return res;
561 }
562
563 // Reset prepared state, since we've reconnected to the queue and can prepare again.
564 mPrepared = false;
565 mStreamUnpreparable = false;
566
567 mState = STATE_CONFIGURED;
568
569 return OK;
570}
571
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800572status_t Camera3Stream::getBuffer(camera3_stream_buffer *buffer,
Yin-Chia Yehb3a80b12018-09-04 12:13:05 -0700573 nsecs_t waitBufferTimeout,
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800574 const std::vector<size_t>& surface_ids) {
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800575 ATRACE_CALL();
576 Mutex::Autolock l(mLock);
Zhijun He6adc9cc2014-04-15 14:09:55 -0700577 status_t res = OK;
Igor Murashkin2fba5842013-04-22 14:03:54 -0700578
Zhijun He6adc9cc2014-04-15 14:09:55 -0700579 // This function should be only called when the stream is configured already.
580 if (mState != STATE_CONFIGURED) {
581 ALOGE("%s: Stream %d: Can't get buffers if stream is not in CONFIGURED state %d",
582 __FUNCTION__, mId, mState);
583 return INVALID_OPERATION;
584 }
585
586 // Wait for new buffer returned back if we are running into the limit.
587 if (getHandoutOutputBufferCountLocked() == camera3_stream::max_buffers) {
588 ALOGV("%s: Already dequeued max output buffers (%d), wait for next returned one.",
Shuzhen Wang686f6442017-06-20 16:16:04 -0700589 __FUNCTION__, camera3_stream::max_buffers);
590 nsecs_t waitStart = systemTime(SYSTEM_TIME_MONOTONIC);
Yin-Chia Yehb3a80b12018-09-04 12:13:05 -0700591 if (waitBufferTimeout < kWaitForBufferDuration) {
592 waitBufferTimeout = kWaitForBufferDuration;
593 }
594 res = mOutputBufferReturnedSignal.waitRelative(mLock, waitBufferTimeout);
Shuzhen Wang686f6442017-06-20 16:16:04 -0700595 nsecs_t waitEnd = systemTime(SYSTEM_TIME_MONOTONIC);
596 mBufferLimitLatency.add(waitStart, waitEnd);
Zhijun He6adc9cc2014-04-15 14:09:55 -0700597 if (res != OK) {
598 if (res == TIMED_OUT) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -0700599 ALOGE("%s: wait for output buffer return timed out after %lldms (max_buffers %d)",
Yin-Chia Yehb3a80b12018-09-04 12:13:05 -0700600 __FUNCTION__, waitBufferTimeout / 1000000LL,
Chien-Yu Chen85a64552015-08-28 15:46:12 -0700601 camera3_stream::max_buffers);
Zhijun He6adc9cc2014-04-15 14:09:55 -0700602 }
603 return res;
604 }
605 }
606
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800607 res = getBufferLocked(buffer, surface_ids);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700608 if (res == OK) {
609 fireBufferListenersLocked(*buffer, /*acquired*/true, /*output*/true);
Chien-Yu Chene02e9322016-04-11 16:59:33 -0700610 if (buffer->buffer) {
Emilian Peev889234d2017-07-18 18:21:26 -0700611 Mutex::Autolock l(mOutstandingBuffersLock);
Chien-Yu Chene02e9322016-04-11 16:59:33 -0700612 mOutstandingBuffers.push_back(*buffer->buffer);
613 }
Igor Murashkin2fba5842013-04-22 14:03:54 -0700614 }
615
616 return res;
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800617}
618
Emilian Peev889234d2017-07-18 18:21:26 -0700619bool Camera3Stream::isOutstandingBuffer(const camera3_stream_buffer &buffer) const{
Chien-Yu Chene02e9322016-04-11 16:59:33 -0700620 if (buffer.buffer == nullptr) {
621 return false;
622 }
623
Emilian Peev889234d2017-07-18 18:21:26 -0700624 Mutex::Autolock l(mOutstandingBuffersLock);
625
Chien-Yu Chene02e9322016-04-11 16:59:33 -0700626 for (auto b : mOutstandingBuffers) {
627 if (b == *buffer.buffer) {
628 return true;
629 }
630 }
631 return false;
632}
633
634void Camera3Stream::removeOutstandingBuffer(const camera3_stream_buffer &buffer) {
635 if (buffer.buffer == nullptr) {
636 return;
637 }
638
Emilian Peev889234d2017-07-18 18:21:26 -0700639 Mutex::Autolock l(mOutstandingBuffersLock);
640
Chien-Yu Chene02e9322016-04-11 16:59:33 -0700641 for (auto b = mOutstandingBuffers.begin(); b != mOutstandingBuffers.end(); b++) {
642 if (*b == *buffer.buffer) {
643 mOutstandingBuffers.erase(b);
644 return;
645 }
646 }
647}
648
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800649status_t Camera3Stream::returnBuffer(const camera3_stream_buffer &buffer,
650 nsecs_t timestamp) {
651 ATRACE_CALL();
652 Mutex::Autolock l(mLock);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700653
Chien-Yu Chene02e9322016-04-11 16:59:33 -0700654 // Check if this buffer is outstanding.
655 if (!isOutstandingBuffer(buffer)) {
656 ALOGE("%s: Stream %d: Returning an unknown buffer.", __FUNCTION__, mId);
657 return BAD_VALUE;
658 }
659
Shuzhen Wang1c484a62017-07-14 15:14:19 -0700660 removeOutstandingBuffer(buffer);
661
Igor Murashkin13d315e2014-04-03 18:09:04 -0700662 /**
663 * TODO: Check that the state is valid first.
664 *
665 * <HAL3.2 IN_CONFIG and IN_RECONFIG in addition to CONFIGURED.
666 * >= HAL3.2 CONFIGURED only
667 *
668 * Do this for getBuffer as well.
669 */
Igor Murashkin2fba5842013-04-22 14:03:54 -0700670 status_t res = returnBufferLocked(buffer, timestamp);
671 if (res == OK) {
672 fireBufferListenersLocked(buffer, /*acquired*/false, /*output*/true);
673 }
674
Chien-Yu Chenb83c1fe2015-09-10 16:15:21 -0700675 // Even if returning the buffer failed, we still want to signal whoever is waiting for the
676 // buffer to be returned.
677 mOutputBufferReturnedSignal.signal();
678
Igor Murashkin2fba5842013-04-22 14:03:54 -0700679 return res;
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800680}
681
Eino-Ville Talvalaba435252017-06-21 16:07:25 -0700682status_t Camera3Stream::getInputBuffer(camera3_stream_buffer *buffer, bool respectHalLimit) {
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700683 ATRACE_CALL();
684 Mutex::Autolock l(mLock);
Zhijun He6adc9cc2014-04-15 14:09:55 -0700685 status_t res = OK;
Igor Murashkin2fba5842013-04-22 14:03:54 -0700686
Zhijun He6adc9cc2014-04-15 14:09:55 -0700687 // This function should be only called when the stream is configured already.
688 if (mState != STATE_CONFIGURED) {
689 ALOGE("%s: Stream %d: Can't get input buffers if stream is not in CONFIGURED state %d",
690 __FUNCTION__, mId, mState);
691 return INVALID_OPERATION;
692 }
693
694 // Wait for new buffer returned back if we are running into the limit.
Eino-Ville Talvalaba435252017-06-21 16:07:25 -0700695 if (getHandoutInputBufferCountLocked() == camera3_stream::max_buffers && respectHalLimit) {
Zhijun He6adc9cc2014-04-15 14:09:55 -0700696 ALOGV("%s: Already dequeued max input buffers (%d), wait for next returned one.",
697 __FUNCTION__, camera3_stream::max_buffers);
698 res = mInputBufferReturnedSignal.waitRelative(mLock, kWaitForBufferDuration);
699 if (res != OK) {
700 if (res == TIMED_OUT) {
701 ALOGE("%s: wait for input buffer return timed out after %lldms", __FUNCTION__,
702 kWaitForBufferDuration / 1000000LL);
703 }
704 return res;
705 }
706 }
707
708 res = getInputBufferLocked(buffer);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700709 if (res == OK) {
710 fireBufferListenersLocked(*buffer, /*acquired*/true, /*output*/false);
Chien-Yu Chene02e9322016-04-11 16:59:33 -0700711 if (buffer->buffer) {
Emilian Peev889234d2017-07-18 18:21:26 -0700712 Mutex::Autolock l(mOutstandingBuffersLock);
Chien-Yu Chene02e9322016-04-11 16:59:33 -0700713 mOutstandingBuffers.push_back(*buffer->buffer);
714 }
Igor Murashkin2fba5842013-04-22 14:03:54 -0700715 }
716
717 return res;
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700718}
719
720status_t Camera3Stream::returnInputBuffer(const camera3_stream_buffer &buffer) {
721 ATRACE_CALL();
722 Mutex::Autolock l(mLock);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700723
Chien-Yu Chene02e9322016-04-11 16:59:33 -0700724 // Check if this buffer is outstanding.
725 if (!isOutstandingBuffer(buffer)) {
726 ALOGE("%s: Stream %d: Returning an unknown buffer.", __FUNCTION__, mId);
727 return BAD_VALUE;
728 }
729
Shuzhen Wang1c484a62017-07-14 15:14:19 -0700730 removeOutstandingBuffer(buffer);
731
Igor Murashkin2fba5842013-04-22 14:03:54 -0700732 status_t res = returnInputBufferLocked(buffer);
733 if (res == OK) {
734 fireBufferListenersLocked(buffer, /*acquired*/false, /*output*/false);
Zhijun He6adc9cc2014-04-15 14:09:55 -0700735 mInputBufferReturnedSignal.signal();
Igor Murashkin2fba5842013-04-22 14:03:54 -0700736 }
Chien-Yu Chene02e9322016-04-11 16:59:33 -0700737
Igor Murashkin2fba5842013-04-22 14:03:54 -0700738 return res;
739}
740
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700741status_t Camera3Stream::getInputBufferProducer(sp<IGraphicBufferProducer> *producer) {
742 ATRACE_CALL();
743 Mutex::Autolock l(mLock);
744
745 return getInputBufferProducerLocked(producer);
746}
747
Igor Murashkin2fba5842013-04-22 14:03:54 -0700748void Camera3Stream::fireBufferListenersLocked(
Shuzhen Wangb0fdc1e2016-03-20 23:21:39 -0700749 const camera3_stream_buffer& buffer, bool acquired, bool output) {
Igor Murashkin2fba5842013-04-22 14:03:54 -0700750 List<wp<Camera3StreamBufferListener> >::iterator it, end;
751
752 // TODO: finish implementing
753
754 Camera3StreamBufferListener::BufferInfo info =
755 Camera3StreamBufferListener::BufferInfo();
756 info.mOutput = output;
Shuzhen Wangb0fdc1e2016-03-20 23:21:39 -0700757 info.mError = (buffer.status == CAMERA3_BUFFER_STATUS_ERROR);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700758 // TODO: rest of fields
759
760 for (it = mBufferListenerList.begin(), end = mBufferListenerList.end();
761 it != end;
762 ++it) {
763
764 sp<Camera3StreamBufferListener> listener = it->promote();
765 if (listener != 0) {
766 if (acquired) {
767 listener->onBufferAcquired(info);
768 } else {
769 listener->onBufferReleased(info);
770 }
771 }
772 }
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700773}
774
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800775bool Camera3Stream::hasOutstandingBuffers() const {
776 ATRACE_CALL();
777 Mutex::Autolock l(mLock);
778 return hasOutstandingBuffersLocked();
779}
780
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700781status_t Camera3Stream::setStatusTracker(sp<StatusTracker> statusTracker) {
782 Mutex::Autolock l(mLock);
783 sp<StatusTracker> oldTracker = mStatusTracker.promote();
784 if (oldTracker != 0 && mStatusId != StatusTracker::NO_STATUS_ID) {
785 oldTracker->removeComponent(mStatusId);
786 }
787 mStatusId = StatusTracker::NO_STATUS_ID;
788 mStatusTracker = statusTracker;
789
790 return OK;
791}
792
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800793status_t Camera3Stream::disconnect() {
794 ATRACE_CALL();
795 Mutex::Autolock l(mLock);
Igor Murashkine2172be2013-05-28 15:31:39 -0700796 ALOGV("%s: Stream %d: Disconnecting...", __FUNCTION__, mId);
797 status_t res = disconnectLocked();
798
Shuzhen Wang686f6442017-06-20 16:16:04 -0700799 mBufferLimitLatency.log("Stream %d latency histogram for wait on max_buffers", mId);
800 mBufferLimitLatency.reset();
801
Igor Murashkine2172be2013-05-28 15:31:39 -0700802 if (res == -ENOTCONN) {
803 // "Already disconnected" -- not an error
804 return OK;
805 } else {
806 return res;
807 }
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800808}
809
Shuzhen Wang686f6442017-06-20 16:16:04 -0700810void Camera3Stream::dump(int fd, const Vector<String16> &args) const
811{
812 (void)args;
813 mBufferLimitLatency.dump(fd,
814 " Latency histogram for wait on max_buffers");
815}
816
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800817status_t Camera3Stream::getBufferLocked(camera3_stream_buffer *,
818 const std::vector<size_t>&) {
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700819 ALOGE("%s: This type of stream does not support output", __FUNCTION__);
820 return INVALID_OPERATION;
821}
822status_t Camera3Stream::returnBufferLocked(const camera3_stream_buffer &,
823 nsecs_t) {
824 ALOGE("%s: This type of stream does not support output", __FUNCTION__);
825 return INVALID_OPERATION;
826}
827status_t Camera3Stream::getInputBufferLocked(camera3_stream_buffer *) {
828 ALOGE("%s: This type of stream does not support input", __FUNCTION__);
829 return INVALID_OPERATION;
830}
831status_t Camera3Stream::returnInputBufferLocked(
832 const camera3_stream_buffer &) {
833 ALOGE("%s: This type of stream does not support input", __FUNCTION__);
834 return INVALID_OPERATION;
835}
Eino-Ville Talvala02bf0322016-02-18 12:41:10 -0800836status_t Camera3Stream::getInputBufferProducerLocked(sp<IGraphicBufferProducer>*) {
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700837 ALOGE("%s: This type of stream does not support input", __FUNCTION__);
838 return INVALID_OPERATION;
839}
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700840
Igor Murashkin2fba5842013-04-22 14:03:54 -0700841void Camera3Stream::addBufferListener(
842 wp<Camera3StreamBufferListener> listener) {
843 Mutex::Autolock l(mLock);
Zhijun Hef0d962a2014-06-30 10:24:11 -0700844
845 List<wp<Camera3StreamBufferListener> >::iterator it, end;
846 for (it = mBufferListenerList.begin(), end = mBufferListenerList.end();
847 it != end;
848 ) {
849 if (*it == listener) {
850 ALOGE("%s: Try to add the same listener twice, ignoring...", __FUNCTION__);
851 return;
852 }
853 it++;
854 }
855
Igor Murashkin2fba5842013-04-22 14:03:54 -0700856 mBufferListenerList.push_back(listener);
857}
858
859void Camera3Stream::removeBufferListener(
860 const sp<Camera3StreamBufferListener>& listener) {
861 Mutex::Autolock l(mLock);
862
863 bool erased = true;
864 List<wp<Camera3StreamBufferListener> >::iterator it, end;
865 for (it = mBufferListenerList.begin(), end = mBufferListenerList.end();
866 it != end;
867 ) {
868
869 if (*it == listener) {
870 it = mBufferListenerList.erase(it);
871 erased = true;
872 } else {
873 ++it;
874 }
875 }
876
877 if (!erased) {
878 ALOGW("%s: Could not find listener to remove, already removed",
879 __FUNCTION__);
880 }
881}
882
Yin-Chia Yehbe83fa72017-03-30 13:35:36 -0700883void Camera3Stream::setBufferFreedListener(
Yin-Chia Yehdb1e8642017-07-14 15:19:30 -0700884 wp<Camera3StreamBufferFreedListener> listener) {
Yin-Chia Yehbe83fa72017-03-30 13:35:36 -0700885 Mutex::Autolock l(mLock);
886 // Only allow set listener during stream configuration because stream is guaranteed to be IDLE
887 // at this state, so setBufferFreedListener won't collide with onBufferFreed callbacks
888 if (mState != STATE_IN_CONFIG && mState != STATE_IN_RECONFIG) {
889 ALOGE("%s: listener must be set during stream configuration!",__FUNCTION__);
890 return;
891 }
892 mBufferFreedListener = listener;
893}
894
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800895}; // namespace camera3
896
897}; // namespace android