blob: ba352c420056e9c6acbfb41960ac6bfb2b6ab9d8 [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) {
482 mOutstandingBuffers.push_back(*buffer->buffer);
483 }
Igor Murashkin2fba5842013-04-22 14:03:54 -0700484 }
485
486 return res;
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800487}
488
Chien-Yu Chene02e9322016-04-11 16:59:33 -0700489bool Camera3Stream::isOutstandingBuffer(const camera3_stream_buffer &buffer) {
490 if (buffer.buffer == nullptr) {
491 return false;
492 }
493
494 for (auto b : mOutstandingBuffers) {
495 if (b == *buffer.buffer) {
496 return true;
497 }
498 }
499 return false;
500}
501
502void Camera3Stream::removeOutstandingBuffer(const camera3_stream_buffer &buffer) {
503 if (buffer.buffer == nullptr) {
504 return;
505 }
506
507 for (auto b = mOutstandingBuffers.begin(); b != mOutstandingBuffers.end(); b++) {
508 if (*b == *buffer.buffer) {
509 mOutstandingBuffers.erase(b);
510 return;
511 }
512 }
513}
514
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800515status_t Camera3Stream::returnBuffer(const camera3_stream_buffer &buffer,
516 nsecs_t timestamp) {
517 ATRACE_CALL();
518 Mutex::Autolock l(mLock);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700519
Chien-Yu Chene02e9322016-04-11 16:59:33 -0700520 // Check if this buffer is outstanding.
521 if (!isOutstandingBuffer(buffer)) {
522 ALOGE("%s: Stream %d: Returning an unknown buffer.", __FUNCTION__, mId);
523 return BAD_VALUE;
524 }
525
Igor Murashkin13d315e2014-04-03 18:09:04 -0700526 /**
527 * TODO: Check that the state is valid first.
528 *
529 * <HAL3.2 IN_CONFIG and IN_RECONFIG in addition to CONFIGURED.
530 * >= HAL3.2 CONFIGURED only
531 *
532 * Do this for getBuffer as well.
533 */
Igor Murashkin2fba5842013-04-22 14:03:54 -0700534 status_t res = returnBufferLocked(buffer, timestamp);
535 if (res == OK) {
536 fireBufferListenersLocked(buffer, /*acquired*/false, /*output*/true);
537 }
538
Chien-Yu Chenb83c1fe2015-09-10 16:15:21 -0700539 // Even if returning the buffer failed, we still want to signal whoever is waiting for the
540 // buffer to be returned.
541 mOutputBufferReturnedSignal.signal();
542
Chien-Yu Chene02e9322016-04-11 16:59:33 -0700543 removeOutstandingBuffer(buffer);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700544 return res;
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800545}
546
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700547status_t Camera3Stream::getInputBuffer(camera3_stream_buffer *buffer) {
548 ATRACE_CALL();
549 Mutex::Autolock l(mLock);
Zhijun He6adc9cc2014-04-15 14:09:55 -0700550 status_t res = OK;
Igor Murashkin2fba5842013-04-22 14:03:54 -0700551
Zhijun He6adc9cc2014-04-15 14:09:55 -0700552 // This function should be only called when the stream is configured already.
553 if (mState != STATE_CONFIGURED) {
554 ALOGE("%s: Stream %d: Can't get input buffers if stream is not in CONFIGURED state %d",
555 __FUNCTION__, mId, mState);
556 return INVALID_OPERATION;
557 }
558
559 // Wait for new buffer returned back if we are running into the limit.
560 if (getHandoutInputBufferCountLocked() == camera3_stream::max_buffers) {
561 ALOGV("%s: Already dequeued max input buffers (%d), wait for next returned one.",
562 __FUNCTION__, camera3_stream::max_buffers);
563 res = mInputBufferReturnedSignal.waitRelative(mLock, kWaitForBufferDuration);
564 if (res != OK) {
565 if (res == TIMED_OUT) {
566 ALOGE("%s: wait for input buffer return timed out after %lldms", __FUNCTION__,
567 kWaitForBufferDuration / 1000000LL);
568 }
569 return res;
570 }
571 }
572
573 res = getInputBufferLocked(buffer);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700574 if (res == OK) {
575 fireBufferListenersLocked(*buffer, /*acquired*/true, /*output*/false);
Chien-Yu Chene02e9322016-04-11 16:59:33 -0700576 if (buffer->buffer) {
577 mOutstandingBuffers.push_back(*buffer->buffer);
578 }
Igor Murashkin2fba5842013-04-22 14:03:54 -0700579 }
580
581 return res;
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700582}
583
584status_t Camera3Stream::returnInputBuffer(const camera3_stream_buffer &buffer) {
585 ATRACE_CALL();
586 Mutex::Autolock l(mLock);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700587
Chien-Yu Chene02e9322016-04-11 16:59:33 -0700588 // Check if this buffer is outstanding.
589 if (!isOutstandingBuffer(buffer)) {
590 ALOGE("%s: Stream %d: Returning an unknown buffer.", __FUNCTION__, mId);
591 return BAD_VALUE;
592 }
593
Igor Murashkin2fba5842013-04-22 14:03:54 -0700594 status_t res = returnInputBufferLocked(buffer);
595 if (res == OK) {
596 fireBufferListenersLocked(buffer, /*acquired*/false, /*output*/false);
Zhijun He6adc9cc2014-04-15 14:09:55 -0700597 mInputBufferReturnedSignal.signal();
Igor Murashkin2fba5842013-04-22 14:03:54 -0700598 }
Chien-Yu Chene02e9322016-04-11 16:59:33 -0700599
600 removeOutstandingBuffer(buffer);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700601 return res;
602}
603
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700604status_t Camera3Stream::getInputBufferProducer(sp<IGraphicBufferProducer> *producer) {
605 ATRACE_CALL();
606 Mutex::Autolock l(mLock);
607
608 return getInputBufferProducerLocked(producer);
609}
610
Igor Murashkin2fba5842013-04-22 14:03:54 -0700611void Camera3Stream::fireBufferListenersLocked(
Shuzhen Wangb0fdc1e2016-03-20 23:21:39 -0700612 const camera3_stream_buffer& buffer, bool acquired, bool output) {
Igor Murashkin2fba5842013-04-22 14:03:54 -0700613 List<wp<Camera3StreamBufferListener> >::iterator it, end;
614
615 // TODO: finish implementing
616
617 Camera3StreamBufferListener::BufferInfo info =
618 Camera3StreamBufferListener::BufferInfo();
619 info.mOutput = output;
Shuzhen Wangb0fdc1e2016-03-20 23:21:39 -0700620 info.mError = (buffer.status == CAMERA3_BUFFER_STATUS_ERROR);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700621 // TODO: rest of fields
622
623 for (it = mBufferListenerList.begin(), end = mBufferListenerList.end();
624 it != end;
625 ++it) {
626
627 sp<Camera3StreamBufferListener> listener = it->promote();
628 if (listener != 0) {
629 if (acquired) {
630 listener->onBufferAcquired(info);
631 } else {
632 listener->onBufferReleased(info);
633 }
634 }
635 }
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700636}
637
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800638bool Camera3Stream::hasOutstandingBuffers() const {
639 ATRACE_CALL();
640 Mutex::Autolock l(mLock);
641 return hasOutstandingBuffersLocked();
642}
643
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700644status_t Camera3Stream::setStatusTracker(sp<StatusTracker> statusTracker) {
645 Mutex::Autolock l(mLock);
646 sp<StatusTracker> oldTracker = mStatusTracker.promote();
647 if (oldTracker != 0 && mStatusId != StatusTracker::NO_STATUS_ID) {
648 oldTracker->removeComponent(mStatusId);
649 }
650 mStatusId = StatusTracker::NO_STATUS_ID;
651 mStatusTracker = statusTracker;
652
653 return OK;
654}
655
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800656status_t Camera3Stream::disconnect() {
657 ATRACE_CALL();
658 Mutex::Autolock l(mLock);
Igor Murashkine2172be2013-05-28 15:31:39 -0700659 ALOGV("%s: Stream %d: Disconnecting...", __FUNCTION__, mId);
660 status_t res = disconnectLocked();
661
Shuzhen Wang686f6442017-06-20 16:16:04 -0700662 mBufferLimitLatency.log("Stream %d latency histogram for wait on max_buffers", mId);
663 mBufferLimitLatency.reset();
664
Igor Murashkine2172be2013-05-28 15:31:39 -0700665 if (res == -ENOTCONN) {
666 // "Already disconnected" -- not an error
667 return OK;
668 } else {
669 return res;
670 }
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800671}
672
Shuzhen Wang686f6442017-06-20 16:16:04 -0700673void Camera3Stream::dump(int fd, const Vector<String16> &args) const
674{
675 (void)args;
676 mBufferLimitLatency.dump(fd,
677 " Latency histogram for wait on max_buffers");
678}
679
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800680status_t Camera3Stream::getBufferLocked(camera3_stream_buffer *,
681 const std::vector<size_t>&) {
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700682 ALOGE("%s: This type of stream does not support output", __FUNCTION__);
683 return INVALID_OPERATION;
684}
685status_t Camera3Stream::returnBufferLocked(const camera3_stream_buffer &,
686 nsecs_t) {
687 ALOGE("%s: This type of stream does not support output", __FUNCTION__);
688 return INVALID_OPERATION;
689}
690status_t Camera3Stream::getInputBufferLocked(camera3_stream_buffer *) {
691 ALOGE("%s: This type of stream does not support input", __FUNCTION__);
692 return INVALID_OPERATION;
693}
694status_t Camera3Stream::returnInputBufferLocked(
695 const camera3_stream_buffer &) {
696 ALOGE("%s: This type of stream does not support input", __FUNCTION__);
697 return INVALID_OPERATION;
698}
Eino-Ville Talvala02bf0322016-02-18 12:41:10 -0800699status_t Camera3Stream::getInputBufferProducerLocked(sp<IGraphicBufferProducer>*) {
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700700 ALOGE("%s: This type of stream does not support input", __FUNCTION__);
701 return INVALID_OPERATION;
702}
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700703
Igor Murashkin2fba5842013-04-22 14:03:54 -0700704void Camera3Stream::addBufferListener(
705 wp<Camera3StreamBufferListener> listener) {
706 Mutex::Autolock l(mLock);
Zhijun Hef0d962a2014-06-30 10:24:11 -0700707
708 List<wp<Camera3StreamBufferListener> >::iterator it, end;
709 for (it = mBufferListenerList.begin(), end = mBufferListenerList.end();
710 it != end;
711 ) {
712 if (*it == listener) {
713 ALOGE("%s: Try to add the same listener twice, ignoring...", __FUNCTION__);
714 return;
715 }
716 it++;
717 }
718
Igor Murashkin2fba5842013-04-22 14:03:54 -0700719 mBufferListenerList.push_back(listener);
720}
721
722void Camera3Stream::removeBufferListener(
723 const sp<Camera3StreamBufferListener>& listener) {
724 Mutex::Autolock l(mLock);
725
726 bool erased = true;
727 List<wp<Camera3StreamBufferListener> >::iterator it, end;
728 for (it = mBufferListenerList.begin(), end = mBufferListenerList.end();
729 it != end;
730 ) {
731
732 if (*it == listener) {
733 it = mBufferListenerList.erase(it);
734 erased = true;
735 } else {
736 ++it;
737 }
738 }
739
740 if (!erased) {
741 ALOGW("%s: Could not find listener to remove, already removed",
742 __FUNCTION__);
743 }
744}
745
Yin-Chia Yehbe83fa72017-03-30 13:35:36 -0700746void Camera3Stream::setBufferFreedListener(
747 Camera3StreamBufferFreedListener* listener) {
748 Mutex::Autolock l(mLock);
749 // Only allow set listener during stream configuration because stream is guaranteed to be IDLE
750 // at this state, so setBufferFreedListener won't collide with onBufferFreed callbacks
751 if (mState != STATE_IN_CONFIG && mState != STATE_IN_RECONFIG) {
752 ALOGE("%s: listener must be set during stream configuration!",__FUNCTION__);
753 return;
754 }
755 mBufferFreedListener = listener;
756}
757
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800758}; // namespace camera3
759
760}; // namespace android