blob: 53ff9fe080ad3524762a1524aa4dfad7d3d3dfeb [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),
64 mPreparedBufferIdx(0),
Shuzhen Wang686f6442017-06-20 16:16:04 -070065 mLastMaxCount(Camera3StreamInterface::ALLOCATE_PIPELINE_MAX),
Emilian Peev710c1422017-08-30 11:19:38 +010066 mBufferLimitLatency(kBufferLimitLatencyBinSize),
67 mFormatOverridden(false),
Shuzhen Wangc28189a2017-11-27 23:05:10 -080068 mOriginalFormat(-1),
Shuzhen Wang26abaf42018-08-28 15:41:20 -070069 mPhysicalCameraId(physicalCameraId),
70 mLastTimestamp(0) {
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;
331 mStreamUnpreparable = false;
332
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800333 status_t res;
334 res = configureQueueLocked();
Shuzhen Wang210ba5c2018-07-25 16:47:40 -0700335 // configureQueueLocked could return error in case of abandoned surface.
336 // Treat as non-fatal error.
337 if (res == NO_INIT || res == DEAD_OBJECT) {
338 ALOGE("%s: Unable to configure stream %d queue (non-fatal): %s (%d)",
339 __FUNCTION__, mId, strerror(-res), res);
340 mState = STATE_ABANDONED;
341 return res;
342 } else if (res != OK) {
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800343 ALOGE("%s: Unable to configure stream %d queue: %s (%d)",
344 __FUNCTION__, mId, strerror(-res), res);
345 mState = STATE_ERROR;
346 return res;
347 }
348
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800349 mState = STATE_CONFIGURED;
350
351 return res;
352}
353
Eino-Ville Talvala17543512014-08-06 14:32:02 -0700354status_t Camera3Stream::cancelConfiguration() {
355 ATRACE_CALL();
356 Mutex::Autolock l(mLock);
357 switch (mState) {
358 case STATE_ERROR:
359 ALOGE("%s: In error state", __FUNCTION__);
360 return INVALID_OPERATION;
361 case STATE_IN_CONFIG:
362 case STATE_IN_RECONFIG:
Emilian Peevac3ce6c2017-12-12 15:27:02 +0000363 case STATE_IN_IDLE:
Eino-Ville Talvala17543512014-08-06 14:32:02 -0700364 // OK
365 break;
366 case STATE_CONSTRUCTED:
367 case STATE_CONFIGURED:
368 ALOGE("%s: Cannot cancel configuration that hasn't been started",
369 __FUNCTION__);
370 return INVALID_OPERATION;
371 default:
372 ALOGE("%s: Unknown state", __FUNCTION__);
373 return INVALID_OPERATION;
374 }
375
Emilian Peev050f5dc2017-05-18 14:43:56 +0100376 mUsage = mOldUsage;
Eino-Ville Talvala02bf0322016-02-18 12:41:10 -0800377 camera3_stream::max_buffers = mOldMaxBuffers;
Eino-Ville Talvala17543512014-08-06 14:32:02 -0700378
Emilian Peevac3ce6c2017-12-12 15:27:02 +0000379 mState = ((mState == STATE_IN_RECONFIG) || (mState == STATE_IN_IDLE)) ? STATE_CONFIGURED :
380 STATE_CONSTRUCTED;
381
Eino-Ville Talvala17543512014-08-06 14:32:02 -0700382 return OK;
383}
384
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700385bool Camera3Stream::isUnpreparable() {
386 ATRACE_CALL();
387
388 Mutex::Autolock l(mLock);
389 return mStreamUnpreparable;
390}
391
Ruben Brunkc78ac262015-08-13 17:58:46 -0700392status_t Camera3Stream::startPrepare(int maxCount) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700393 ATRACE_CALL();
394
395 Mutex::Autolock l(mLock);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700396
Ruben Brunkc78ac262015-08-13 17:58:46 -0700397 if (maxCount < 0) {
398 ALOGE("%s: Stream %d: Can't prepare stream if max buffer count (%d) is < 0",
399 __FUNCTION__, mId, maxCount);
400 return BAD_VALUE;
401 }
402
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700403 // This function should be only called when the stream is configured already.
404 if (mState != STATE_CONFIGURED) {
405 ALOGE("%s: Stream %d: Can't prepare stream if stream is not in CONFIGURED "
406 "state %d", __FUNCTION__, mId, mState);
407 return INVALID_OPERATION;
408 }
409
410 // This function can't be called if the stream has already received filled
411 // buffers
412 if (mStreamUnpreparable) {
413 ALOGE("%s: Stream %d: Can't prepare stream that's already in use",
414 __FUNCTION__, mId);
415 return INVALID_OPERATION;
416 }
417
418 if (getHandoutOutputBufferCountLocked() > 0) {
419 ALOGE("%s: Stream %d: Can't prepare stream that has outstanding buffers",
420 __FUNCTION__, mId);
421 return INVALID_OPERATION;
422 }
423
Ruben Brunkc78ac262015-08-13 17:58:46 -0700424
425
426 size_t pipelineMax = getBufferCountLocked();
427 size_t clampedCount = (pipelineMax < static_cast<size_t>(maxCount)) ?
428 pipelineMax : static_cast<size_t>(maxCount);
429 size_t bufferCount = (maxCount == Camera3StreamInterface::ALLOCATE_PIPELINE_MAX) ?
430 pipelineMax : clampedCount;
431
432 mPrepared = bufferCount <= mLastMaxCount;
433
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700434 if (mPrepared) return OK;
435
Ruben Brunkc78ac262015-08-13 17:58:46 -0700436 mLastMaxCount = bufferCount;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700437
438 mPreparedBuffers.insertAt(camera3_stream_buffer_t(), /*index*/0, bufferCount);
439 mPreparedBufferIdx = 0;
440
441 mState = STATE_PREPARING;
442
443 return NOT_ENOUGH_DATA;
444}
445
446bool Camera3Stream::isPreparing() const {
447 Mutex::Autolock l(mLock);
448 return mState == STATE_PREPARING;
449}
450
Chien-Yu Chene8c535e2016-04-14 12:18:26 -0700451bool Camera3Stream::isAbandoned() const {
452 Mutex::Autolock l(mLock);
453 return mState == STATE_ABANDONED;
454}
455
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700456status_t Camera3Stream::prepareNextBuffer() {
457 ATRACE_CALL();
458
459 Mutex::Autolock l(mLock);
460 status_t res = OK;
461
462 // This function should be only called when the stream is preparing
463 if (mState != STATE_PREPARING) {
464 ALOGE("%s: Stream %d: Can't prepare buffer if stream is not in PREPARING "
465 "state %d", __FUNCTION__, mId, mState);
466 return INVALID_OPERATION;
467 }
468
469 // Get next buffer - this may allocate, and take a while for large buffers
470 res = getBufferLocked( &mPreparedBuffers.editItemAt(mPreparedBufferIdx) );
471 if (res != OK) {
Eino-Ville Talvala02bf0322016-02-18 12:41:10 -0800472 ALOGE("%s: Stream %d: Unable to allocate buffer %zu during preparation",
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700473 __FUNCTION__, mId, mPreparedBufferIdx);
474 return NO_INIT;
475 }
476
477 mPreparedBufferIdx++;
478
479 // Check if we still have buffers left to allocate
480 if (mPreparedBufferIdx < mPreparedBuffers.size()) {
481 return NOT_ENOUGH_DATA;
482 }
483
484 // Done with prepare - mark stream as such, and return all buffers
485 // via cancelPrepare
486 mPrepared = true;
487
488 return cancelPrepareLocked();
489}
490
491status_t Camera3Stream::cancelPrepare() {
492 ATRACE_CALL();
493
494 Mutex::Autolock l(mLock);
495
496 return cancelPrepareLocked();
497}
498
499status_t Camera3Stream::cancelPrepareLocked() {
500 status_t res = OK;
501
502 // This function should be only called when the stream is mid-preparing.
503 if (mState != STATE_PREPARING) {
504 ALOGE("%s: Stream %d: Can't cancel prepare stream if stream is not in "
505 "PREPARING state %d", __FUNCTION__, mId, mState);
506 return INVALID_OPERATION;
507 }
508
509 // Return all valid buffers to stream, in ERROR state to indicate
510 // they weren't filled.
511 for (size_t i = 0; i < mPreparedBufferIdx; i++) {
512 mPreparedBuffers.editItemAt(i).release_fence = -1;
513 mPreparedBuffers.editItemAt(i).status = CAMERA3_BUFFER_STATUS_ERROR;
514 returnBufferLocked(mPreparedBuffers[i], 0);
515 }
516 mPreparedBuffers.clear();
517 mPreparedBufferIdx = 0;
518
519 mState = STATE_CONFIGURED;
520
521 return res;
522}
523
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -0700524status_t Camera3Stream::tearDown() {
525 ATRACE_CALL();
526 Mutex::Autolock l(mLock);
527
528 status_t res = OK;
529
530 // This function should be only called when the stream is configured.
531 if (mState != STATE_CONFIGURED) {
532 ALOGE("%s: Stream %d: Can't tear down stream if stream is not in "
533 "CONFIGURED state %d", __FUNCTION__, mId, mState);
534 return INVALID_OPERATION;
535 }
536
537 // If any buffers have been handed to the HAL, the stream cannot be torn down.
538 if (getHandoutOutputBufferCountLocked() > 0) {
539 ALOGE("%s: Stream %d: Can't tear down a stream that has outstanding buffers",
540 __FUNCTION__, mId);
541 return INVALID_OPERATION;
542 }
543
544 // Free buffers by disconnecting and then reconnecting to the buffer queue
545 // Only unused buffers will be dropped immediately; buffers that have been filled
546 // and are waiting to be acquired by the consumer and buffers that are currently
547 // acquired will be freed once they are released by the consumer.
548
549 res = disconnectLocked();
550 if (res != OK) {
551 if (res == -ENOTCONN) {
552 // queue has been disconnected, nothing left to do, so exit with success
553 return OK;
554 }
555 ALOGE("%s: Stream %d: Unable to disconnect to tear down buffers: %s (%d)",
556 __FUNCTION__, mId, strerror(-res), res);
557 return res;
558 }
559
560 mState = STATE_IN_CONFIG;
561
562 res = configureQueueLocked();
563 if (res != OK) {
564 ALOGE("%s: Unable to configure stream %d queue: %s (%d)",
565 __FUNCTION__, mId, strerror(-res), res);
566 mState = STATE_ERROR;
567 return res;
568 }
569
570 // Reset prepared state, since we've reconnected to the queue and can prepare again.
571 mPrepared = false;
572 mStreamUnpreparable = false;
573
574 mState = STATE_CONFIGURED;
575
576 return OK;
577}
578
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800579status_t Camera3Stream::getBuffer(camera3_stream_buffer *buffer,
580 const std::vector<size_t>& surface_ids) {
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800581 ATRACE_CALL();
582 Mutex::Autolock l(mLock);
Zhijun He6adc9cc2014-04-15 14:09:55 -0700583 status_t res = OK;
Igor Murashkin2fba5842013-04-22 14:03:54 -0700584
Zhijun He6adc9cc2014-04-15 14:09:55 -0700585 // This function should be only called when the stream is configured already.
586 if (mState != STATE_CONFIGURED) {
587 ALOGE("%s: Stream %d: Can't get buffers if stream is not in CONFIGURED state %d",
588 __FUNCTION__, mId, mState);
589 return INVALID_OPERATION;
590 }
591
592 // Wait for new buffer returned back if we are running into the limit.
593 if (getHandoutOutputBufferCountLocked() == camera3_stream::max_buffers) {
594 ALOGV("%s: Already dequeued max output buffers (%d), wait for next returned one.",
Shuzhen Wang686f6442017-06-20 16:16:04 -0700595 __FUNCTION__, camera3_stream::max_buffers);
596 nsecs_t waitStart = systemTime(SYSTEM_TIME_MONOTONIC);
Zhijun He6adc9cc2014-04-15 14:09:55 -0700597 res = mOutputBufferReturnedSignal.waitRelative(mLock, kWaitForBufferDuration);
Shuzhen Wang686f6442017-06-20 16:16:04 -0700598 nsecs_t waitEnd = systemTime(SYSTEM_TIME_MONOTONIC);
599 mBufferLimitLatency.add(waitStart, waitEnd);
Zhijun He6adc9cc2014-04-15 14:09:55 -0700600 if (res != OK) {
601 if (res == TIMED_OUT) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -0700602 ALOGE("%s: wait for output buffer return timed out after %lldms (max_buffers %d)",
603 __FUNCTION__, kWaitForBufferDuration / 1000000LL,
604 camera3_stream::max_buffers);
Zhijun He6adc9cc2014-04-15 14:09:55 -0700605 }
606 return res;
607 }
608 }
609
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800610 res = getBufferLocked(buffer, surface_ids);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700611 if (res == OK) {
612 fireBufferListenersLocked(*buffer, /*acquired*/true, /*output*/true);
Chien-Yu Chene02e9322016-04-11 16:59:33 -0700613 if (buffer->buffer) {
Emilian Peev889234d2017-07-18 18:21:26 -0700614 Mutex::Autolock l(mOutstandingBuffersLock);
Chien-Yu Chene02e9322016-04-11 16:59:33 -0700615 mOutstandingBuffers.push_back(*buffer->buffer);
616 }
Igor Murashkin2fba5842013-04-22 14:03:54 -0700617 }
618
619 return res;
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800620}
621
Emilian Peev889234d2017-07-18 18:21:26 -0700622bool Camera3Stream::isOutstandingBuffer(const camera3_stream_buffer &buffer) const{
Chien-Yu Chene02e9322016-04-11 16:59:33 -0700623 if (buffer.buffer == nullptr) {
624 return false;
625 }
626
Emilian Peev889234d2017-07-18 18:21:26 -0700627 Mutex::Autolock l(mOutstandingBuffersLock);
628
Chien-Yu Chene02e9322016-04-11 16:59:33 -0700629 for (auto b : mOutstandingBuffers) {
630 if (b == *buffer.buffer) {
631 return true;
632 }
633 }
634 return false;
635}
636
637void Camera3Stream::removeOutstandingBuffer(const camera3_stream_buffer &buffer) {
638 if (buffer.buffer == nullptr) {
639 return;
640 }
641
Emilian Peev889234d2017-07-18 18:21:26 -0700642 Mutex::Autolock l(mOutstandingBuffersLock);
643
Chien-Yu Chene02e9322016-04-11 16:59:33 -0700644 for (auto b = mOutstandingBuffers.begin(); b != mOutstandingBuffers.end(); b++) {
645 if (*b == *buffer.buffer) {
646 mOutstandingBuffers.erase(b);
647 return;
648 }
649 }
650}
651
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800652status_t Camera3Stream::returnBuffer(const camera3_stream_buffer &buffer,
Shuzhen Wang26abaf42018-08-28 15:41:20 -0700653 nsecs_t timestamp, bool timestampIncreasing) {
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800654 ATRACE_CALL();
655 Mutex::Autolock l(mLock);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700656
Chien-Yu Chene02e9322016-04-11 16:59:33 -0700657 // Check if this buffer is outstanding.
658 if (!isOutstandingBuffer(buffer)) {
659 ALOGE("%s: Stream %d: Returning an unknown buffer.", __FUNCTION__, mId);
660 return BAD_VALUE;
661 }
662
Shuzhen Wang1c484a62017-07-14 15:14:19 -0700663 removeOutstandingBuffer(buffer);
664
Shuzhen Wang26abaf42018-08-28 15:41:20 -0700665 if (timestampIncreasing && timestamp != 0 && timestamp <= mLastTimestamp) {
666 ALOGE("%s: Stream %d: timestamp %" PRId64 " is not increasing. Prev timestamp %" PRId64,
667 __FUNCTION__, mId, timestamp, mLastTimestamp);
668 return BAD_VALUE;
669 }
670 mLastTimestamp = timestamp;
671
Igor Murashkin13d315e2014-04-03 18:09:04 -0700672 /**
673 * TODO: Check that the state is valid first.
674 *
675 * <HAL3.2 IN_CONFIG and IN_RECONFIG in addition to CONFIGURED.
676 * >= HAL3.2 CONFIGURED only
677 *
678 * Do this for getBuffer as well.
679 */
Igor Murashkin2fba5842013-04-22 14:03:54 -0700680 status_t res = returnBufferLocked(buffer, timestamp);
681 if (res == OK) {
682 fireBufferListenersLocked(buffer, /*acquired*/false, /*output*/true);
683 }
684
Chien-Yu Chenb83c1fe2015-09-10 16:15:21 -0700685 // Even if returning the buffer failed, we still want to signal whoever is waiting for the
686 // buffer to be returned.
687 mOutputBufferReturnedSignal.signal();
688
Igor Murashkin2fba5842013-04-22 14:03:54 -0700689 return res;
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800690}
691
Eino-Ville Talvalaba435252017-06-21 16:07:25 -0700692status_t Camera3Stream::getInputBuffer(camera3_stream_buffer *buffer, bool respectHalLimit) {
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700693 ATRACE_CALL();
694 Mutex::Autolock l(mLock);
Zhijun He6adc9cc2014-04-15 14:09:55 -0700695 status_t res = OK;
Igor Murashkin2fba5842013-04-22 14:03:54 -0700696
Zhijun He6adc9cc2014-04-15 14:09:55 -0700697 // This function should be only called when the stream is configured already.
698 if (mState != STATE_CONFIGURED) {
699 ALOGE("%s: Stream %d: Can't get input buffers if stream is not in CONFIGURED state %d",
700 __FUNCTION__, mId, mState);
701 return INVALID_OPERATION;
702 }
703
704 // Wait for new buffer returned back if we are running into the limit.
Eino-Ville Talvalaba435252017-06-21 16:07:25 -0700705 if (getHandoutInputBufferCountLocked() == camera3_stream::max_buffers && respectHalLimit) {
Zhijun He6adc9cc2014-04-15 14:09:55 -0700706 ALOGV("%s: Already dequeued max input buffers (%d), wait for next returned one.",
707 __FUNCTION__, camera3_stream::max_buffers);
708 res = mInputBufferReturnedSignal.waitRelative(mLock, kWaitForBufferDuration);
709 if (res != OK) {
710 if (res == TIMED_OUT) {
711 ALOGE("%s: wait for input buffer return timed out after %lldms", __FUNCTION__,
712 kWaitForBufferDuration / 1000000LL);
713 }
714 return res;
715 }
716 }
717
718 res = getInputBufferLocked(buffer);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700719 if (res == OK) {
720 fireBufferListenersLocked(*buffer, /*acquired*/true, /*output*/false);
Chien-Yu Chene02e9322016-04-11 16:59:33 -0700721 if (buffer->buffer) {
Emilian Peev889234d2017-07-18 18:21:26 -0700722 Mutex::Autolock l(mOutstandingBuffersLock);
Chien-Yu Chene02e9322016-04-11 16:59:33 -0700723 mOutstandingBuffers.push_back(*buffer->buffer);
724 }
Igor Murashkin2fba5842013-04-22 14:03:54 -0700725 }
726
727 return res;
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700728}
729
730status_t Camera3Stream::returnInputBuffer(const camera3_stream_buffer &buffer) {
731 ATRACE_CALL();
732 Mutex::Autolock l(mLock);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700733
Chien-Yu Chene02e9322016-04-11 16:59:33 -0700734 // Check if this buffer is outstanding.
735 if (!isOutstandingBuffer(buffer)) {
736 ALOGE("%s: Stream %d: Returning an unknown buffer.", __FUNCTION__, mId);
737 return BAD_VALUE;
738 }
739
Shuzhen Wang1c484a62017-07-14 15:14:19 -0700740 removeOutstandingBuffer(buffer);
741
Igor Murashkin2fba5842013-04-22 14:03:54 -0700742 status_t res = returnInputBufferLocked(buffer);
743 if (res == OK) {
744 fireBufferListenersLocked(buffer, /*acquired*/false, /*output*/false);
Zhijun He6adc9cc2014-04-15 14:09:55 -0700745 mInputBufferReturnedSignal.signal();
Igor Murashkin2fba5842013-04-22 14:03:54 -0700746 }
Chien-Yu Chene02e9322016-04-11 16:59:33 -0700747
Igor Murashkin2fba5842013-04-22 14:03:54 -0700748 return res;
749}
750
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700751status_t Camera3Stream::getInputBufferProducer(sp<IGraphicBufferProducer> *producer) {
752 ATRACE_CALL();
753 Mutex::Autolock l(mLock);
754
755 return getInputBufferProducerLocked(producer);
756}
757
Igor Murashkin2fba5842013-04-22 14:03:54 -0700758void Camera3Stream::fireBufferListenersLocked(
Shuzhen Wangb0fdc1e2016-03-20 23:21:39 -0700759 const camera3_stream_buffer& buffer, bool acquired, bool output) {
Igor Murashkin2fba5842013-04-22 14:03:54 -0700760 List<wp<Camera3StreamBufferListener> >::iterator it, end;
761
762 // TODO: finish implementing
763
764 Camera3StreamBufferListener::BufferInfo info =
765 Camera3StreamBufferListener::BufferInfo();
766 info.mOutput = output;
Shuzhen Wangb0fdc1e2016-03-20 23:21:39 -0700767 info.mError = (buffer.status == CAMERA3_BUFFER_STATUS_ERROR);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700768 // TODO: rest of fields
769
770 for (it = mBufferListenerList.begin(), end = mBufferListenerList.end();
771 it != end;
772 ++it) {
773
774 sp<Camera3StreamBufferListener> listener = it->promote();
775 if (listener != 0) {
776 if (acquired) {
777 listener->onBufferAcquired(info);
778 } else {
779 listener->onBufferReleased(info);
780 }
781 }
782 }
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700783}
784
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800785bool Camera3Stream::hasOutstandingBuffers() const {
786 ATRACE_CALL();
787 Mutex::Autolock l(mLock);
788 return hasOutstandingBuffersLocked();
789}
790
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700791status_t Camera3Stream::setStatusTracker(sp<StatusTracker> statusTracker) {
792 Mutex::Autolock l(mLock);
793 sp<StatusTracker> oldTracker = mStatusTracker.promote();
794 if (oldTracker != 0 && mStatusId != StatusTracker::NO_STATUS_ID) {
795 oldTracker->removeComponent(mStatusId);
796 }
797 mStatusId = StatusTracker::NO_STATUS_ID;
798 mStatusTracker = statusTracker;
799
800 return OK;
801}
802
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800803status_t Camera3Stream::disconnect() {
804 ATRACE_CALL();
805 Mutex::Autolock l(mLock);
Igor Murashkine2172be2013-05-28 15:31:39 -0700806 ALOGV("%s: Stream %d: Disconnecting...", __FUNCTION__, mId);
807 status_t res = disconnectLocked();
808
Shuzhen Wang686f6442017-06-20 16:16:04 -0700809 mBufferLimitLatency.log("Stream %d latency histogram for wait on max_buffers", mId);
810 mBufferLimitLatency.reset();
811
Igor Murashkine2172be2013-05-28 15:31:39 -0700812 if (res == -ENOTCONN) {
813 // "Already disconnected" -- not an error
814 return OK;
815 } else {
816 return res;
817 }
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800818}
819
Shuzhen Wang686f6442017-06-20 16:16:04 -0700820void Camera3Stream::dump(int fd, const Vector<String16> &args) const
821{
822 (void)args;
823 mBufferLimitLatency.dump(fd,
824 " Latency histogram for wait on max_buffers");
825}
826
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800827status_t Camera3Stream::getBufferLocked(camera3_stream_buffer *,
828 const std::vector<size_t>&) {
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700829 ALOGE("%s: This type of stream does not support output", __FUNCTION__);
830 return INVALID_OPERATION;
831}
832status_t Camera3Stream::returnBufferLocked(const camera3_stream_buffer &,
833 nsecs_t) {
834 ALOGE("%s: This type of stream does not support output", __FUNCTION__);
835 return INVALID_OPERATION;
836}
837status_t Camera3Stream::getInputBufferLocked(camera3_stream_buffer *) {
838 ALOGE("%s: This type of stream does not support input", __FUNCTION__);
839 return INVALID_OPERATION;
840}
841status_t Camera3Stream::returnInputBufferLocked(
842 const camera3_stream_buffer &) {
843 ALOGE("%s: This type of stream does not support input", __FUNCTION__);
844 return INVALID_OPERATION;
845}
Eino-Ville Talvala02bf0322016-02-18 12:41:10 -0800846status_t Camera3Stream::getInputBufferProducerLocked(sp<IGraphicBufferProducer>*) {
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700847 ALOGE("%s: This type of stream does not support input", __FUNCTION__);
848 return INVALID_OPERATION;
849}
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700850
Igor Murashkin2fba5842013-04-22 14:03:54 -0700851void Camera3Stream::addBufferListener(
852 wp<Camera3StreamBufferListener> listener) {
853 Mutex::Autolock l(mLock);
Zhijun Hef0d962a2014-06-30 10:24:11 -0700854
855 List<wp<Camera3StreamBufferListener> >::iterator it, end;
856 for (it = mBufferListenerList.begin(), end = mBufferListenerList.end();
857 it != end;
858 ) {
859 if (*it == listener) {
860 ALOGE("%s: Try to add the same listener twice, ignoring...", __FUNCTION__);
861 return;
862 }
863 it++;
864 }
865
Igor Murashkin2fba5842013-04-22 14:03:54 -0700866 mBufferListenerList.push_back(listener);
867}
868
869void Camera3Stream::removeBufferListener(
870 const sp<Camera3StreamBufferListener>& listener) {
871 Mutex::Autolock l(mLock);
872
873 bool erased = true;
874 List<wp<Camera3StreamBufferListener> >::iterator it, end;
875 for (it = mBufferListenerList.begin(), end = mBufferListenerList.end();
876 it != end;
877 ) {
878
879 if (*it == listener) {
880 it = mBufferListenerList.erase(it);
881 erased = true;
882 } else {
883 ++it;
884 }
885 }
886
887 if (!erased) {
888 ALOGW("%s: Could not find listener to remove, already removed",
889 __FUNCTION__);
890 }
891}
892
Yin-Chia Yehbe83fa72017-03-30 13:35:36 -0700893void Camera3Stream::setBufferFreedListener(
Yin-Chia Yehdb1e8642017-07-14 15:19:30 -0700894 wp<Camera3StreamBufferFreedListener> listener) {
Yin-Chia Yehbe83fa72017-03-30 13:35:36 -0700895 Mutex::Autolock l(mLock);
896 // Only allow set listener during stream configuration because stream is guaranteed to be IDLE
897 // at this state, so setBufferFreedListener won't collide with onBufferFreed callbacks
898 if (mState != STATE_IN_CONFIG && mState != STATE_IN_RECONFIG) {
899 ALOGE("%s: listener must be set during stream configuration!",__FUNCTION__);
900 return;
901 }
902 mBufferFreedListener = listener;
903}
904
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800905}; // namespace camera3
906
907}; // namespace android