blob: b45ef77b273f7edc9a104f5b3989bbe304e9935f [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),
Ruben Brunkc78ac262015-08-13 17:58:46 -070063 mLastMaxCount(Camera3StreamInterface::ALLOCATE_PIPELINE_MAX) {
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -080064
65 camera3_stream::stream_type = type;
66 camera3_stream::width = width;
67 camera3_stream::height = height;
68 camera3_stream::format = format;
Eino-Ville Talvala3d82c0d2015-02-23 15:19:19 -080069 camera3_stream::data_space = dataSpace;
Yin-Chia Yehb97babb2015-03-12 13:42:44 -070070 camera3_stream::rotation = rotation;
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -080071 camera3_stream::usage = 0;
72 camera3_stream::max_buffers = 0;
73 camera3_stream::priv = NULL;
74
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -080075 if ((format == HAL_PIXEL_FORMAT_BLOB || format == HAL_PIXEL_FORMAT_RAW_OPAQUE) &&
76 maxSize == 0) {
77 ALOGE("%s: BLOB or RAW_OPAQUE format with size == 0", __FUNCTION__);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -080078 mState = STATE_ERROR;
79 }
80}
81
82int Camera3Stream::getId() const {
83 return mId;
84}
85
Zhijun He125684a2015-12-26 15:07:30 -080086int Camera3Stream::getStreamSetId() const {
87 return mSetId;
88}
89
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -080090uint32_t Camera3Stream::getWidth() const {
91 return camera3_stream::width;
92}
93
94uint32_t Camera3Stream::getHeight() const {
95 return camera3_stream::height;
96}
97
98int Camera3Stream::getFormat() const {
99 return camera3_stream::format;
100}
101
Eino-Ville Talvala3d82c0d2015-02-23 15:19:19 -0800102android_dataspace Camera3Stream::getDataSpace() const {
103 return camera3_stream::data_space;
104}
105
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800106camera3_stream* Camera3Stream::startConfiguration() {
Eino-Ville Talvalab2f5b192013-07-30 14:36:03 -0700107 ATRACE_CALL();
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800108 Mutex::Autolock l(mLock);
Eino-Ville Talvalab2f5b192013-07-30 14:36:03 -0700109 status_t res;
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800110
111 switch (mState) {
112 case STATE_ERROR:
113 ALOGE("%s: In error state", __FUNCTION__);
114 return NULL;
115 case STATE_CONSTRUCTED:
116 // OK
117 break;
118 case STATE_IN_CONFIG:
119 case STATE_IN_RECONFIG:
120 // Can start config again with no trouble; but don't redo
Eino-Ville Talvala02bf0322016-02-18 12:41:10 -0800121 // mOldUsage/mOldMaxBuffers
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800122 return this;
123 case STATE_CONFIGURED:
Chien-Yu Chen90746f42015-04-15 13:50:13 -0700124 if (hasOutstandingBuffersLocked()) {
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800125 ALOGE("%s: Cannot configure stream; has outstanding buffers",
126 __FUNCTION__);
127 return NULL;
128 }
129 break;
130 default:
131 ALOGE("%s: Unknown state %d", __FUNCTION__, mState);
132 return NULL;
133 }
134
Eino-Ville Talvala02bf0322016-02-18 12:41:10 -0800135 mOldUsage = camera3_stream::usage;
136 mOldMaxBuffers = camera3_stream::max_buffers;
Eino-Ville Talvalab2f5b192013-07-30 14:36:03 -0700137
138 res = getEndpointUsage(&(camera3_stream::usage));
139 if (res != OK) {
140 ALOGE("%s: Cannot query consumer endpoint usage!",
141 __FUNCTION__);
142 return NULL;
143 }
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800144
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700145 // Stop tracking if currently doing so
146 if (mStatusId != StatusTracker::NO_STATUS_ID) {
147 sp<StatusTracker> statusTracker = mStatusTracker.promote();
148 if (statusTracker != 0) {
149 statusTracker->removeComponent(mStatusId);
150 }
151 mStatusId = StatusTracker::NO_STATUS_ID;
152 }
153
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800154 if (mState == STATE_CONSTRUCTED) {
155 mState = STATE_IN_CONFIG;
156 } else { // mState == STATE_CONFIGURED
Igor Murashkin13d315e2014-04-03 18:09:04 -0700157 LOG_ALWAYS_FATAL_IF(mState != STATE_CONFIGURED, "Invalid state: 0x%x", mState);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800158 mState = STATE_IN_RECONFIG;
159 }
160
161 return this;
162}
163
164bool Camera3Stream::isConfiguring() const {
165 Mutex::Autolock l(mLock);
166 return (mState == STATE_IN_CONFIG) || (mState == STATE_IN_RECONFIG);
167}
168
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800169status_t Camera3Stream::finishConfiguration() {
Eino-Ville Talvalab2f5b192013-07-30 14:36:03 -0700170 ATRACE_CALL();
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800171 Mutex::Autolock l(mLock);
172 switch (mState) {
173 case STATE_ERROR:
174 ALOGE("%s: In error state", __FUNCTION__);
175 return INVALID_OPERATION;
176 case STATE_IN_CONFIG:
177 case STATE_IN_RECONFIG:
178 // OK
179 break;
180 case STATE_CONSTRUCTED:
181 case STATE_CONFIGURED:
182 ALOGE("%s: Cannot finish configuration that hasn't been started",
183 __FUNCTION__);
184 return INVALID_OPERATION;
185 default:
186 ALOGE("%s: Unknown state", __FUNCTION__);
187 return INVALID_OPERATION;
188 }
189
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700190 // Register for idle tracking
191 sp<StatusTracker> statusTracker = mStatusTracker.promote();
192 if (statusTracker != 0) {
193 mStatusId = statusTracker->addComponent();
194 }
195
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800196 // Check if the stream configuration is unchanged, and skip reallocation if
197 // so. As documented in hardware/camera3.h:configure_streams().
198 if (mState == STATE_IN_RECONFIG &&
Eino-Ville Talvala02bf0322016-02-18 12:41:10 -0800199 mOldUsage == camera3_stream::usage &&
200 mOldMaxBuffers == camera3_stream::max_buffers) {
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800201 mState = STATE_CONFIGURED;
202 return OK;
203 }
204
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700205 // Reset prepared state, since buffer config has changed, and existing
206 // allocations are no longer valid
207 mPrepared = false;
208 mStreamUnpreparable = false;
209
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800210 status_t res;
211 res = configureQueueLocked();
212 if (res != OK) {
213 ALOGE("%s: Unable to configure stream %d queue: %s (%d)",
214 __FUNCTION__, mId, strerror(-res), res);
215 mState = STATE_ERROR;
216 return res;
217 }
218
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800219 mState = STATE_CONFIGURED;
220
221 return res;
222}
223
Eino-Ville Talvala17543512014-08-06 14:32:02 -0700224status_t Camera3Stream::cancelConfiguration() {
225 ATRACE_CALL();
226 Mutex::Autolock l(mLock);
227 switch (mState) {
228 case STATE_ERROR:
229 ALOGE("%s: In error state", __FUNCTION__);
230 return INVALID_OPERATION;
231 case STATE_IN_CONFIG:
232 case STATE_IN_RECONFIG:
233 // OK
234 break;
235 case STATE_CONSTRUCTED:
236 case STATE_CONFIGURED:
237 ALOGE("%s: Cannot cancel configuration that hasn't been started",
238 __FUNCTION__);
239 return INVALID_OPERATION;
240 default:
241 ALOGE("%s: Unknown state", __FUNCTION__);
242 return INVALID_OPERATION;
243 }
244
Eino-Ville Talvala02bf0322016-02-18 12:41:10 -0800245 camera3_stream::usage = mOldUsage;
246 camera3_stream::max_buffers = mOldMaxBuffers;
Eino-Ville Talvala17543512014-08-06 14:32:02 -0700247
Yin-Chia Yeh3ea3fcd2014-09-05 14:14:44 -0700248 mState = (mState == STATE_IN_RECONFIG) ? STATE_CONFIGURED : STATE_CONSTRUCTED;
Eino-Ville Talvala17543512014-08-06 14:32:02 -0700249 return OK;
250}
251
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700252bool Camera3Stream::isUnpreparable() {
253 ATRACE_CALL();
254
255 Mutex::Autolock l(mLock);
256 return mStreamUnpreparable;
257}
258
Ruben Brunkc78ac262015-08-13 17:58:46 -0700259status_t Camera3Stream::startPrepare(int maxCount) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700260 ATRACE_CALL();
261
262 Mutex::Autolock l(mLock);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700263
Ruben Brunkc78ac262015-08-13 17:58:46 -0700264 if (maxCount < 0) {
265 ALOGE("%s: Stream %d: Can't prepare stream if max buffer count (%d) is < 0",
266 __FUNCTION__, mId, maxCount);
267 return BAD_VALUE;
268 }
269
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700270 // This function should be only called when the stream is configured already.
271 if (mState != STATE_CONFIGURED) {
272 ALOGE("%s: Stream %d: Can't prepare stream if stream is not in CONFIGURED "
273 "state %d", __FUNCTION__, mId, mState);
274 return INVALID_OPERATION;
275 }
276
277 // This function can't be called if the stream has already received filled
278 // buffers
279 if (mStreamUnpreparable) {
280 ALOGE("%s: Stream %d: Can't prepare stream that's already in use",
281 __FUNCTION__, mId);
282 return INVALID_OPERATION;
283 }
284
285 if (getHandoutOutputBufferCountLocked() > 0) {
286 ALOGE("%s: Stream %d: Can't prepare stream that has outstanding buffers",
287 __FUNCTION__, mId);
288 return INVALID_OPERATION;
289 }
290
Ruben Brunkc78ac262015-08-13 17:58:46 -0700291
292
293 size_t pipelineMax = getBufferCountLocked();
294 size_t clampedCount = (pipelineMax < static_cast<size_t>(maxCount)) ?
295 pipelineMax : static_cast<size_t>(maxCount);
296 size_t bufferCount = (maxCount == Camera3StreamInterface::ALLOCATE_PIPELINE_MAX) ?
297 pipelineMax : clampedCount;
298
299 mPrepared = bufferCount <= mLastMaxCount;
300
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700301 if (mPrepared) return OK;
302
Ruben Brunkc78ac262015-08-13 17:58:46 -0700303 mLastMaxCount = bufferCount;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700304
305 mPreparedBuffers.insertAt(camera3_stream_buffer_t(), /*index*/0, bufferCount);
306 mPreparedBufferIdx = 0;
307
308 mState = STATE_PREPARING;
309
310 return NOT_ENOUGH_DATA;
311}
312
313bool Camera3Stream::isPreparing() const {
314 Mutex::Autolock l(mLock);
315 return mState == STATE_PREPARING;
316}
317
Chien-Yu Chene8c535e2016-04-14 12:18:26 -0700318bool Camera3Stream::isAbandoned() const {
319 Mutex::Autolock l(mLock);
320 return mState == STATE_ABANDONED;
321}
322
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700323status_t Camera3Stream::prepareNextBuffer() {
324 ATRACE_CALL();
325
326 Mutex::Autolock l(mLock);
327 status_t res = OK;
328
329 // This function should be only called when the stream is preparing
330 if (mState != STATE_PREPARING) {
331 ALOGE("%s: Stream %d: Can't prepare buffer if stream is not in PREPARING "
332 "state %d", __FUNCTION__, mId, mState);
333 return INVALID_OPERATION;
334 }
335
336 // Get next buffer - this may allocate, and take a while for large buffers
337 res = getBufferLocked( &mPreparedBuffers.editItemAt(mPreparedBufferIdx) );
338 if (res != OK) {
Eino-Ville Talvala02bf0322016-02-18 12:41:10 -0800339 ALOGE("%s: Stream %d: Unable to allocate buffer %zu during preparation",
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700340 __FUNCTION__, mId, mPreparedBufferIdx);
341 return NO_INIT;
342 }
343
344 mPreparedBufferIdx++;
345
346 // Check if we still have buffers left to allocate
347 if (mPreparedBufferIdx < mPreparedBuffers.size()) {
348 return NOT_ENOUGH_DATA;
349 }
350
351 // Done with prepare - mark stream as such, and return all buffers
352 // via cancelPrepare
353 mPrepared = true;
354
355 return cancelPrepareLocked();
356}
357
358status_t Camera3Stream::cancelPrepare() {
359 ATRACE_CALL();
360
361 Mutex::Autolock l(mLock);
362
363 return cancelPrepareLocked();
364}
365
366status_t Camera3Stream::cancelPrepareLocked() {
367 status_t res = OK;
368
369 // This function should be only called when the stream is mid-preparing.
370 if (mState != STATE_PREPARING) {
371 ALOGE("%s: Stream %d: Can't cancel prepare stream if stream is not in "
372 "PREPARING state %d", __FUNCTION__, mId, mState);
373 return INVALID_OPERATION;
374 }
375
376 // Return all valid buffers to stream, in ERROR state to indicate
377 // they weren't filled.
378 for (size_t i = 0; i < mPreparedBufferIdx; i++) {
379 mPreparedBuffers.editItemAt(i).release_fence = -1;
380 mPreparedBuffers.editItemAt(i).status = CAMERA3_BUFFER_STATUS_ERROR;
381 returnBufferLocked(mPreparedBuffers[i], 0);
382 }
383 mPreparedBuffers.clear();
384 mPreparedBufferIdx = 0;
385
386 mState = STATE_CONFIGURED;
387
388 return res;
389}
390
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -0700391status_t Camera3Stream::tearDown() {
392 ATRACE_CALL();
393 Mutex::Autolock l(mLock);
394
395 status_t res = OK;
396
397 // This function should be only called when the stream is configured.
398 if (mState != STATE_CONFIGURED) {
399 ALOGE("%s: Stream %d: Can't tear down stream if stream is not in "
400 "CONFIGURED state %d", __FUNCTION__, mId, mState);
401 return INVALID_OPERATION;
402 }
403
404 // If any buffers have been handed to the HAL, the stream cannot be torn down.
405 if (getHandoutOutputBufferCountLocked() > 0) {
406 ALOGE("%s: Stream %d: Can't tear down a stream that has outstanding buffers",
407 __FUNCTION__, mId);
408 return INVALID_OPERATION;
409 }
410
411 // Free buffers by disconnecting and then reconnecting to the buffer queue
412 // Only unused buffers will be dropped immediately; buffers that have been filled
413 // and are waiting to be acquired by the consumer and buffers that are currently
414 // acquired will be freed once they are released by the consumer.
415
416 res = disconnectLocked();
417 if (res != OK) {
418 if (res == -ENOTCONN) {
419 // queue has been disconnected, nothing left to do, so exit with success
420 return OK;
421 }
422 ALOGE("%s: Stream %d: Unable to disconnect to tear down buffers: %s (%d)",
423 __FUNCTION__, mId, strerror(-res), res);
424 return res;
425 }
426
427 mState = STATE_IN_CONFIG;
428
429 res = configureQueueLocked();
430 if (res != OK) {
431 ALOGE("%s: Unable to configure stream %d queue: %s (%d)",
432 __FUNCTION__, mId, strerror(-res), res);
433 mState = STATE_ERROR;
434 return res;
435 }
436
437 // Reset prepared state, since we've reconnected to the queue and can prepare again.
438 mPrepared = false;
439 mStreamUnpreparable = false;
440
441 mState = STATE_CONFIGURED;
442
443 return OK;
444}
445
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800446status_t Camera3Stream::getBuffer(camera3_stream_buffer *buffer,
447 const std::vector<size_t>& surface_ids) {
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800448 ATRACE_CALL();
449 Mutex::Autolock l(mLock);
Zhijun He6adc9cc2014-04-15 14:09:55 -0700450 status_t res = OK;
Igor Murashkin2fba5842013-04-22 14:03:54 -0700451
Zhijun He6adc9cc2014-04-15 14:09:55 -0700452 // This function should be only called when the stream is configured already.
453 if (mState != STATE_CONFIGURED) {
454 ALOGE("%s: Stream %d: Can't get buffers if stream is not in CONFIGURED state %d",
455 __FUNCTION__, mId, mState);
456 return INVALID_OPERATION;
457 }
458
459 // Wait for new buffer returned back if we are running into the limit.
460 if (getHandoutOutputBufferCountLocked() == camera3_stream::max_buffers) {
461 ALOGV("%s: Already dequeued max output buffers (%d), wait for next returned one.",
462 __FUNCTION__, camera3_stream::max_buffers);
463 res = mOutputBufferReturnedSignal.waitRelative(mLock, kWaitForBufferDuration);
464 if (res != OK) {
465 if (res == TIMED_OUT) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -0700466 ALOGE("%s: wait for output buffer return timed out after %lldms (max_buffers %d)",
467 __FUNCTION__, kWaitForBufferDuration / 1000000LL,
468 camera3_stream::max_buffers);
Zhijun He6adc9cc2014-04-15 14:09:55 -0700469 }
470 return res;
471 }
472 }
473
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800474 res = getBufferLocked(buffer, surface_ids);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700475 if (res == OK) {
476 fireBufferListenersLocked(*buffer, /*acquired*/true, /*output*/true);
Chien-Yu Chene02e9322016-04-11 16:59:33 -0700477 if (buffer->buffer) {
478 mOutstandingBuffers.push_back(*buffer->buffer);
479 }
Igor Murashkin2fba5842013-04-22 14:03:54 -0700480 }
481
482 return res;
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800483}
484
Chien-Yu Chene02e9322016-04-11 16:59:33 -0700485bool Camera3Stream::isOutstandingBuffer(const camera3_stream_buffer &buffer) {
486 if (buffer.buffer == nullptr) {
487 return false;
488 }
489
490 for (auto b : mOutstandingBuffers) {
491 if (b == *buffer.buffer) {
492 return true;
493 }
494 }
495 return false;
496}
497
498void Camera3Stream::removeOutstandingBuffer(const camera3_stream_buffer &buffer) {
499 if (buffer.buffer == nullptr) {
500 return;
501 }
502
503 for (auto b = mOutstandingBuffers.begin(); b != mOutstandingBuffers.end(); b++) {
504 if (*b == *buffer.buffer) {
505 mOutstandingBuffers.erase(b);
506 return;
507 }
508 }
509}
510
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800511status_t Camera3Stream::returnBuffer(const camera3_stream_buffer &buffer,
512 nsecs_t timestamp) {
513 ATRACE_CALL();
514 Mutex::Autolock l(mLock);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700515
Chien-Yu Chene02e9322016-04-11 16:59:33 -0700516 // Check if this buffer is outstanding.
517 if (!isOutstandingBuffer(buffer)) {
518 ALOGE("%s: Stream %d: Returning an unknown buffer.", __FUNCTION__, mId);
519 return BAD_VALUE;
520 }
521
Igor Murashkin13d315e2014-04-03 18:09:04 -0700522 /**
523 * TODO: Check that the state is valid first.
524 *
525 * <HAL3.2 IN_CONFIG and IN_RECONFIG in addition to CONFIGURED.
526 * >= HAL3.2 CONFIGURED only
527 *
528 * Do this for getBuffer as well.
529 */
Igor Murashkin2fba5842013-04-22 14:03:54 -0700530 status_t res = returnBufferLocked(buffer, timestamp);
531 if (res == OK) {
532 fireBufferListenersLocked(buffer, /*acquired*/false, /*output*/true);
533 }
534
Chien-Yu Chenb83c1fe2015-09-10 16:15:21 -0700535 // Even if returning the buffer failed, we still want to signal whoever is waiting for the
536 // buffer to be returned.
537 mOutputBufferReturnedSignal.signal();
538
Chien-Yu Chene02e9322016-04-11 16:59:33 -0700539 removeOutstandingBuffer(buffer);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700540 return res;
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800541}
542
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700543status_t Camera3Stream::getInputBuffer(camera3_stream_buffer *buffer) {
544 ATRACE_CALL();
545 Mutex::Autolock l(mLock);
Zhijun He6adc9cc2014-04-15 14:09:55 -0700546 status_t res = OK;
Igor Murashkin2fba5842013-04-22 14:03:54 -0700547
Zhijun He6adc9cc2014-04-15 14:09:55 -0700548 // This function should be only called when the stream is configured already.
549 if (mState != STATE_CONFIGURED) {
550 ALOGE("%s: Stream %d: Can't get input buffers if stream is not in CONFIGURED state %d",
551 __FUNCTION__, mId, mState);
552 return INVALID_OPERATION;
553 }
554
555 // Wait for new buffer returned back if we are running into the limit.
556 if (getHandoutInputBufferCountLocked() == camera3_stream::max_buffers) {
557 ALOGV("%s: Already dequeued max input buffers (%d), wait for next returned one.",
558 __FUNCTION__, camera3_stream::max_buffers);
559 res = mInputBufferReturnedSignal.waitRelative(mLock, kWaitForBufferDuration);
560 if (res != OK) {
561 if (res == TIMED_OUT) {
562 ALOGE("%s: wait for input buffer return timed out after %lldms", __FUNCTION__,
563 kWaitForBufferDuration / 1000000LL);
564 }
565 return res;
566 }
567 }
568
569 res = getInputBufferLocked(buffer);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700570 if (res == OK) {
571 fireBufferListenersLocked(*buffer, /*acquired*/true, /*output*/false);
Chien-Yu Chene02e9322016-04-11 16:59:33 -0700572 if (buffer->buffer) {
573 mOutstandingBuffers.push_back(*buffer->buffer);
574 }
Igor Murashkin2fba5842013-04-22 14:03:54 -0700575 }
576
577 return res;
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700578}
579
580status_t Camera3Stream::returnInputBuffer(const camera3_stream_buffer &buffer) {
581 ATRACE_CALL();
582 Mutex::Autolock l(mLock);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700583
Chien-Yu Chene02e9322016-04-11 16:59:33 -0700584 // Check if this buffer is outstanding.
585 if (!isOutstandingBuffer(buffer)) {
586 ALOGE("%s: Stream %d: Returning an unknown buffer.", __FUNCTION__, mId);
587 return BAD_VALUE;
588 }
589
Igor Murashkin2fba5842013-04-22 14:03:54 -0700590 status_t res = returnInputBufferLocked(buffer);
591 if (res == OK) {
592 fireBufferListenersLocked(buffer, /*acquired*/false, /*output*/false);
Zhijun He6adc9cc2014-04-15 14:09:55 -0700593 mInputBufferReturnedSignal.signal();
Igor Murashkin2fba5842013-04-22 14:03:54 -0700594 }
Chien-Yu Chene02e9322016-04-11 16:59:33 -0700595
596 removeOutstandingBuffer(buffer);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700597 return res;
598}
599
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700600status_t Camera3Stream::getInputBufferProducer(sp<IGraphicBufferProducer> *producer) {
601 ATRACE_CALL();
602 Mutex::Autolock l(mLock);
603
604 return getInputBufferProducerLocked(producer);
605}
606
Igor Murashkin2fba5842013-04-22 14:03:54 -0700607void Camera3Stream::fireBufferListenersLocked(
Shuzhen Wangb0fdc1e2016-03-20 23:21:39 -0700608 const camera3_stream_buffer& buffer, bool acquired, bool output) {
Igor Murashkin2fba5842013-04-22 14:03:54 -0700609 List<wp<Camera3StreamBufferListener> >::iterator it, end;
610
611 // TODO: finish implementing
612
613 Camera3StreamBufferListener::BufferInfo info =
614 Camera3StreamBufferListener::BufferInfo();
615 info.mOutput = output;
Shuzhen Wangb0fdc1e2016-03-20 23:21:39 -0700616 info.mError = (buffer.status == CAMERA3_BUFFER_STATUS_ERROR);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700617 // TODO: rest of fields
618
619 for (it = mBufferListenerList.begin(), end = mBufferListenerList.end();
620 it != end;
621 ++it) {
622
623 sp<Camera3StreamBufferListener> listener = it->promote();
624 if (listener != 0) {
625 if (acquired) {
626 listener->onBufferAcquired(info);
627 } else {
628 listener->onBufferReleased(info);
629 }
630 }
631 }
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700632}
633
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800634bool Camera3Stream::hasOutstandingBuffers() const {
635 ATRACE_CALL();
636 Mutex::Autolock l(mLock);
637 return hasOutstandingBuffersLocked();
638}
639
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700640status_t Camera3Stream::setStatusTracker(sp<StatusTracker> statusTracker) {
641 Mutex::Autolock l(mLock);
642 sp<StatusTracker> oldTracker = mStatusTracker.promote();
643 if (oldTracker != 0 && mStatusId != StatusTracker::NO_STATUS_ID) {
644 oldTracker->removeComponent(mStatusId);
645 }
646 mStatusId = StatusTracker::NO_STATUS_ID;
647 mStatusTracker = statusTracker;
648
649 return OK;
650}
651
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800652status_t Camera3Stream::disconnect() {
653 ATRACE_CALL();
654 Mutex::Autolock l(mLock);
Igor Murashkine2172be2013-05-28 15:31:39 -0700655 ALOGV("%s: Stream %d: Disconnecting...", __FUNCTION__, mId);
656 status_t res = disconnectLocked();
657
658 if (res == -ENOTCONN) {
659 // "Already disconnected" -- not an error
660 return OK;
661 } else {
662 return res;
663 }
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800664}
665
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800666status_t Camera3Stream::getBufferLocked(camera3_stream_buffer *,
667 const std::vector<size_t>&) {
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700668 ALOGE("%s: This type of stream does not support output", __FUNCTION__);
669 return INVALID_OPERATION;
670}
671status_t Camera3Stream::returnBufferLocked(const camera3_stream_buffer &,
672 nsecs_t) {
673 ALOGE("%s: This type of stream does not support output", __FUNCTION__);
674 return INVALID_OPERATION;
675}
676status_t Camera3Stream::getInputBufferLocked(camera3_stream_buffer *) {
677 ALOGE("%s: This type of stream does not support input", __FUNCTION__);
678 return INVALID_OPERATION;
679}
680status_t Camera3Stream::returnInputBufferLocked(
681 const camera3_stream_buffer &) {
682 ALOGE("%s: This type of stream does not support input", __FUNCTION__);
683 return INVALID_OPERATION;
684}
Eino-Ville Talvala02bf0322016-02-18 12:41:10 -0800685status_t Camera3Stream::getInputBufferProducerLocked(sp<IGraphicBufferProducer>*) {
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700686 ALOGE("%s: This type of stream does not support input", __FUNCTION__);
687 return INVALID_OPERATION;
688}
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700689
Igor Murashkin2fba5842013-04-22 14:03:54 -0700690void Camera3Stream::addBufferListener(
691 wp<Camera3StreamBufferListener> listener) {
692 Mutex::Autolock l(mLock);
Zhijun Hef0d962a2014-06-30 10:24:11 -0700693
694 List<wp<Camera3StreamBufferListener> >::iterator it, end;
695 for (it = mBufferListenerList.begin(), end = mBufferListenerList.end();
696 it != end;
697 ) {
698 if (*it == listener) {
699 ALOGE("%s: Try to add the same listener twice, ignoring...", __FUNCTION__);
700 return;
701 }
702 it++;
703 }
704
Igor Murashkin2fba5842013-04-22 14:03:54 -0700705 mBufferListenerList.push_back(listener);
706}
707
708void Camera3Stream::removeBufferListener(
709 const sp<Camera3StreamBufferListener>& listener) {
710 Mutex::Autolock l(mLock);
711
712 bool erased = true;
713 List<wp<Camera3StreamBufferListener> >::iterator it, end;
714 for (it = mBufferListenerList.begin(), end = mBufferListenerList.end();
715 it != end;
716 ) {
717
718 if (*it == listener) {
719 it = mBufferListenerList.erase(it);
720 erased = true;
721 } else {
722 ++it;
723 }
724 }
725
726 if (!erased) {
727 ALOGW("%s: Could not find listener to remove, already removed",
728 __FUNCTION__);
729 }
730}
731
Yin-Chia Yehbe83fa72017-03-30 13:35:36 -0700732void Camera3Stream::setBufferFreedListener(
733 Camera3StreamBufferFreedListener* listener) {
734 Mutex::Autolock l(mLock);
735 // Only allow set listener during stream configuration because stream is guaranteed to be IDLE
736 // at this state, so setBufferFreedListener won't collide with onBufferFreed callbacks
737 if (mState != STATE_IN_CONFIG && mState != STATE_IN_RECONFIG) {
738 ALOGE("%s: listener must be set during stream configuration!",__FUNCTION__);
739 return;
740 }
741 mBufferFreedListener = listener;
742}
743
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800744}; // namespace camera3
745
746}; // namespace android