blob: c18620833bb92ab6110eed08a9e1eef289fd7690 [file] [log] [blame]
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -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-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,
Zhijun He125684a2015-12-26 15:07:30 -080050 android_dataspace dataSpace, camera3_stream_rotation_t rotation, int setId) :
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -080051 camera3_stream(),
52 mId(id),
Zhijun He125684a2015-12-26 15:07:30 -080053 mSetId(setId),
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -080054 mName(String8::format("Camera3Stream[%d]", id)),
55 mMaxSize(maxSize),
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -070056 mState(STATE_CONSTRUCTED),
Ruben Brunkc78ac262015-08-13 17:58:46 -070057 mStatusId(StatusTracker::NO_STATUS_ID),
Zhijun He5d677d12016-05-29 16:52:39 -070058 mStreamUnpreparable(true),
Emilian Peev050f5dc2017-05-18 14:43:56 +010059 mUsage(0),
Eino-Ville Talvala02bf0322016-02-18 12:41:10 -080060 mOldUsage(0),
61 mOldMaxBuffers(0),
Zhijun He125684a2015-12-26 15:07:30 -080062 mPrepared(false),
63 mPreparedBufferIdx(0),
Shuzhen Wang686f6442017-06-20 16:16:04 -070064 mLastMaxCount(Camera3StreamInterface::ALLOCATE_PIPELINE_MAX),
Emilian Peev710c1422017-08-30 11:19:38 +010065 mBufferLimitLatency(kBufferLimitLatencyBinSize),
66 mFormatOverridden(false),
67 mOriginalFormat(-1) {
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -080068
69 camera3_stream::stream_type = type;
70 camera3_stream::width = width;
71 camera3_stream::height = height;
72 camera3_stream::format = format;
Eino-Ville Talvala3d82c0d2015-02-23 15:19:19 -080073 camera3_stream::data_space = dataSpace;
Yin-Chia Yehb97babb2015-03-12 13:42:44 -070074 camera3_stream::rotation = rotation;
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -080075 camera3_stream::max_buffers = 0;
76 camera3_stream::priv = NULL;
77
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -080078 if ((format == HAL_PIXEL_FORMAT_BLOB || format == HAL_PIXEL_FORMAT_RAW_OPAQUE) &&
79 maxSize == 0) {
80 ALOGE("%s: BLOB or RAW_OPAQUE format with size == 0", __FUNCTION__);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -080081 mState = STATE_ERROR;
82 }
83}
84
85int Camera3Stream::getId() const {
86 return mId;
87}
88
Zhijun He125684a2015-12-26 15:07:30 -080089int Camera3Stream::getStreamSetId() const {
90 return mSetId;
91}
92
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -080093uint32_t Camera3Stream::getWidth() const {
94 return camera3_stream::width;
95}
96
97uint32_t Camera3Stream::getHeight() const {
98 return camera3_stream::height;
99}
100
101int Camera3Stream::getFormat() const {
102 return camera3_stream::format;
103}
104
Eino-Ville Talvala3d82c0d2015-02-23 15:19:19 -0800105android_dataspace Camera3Stream::getDataSpace() const {
106 return camera3_stream::data_space;
107}
108
Emilian Peev050f5dc2017-05-18 14:43:56 +0100109uint64_t Camera3Stream::getUsage() const {
110 return mUsage;
111}
112
113void Camera3Stream::setUsage(uint64_t usage) {
114 mUsage = usage;
115}
116
Emilian Peev710c1422017-08-30 11:19:38 +0100117void Camera3Stream::setFormatOverride(bool formatOverridden) {
118 mFormatOverridden = formatOverridden;
119}
120
121bool Camera3Stream::isFormatOverridden() {
122 return mFormatOverridden;
123}
124
125void Camera3Stream::setOriginalFormat(int originalFormat) {
126 mOriginalFormat = originalFormat;
127}
128
129int Camera3Stream::getOriginalFormat() {
130 return mOriginalFormat;
131}
132
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800133camera3_stream* Camera3Stream::startConfiguration() {
Eino-Ville Talvalab2f5b192013-07-30 14:36:03 -0700134 ATRACE_CALL();
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800135 Mutex::Autolock l(mLock);
Eino-Ville Talvalab2f5b192013-07-30 14:36:03 -0700136 status_t res;
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800137
138 switch (mState) {
139 case STATE_ERROR:
140 ALOGE("%s: In error state", __FUNCTION__);
141 return NULL;
142 case STATE_CONSTRUCTED:
143 // OK
144 break;
145 case STATE_IN_CONFIG:
146 case STATE_IN_RECONFIG:
147 // Can start config again with no trouble; but don't redo
Eino-Ville Talvala02bf0322016-02-18 12:41:10 -0800148 // mOldUsage/mOldMaxBuffers
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800149 return this;
150 case STATE_CONFIGURED:
Chien-Yu Chen90746f42015-04-15 13:50:13 -0700151 if (hasOutstandingBuffersLocked()) {
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800152 ALOGE("%s: Cannot configure stream; has outstanding buffers",
153 __FUNCTION__);
154 return NULL;
155 }
156 break;
157 default:
158 ALOGE("%s: Unknown state %d", __FUNCTION__, mState);
159 return NULL;
160 }
161
Emilian Peev050f5dc2017-05-18 14:43:56 +0100162 mOldUsage = mUsage;
Eino-Ville Talvala02bf0322016-02-18 12:41:10 -0800163 mOldMaxBuffers = camera3_stream::max_buffers;
Eino-Ville Talvalab2f5b192013-07-30 14:36:03 -0700164
Emilian Peev050f5dc2017-05-18 14:43:56 +0100165 res = getEndpointUsage(&mUsage);
Eino-Ville Talvalab2f5b192013-07-30 14:36:03 -0700166 if (res != OK) {
167 ALOGE("%s: Cannot query consumer endpoint usage!",
168 __FUNCTION__);
169 return NULL;
170 }
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800171
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700172 // Stop tracking if currently doing so
173 if (mStatusId != StatusTracker::NO_STATUS_ID) {
174 sp<StatusTracker> statusTracker = mStatusTracker.promote();
175 if (statusTracker != 0) {
176 statusTracker->removeComponent(mStatusId);
177 }
178 mStatusId = StatusTracker::NO_STATUS_ID;
179 }
180
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800181 if (mState == STATE_CONSTRUCTED) {
182 mState = STATE_IN_CONFIG;
183 } else { // mState == STATE_CONFIGURED
Igor Murashkin13d315e2014-04-03 18:09:04 -0700184 LOG_ALWAYS_FATAL_IF(mState != STATE_CONFIGURED, "Invalid state: 0x%x", mState);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800185 mState = STATE_IN_RECONFIG;
186 }
187
188 return this;
189}
190
191bool Camera3Stream::isConfiguring() const {
192 Mutex::Autolock l(mLock);
193 return (mState == STATE_IN_CONFIG) || (mState == STATE_IN_RECONFIG);
194}
195
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800196status_t Camera3Stream::finishConfiguration() {
Eino-Ville Talvalab2f5b192013-07-30 14:36:03 -0700197 ATRACE_CALL();
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800198 Mutex::Autolock l(mLock);
199 switch (mState) {
200 case STATE_ERROR:
201 ALOGE("%s: In error state", __FUNCTION__);
202 return INVALID_OPERATION;
203 case STATE_IN_CONFIG:
204 case STATE_IN_RECONFIG:
205 // OK
206 break;
207 case STATE_CONSTRUCTED:
208 case STATE_CONFIGURED:
209 ALOGE("%s: Cannot finish configuration that hasn't been started",
210 __FUNCTION__);
211 return INVALID_OPERATION;
212 default:
213 ALOGE("%s: Unknown state", __FUNCTION__);
214 return INVALID_OPERATION;
215 }
216
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700217 // Register for idle tracking
218 sp<StatusTracker> statusTracker = mStatusTracker.promote();
219 if (statusTracker != 0) {
220 mStatusId = statusTracker->addComponent();
221 }
222
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800223 // Check if the stream configuration is unchanged, and skip reallocation if
224 // so. As documented in hardware/camera3.h:configure_streams().
225 if (mState == STATE_IN_RECONFIG &&
Emilian Peev050f5dc2017-05-18 14:43:56 +0100226 mOldUsage == mUsage &&
Eino-Ville Talvala02bf0322016-02-18 12:41:10 -0800227 mOldMaxBuffers == camera3_stream::max_buffers) {
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800228 mState = STATE_CONFIGURED;
229 return OK;
230 }
231
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700232 // Reset prepared state, since buffer config has changed, and existing
233 // allocations are no longer valid
234 mPrepared = false;
235 mStreamUnpreparable = false;
236
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800237 status_t res;
238 res = configureQueueLocked();
239 if (res != OK) {
240 ALOGE("%s: Unable to configure stream %d queue: %s (%d)",
241 __FUNCTION__, mId, strerror(-res), res);
242 mState = STATE_ERROR;
243 return res;
244 }
245
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800246 mState = STATE_CONFIGURED;
247
248 return res;
249}
250
Eino-Ville Talvala17543512014-08-06 14:32:02 -0700251status_t Camera3Stream::cancelConfiguration() {
252 ATRACE_CALL();
253 Mutex::Autolock l(mLock);
254 switch (mState) {
255 case STATE_ERROR:
256 ALOGE("%s: In error state", __FUNCTION__);
257 return INVALID_OPERATION;
258 case STATE_IN_CONFIG:
259 case STATE_IN_RECONFIG:
260 // OK
261 break;
262 case STATE_CONSTRUCTED:
263 case STATE_CONFIGURED:
264 ALOGE("%s: Cannot cancel configuration that hasn't been started",
265 __FUNCTION__);
266 return INVALID_OPERATION;
267 default:
268 ALOGE("%s: Unknown state", __FUNCTION__);
269 return INVALID_OPERATION;
270 }
271
Emilian Peev050f5dc2017-05-18 14:43:56 +0100272 mUsage = mOldUsage;
Eino-Ville Talvala02bf0322016-02-18 12:41:10 -0800273 camera3_stream::max_buffers = mOldMaxBuffers;
Eino-Ville Talvala17543512014-08-06 14:32:02 -0700274
Yin-Chia Yeh3ea3fcd2014-09-05 14:14:44 -0700275 mState = (mState == STATE_IN_RECONFIG) ? STATE_CONFIGURED : STATE_CONSTRUCTED;
Eino-Ville Talvala17543512014-08-06 14:32:02 -0700276 return OK;
277}
278
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700279bool Camera3Stream::isUnpreparable() {
280 ATRACE_CALL();
281
282 Mutex::Autolock l(mLock);
283 return mStreamUnpreparable;
284}
285
Ruben Brunkc78ac262015-08-13 17:58:46 -0700286status_t Camera3Stream::startPrepare(int maxCount) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700287 ATRACE_CALL();
288
289 Mutex::Autolock l(mLock);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700290
Ruben Brunkc78ac262015-08-13 17:58:46 -0700291 if (maxCount < 0) {
292 ALOGE("%s: Stream %d: Can't prepare stream if max buffer count (%d) is < 0",
293 __FUNCTION__, mId, maxCount);
294 return BAD_VALUE;
295 }
296
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700297 // This function should be only called when the stream is configured already.
298 if (mState != STATE_CONFIGURED) {
299 ALOGE("%s: Stream %d: Can't prepare stream if stream is not in CONFIGURED "
300 "state %d", __FUNCTION__, mId, mState);
301 return INVALID_OPERATION;
302 }
303
304 // This function can't be called if the stream has already received filled
305 // buffers
306 if (mStreamUnpreparable) {
307 ALOGE("%s: Stream %d: Can't prepare stream that's already in use",
308 __FUNCTION__, mId);
309 return INVALID_OPERATION;
310 }
311
312 if (getHandoutOutputBufferCountLocked() > 0) {
313 ALOGE("%s: Stream %d: Can't prepare stream that has outstanding buffers",
314 __FUNCTION__, mId);
315 return INVALID_OPERATION;
316 }
317
Ruben Brunkc78ac262015-08-13 17:58:46 -0700318
319
320 size_t pipelineMax = getBufferCountLocked();
321 size_t clampedCount = (pipelineMax < static_cast<size_t>(maxCount)) ?
322 pipelineMax : static_cast<size_t>(maxCount);
323 size_t bufferCount = (maxCount == Camera3StreamInterface::ALLOCATE_PIPELINE_MAX) ?
324 pipelineMax : clampedCount;
325
326 mPrepared = bufferCount <= mLastMaxCount;
327
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700328 if (mPrepared) return OK;
329
Ruben Brunkc78ac262015-08-13 17:58:46 -0700330 mLastMaxCount = bufferCount;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700331
332 mPreparedBuffers.insertAt(camera3_stream_buffer_t(), /*index*/0, bufferCount);
333 mPreparedBufferIdx = 0;
334
335 mState = STATE_PREPARING;
336
337 return NOT_ENOUGH_DATA;
338}
339
340bool Camera3Stream::isPreparing() const {
341 Mutex::Autolock l(mLock);
342 return mState == STATE_PREPARING;
343}
344
Chien-Yu Chene8c535e2016-04-14 12:18:26 -0700345bool Camera3Stream::isAbandoned() const {
346 Mutex::Autolock l(mLock);
347 return mState == STATE_ABANDONED;
348}
349
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700350status_t Camera3Stream::prepareNextBuffer() {
351 ATRACE_CALL();
352
353 Mutex::Autolock l(mLock);
354 status_t res = OK;
355
356 // This function should be only called when the stream is preparing
357 if (mState != STATE_PREPARING) {
358 ALOGE("%s: Stream %d: Can't prepare buffer if stream is not in PREPARING "
359 "state %d", __FUNCTION__, mId, mState);
360 return INVALID_OPERATION;
361 }
362
363 // Get next buffer - this may allocate, and take a while for large buffers
364 res = getBufferLocked( &mPreparedBuffers.editItemAt(mPreparedBufferIdx) );
365 if (res != OK) {
Eino-Ville Talvala02bf0322016-02-18 12:41:10 -0800366 ALOGE("%s: Stream %d: Unable to allocate buffer %zu during preparation",
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700367 __FUNCTION__, mId, mPreparedBufferIdx);
368 return NO_INIT;
369 }
370
371 mPreparedBufferIdx++;
372
373 // Check if we still have buffers left to allocate
374 if (mPreparedBufferIdx < mPreparedBuffers.size()) {
375 return NOT_ENOUGH_DATA;
376 }
377
378 // Done with prepare - mark stream as such, and return all buffers
379 // via cancelPrepare
380 mPrepared = true;
381
382 return cancelPrepareLocked();
383}
384
385status_t Camera3Stream::cancelPrepare() {
386 ATRACE_CALL();
387
388 Mutex::Autolock l(mLock);
389
390 return cancelPrepareLocked();
391}
392
393status_t Camera3Stream::cancelPrepareLocked() {
394 status_t res = OK;
395
396 // This function should be only called when the stream is mid-preparing.
397 if (mState != STATE_PREPARING) {
398 ALOGE("%s: Stream %d: Can't cancel prepare stream if stream is not in "
399 "PREPARING state %d", __FUNCTION__, mId, mState);
400 return INVALID_OPERATION;
401 }
402
403 // Return all valid buffers to stream, in ERROR state to indicate
404 // they weren't filled.
405 for (size_t i = 0; i < mPreparedBufferIdx; i++) {
406 mPreparedBuffers.editItemAt(i).release_fence = -1;
407 mPreparedBuffers.editItemAt(i).status = CAMERA3_BUFFER_STATUS_ERROR;
408 returnBufferLocked(mPreparedBuffers[i], 0);
409 }
410 mPreparedBuffers.clear();
411 mPreparedBufferIdx = 0;
412
413 mState = STATE_CONFIGURED;
414
415 return res;
416}
417
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -0700418status_t Camera3Stream::tearDown() {
419 ATRACE_CALL();
420 Mutex::Autolock l(mLock);
421
422 status_t res = OK;
423
424 // This function should be only called when the stream is configured.
425 if (mState != STATE_CONFIGURED) {
426 ALOGE("%s: Stream %d: Can't tear down stream if stream is not in "
427 "CONFIGURED state %d", __FUNCTION__, mId, mState);
428 return INVALID_OPERATION;
429 }
430
431 // If any buffers have been handed to the HAL, the stream cannot be torn down.
432 if (getHandoutOutputBufferCountLocked() > 0) {
433 ALOGE("%s: Stream %d: Can't tear down a stream that has outstanding buffers",
434 __FUNCTION__, mId);
435 return INVALID_OPERATION;
436 }
437
438 // Free buffers by disconnecting and then reconnecting to the buffer queue
439 // Only unused buffers will be dropped immediately; buffers that have been filled
440 // and are waiting to be acquired by the consumer and buffers that are currently
441 // acquired will be freed once they are released by the consumer.
442
443 res = disconnectLocked();
444 if (res != OK) {
445 if (res == -ENOTCONN) {
446 // queue has been disconnected, nothing left to do, so exit with success
447 return OK;
448 }
449 ALOGE("%s: Stream %d: Unable to disconnect to tear down buffers: %s (%d)",
450 __FUNCTION__, mId, strerror(-res), res);
451 return res;
452 }
453
454 mState = STATE_IN_CONFIG;
455
456 res = configureQueueLocked();
457 if (res != OK) {
458 ALOGE("%s: Unable to configure stream %d queue: %s (%d)",
459 __FUNCTION__, mId, strerror(-res), res);
460 mState = STATE_ERROR;
461 return res;
462 }
463
464 // Reset prepared state, since we've reconnected to the queue and can prepare again.
465 mPrepared = false;
466 mStreamUnpreparable = false;
467
468 mState = STATE_CONFIGURED;
469
470 return OK;
471}
472
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800473status_t Camera3Stream::getBuffer(camera3_stream_buffer *buffer,
474 const std::vector<size_t>& surface_ids) {
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800475 ATRACE_CALL();
476 Mutex::Autolock l(mLock);
Zhijun He6adc9cc2014-04-15 14:09:55 -0700477 status_t res = OK;
Igor Murashkin2fba5842013-04-22 14:03:54 -0700478
Zhijun He6adc9cc2014-04-15 14:09:55 -0700479 // This function should be only called when the stream is configured already.
480 if (mState != STATE_CONFIGURED) {
481 ALOGE("%s: Stream %d: Can't get buffers if stream is not in CONFIGURED state %d",
482 __FUNCTION__, mId, mState);
483 return INVALID_OPERATION;
484 }
485
486 // Wait for new buffer returned back if we are running into the limit.
487 if (getHandoutOutputBufferCountLocked() == camera3_stream::max_buffers) {
488 ALOGV("%s: Already dequeued max output buffers (%d), wait for next returned one.",
Shuzhen Wang686f6442017-06-20 16:16:04 -0700489 __FUNCTION__, camera3_stream::max_buffers);
490 nsecs_t waitStart = systemTime(SYSTEM_TIME_MONOTONIC);
Zhijun He6adc9cc2014-04-15 14:09:55 -0700491 res = mOutputBufferReturnedSignal.waitRelative(mLock, kWaitForBufferDuration);
Shuzhen Wang686f6442017-06-20 16:16:04 -0700492 nsecs_t waitEnd = systemTime(SYSTEM_TIME_MONOTONIC);
493 mBufferLimitLatency.add(waitStart, waitEnd);
Zhijun He6adc9cc2014-04-15 14:09:55 -0700494 if (res != OK) {
495 if (res == TIMED_OUT) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -0700496 ALOGE("%s: wait for output buffer return timed out after %lldms (max_buffers %d)",
497 __FUNCTION__, kWaitForBufferDuration / 1000000LL,
498 camera3_stream::max_buffers);
Zhijun He6adc9cc2014-04-15 14:09:55 -0700499 }
500 return res;
501 }
502 }
503
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800504 res = getBufferLocked(buffer, surface_ids);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700505 if (res == OK) {
506 fireBufferListenersLocked(*buffer, /*acquired*/true, /*output*/true);
Chien-Yu Chene02e9322016-04-11 16:59:33 -0700507 if (buffer->buffer) {
Emilian Peev889234d2017-07-18 18:21:26 -0700508 Mutex::Autolock l(mOutstandingBuffersLock);
Chien-Yu Chene02e9322016-04-11 16:59:33 -0700509 mOutstandingBuffers.push_back(*buffer->buffer);
510 }
Igor Murashkin2fba5842013-04-22 14:03:54 -0700511 }
512
513 return res;
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800514}
515
Emilian Peev889234d2017-07-18 18:21:26 -0700516bool Camera3Stream::isOutstandingBuffer(const camera3_stream_buffer &buffer) const{
Chien-Yu Chene02e9322016-04-11 16:59:33 -0700517 if (buffer.buffer == nullptr) {
518 return false;
519 }
520
Emilian Peev889234d2017-07-18 18:21:26 -0700521 Mutex::Autolock l(mOutstandingBuffersLock);
522
Chien-Yu Chene02e9322016-04-11 16:59:33 -0700523 for (auto b : mOutstandingBuffers) {
524 if (b == *buffer.buffer) {
525 return true;
526 }
527 }
528 return false;
529}
530
531void Camera3Stream::removeOutstandingBuffer(const camera3_stream_buffer &buffer) {
532 if (buffer.buffer == nullptr) {
533 return;
534 }
535
Emilian Peev889234d2017-07-18 18:21:26 -0700536 Mutex::Autolock l(mOutstandingBuffersLock);
537
Chien-Yu Chene02e9322016-04-11 16:59:33 -0700538 for (auto b = mOutstandingBuffers.begin(); b != mOutstandingBuffers.end(); b++) {
539 if (*b == *buffer.buffer) {
540 mOutstandingBuffers.erase(b);
541 return;
542 }
543 }
544}
545
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800546status_t Camera3Stream::returnBuffer(const camera3_stream_buffer &buffer,
547 nsecs_t timestamp) {
548 ATRACE_CALL();
549 Mutex::Autolock l(mLock);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700550
Chien-Yu Chene02e9322016-04-11 16:59:33 -0700551 // Check if this buffer is outstanding.
552 if (!isOutstandingBuffer(buffer)) {
553 ALOGE("%s: Stream %d: Returning an unknown buffer.", __FUNCTION__, mId);
554 return BAD_VALUE;
555 }
556
Shuzhen Wang1c484a62017-07-14 15:14:19 -0700557 removeOutstandingBuffer(buffer);
558
Igor Murashkin13d315e2014-04-03 18:09:04 -0700559 /**
560 * TODO: Check that the state is valid first.
561 *
562 * <HAL3.2 IN_CONFIG and IN_RECONFIG in addition to CONFIGURED.
563 * >= HAL3.2 CONFIGURED only
564 *
565 * Do this for getBuffer as well.
566 */
Igor Murashkin2fba5842013-04-22 14:03:54 -0700567 status_t res = returnBufferLocked(buffer, timestamp);
568 if (res == OK) {
569 fireBufferListenersLocked(buffer, /*acquired*/false, /*output*/true);
570 }
571
Chien-Yu Chenb83c1fe2015-09-10 16:15:21 -0700572 // Even if returning the buffer failed, we still want to signal whoever is waiting for the
573 // buffer to be returned.
574 mOutputBufferReturnedSignal.signal();
575
Igor Murashkin2fba5842013-04-22 14:03:54 -0700576 return res;
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800577}
578
Eino-Ville Talvalaba435252017-06-21 16:07:25 -0700579status_t Camera3Stream::getInputBuffer(camera3_stream_buffer *buffer, bool respectHalLimit) {
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700580 ATRACE_CALL();
581 Mutex::Autolock l(mLock);
Zhijun He6adc9cc2014-04-15 14:09:55 -0700582 status_t res = OK;
Igor Murashkin2fba5842013-04-22 14:03:54 -0700583
Zhijun He6adc9cc2014-04-15 14:09:55 -0700584 // This function should be only called when the stream is configured already.
585 if (mState != STATE_CONFIGURED) {
586 ALOGE("%s: Stream %d: Can't get input buffers if stream is not in CONFIGURED state %d",
587 __FUNCTION__, mId, mState);
588 return INVALID_OPERATION;
589 }
590
591 // Wait for new buffer returned back if we are running into the limit.
Eino-Ville Talvalaba435252017-06-21 16:07:25 -0700592 if (getHandoutInputBufferCountLocked() == camera3_stream::max_buffers && respectHalLimit) {
Zhijun He6adc9cc2014-04-15 14:09:55 -0700593 ALOGV("%s: Already dequeued max input buffers (%d), wait for next returned one.",
594 __FUNCTION__, camera3_stream::max_buffers);
595 res = mInputBufferReturnedSignal.waitRelative(mLock, kWaitForBufferDuration);
596 if (res != OK) {
597 if (res == TIMED_OUT) {
598 ALOGE("%s: wait for input buffer return timed out after %lldms", __FUNCTION__,
599 kWaitForBufferDuration / 1000000LL);
600 }
601 return res;
602 }
603 }
604
605 res = getInputBufferLocked(buffer);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700606 if (res == OK) {
607 fireBufferListenersLocked(*buffer, /*acquired*/true, /*output*/false);
Chien-Yu Chene02e9322016-04-11 16:59:33 -0700608 if (buffer->buffer) {
Emilian Peev889234d2017-07-18 18:21:26 -0700609 Mutex::Autolock l(mOutstandingBuffersLock);
Chien-Yu Chene02e9322016-04-11 16:59:33 -0700610 mOutstandingBuffers.push_back(*buffer->buffer);
611 }
Igor Murashkin2fba5842013-04-22 14:03:54 -0700612 }
613
614 return res;
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700615}
616
617status_t Camera3Stream::returnInputBuffer(const camera3_stream_buffer &buffer) {
618 ATRACE_CALL();
619 Mutex::Autolock l(mLock);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700620
Chien-Yu Chene02e9322016-04-11 16:59:33 -0700621 // Check if this buffer is outstanding.
622 if (!isOutstandingBuffer(buffer)) {
623 ALOGE("%s: Stream %d: Returning an unknown buffer.", __FUNCTION__, mId);
624 return BAD_VALUE;
625 }
626
Shuzhen Wang1c484a62017-07-14 15:14:19 -0700627 removeOutstandingBuffer(buffer);
628
Igor Murashkin2fba5842013-04-22 14:03:54 -0700629 status_t res = returnInputBufferLocked(buffer);
630 if (res == OK) {
631 fireBufferListenersLocked(buffer, /*acquired*/false, /*output*/false);
Zhijun He6adc9cc2014-04-15 14:09:55 -0700632 mInputBufferReturnedSignal.signal();
Igor Murashkin2fba5842013-04-22 14:03:54 -0700633 }
Chien-Yu Chene02e9322016-04-11 16:59:33 -0700634
Igor Murashkin2fba5842013-04-22 14:03:54 -0700635 return res;
636}
637
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700638status_t Camera3Stream::getInputBufferProducer(sp<IGraphicBufferProducer> *producer) {
639 ATRACE_CALL();
640 Mutex::Autolock l(mLock);
641
642 return getInputBufferProducerLocked(producer);
643}
644
Igor Murashkin2fba5842013-04-22 14:03:54 -0700645void Camera3Stream::fireBufferListenersLocked(
Shuzhen Wangb0fdc1e2016-03-20 23:21:39 -0700646 const camera3_stream_buffer& buffer, bool acquired, bool output) {
Igor Murashkin2fba5842013-04-22 14:03:54 -0700647 List<wp<Camera3StreamBufferListener> >::iterator it, end;
648
649 // TODO: finish implementing
650
651 Camera3StreamBufferListener::BufferInfo info =
652 Camera3StreamBufferListener::BufferInfo();
653 info.mOutput = output;
Shuzhen Wangb0fdc1e2016-03-20 23:21:39 -0700654 info.mError = (buffer.status == CAMERA3_BUFFER_STATUS_ERROR);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700655 // TODO: rest of fields
656
657 for (it = mBufferListenerList.begin(), end = mBufferListenerList.end();
658 it != end;
659 ++it) {
660
661 sp<Camera3StreamBufferListener> listener = it->promote();
662 if (listener != 0) {
663 if (acquired) {
664 listener->onBufferAcquired(info);
665 } else {
666 listener->onBufferReleased(info);
667 }
668 }
669 }
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700670}
671
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800672bool Camera3Stream::hasOutstandingBuffers() const {
673 ATRACE_CALL();
674 Mutex::Autolock l(mLock);
675 return hasOutstandingBuffersLocked();
676}
677
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700678status_t Camera3Stream::setStatusTracker(sp<StatusTracker> statusTracker) {
679 Mutex::Autolock l(mLock);
680 sp<StatusTracker> oldTracker = mStatusTracker.promote();
681 if (oldTracker != 0 && mStatusId != StatusTracker::NO_STATUS_ID) {
682 oldTracker->removeComponent(mStatusId);
683 }
684 mStatusId = StatusTracker::NO_STATUS_ID;
685 mStatusTracker = statusTracker;
686
687 return OK;
688}
689
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800690status_t Camera3Stream::disconnect() {
691 ATRACE_CALL();
692 Mutex::Autolock l(mLock);
Igor Murashkine2172be2013-05-28 15:31:39 -0700693 ALOGV("%s: Stream %d: Disconnecting...", __FUNCTION__, mId);
694 status_t res = disconnectLocked();
695
Shuzhen Wang686f6442017-06-20 16:16:04 -0700696 mBufferLimitLatency.log("Stream %d latency histogram for wait on max_buffers", mId);
697 mBufferLimitLatency.reset();
698
Igor Murashkine2172be2013-05-28 15:31:39 -0700699 if (res == -ENOTCONN) {
700 // "Already disconnected" -- not an error
701 return OK;
702 } else {
703 return res;
704 }
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800705}
706
Shuzhen Wang686f6442017-06-20 16:16:04 -0700707void Camera3Stream::dump(int fd, const Vector<String16> &args) const
708{
709 (void)args;
710 mBufferLimitLatency.dump(fd,
711 " Latency histogram for wait on max_buffers");
712}
713
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800714status_t Camera3Stream::getBufferLocked(camera3_stream_buffer *,
715 const std::vector<size_t>&) {
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700716 ALOGE("%s: This type of stream does not support output", __FUNCTION__);
717 return INVALID_OPERATION;
718}
719status_t Camera3Stream::returnBufferLocked(const camera3_stream_buffer &,
720 nsecs_t) {
721 ALOGE("%s: This type of stream does not support output", __FUNCTION__);
722 return INVALID_OPERATION;
723}
724status_t Camera3Stream::getInputBufferLocked(camera3_stream_buffer *) {
725 ALOGE("%s: This type of stream does not support input", __FUNCTION__);
726 return INVALID_OPERATION;
727}
728status_t Camera3Stream::returnInputBufferLocked(
729 const camera3_stream_buffer &) {
730 ALOGE("%s: This type of stream does not support input", __FUNCTION__);
731 return INVALID_OPERATION;
732}
Eino-Ville Talvala02bf0322016-02-18 12:41:10 -0800733status_t Camera3Stream::getInputBufferProducerLocked(sp<IGraphicBufferProducer>*) {
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700734 ALOGE("%s: This type of stream does not support input", __FUNCTION__);
735 return INVALID_OPERATION;
736}
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700737
Igor Murashkin2fba5842013-04-22 14:03:54 -0700738void Camera3Stream::addBufferListener(
739 wp<Camera3StreamBufferListener> listener) {
740 Mutex::Autolock l(mLock);
Zhijun Hef0d962a2014-06-30 10:24:11 -0700741
742 List<wp<Camera3StreamBufferListener> >::iterator it, end;
743 for (it = mBufferListenerList.begin(), end = mBufferListenerList.end();
744 it != end;
745 ) {
746 if (*it == listener) {
747 ALOGE("%s: Try to add the same listener twice, ignoring...", __FUNCTION__);
748 return;
749 }
750 it++;
751 }
752
Igor Murashkin2fba5842013-04-22 14:03:54 -0700753 mBufferListenerList.push_back(listener);
754}
755
756void Camera3Stream::removeBufferListener(
757 const sp<Camera3StreamBufferListener>& listener) {
758 Mutex::Autolock l(mLock);
759
760 bool erased = true;
761 List<wp<Camera3StreamBufferListener> >::iterator it, end;
762 for (it = mBufferListenerList.begin(), end = mBufferListenerList.end();
763 it != end;
764 ) {
765
766 if (*it == listener) {
767 it = mBufferListenerList.erase(it);
768 erased = true;
769 } else {
770 ++it;
771 }
772 }
773
774 if (!erased) {
775 ALOGW("%s: Could not find listener to remove, already removed",
776 __FUNCTION__);
777 }
778}
779
Yin-Chia Yehbe83fa72017-03-30 13:35:36 -0700780void Camera3Stream::setBufferFreedListener(
Yin-Chia Yehdb1e8642017-07-14 15:19:30 -0700781 wp<Camera3StreamBufferFreedListener> listener) {
Yin-Chia Yehbe83fa72017-03-30 13:35:36 -0700782 Mutex::Autolock l(mLock);
783 // Only allow set listener during stream configuration because stream is guaranteed to be IDLE
784 // at this state, so setBufferFreedListener won't collide with onBufferFreed callbacks
785 if (mState != STATE_IN_CONFIG && mState != STATE_IN_RECONFIG) {
786 ALOGE("%s: listener must be set during stream configuration!",__FUNCTION__);
787 return;
788 }
789 mBufferFreedListener = listener;
790}
791
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800792}; // namespace camera3
793
794}; // namespace android