blob: 25e44a5c19d4b463e2556196c586a2e793694ca9 [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),
65 mBufferLimitLatency(kBufferLimitLatencyBinSize) {
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -080066
67 camera3_stream::stream_type = type;
68 camera3_stream::width = width;
69 camera3_stream::height = height;
70 camera3_stream::format = format;
Eino-Ville Talvala3d82c0d2015-02-23 15:19:19 -080071 camera3_stream::data_space = dataSpace;
Yin-Chia Yehb97babb2015-03-12 13:42:44 -070072 camera3_stream::rotation = rotation;
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -080073 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
Emilian Peev050f5dc2017-05-18 14:43:56 +0100107uint64_t Camera3Stream::getUsage() const {
108 return mUsage;
109}
110
111void Camera3Stream::setUsage(uint64_t usage) {
112 mUsage = usage;
113}
114
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800115camera3_stream* Camera3Stream::startConfiguration() {
Eino-Ville Talvalab2f5b192013-07-30 14:36:03 -0700116 ATRACE_CALL();
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800117 Mutex::Autolock l(mLock);
Eino-Ville Talvalab2f5b192013-07-30 14:36:03 -0700118 status_t res;
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800119
120 switch (mState) {
121 case STATE_ERROR:
122 ALOGE("%s: In error state", __FUNCTION__);
123 return NULL;
124 case STATE_CONSTRUCTED:
125 // OK
126 break;
127 case STATE_IN_CONFIG:
128 case STATE_IN_RECONFIG:
129 // Can start config again with no trouble; but don't redo
Eino-Ville Talvala02bf0322016-02-18 12:41:10 -0800130 // mOldUsage/mOldMaxBuffers
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800131 return this;
132 case STATE_CONFIGURED:
Chien-Yu Chen90746f42015-04-15 13:50:13 -0700133 if (hasOutstandingBuffersLocked()) {
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800134 ALOGE("%s: Cannot configure stream; has outstanding buffers",
135 __FUNCTION__);
136 return NULL;
137 }
138 break;
139 default:
140 ALOGE("%s: Unknown state %d", __FUNCTION__, mState);
141 return NULL;
142 }
143
Emilian Peev050f5dc2017-05-18 14:43:56 +0100144 mOldUsage = mUsage;
Eino-Ville Talvala02bf0322016-02-18 12:41:10 -0800145 mOldMaxBuffers = camera3_stream::max_buffers;
Eino-Ville Talvalab2f5b192013-07-30 14:36:03 -0700146
Emilian Peev050f5dc2017-05-18 14:43:56 +0100147 res = getEndpointUsage(&mUsage);
Eino-Ville Talvalab2f5b192013-07-30 14:36:03 -0700148 if (res != OK) {
149 ALOGE("%s: Cannot query consumer endpoint usage!",
150 __FUNCTION__);
151 return NULL;
152 }
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800153
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700154 // Stop tracking if currently doing so
155 if (mStatusId != StatusTracker::NO_STATUS_ID) {
156 sp<StatusTracker> statusTracker = mStatusTracker.promote();
157 if (statusTracker != 0) {
158 statusTracker->removeComponent(mStatusId);
159 }
160 mStatusId = StatusTracker::NO_STATUS_ID;
161 }
162
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800163 if (mState == STATE_CONSTRUCTED) {
164 mState = STATE_IN_CONFIG;
165 } else { // mState == STATE_CONFIGURED
Igor Murashkin13d315e2014-04-03 18:09:04 -0700166 LOG_ALWAYS_FATAL_IF(mState != STATE_CONFIGURED, "Invalid state: 0x%x", mState);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800167 mState = STATE_IN_RECONFIG;
168 }
169
170 return this;
171}
172
173bool Camera3Stream::isConfiguring() const {
174 Mutex::Autolock l(mLock);
175 return (mState == STATE_IN_CONFIG) || (mState == STATE_IN_RECONFIG);
176}
177
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800178status_t Camera3Stream::finishConfiguration() {
Eino-Ville Talvalab2f5b192013-07-30 14:36:03 -0700179 ATRACE_CALL();
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800180 Mutex::Autolock l(mLock);
181 switch (mState) {
182 case STATE_ERROR:
183 ALOGE("%s: In error state", __FUNCTION__);
184 return INVALID_OPERATION;
185 case STATE_IN_CONFIG:
186 case STATE_IN_RECONFIG:
187 // OK
188 break;
189 case STATE_CONSTRUCTED:
190 case STATE_CONFIGURED:
191 ALOGE("%s: Cannot finish configuration that hasn't been started",
192 __FUNCTION__);
193 return INVALID_OPERATION;
194 default:
195 ALOGE("%s: Unknown state", __FUNCTION__);
196 return INVALID_OPERATION;
197 }
198
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700199 // Register for idle tracking
200 sp<StatusTracker> statusTracker = mStatusTracker.promote();
201 if (statusTracker != 0) {
202 mStatusId = statusTracker->addComponent();
203 }
204
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800205 // Check if the stream configuration is unchanged, and skip reallocation if
206 // so. As documented in hardware/camera3.h:configure_streams().
207 if (mState == STATE_IN_RECONFIG &&
Emilian Peev050f5dc2017-05-18 14:43:56 +0100208 mOldUsage == mUsage &&
Eino-Ville Talvala02bf0322016-02-18 12:41:10 -0800209 mOldMaxBuffers == camera3_stream::max_buffers) {
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800210 mState = STATE_CONFIGURED;
211 return OK;
212 }
213
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700214 // Reset prepared state, since buffer config has changed, and existing
215 // allocations are no longer valid
216 mPrepared = false;
217 mStreamUnpreparable = false;
218
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800219 status_t res;
220 res = configureQueueLocked();
221 if (res != OK) {
222 ALOGE("%s: Unable to configure stream %d queue: %s (%d)",
223 __FUNCTION__, mId, strerror(-res), res);
224 mState = STATE_ERROR;
225 return res;
226 }
227
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800228 mState = STATE_CONFIGURED;
229
230 return res;
231}
232
Eino-Ville Talvala17543512014-08-06 14:32:02 -0700233status_t Camera3Stream::cancelConfiguration() {
234 ATRACE_CALL();
235 Mutex::Autolock l(mLock);
236 switch (mState) {
237 case STATE_ERROR:
238 ALOGE("%s: In error state", __FUNCTION__);
239 return INVALID_OPERATION;
240 case STATE_IN_CONFIG:
241 case STATE_IN_RECONFIG:
242 // OK
243 break;
244 case STATE_CONSTRUCTED:
245 case STATE_CONFIGURED:
246 ALOGE("%s: Cannot cancel configuration that hasn't been started",
247 __FUNCTION__);
248 return INVALID_OPERATION;
249 default:
250 ALOGE("%s: Unknown state", __FUNCTION__);
251 return INVALID_OPERATION;
252 }
253
Emilian Peev050f5dc2017-05-18 14:43:56 +0100254 mUsage = mOldUsage;
Eino-Ville Talvala02bf0322016-02-18 12:41:10 -0800255 camera3_stream::max_buffers = mOldMaxBuffers;
Eino-Ville Talvala17543512014-08-06 14:32:02 -0700256
Yin-Chia Yeh3ea3fcd2014-09-05 14:14:44 -0700257 mState = (mState == STATE_IN_RECONFIG) ? STATE_CONFIGURED : STATE_CONSTRUCTED;
Eino-Ville Talvala17543512014-08-06 14:32:02 -0700258 return OK;
259}
260
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700261bool Camera3Stream::isUnpreparable() {
262 ATRACE_CALL();
263
264 Mutex::Autolock l(mLock);
265 return mStreamUnpreparable;
266}
267
Ruben Brunkc78ac262015-08-13 17:58:46 -0700268status_t Camera3Stream::startPrepare(int maxCount) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700269 ATRACE_CALL();
270
271 Mutex::Autolock l(mLock);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700272
Ruben Brunkc78ac262015-08-13 17:58:46 -0700273 if (maxCount < 0) {
274 ALOGE("%s: Stream %d: Can't prepare stream if max buffer count (%d) is < 0",
275 __FUNCTION__, mId, maxCount);
276 return BAD_VALUE;
277 }
278
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700279 // This function should be only called when the stream is configured already.
280 if (mState != STATE_CONFIGURED) {
281 ALOGE("%s: Stream %d: Can't prepare stream if stream is not in CONFIGURED "
282 "state %d", __FUNCTION__, mId, mState);
283 return INVALID_OPERATION;
284 }
285
286 // This function can't be called if the stream has already received filled
287 // buffers
288 if (mStreamUnpreparable) {
289 ALOGE("%s: Stream %d: Can't prepare stream that's already in use",
290 __FUNCTION__, mId);
291 return INVALID_OPERATION;
292 }
293
294 if (getHandoutOutputBufferCountLocked() > 0) {
295 ALOGE("%s: Stream %d: Can't prepare stream that has outstanding buffers",
296 __FUNCTION__, mId);
297 return INVALID_OPERATION;
298 }
299
Ruben Brunkc78ac262015-08-13 17:58:46 -0700300
301
302 size_t pipelineMax = getBufferCountLocked();
303 size_t clampedCount = (pipelineMax < static_cast<size_t>(maxCount)) ?
304 pipelineMax : static_cast<size_t>(maxCount);
305 size_t bufferCount = (maxCount == Camera3StreamInterface::ALLOCATE_PIPELINE_MAX) ?
306 pipelineMax : clampedCount;
307
308 mPrepared = bufferCount <= mLastMaxCount;
309
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700310 if (mPrepared) return OK;
311
Ruben Brunkc78ac262015-08-13 17:58:46 -0700312 mLastMaxCount = bufferCount;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700313
314 mPreparedBuffers.insertAt(camera3_stream_buffer_t(), /*index*/0, bufferCount);
315 mPreparedBufferIdx = 0;
316
317 mState = STATE_PREPARING;
318
319 return NOT_ENOUGH_DATA;
320}
321
322bool Camera3Stream::isPreparing() const {
323 Mutex::Autolock l(mLock);
324 return mState == STATE_PREPARING;
325}
326
Chien-Yu Chene8c535e2016-04-14 12:18:26 -0700327bool Camera3Stream::isAbandoned() const {
328 Mutex::Autolock l(mLock);
329 return mState == STATE_ABANDONED;
330}
331
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700332status_t Camera3Stream::prepareNextBuffer() {
333 ATRACE_CALL();
334
335 Mutex::Autolock l(mLock);
336 status_t res = OK;
337
338 // This function should be only called when the stream is preparing
339 if (mState != STATE_PREPARING) {
340 ALOGE("%s: Stream %d: Can't prepare buffer if stream is not in PREPARING "
341 "state %d", __FUNCTION__, mId, mState);
342 return INVALID_OPERATION;
343 }
344
345 // Get next buffer - this may allocate, and take a while for large buffers
346 res = getBufferLocked( &mPreparedBuffers.editItemAt(mPreparedBufferIdx) );
347 if (res != OK) {
Eino-Ville Talvala02bf0322016-02-18 12:41:10 -0800348 ALOGE("%s: Stream %d: Unable to allocate buffer %zu during preparation",
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700349 __FUNCTION__, mId, mPreparedBufferIdx);
350 return NO_INIT;
351 }
352
353 mPreparedBufferIdx++;
354
355 // Check if we still have buffers left to allocate
356 if (mPreparedBufferIdx < mPreparedBuffers.size()) {
357 return NOT_ENOUGH_DATA;
358 }
359
360 // Done with prepare - mark stream as such, and return all buffers
361 // via cancelPrepare
362 mPrepared = true;
363
364 return cancelPrepareLocked();
365}
366
367status_t Camera3Stream::cancelPrepare() {
368 ATRACE_CALL();
369
370 Mutex::Autolock l(mLock);
371
372 return cancelPrepareLocked();
373}
374
375status_t Camera3Stream::cancelPrepareLocked() {
376 status_t res = OK;
377
378 // This function should be only called when the stream is mid-preparing.
379 if (mState != STATE_PREPARING) {
380 ALOGE("%s: Stream %d: Can't cancel prepare stream if stream is not in "
381 "PREPARING state %d", __FUNCTION__, mId, mState);
382 return INVALID_OPERATION;
383 }
384
385 // Return all valid buffers to stream, in ERROR state to indicate
386 // they weren't filled.
387 for (size_t i = 0; i < mPreparedBufferIdx; i++) {
388 mPreparedBuffers.editItemAt(i).release_fence = -1;
389 mPreparedBuffers.editItemAt(i).status = CAMERA3_BUFFER_STATUS_ERROR;
390 returnBufferLocked(mPreparedBuffers[i], 0);
391 }
392 mPreparedBuffers.clear();
393 mPreparedBufferIdx = 0;
394
395 mState = STATE_CONFIGURED;
396
397 return res;
398}
399
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -0700400status_t Camera3Stream::tearDown() {
401 ATRACE_CALL();
402 Mutex::Autolock l(mLock);
403
404 status_t res = OK;
405
406 // This function should be only called when the stream is configured.
407 if (mState != STATE_CONFIGURED) {
408 ALOGE("%s: Stream %d: Can't tear down stream if stream is not in "
409 "CONFIGURED state %d", __FUNCTION__, mId, mState);
410 return INVALID_OPERATION;
411 }
412
413 // If any buffers have been handed to the HAL, the stream cannot be torn down.
414 if (getHandoutOutputBufferCountLocked() > 0) {
415 ALOGE("%s: Stream %d: Can't tear down a stream that has outstanding buffers",
416 __FUNCTION__, mId);
417 return INVALID_OPERATION;
418 }
419
420 // Free buffers by disconnecting and then reconnecting to the buffer queue
421 // Only unused buffers will be dropped immediately; buffers that have been filled
422 // and are waiting to be acquired by the consumer and buffers that are currently
423 // acquired will be freed once they are released by the consumer.
424
425 res = disconnectLocked();
426 if (res != OK) {
427 if (res == -ENOTCONN) {
428 // queue has been disconnected, nothing left to do, so exit with success
429 return OK;
430 }
431 ALOGE("%s: Stream %d: Unable to disconnect to tear down buffers: %s (%d)",
432 __FUNCTION__, mId, strerror(-res), res);
433 return res;
434 }
435
436 mState = STATE_IN_CONFIG;
437
438 res = configureQueueLocked();
439 if (res != OK) {
440 ALOGE("%s: Unable to configure stream %d queue: %s (%d)",
441 __FUNCTION__, mId, strerror(-res), res);
442 mState = STATE_ERROR;
443 return res;
444 }
445
446 // Reset prepared state, since we've reconnected to the queue and can prepare again.
447 mPrepared = false;
448 mStreamUnpreparable = false;
449
450 mState = STATE_CONFIGURED;
451
452 return OK;
453}
454
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800455status_t Camera3Stream::getBuffer(camera3_stream_buffer *buffer,
456 const std::vector<size_t>& surface_ids) {
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800457 ATRACE_CALL();
458 Mutex::Autolock l(mLock);
Zhijun He6adc9cc2014-04-15 14:09:55 -0700459 status_t res = OK;
Igor Murashkin2fba5842013-04-22 14:03:54 -0700460
Zhijun He6adc9cc2014-04-15 14:09:55 -0700461 // This function should be only called when the stream is configured already.
462 if (mState != STATE_CONFIGURED) {
463 ALOGE("%s: Stream %d: Can't get buffers if stream is not in CONFIGURED state %d",
464 __FUNCTION__, mId, mState);
465 return INVALID_OPERATION;
466 }
467
468 // Wait for new buffer returned back if we are running into the limit.
469 if (getHandoutOutputBufferCountLocked() == camera3_stream::max_buffers) {
470 ALOGV("%s: Already dequeued max output buffers (%d), wait for next returned one.",
Shuzhen Wang686f6442017-06-20 16:16:04 -0700471 __FUNCTION__, camera3_stream::max_buffers);
472 nsecs_t waitStart = systemTime(SYSTEM_TIME_MONOTONIC);
Zhijun He6adc9cc2014-04-15 14:09:55 -0700473 res = mOutputBufferReturnedSignal.waitRelative(mLock, kWaitForBufferDuration);
Shuzhen Wang686f6442017-06-20 16:16:04 -0700474 nsecs_t waitEnd = systemTime(SYSTEM_TIME_MONOTONIC);
475 mBufferLimitLatency.add(waitStart, waitEnd);
Zhijun He6adc9cc2014-04-15 14:09:55 -0700476 if (res != OK) {
477 if (res == TIMED_OUT) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -0700478 ALOGE("%s: wait for output buffer return timed out after %lldms (max_buffers %d)",
479 __FUNCTION__, kWaitForBufferDuration / 1000000LL,
480 camera3_stream::max_buffers);
Zhijun He6adc9cc2014-04-15 14:09:55 -0700481 }
482 return res;
483 }
484 }
485
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800486 res = getBufferLocked(buffer, surface_ids);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700487 if (res == OK) {
488 fireBufferListenersLocked(*buffer, /*acquired*/true, /*output*/true);
Chien-Yu Chene02e9322016-04-11 16:59:33 -0700489 if (buffer->buffer) {
Emilian Peev889234d2017-07-18 18:21:26 -0700490 Mutex::Autolock l(mOutstandingBuffersLock);
Chien-Yu Chene02e9322016-04-11 16:59:33 -0700491 mOutstandingBuffers.push_back(*buffer->buffer);
492 }
Igor Murashkin2fba5842013-04-22 14:03:54 -0700493 }
494
495 return res;
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800496}
497
Emilian Peev889234d2017-07-18 18:21:26 -0700498bool Camera3Stream::isOutstandingBuffer(const camera3_stream_buffer &buffer) const{
Chien-Yu Chene02e9322016-04-11 16:59:33 -0700499 if (buffer.buffer == nullptr) {
500 return false;
501 }
502
Emilian Peev889234d2017-07-18 18:21:26 -0700503 Mutex::Autolock l(mOutstandingBuffersLock);
504
Chien-Yu Chene02e9322016-04-11 16:59:33 -0700505 for (auto b : mOutstandingBuffers) {
506 if (b == *buffer.buffer) {
507 return true;
508 }
509 }
510 return false;
511}
512
513void Camera3Stream::removeOutstandingBuffer(const camera3_stream_buffer &buffer) {
514 if (buffer.buffer == nullptr) {
515 return;
516 }
517
Emilian Peev889234d2017-07-18 18:21:26 -0700518 Mutex::Autolock l(mOutstandingBuffersLock);
519
Chien-Yu Chene02e9322016-04-11 16:59:33 -0700520 for (auto b = mOutstandingBuffers.begin(); b != mOutstandingBuffers.end(); b++) {
521 if (*b == *buffer.buffer) {
522 mOutstandingBuffers.erase(b);
523 return;
524 }
525 }
526}
527
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800528status_t Camera3Stream::returnBuffer(const camera3_stream_buffer &buffer,
529 nsecs_t timestamp) {
530 ATRACE_CALL();
531 Mutex::Autolock l(mLock);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700532
Chien-Yu Chene02e9322016-04-11 16:59:33 -0700533 // Check if this buffer is outstanding.
534 if (!isOutstandingBuffer(buffer)) {
535 ALOGE("%s: Stream %d: Returning an unknown buffer.", __FUNCTION__, mId);
536 return BAD_VALUE;
537 }
538
Shuzhen Wang1c484a62017-07-14 15:14:19 -0700539 removeOutstandingBuffer(buffer);
540
Igor Murashkin13d315e2014-04-03 18:09:04 -0700541 /**
542 * TODO: Check that the state is valid first.
543 *
544 * <HAL3.2 IN_CONFIG and IN_RECONFIG in addition to CONFIGURED.
545 * >= HAL3.2 CONFIGURED only
546 *
547 * Do this for getBuffer as well.
548 */
Igor Murashkin2fba5842013-04-22 14:03:54 -0700549 status_t res = returnBufferLocked(buffer, timestamp);
550 if (res == OK) {
551 fireBufferListenersLocked(buffer, /*acquired*/false, /*output*/true);
552 }
553
Chien-Yu Chenb83c1fe2015-09-10 16:15:21 -0700554 // Even if returning the buffer failed, we still want to signal whoever is waiting for the
555 // buffer to be returned.
556 mOutputBufferReturnedSignal.signal();
557
Igor Murashkin2fba5842013-04-22 14:03:54 -0700558 return res;
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800559}
560
Eino-Ville Talvalaba435252017-06-21 16:07:25 -0700561status_t Camera3Stream::getInputBuffer(camera3_stream_buffer *buffer, bool respectHalLimit) {
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700562 ATRACE_CALL();
563 Mutex::Autolock l(mLock);
Zhijun He6adc9cc2014-04-15 14:09:55 -0700564 status_t res = OK;
Igor Murashkin2fba5842013-04-22 14:03:54 -0700565
Zhijun He6adc9cc2014-04-15 14:09:55 -0700566 // This function should be only called when the stream is configured already.
567 if (mState != STATE_CONFIGURED) {
568 ALOGE("%s: Stream %d: Can't get input buffers if stream is not in CONFIGURED state %d",
569 __FUNCTION__, mId, mState);
570 return INVALID_OPERATION;
571 }
572
573 // Wait for new buffer returned back if we are running into the limit.
Eino-Ville Talvalaba435252017-06-21 16:07:25 -0700574 if (getHandoutInputBufferCountLocked() == camera3_stream::max_buffers && respectHalLimit) {
Zhijun He6adc9cc2014-04-15 14:09:55 -0700575 ALOGV("%s: Already dequeued max input buffers (%d), wait for next returned one.",
576 __FUNCTION__, camera3_stream::max_buffers);
577 res = mInputBufferReturnedSignal.waitRelative(mLock, kWaitForBufferDuration);
578 if (res != OK) {
579 if (res == TIMED_OUT) {
580 ALOGE("%s: wait for input buffer return timed out after %lldms", __FUNCTION__,
581 kWaitForBufferDuration / 1000000LL);
582 }
583 return res;
584 }
585 }
586
587 res = getInputBufferLocked(buffer);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700588 if (res == OK) {
589 fireBufferListenersLocked(*buffer, /*acquired*/true, /*output*/false);
Chien-Yu Chene02e9322016-04-11 16:59:33 -0700590 if (buffer->buffer) {
Emilian Peev889234d2017-07-18 18:21:26 -0700591 Mutex::Autolock l(mOutstandingBuffersLock);
Chien-Yu Chene02e9322016-04-11 16:59:33 -0700592 mOutstandingBuffers.push_back(*buffer->buffer);
593 }
Igor Murashkin2fba5842013-04-22 14:03:54 -0700594 }
595
596 return res;
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700597}
598
599status_t Camera3Stream::returnInputBuffer(const camera3_stream_buffer &buffer) {
600 ATRACE_CALL();
601 Mutex::Autolock l(mLock);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700602
Chien-Yu Chene02e9322016-04-11 16:59:33 -0700603 // Check if this buffer is outstanding.
604 if (!isOutstandingBuffer(buffer)) {
605 ALOGE("%s: Stream %d: Returning an unknown buffer.", __FUNCTION__, mId);
606 return BAD_VALUE;
607 }
608
Shuzhen Wang1c484a62017-07-14 15:14:19 -0700609 removeOutstandingBuffer(buffer);
610
Igor Murashkin2fba5842013-04-22 14:03:54 -0700611 status_t res = returnInputBufferLocked(buffer);
612 if (res == OK) {
613 fireBufferListenersLocked(buffer, /*acquired*/false, /*output*/false);
Zhijun He6adc9cc2014-04-15 14:09:55 -0700614 mInputBufferReturnedSignal.signal();
Igor Murashkin2fba5842013-04-22 14:03:54 -0700615 }
Chien-Yu Chene02e9322016-04-11 16:59:33 -0700616
Igor Murashkin2fba5842013-04-22 14:03:54 -0700617 return res;
618}
619
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700620status_t Camera3Stream::getInputBufferProducer(sp<IGraphicBufferProducer> *producer) {
621 ATRACE_CALL();
622 Mutex::Autolock l(mLock);
623
624 return getInputBufferProducerLocked(producer);
625}
626
Igor Murashkin2fba5842013-04-22 14:03:54 -0700627void Camera3Stream::fireBufferListenersLocked(
Shuzhen Wangb0fdc1e2016-03-20 23:21:39 -0700628 const camera3_stream_buffer& buffer, bool acquired, bool output) {
Igor Murashkin2fba5842013-04-22 14:03:54 -0700629 List<wp<Camera3StreamBufferListener> >::iterator it, end;
630
631 // TODO: finish implementing
632
633 Camera3StreamBufferListener::BufferInfo info =
634 Camera3StreamBufferListener::BufferInfo();
635 info.mOutput = output;
Shuzhen Wangb0fdc1e2016-03-20 23:21:39 -0700636 info.mError = (buffer.status == CAMERA3_BUFFER_STATUS_ERROR);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700637 // TODO: rest of fields
638
639 for (it = mBufferListenerList.begin(), end = mBufferListenerList.end();
640 it != end;
641 ++it) {
642
643 sp<Camera3StreamBufferListener> listener = it->promote();
644 if (listener != 0) {
645 if (acquired) {
646 listener->onBufferAcquired(info);
647 } else {
648 listener->onBufferReleased(info);
649 }
650 }
651 }
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700652}
653
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800654bool Camera3Stream::hasOutstandingBuffers() const {
655 ATRACE_CALL();
656 Mutex::Autolock l(mLock);
657 return hasOutstandingBuffersLocked();
658}
659
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700660status_t Camera3Stream::setStatusTracker(sp<StatusTracker> statusTracker) {
661 Mutex::Autolock l(mLock);
662 sp<StatusTracker> oldTracker = mStatusTracker.promote();
663 if (oldTracker != 0 && mStatusId != StatusTracker::NO_STATUS_ID) {
664 oldTracker->removeComponent(mStatusId);
665 }
666 mStatusId = StatusTracker::NO_STATUS_ID;
667 mStatusTracker = statusTracker;
668
669 return OK;
670}
671
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800672status_t Camera3Stream::disconnect() {
673 ATRACE_CALL();
674 Mutex::Autolock l(mLock);
Igor Murashkine2172be2013-05-28 15:31:39 -0700675 ALOGV("%s: Stream %d: Disconnecting...", __FUNCTION__, mId);
676 status_t res = disconnectLocked();
677
Shuzhen Wang686f6442017-06-20 16:16:04 -0700678 mBufferLimitLatency.log("Stream %d latency histogram for wait on max_buffers", mId);
679 mBufferLimitLatency.reset();
680
Igor Murashkine2172be2013-05-28 15:31:39 -0700681 if (res == -ENOTCONN) {
682 // "Already disconnected" -- not an error
683 return OK;
684 } else {
685 return res;
686 }
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800687}
688
Shuzhen Wang686f6442017-06-20 16:16:04 -0700689void Camera3Stream::dump(int fd, const Vector<String16> &args) const
690{
691 (void)args;
692 mBufferLimitLatency.dump(fd,
693 " Latency histogram for wait on max_buffers");
694}
695
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800696status_t Camera3Stream::getBufferLocked(camera3_stream_buffer *,
697 const std::vector<size_t>&) {
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700698 ALOGE("%s: This type of stream does not support output", __FUNCTION__);
699 return INVALID_OPERATION;
700}
701status_t Camera3Stream::returnBufferLocked(const camera3_stream_buffer &,
702 nsecs_t) {
703 ALOGE("%s: This type of stream does not support output", __FUNCTION__);
704 return INVALID_OPERATION;
705}
706status_t Camera3Stream::getInputBufferLocked(camera3_stream_buffer *) {
707 ALOGE("%s: This type of stream does not support input", __FUNCTION__);
708 return INVALID_OPERATION;
709}
710status_t Camera3Stream::returnInputBufferLocked(
711 const camera3_stream_buffer &) {
712 ALOGE("%s: This type of stream does not support input", __FUNCTION__);
713 return INVALID_OPERATION;
714}
Eino-Ville Talvala02bf0322016-02-18 12:41:10 -0800715status_t Camera3Stream::getInputBufferProducerLocked(sp<IGraphicBufferProducer>*) {
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700716 ALOGE("%s: This type of stream does not support input", __FUNCTION__);
717 return INVALID_OPERATION;
718}
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700719
Igor Murashkin2fba5842013-04-22 14:03:54 -0700720void Camera3Stream::addBufferListener(
721 wp<Camera3StreamBufferListener> listener) {
722 Mutex::Autolock l(mLock);
Zhijun Hef0d962a2014-06-30 10:24:11 -0700723
724 List<wp<Camera3StreamBufferListener> >::iterator it, end;
725 for (it = mBufferListenerList.begin(), end = mBufferListenerList.end();
726 it != end;
727 ) {
728 if (*it == listener) {
729 ALOGE("%s: Try to add the same listener twice, ignoring...", __FUNCTION__);
730 return;
731 }
732 it++;
733 }
734
Igor Murashkin2fba5842013-04-22 14:03:54 -0700735 mBufferListenerList.push_back(listener);
736}
737
738void Camera3Stream::removeBufferListener(
739 const sp<Camera3StreamBufferListener>& listener) {
740 Mutex::Autolock l(mLock);
741
742 bool erased = true;
743 List<wp<Camera3StreamBufferListener> >::iterator it, end;
744 for (it = mBufferListenerList.begin(), end = mBufferListenerList.end();
745 it != end;
746 ) {
747
748 if (*it == listener) {
749 it = mBufferListenerList.erase(it);
750 erased = true;
751 } else {
752 ++it;
753 }
754 }
755
756 if (!erased) {
757 ALOGW("%s: Could not find listener to remove, already removed",
758 __FUNCTION__);
759 }
760}
761
Yin-Chia Yehbe83fa72017-03-30 13:35:36 -0700762void Camera3Stream::setBufferFreedListener(
Yin-Chia Yehdb1e8642017-07-14 15:19:30 -0700763 wp<Camera3StreamBufferFreedListener> listener) {
Yin-Chia Yehbe83fa72017-03-30 13:35:36 -0700764 Mutex::Autolock l(mLock);
765 // Only allow set listener during stream configuration because stream is guaranteed to be IDLE
766 // at this state, so setBufferFreedListener won't collide with onBufferFreed callbacks
767 if (mState != STATE_IN_CONFIG && mState != STATE_IN_RECONFIG) {
768 ALOGE("%s: listener must be set during stream configuration!",__FUNCTION__);
769 return;
770 }
771 mBufferFreedListener = listener;
772}
773
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800774}; // namespace camera3
775
776}; // namespace android