blob: 9e6ac798b3a6a09f60295748efe53bf41807f48c [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),
Eino-Ville Talvala02bf0322016-02-18 12:41:10 -080059 mOldUsage(0),
60 mOldMaxBuffers(0),
Zhijun He125684a2015-12-26 15:07:30 -080061 mPrepared(false),
62 mPreparedBufferIdx(0),
Shuzhen Wang686f6442017-06-20 16:16:04 -070063 mLastMaxCount(Camera3StreamInterface::ALLOCATE_PIPELINE_MAX),
64 mBufferLimitLatency(kBufferLimitLatencyBinSize) {
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -080065
66 camera3_stream::stream_type = type;
67 camera3_stream::width = width;
68 camera3_stream::height = height;
69 camera3_stream::format = format;
Eino-Ville Talvala3d82c0d2015-02-23 15:19:19 -080070 camera3_stream::data_space = dataSpace;
Yin-Chia Yehb97babb2015-03-12 13:42:44 -070071 camera3_stream::rotation = rotation;
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -080072 camera3_stream::usage = 0;
73 camera3_stream::max_buffers = 0;
74 camera3_stream::priv = NULL;
75
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -080076 if ((format == HAL_PIXEL_FORMAT_BLOB || format == HAL_PIXEL_FORMAT_RAW_OPAQUE) &&
77 maxSize == 0) {
78 ALOGE("%s: BLOB or RAW_OPAQUE format with size == 0", __FUNCTION__);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -080079 mState = STATE_ERROR;
80 }
81}
82
83int Camera3Stream::getId() const {
84 return mId;
85}
86
Zhijun He125684a2015-12-26 15:07:30 -080087int Camera3Stream::getStreamSetId() const {
88 return mSetId;
89}
90
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -080091uint32_t Camera3Stream::getWidth() const {
92 return camera3_stream::width;
93}
94
95uint32_t Camera3Stream::getHeight() const {
96 return camera3_stream::height;
97}
98
99int Camera3Stream::getFormat() const {
100 return camera3_stream::format;
101}
102
Eino-Ville Talvala3d82c0d2015-02-23 15:19:19 -0800103android_dataspace Camera3Stream::getDataSpace() const {
104 return camera3_stream::data_space;
105}
106
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800107camera3_stream* Camera3Stream::startConfiguration() {
Eino-Ville Talvalab2f5b192013-07-30 14:36:03 -0700108 ATRACE_CALL();
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800109 Mutex::Autolock l(mLock);
Eino-Ville Talvalab2f5b192013-07-30 14:36:03 -0700110 status_t res;
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800111
112 switch (mState) {
113 case STATE_ERROR:
114 ALOGE("%s: In error state", __FUNCTION__);
115 return NULL;
116 case STATE_CONSTRUCTED:
117 // OK
118 break;
119 case STATE_IN_CONFIG:
120 case STATE_IN_RECONFIG:
121 // Can start config again with no trouble; but don't redo
Eino-Ville Talvala02bf0322016-02-18 12:41:10 -0800122 // mOldUsage/mOldMaxBuffers
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800123 return this;
124 case STATE_CONFIGURED:
Chien-Yu Chen90746f42015-04-15 13:50:13 -0700125 if (hasOutstandingBuffersLocked()) {
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800126 ALOGE("%s: Cannot configure stream; has outstanding buffers",
127 __FUNCTION__);
128 return NULL;
129 }
130 break;
131 default:
132 ALOGE("%s: Unknown state %d", __FUNCTION__, mState);
133 return NULL;
134 }
135
Eino-Ville Talvala02bf0322016-02-18 12:41:10 -0800136 mOldUsage = camera3_stream::usage;
137 mOldMaxBuffers = camera3_stream::max_buffers;
Eino-Ville Talvalab2f5b192013-07-30 14:36:03 -0700138
139 res = getEndpointUsage(&(camera3_stream::usage));
140 if (res != OK) {
141 ALOGE("%s: Cannot query consumer endpoint usage!",
142 __FUNCTION__);
143 return NULL;
144 }
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800145
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700146 // Stop tracking if currently doing so
147 if (mStatusId != StatusTracker::NO_STATUS_ID) {
148 sp<StatusTracker> statusTracker = mStatusTracker.promote();
149 if (statusTracker != 0) {
150 statusTracker->removeComponent(mStatusId);
151 }
152 mStatusId = StatusTracker::NO_STATUS_ID;
153 }
154
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800155 if (mState == STATE_CONSTRUCTED) {
156 mState = STATE_IN_CONFIG;
157 } else { // mState == STATE_CONFIGURED
Igor Murashkin13d315e2014-04-03 18:09:04 -0700158 LOG_ALWAYS_FATAL_IF(mState != STATE_CONFIGURED, "Invalid state: 0x%x", mState);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800159 mState = STATE_IN_RECONFIG;
160 }
161
162 return this;
163}
164
165bool Camera3Stream::isConfiguring() const {
166 Mutex::Autolock l(mLock);
167 return (mState == STATE_IN_CONFIG) || (mState == STATE_IN_RECONFIG);
168}
169
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800170status_t Camera3Stream::finishConfiguration() {
Eino-Ville Talvalab2f5b192013-07-30 14:36:03 -0700171 ATRACE_CALL();
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800172 Mutex::Autolock l(mLock);
173 switch (mState) {
174 case STATE_ERROR:
175 ALOGE("%s: In error state", __FUNCTION__);
176 return INVALID_OPERATION;
177 case STATE_IN_CONFIG:
178 case STATE_IN_RECONFIG:
179 // OK
180 break;
181 case STATE_CONSTRUCTED:
182 case STATE_CONFIGURED:
183 ALOGE("%s: Cannot finish configuration that hasn't been started",
184 __FUNCTION__);
185 return INVALID_OPERATION;
186 default:
187 ALOGE("%s: Unknown state", __FUNCTION__);
188 return INVALID_OPERATION;
189 }
190
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700191 // Register for idle tracking
192 sp<StatusTracker> statusTracker = mStatusTracker.promote();
193 if (statusTracker != 0) {
194 mStatusId = statusTracker->addComponent();
195 }
196
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800197 // Check if the stream configuration is unchanged, and skip reallocation if
198 // so. As documented in hardware/camera3.h:configure_streams().
199 if (mState == STATE_IN_RECONFIG &&
Eino-Ville Talvala02bf0322016-02-18 12:41:10 -0800200 mOldUsage == camera3_stream::usage &&
201 mOldMaxBuffers == camera3_stream::max_buffers) {
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800202 mState = STATE_CONFIGURED;
203 return OK;
204 }
205
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700206 // Reset prepared state, since buffer config has changed, and existing
207 // allocations are no longer valid
208 mPrepared = false;
209 mStreamUnpreparable = false;
210
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800211 status_t res;
212 res = configureQueueLocked();
213 if (res != OK) {
214 ALOGE("%s: Unable to configure stream %d queue: %s (%d)",
215 __FUNCTION__, mId, strerror(-res), res);
216 mState = STATE_ERROR;
217 return res;
218 }
219
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800220 mState = STATE_CONFIGURED;
221
222 return res;
223}
224
Eino-Ville Talvala17543512014-08-06 14:32:02 -0700225status_t Camera3Stream::cancelConfiguration() {
226 ATRACE_CALL();
227 Mutex::Autolock l(mLock);
228 switch (mState) {
229 case STATE_ERROR:
230 ALOGE("%s: In error state", __FUNCTION__);
231 return INVALID_OPERATION;
232 case STATE_IN_CONFIG:
233 case STATE_IN_RECONFIG:
234 // OK
235 break;
236 case STATE_CONSTRUCTED:
237 case STATE_CONFIGURED:
238 ALOGE("%s: Cannot cancel configuration that hasn't been started",
239 __FUNCTION__);
240 return INVALID_OPERATION;
241 default:
242 ALOGE("%s: Unknown state", __FUNCTION__);
243 return INVALID_OPERATION;
244 }
245
Eino-Ville Talvala02bf0322016-02-18 12:41:10 -0800246 camera3_stream::usage = mOldUsage;
247 camera3_stream::max_buffers = mOldMaxBuffers;
Eino-Ville Talvala17543512014-08-06 14:32:02 -0700248
Yin-Chia Yeh3ea3fcd2014-09-05 14:14:44 -0700249 mState = (mState == STATE_IN_RECONFIG) ? STATE_CONFIGURED : STATE_CONSTRUCTED;
Eino-Ville Talvala17543512014-08-06 14:32:02 -0700250 return OK;
251}
252
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700253bool Camera3Stream::isUnpreparable() {
254 ATRACE_CALL();
255
256 Mutex::Autolock l(mLock);
257 return mStreamUnpreparable;
258}
259
Ruben Brunkc78ac262015-08-13 17:58:46 -0700260status_t Camera3Stream::startPrepare(int maxCount) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700261 ATRACE_CALL();
262
263 Mutex::Autolock l(mLock);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700264
Ruben Brunkc78ac262015-08-13 17:58:46 -0700265 if (maxCount < 0) {
266 ALOGE("%s: Stream %d: Can't prepare stream if max buffer count (%d) is < 0",
267 __FUNCTION__, mId, maxCount);
268 return BAD_VALUE;
269 }
270
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700271 // This function should be only called when the stream is configured already.
272 if (mState != STATE_CONFIGURED) {
273 ALOGE("%s: Stream %d: Can't prepare stream if stream is not in CONFIGURED "
274 "state %d", __FUNCTION__, mId, mState);
275 return INVALID_OPERATION;
276 }
277
278 // This function can't be called if the stream has already received filled
279 // buffers
280 if (mStreamUnpreparable) {
281 ALOGE("%s: Stream %d: Can't prepare stream that's already in use",
282 __FUNCTION__, mId);
283 return INVALID_OPERATION;
284 }
285
286 if (getHandoutOutputBufferCountLocked() > 0) {
287 ALOGE("%s: Stream %d: Can't prepare stream that has outstanding buffers",
288 __FUNCTION__, mId);
289 return INVALID_OPERATION;
290 }
291
Ruben Brunkc78ac262015-08-13 17:58:46 -0700292
293
294 size_t pipelineMax = getBufferCountLocked();
295 size_t clampedCount = (pipelineMax < static_cast<size_t>(maxCount)) ?
296 pipelineMax : static_cast<size_t>(maxCount);
297 size_t bufferCount = (maxCount == Camera3StreamInterface::ALLOCATE_PIPELINE_MAX) ?
298 pipelineMax : clampedCount;
299
300 mPrepared = bufferCount <= mLastMaxCount;
301
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700302 if (mPrepared) return OK;
303
Ruben Brunkc78ac262015-08-13 17:58:46 -0700304 mLastMaxCount = bufferCount;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700305
306 mPreparedBuffers.insertAt(camera3_stream_buffer_t(), /*index*/0, bufferCount);
307 mPreparedBufferIdx = 0;
308
309 mState = STATE_PREPARING;
310
311 return NOT_ENOUGH_DATA;
312}
313
314bool Camera3Stream::isPreparing() const {
315 Mutex::Autolock l(mLock);
316 return mState == STATE_PREPARING;
317}
318
Chien-Yu Chene8c535e2016-04-14 12:18:26 -0700319bool Camera3Stream::isAbandoned() const {
320 Mutex::Autolock l(mLock);
321 return mState == STATE_ABANDONED;
322}
323
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700324status_t Camera3Stream::prepareNextBuffer() {
325 ATRACE_CALL();
326
327 Mutex::Autolock l(mLock);
328 status_t res = OK;
329
330 // This function should be only called when the stream is preparing
331 if (mState != STATE_PREPARING) {
332 ALOGE("%s: Stream %d: Can't prepare buffer if stream is not in PREPARING "
333 "state %d", __FUNCTION__, mId, mState);
334 return INVALID_OPERATION;
335 }
336
337 // Get next buffer - this may allocate, and take a while for large buffers
338 res = getBufferLocked( &mPreparedBuffers.editItemAt(mPreparedBufferIdx) );
339 if (res != OK) {
Eino-Ville Talvala02bf0322016-02-18 12:41:10 -0800340 ALOGE("%s: Stream %d: Unable to allocate buffer %zu during preparation",
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700341 __FUNCTION__, mId, mPreparedBufferIdx);
342 return NO_INIT;
343 }
344
345 mPreparedBufferIdx++;
346
347 // Check if we still have buffers left to allocate
348 if (mPreparedBufferIdx < mPreparedBuffers.size()) {
349 return NOT_ENOUGH_DATA;
350 }
351
352 // Done with prepare - mark stream as such, and return all buffers
353 // via cancelPrepare
354 mPrepared = true;
355
356 return cancelPrepareLocked();
357}
358
359status_t Camera3Stream::cancelPrepare() {
360 ATRACE_CALL();
361
362 Mutex::Autolock l(mLock);
363
364 return cancelPrepareLocked();
365}
366
367status_t Camera3Stream::cancelPrepareLocked() {
368 status_t res = OK;
369
370 // This function should be only called when the stream is mid-preparing.
371 if (mState != STATE_PREPARING) {
372 ALOGE("%s: Stream %d: Can't cancel prepare stream if stream is not in "
373 "PREPARING state %d", __FUNCTION__, mId, mState);
374 return INVALID_OPERATION;
375 }
376
377 // Return all valid buffers to stream, in ERROR state to indicate
378 // they weren't filled.
379 for (size_t i = 0; i < mPreparedBufferIdx; i++) {
380 mPreparedBuffers.editItemAt(i).release_fence = -1;
381 mPreparedBuffers.editItemAt(i).status = CAMERA3_BUFFER_STATUS_ERROR;
382 returnBufferLocked(mPreparedBuffers[i], 0);
383 }
384 mPreparedBuffers.clear();
385 mPreparedBufferIdx = 0;
386
387 mState = STATE_CONFIGURED;
388
389 return res;
390}
391
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -0700392status_t Camera3Stream::tearDown() {
393 ATRACE_CALL();
394 Mutex::Autolock l(mLock);
395
396 status_t res = OK;
397
398 // This function should be only called when the stream is configured.
399 if (mState != STATE_CONFIGURED) {
400 ALOGE("%s: Stream %d: Can't tear down stream if stream is not in "
401 "CONFIGURED state %d", __FUNCTION__, mId, mState);
402 return INVALID_OPERATION;
403 }
404
405 // If any buffers have been handed to the HAL, the stream cannot be torn down.
406 if (getHandoutOutputBufferCountLocked() > 0) {
407 ALOGE("%s: Stream %d: Can't tear down a stream that has outstanding buffers",
408 __FUNCTION__, mId);
409 return INVALID_OPERATION;
410 }
411
412 // Free buffers by disconnecting and then reconnecting to the buffer queue
413 // Only unused buffers will be dropped immediately; buffers that have been filled
414 // and are waiting to be acquired by the consumer and buffers that are currently
415 // acquired will be freed once they are released by the consumer.
416
417 res = disconnectLocked();
418 if (res != OK) {
419 if (res == -ENOTCONN) {
420 // queue has been disconnected, nothing left to do, so exit with success
421 return OK;
422 }
423 ALOGE("%s: Stream %d: Unable to disconnect to tear down buffers: %s (%d)",
424 __FUNCTION__, mId, strerror(-res), res);
425 return res;
426 }
427
428 mState = STATE_IN_CONFIG;
429
430 res = configureQueueLocked();
431 if (res != OK) {
432 ALOGE("%s: Unable to configure stream %d queue: %s (%d)",
433 __FUNCTION__, mId, strerror(-res), res);
434 mState = STATE_ERROR;
435 return res;
436 }
437
438 // Reset prepared state, since we've reconnected to the queue and can prepare again.
439 mPrepared = false;
440 mStreamUnpreparable = false;
441
442 mState = STATE_CONFIGURED;
443
444 return OK;
445}
446
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800447status_t Camera3Stream::getBuffer(camera3_stream_buffer *buffer,
448 const std::vector<size_t>& surface_ids) {
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800449 ATRACE_CALL();
450 Mutex::Autolock l(mLock);
Zhijun He6adc9cc2014-04-15 14:09:55 -0700451 status_t res = OK;
Igor Murashkin2fba5842013-04-22 14:03:54 -0700452
Zhijun He6adc9cc2014-04-15 14:09:55 -0700453 // This function should be only called when the stream is configured already.
454 if (mState != STATE_CONFIGURED) {
455 ALOGE("%s: Stream %d: Can't get buffers if stream is not in CONFIGURED state %d",
456 __FUNCTION__, mId, mState);
457 return INVALID_OPERATION;
458 }
459
460 // Wait for new buffer returned back if we are running into the limit.
461 if (getHandoutOutputBufferCountLocked() == camera3_stream::max_buffers) {
462 ALOGV("%s: Already dequeued max output buffers (%d), wait for next returned one.",
Shuzhen Wang686f6442017-06-20 16:16:04 -0700463 __FUNCTION__, camera3_stream::max_buffers);
464 nsecs_t waitStart = systemTime(SYSTEM_TIME_MONOTONIC);
Zhijun He6adc9cc2014-04-15 14:09:55 -0700465 res = mOutputBufferReturnedSignal.waitRelative(mLock, kWaitForBufferDuration);
Shuzhen Wang686f6442017-06-20 16:16:04 -0700466 nsecs_t waitEnd = systemTime(SYSTEM_TIME_MONOTONIC);
467 mBufferLimitLatency.add(waitStart, waitEnd);
Zhijun He6adc9cc2014-04-15 14:09:55 -0700468 if (res != OK) {
469 if (res == TIMED_OUT) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -0700470 ALOGE("%s: wait for output buffer return timed out after %lldms (max_buffers %d)",
471 __FUNCTION__, kWaitForBufferDuration / 1000000LL,
472 camera3_stream::max_buffers);
Zhijun He6adc9cc2014-04-15 14:09:55 -0700473 }
474 return res;
475 }
476 }
477
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800478 res = getBufferLocked(buffer, surface_ids);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700479 if (res == OK) {
480 fireBufferListenersLocked(*buffer, /*acquired*/true, /*output*/true);
Chien-Yu Chene02e9322016-04-11 16:59:33 -0700481 if (buffer->buffer) {
Emilian Peev889234d2017-07-18 18:21:26 -0700482 Mutex::Autolock l(mOutstandingBuffersLock);
Chien-Yu Chene02e9322016-04-11 16:59:33 -0700483 mOutstandingBuffers.push_back(*buffer->buffer);
484 }
Igor Murashkin2fba5842013-04-22 14:03:54 -0700485 }
486
487 return res;
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800488}
489
Emilian Peev889234d2017-07-18 18:21:26 -0700490bool Camera3Stream::isOutstandingBuffer(const camera3_stream_buffer &buffer) const{
Chien-Yu Chene02e9322016-04-11 16:59:33 -0700491 if (buffer.buffer == nullptr) {
492 return false;
493 }
494
Emilian Peev889234d2017-07-18 18:21:26 -0700495 Mutex::Autolock l(mOutstandingBuffersLock);
496
Chien-Yu Chene02e9322016-04-11 16:59:33 -0700497 for (auto b : mOutstandingBuffers) {
498 if (b == *buffer.buffer) {
499 return true;
500 }
501 }
502 return false;
503}
504
505void Camera3Stream::removeOutstandingBuffer(const camera3_stream_buffer &buffer) {
506 if (buffer.buffer == nullptr) {
507 return;
508 }
509
Emilian Peev889234d2017-07-18 18:21:26 -0700510 Mutex::Autolock l(mOutstandingBuffersLock);
511
Chien-Yu Chene02e9322016-04-11 16:59:33 -0700512 for (auto b = mOutstandingBuffers.begin(); b != mOutstandingBuffers.end(); b++) {
513 if (*b == *buffer.buffer) {
514 mOutstandingBuffers.erase(b);
515 return;
516 }
517 }
518}
519
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800520status_t Camera3Stream::returnBuffer(const camera3_stream_buffer &buffer,
521 nsecs_t timestamp) {
522 ATRACE_CALL();
523 Mutex::Autolock l(mLock);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700524
Chien-Yu Chene02e9322016-04-11 16:59:33 -0700525 // Check if this buffer is outstanding.
526 if (!isOutstandingBuffer(buffer)) {
527 ALOGE("%s: Stream %d: Returning an unknown buffer.", __FUNCTION__, mId);
528 return BAD_VALUE;
529 }
530
Shuzhen Wang1c484a62017-07-14 15:14:19 -0700531 removeOutstandingBuffer(buffer);
532
Igor Murashkin13d315e2014-04-03 18:09:04 -0700533 /**
534 * TODO: Check that the state is valid first.
535 *
536 * <HAL3.2 IN_CONFIG and IN_RECONFIG in addition to CONFIGURED.
537 * >= HAL3.2 CONFIGURED only
538 *
539 * Do this for getBuffer as well.
540 */
Igor Murashkin2fba5842013-04-22 14:03:54 -0700541 status_t res = returnBufferLocked(buffer, timestamp);
542 if (res == OK) {
543 fireBufferListenersLocked(buffer, /*acquired*/false, /*output*/true);
544 }
545
Chien-Yu Chenb83c1fe2015-09-10 16:15:21 -0700546 // Even if returning the buffer failed, we still want to signal whoever is waiting for the
547 // buffer to be returned.
548 mOutputBufferReturnedSignal.signal();
549
Igor Murashkin2fba5842013-04-22 14:03:54 -0700550 return res;
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800551}
552
Eino-Ville Talvalaba435252017-06-21 16:07:25 -0700553status_t Camera3Stream::getInputBuffer(camera3_stream_buffer *buffer, bool respectHalLimit) {
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700554 ATRACE_CALL();
555 Mutex::Autolock l(mLock);
Zhijun He6adc9cc2014-04-15 14:09:55 -0700556 status_t res = OK;
Igor Murashkin2fba5842013-04-22 14:03:54 -0700557
Zhijun He6adc9cc2014-04-15 14:09:55 -0700558 // This function should be only called when the stream is configured already.
559 if (mState != STATE_CONFIGURED) {
560 ALOGE("%s: Stream %d: Can't get input buffers if stream is not in CONFIGURED state %d",
561 __FUNCTION__, mId, mState);
562 return INVALID_OPERATION;
563 }
564
565 // Wait for new buffer returned back if we are running into the limit.
Eino-Ville Talvalaba435252017-06-21 16:07:25 -0700566 if (getHandoutInputBufferCountLocked() == camera3_stream::max_buffers && respectHalLimit) {
Zhijun He6adc9cc2014-04-15 14:09:55 -0700567 ALOGV("%s: Already dequeued max input buffers (%d), wait for next returned one.",
568 __FUNCTION__, camera3_stream::max_buffers);
569 res = mInputBufferReturnedSignal.waitRelative(mLock, kWaitForBufferDuration);
570 if (res != OK) {
571 if (res == TIMED_OUT) {
572 ALOGE("%s: wait for input buffer return timed out after %lldms", __FUNCTION__,
573 kWaitForBufferDuration / 1000000LL);
574 }
575 return res;
576 }
577 }
578
579 res = getInputBufferLocked(buffer);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700580 if (res == OK) {
581 fireBufferListenersLocked(*buffer, /*acquired*/true, /*output*/false);
Chien-Yu Chene02e9322016-04-11 16:59:33 -0700582 if (buffer->buffer) {
Emilian Peev889234d2017-07-18 18:21:26 -0700583 Mutex::Autolock l(mOutstandingBuffersLock);
Chien-Yu Chene02e9322016-04-11 16:59:33 -0700584 mOutstandingBuffers.push_back(*buffer->buffer);
585 }
Igor Murashkin2fba5842013-04-22 14:03:54 -0700586 }
587
588 return res;
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700589}
590
591status_t Camera3Stream::returnInputBuffer(const camera3_stream_buffer &buffer) {
592 ATRACE_CALL();
593 Mutex::Autolock l(mLock);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700594
Chien-Yu Chene02e9322016-04-11 16:59:33 -0700595 // Check if this buffer is outstanding.
596 if (!isOutstandingBuffer(buffer)) {
597 ALOGE("%s: Stream %d: Returning an unknown buffer.", __FUNCTION__, mId);
598 return BAD_VALUE;
599 }
600
Shuzhen Wang1c484a62017-07-14 15:14:19 -0700601 removeOutstandingBuffer(buffer);
602
Igor Murashkin2fba5842013-04-22 14:03:54 -0700603 status_t res = returnInputBufferLocked(buffer);
604 if (res == OK) {
605 fireBufferListenersLocked(buffer, /*acquired*/false, /*output*/false);
Zhijun He6adc9cc2014-04-15 14:09:55 -0700606 mInputBufferReturnedSignal.signal();
Igor Murashkin2fba5842013-04-22 14:03:54 -0700607 }
Chien-Yu Chene02e9322016-04-11 16:59:33 -0700608
Igor Murashkin2fba5842013-04-22 14:03:54 -0700609 return res;
610}
611
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700612status_t Camera3Stream::getInputBufferProducer(sp<IGraphicBufferProducer> *producer) {
613 ATRACE_CALL();
614 Mutex::Autolock l(mLock);
615
616 return getInputBufferProducerLocked(producer);
617}
618
Igor Murashkin2fba5842013-04-22 14:03:54 -0700619void Camera3Stream::fireBufferListenersLocked(
Shuzhen Wangb0fdc1e2016-03-20 23:21:39 -0700620 const camera3_stream_buffer& buffer, bool acquired, bool output) {
Igor Murashkin2fba5842013-04-22 14:03:54 -0700621 List<wp<Camera3StreamBufferListener> >::iterator it, end;
622
623 // TODO: finish implementing
624
625 Camera3StreamBufferListener::BufferInfo info =
626 Camera3StreamBufferListener::BufferInfo();
627 info.mOutput = output;
Shuzhen Wangb0fdc1e2016-03-20 23:21:39 -0700628 info.mError = (buffer.status == CAMERA3_BUFFER_STATUS_ERROR);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700629 // TODO: rest of fields
630
631 for (it = mBufferListenerList.begin(), end = mBufferListenerList.end();
632 it != end;
633 ++it) {
634
635 sp<Camera3StreamBufferListener> listener = it->promote();
636 if (listener != 0) {
637 if (acquired) {
638 listener->onBufferAcquired(info);
639 } else {
640 listener->onBufferReleased(info);
641 }
642 }
643 }
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700644}
645
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800646bool Camera3Stream::hasOutstandingBuffers() const {
647 ATRACE_CALL();
648 Mutex::Autolock l(mLock);
649 return hasOutstandingBuffersLocked();
650}
651
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700652status_t Camera3Stream::setStatusTracker(sp<StatusTracker> statusTracker) {
653 Mutex::Autolock l(mLock);
654 sp<StatusTracker> oldTracker = mStatusTracker.promote();
655 if (oldTracker != 0 && mStatusId != StatusTracker::NO_STATUS_ID) {
656 oldTracker->removeComponent(mStatusId);
657 }
658 mStatusId = StatusTracker::NO_STATUS_ID;
659 mStatusTracker = statusTracker;
660
661 return OK;
662}
663
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800664status_t Camera3Stream::disconnect() {
665 ATRACE_CALL();
666 Mutex::Autolock l(mLock);
Igor Murashkine2172be2013-05-28 15:31:39 -0700667 ALOGV("%s: Stream %d: Disconnecting...", __FUNCTION__, mId);
668 status_t res = disconnectLocked();
669
Shuzhen Wang686f6442017-06-20 16:16:04 -0700670 mBufferLimitLatency.log("Stream %d latency histogram for wait on max_buffers", mId);
671 mBufferLimitLatency.reset();
672
Igor Murashkine2172be2013-05-28 15:31:39 -0700673 if (res == -ENOTCONN) {
674 // "Already disconnected" -- not an error
675 return OK;
676 } else {
677 return res;
678 }
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800679}
680
Shuzhen Wang686f6442017-06-20 16:16:04 -0700681void Camera3Stream::dump(int fd, const Vector<String16> &args) const
682{
683 (void)args;
684 mBufferLimitLatency.dump(fd,
685 " Latency histogram for wait on max_buffers");
686}
687
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800688status_t Camera3Stream::getBufferLocked(camera3_stream_buffer *,
689 const std::vector<size_t>&) {
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700690 ALOGE("%s: This type of stream does not support output", __FUNCTION__);
691 return INVALID_OPERATION;
692}
693status_t Camera3Stream::returnBufferLocked(const camera3_stream_buffer &,
694 nsecs_t) {
695 ALOGE("%s: This type of stream does not support output", __FUNCTION__);
696 return INVALID_OPERATION;
697}
698status_t Camera3Stream::getInputBufferLocked(camera3_stream_buffer *) {
699 ALOGE("%s: This type of stream does not support input", __FUNCTION__);
700 return INVALID_OPERATION;
701}
702status_t Camera3Stream::returnInputBufferLocked(
703 const camera3_stream_buffer &) {
704 ALOGE("%s: This type of stream does not support input", __FUNCTION__);
705 return INVALID_OPERATION;
706}
Eino-Ville Talvala02bf0322016-02-18 12:41:10 -0800707status_t Camera3Stream::getInputBufferProducerLocked(sp<IGraphicBufferProducer>*) {
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700708 ALOGE("%s: This type of stream does not support input", __FUNCTION__);
709 return INVALID_OPERATION;
710}
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700711
Igor Murashkin2fba5842013-04-22 14:03:54 -0700712void Camera3Stream::addBufferListener(
713 wp<Camera3StreamBufferListener> listener) {
714 Mutex::Autolock l(mLock);
Zhijun Hef0d962a2014-06-30 10:24:11 -0700715
716 List<wp<Camera3StreamBufferListener> >::iterator it, end;
717 for (it = mBufferListenerList.begin(), end = mBufferListenerList.end();
718 it != end;
719 ) {
720 if (*it == listener) {
721 ALOGE("%s: Try to add the same listener twice, ignoring...", __FUNCTION__);
722 return;
723 }
724 it++;
725 }
726
Igor Murashkin2fba5842013-04-22 14:03:54 -0700727 mBufferListenerList.push_back(listener);
728}
729
730void Camera3Stream::removeBufferListener(
731 const sp<Camera3StreamBufferListener>& listener) {
732 Mutex::Autolock l(mLock);
733
734 bool erased = true;
735 List<wp<Camera3StreamBufferListener> >::iterator it, end;
736 for (it = mBufferListenerList.begin(), end = mBufferListenerList.end();
737 it != end;
738 ) {
739
740 if (*it == listener) {
741 it = mBufferListenerList.erase(it);
742 erased = true;
743 } else {
744 ++it;
745 }
746 }
747
748 if (!erased) {
749 ALOGW("%s: Could not find listener to remove, already removed",
750 __FUNCTION__);
751 }
752}
753
Yin-Chia Yehbe83fa72017-03-30 13:35:36 -0700754void Camera3Stream::setBufferFreedListener(
Yin-Chia Yehdb1e8642017-07-14 15:19:30 -0700755 wp<Camera3StreamBufferFreedListener> listener) {
Yin-Chia Yehbe83fa72017-03-30 13:35:36 -0700756 Mutex::Autolock l(mLock);
757 // Only allow set listener during stream configuration because stream is guaranteed to be IDLE
758 // at this state, so setBufferFreedListener won't collide with onBufferFreed callbacks
759 if (mState != STATE_IN_CONFIG && mState != STATE_IN_RECONFIG) {
760 ALOGE("%s: listener must be set during stream configuration!",__FUNCTION__);
761 return;
762 }
763 mBufferFreedListener = listener;
764}
765
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800766}; // namespace camera3
767
768}; // namespace android