blob: 3ffd9d1f50286dad29af81b98194de100465b83f [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
169status_t Camera3Stream::finishConfiguration(camera3_device *hal3Device) {
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
219 res = registerBuffersLocked(hal3Device);
220 if (res != OK) {
221 ALOGE("%s: Unable to register stream buffers with HAL: %s (%d)",
222 __FUNCTION__, strerror(-res), res);
223 mState = STATE_ERROR;
224 return res;
225 }
226
227 mState = STATE_CONFIGURED;
228
229 return res;
230}
231
Eino-Ville Talvala17543512014-08-06 14:32:02 -0700232status_t Camera3Stream::cancelConfiguration() {
233 ATRACE_CALL();
234 Mutex::Autolock l(mLock);
235 switch (mState) {
236 case STATE_ERROR:
237 ALOGE("%s: In error state", __FUNCTION__);
238 return INVALID_OPERATION;
239 case STATE_IN_CONFIG:
240 case STATE_IN_RECONFIG:
241 // OK
242 break;
243 case STATE_CONSTRUCTED:
244 case STATE_CONFIGURED:
245 ALOGE("%s: Cannot cancel configuration that hasn't been started",
246 __FUNCTION__);
247 return INVALID_OPERATION;
248 default:
249 ALOGE("%s: Unknown state", __FUNCTION__);
250 return INVALID_OPERATION;
251 }
252
Eino-Ville Talvala02bf0322016-02-18 12:41:10 -0800253 camera3_stream::usage = mOldUsage;
254 camera3_stream::max_buffers = mOldMaxBuffers;
Eino-Ville Talvala17543512014-08-06 14:32:02 -0700255
Yin-Chia Yeh3ea3fcd2014-09-05 14:14:44 -0700256 mState = (mState == STATE_IN_RECONFIG) ? STATE_CONFIGURED : STATE_CONSTRUCTED;
Eino-Ville Talvala17543512014-08-06 14:32:02 -0700257 return OK;
258}
259
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700260bool Camera3Stream::isUnpreparable() {
261 ATRACE_CALL();
262
263 Mutex::Autolock l(mLock);
264 return mStreamUnpreparable;
265}
266
Ruben Brunkc78ac262015-08-13 17:58:46 -0700267status_t Camera3Stream::startPrepare(int maxCount) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700268 ATRACE_CALL();
269
270 Mutex::Autolock l(mLock);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700271
Ruben Brunkc78ac262015-08-13 17:58:46 -0700272 if (maxCount < 0) {
273 ALOGE("%s: Stream %d: Can't prepare stream if max buffer count (%d) is < 0",
274 __FUNCTION__, mId, maxCount);
275 return BAD_VALUE;
276 }
277
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700278 // This function should be only called when the stream is configured already.
279 if (mState != STATE_CONFIGURED) {
280 ALOGE("%s: Stream %d: Can't prepare stream if stream is not in CONFIGURED "
281 "state %d", __FUNCTION__, mId, mState);
282 return INVALID_OPERATION;
283 }
284
285 // This function can't be called if the stream has already received filled
286 // buffers
287 if (mStreamUnpreparable) {
288 ALOGE("%s: Stream %d: Can't prepare stream that's already in use",
289 __FUNCTION__, mId);
290 return INVALID_OPERATION;
291 }
292
293 if (getHandoutOutputBufferCountLocked() > 0) {
294 ALOGE("%s: Stream %d: Can't prepare stream that has outstanding buffers",
295 __FUNCTION__, mId);
296 return INVALID_OPERATION;
297 }
298
Ruben Brunkc78ac262015-08-13 17:58:46 -0700299
300
301 size_t pipelineMax = getBufferCountLocked();
302 size_t clampedCount = (pipelineMax < static_cast<size_t>(maxCount)) ?
303 pipelineMax : static_cast<size_t>(maxCount);
304 size_t bufferCount = (maxCount == Camera3StreamInterface::ALLOCATE_PIPELINE_MAX) ?
305 pipelineMax : clampedCount;
306
307 mPrepared = bufferCount <= mLastMaxCount;
308
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700309 if (mPrepared) return OK;
310
Ruben Brunkc78ac262015-08-13 17:58:46 -0700311 mLastMaxCount = bufferCount;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700312
313 mPreparedBuffers.insertAt(camera3_stream_buffer_t(), /*index*/0, bufferCount);
314 mPreparedBufferIdx = 0;
315
316 mState = STATE_PREPARING;
317
318 return NOT_ENOUGH_DATA;
319}
320
321bool Camera3Stream::isPreparing() const {
322 Mutex::Autolock l(mLock);
323 return mState == STATE_PREPARING;
324}
325
Chien-Yu Chene8c535e2016-04-14 12:18:26 -0700326bool Camera3Stream::isAbandoned() const {
327 Mutex::Autolock l(mLock);
328 return mState == STATE_ABANDONED;
329}
330
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700331status_t Camera3Stream::prepareNextBuffer() {
332 ATRACE_CALL();
333
334 Mutex::Autolock l(mLock);
335 status_t res = OK;
336
337 // This function should be only called when the stream is preparing
338 if (mState != STATE_PREPARING) {
339 ALOGE("%s: Stream %d: Can't prepare buffer if stream is not in PREPARING "
340 "state %d", __FUNCTION__, mId, mState);
341 return INVALID_OPERATION;
342 }
343
344 // Get next buffer - this may allocate, and take a while for large buffers
345 res = getBufferLocked( &mPreparedBuffers.editItemAt(mPreparedBufferIdx) );
346 if (res != OK) {
Eino-Ville Talvala02bf0322016-02-18 12:41:10 -0800347 ALOGE("%s: Stream %d: Unable to allocate buffer %zu during preparation",
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700348 __FUNCTION__, mId, mPreparedBufferIdx);
349 return NO_INIT;
350 }
351
352 mPreparedBufferIdx++;
353
354 // Check if we still have buffers left to allocate
355 if (mPreparedBufferIdx < mPreparedBuffers.size()) {
356 return NOT_ENOUGH_DATA;
357 }
358
359 // Done with prepare - mark stream as such, and return all buffers
360 // via cancelPrepare
361 mPrepared = true;
362
363 return cancelPrepareLocked();
364}
365
366status_t Camera3Stream::cancelPrepare() {
367 ATRACE_CALL();
368
369 Mutex::Autolock l(mLock);
370
371 return cancelPrepareLocked();
372}
373
374status_t Camera3Stream::cancelPrepareLocked() {
375 status_t res = OK;
376
377 // This function should be only called when the stream is mid-preparing.
378 if (mState != STATE_PREPARING) {
379 ALOGE("%s: Stream %d: Can't cancel prepare stream if stream is not in "
380 "PREPARING state %d", __FUNCTION__, mId, mState);
381 return INVALID_OPERATION;
382 }
383
384 // Return all valid buffers to stream, in ERROR state to indicate
385 // they weren't filled.
386 for (size_t i = 0; i < mPreparedBufferIdx; i++) {
387 mPreparedBuffers.editItemAt(i).release_fence = -1;
388 mPreparedBuffers.editItemAt(i).status = CAMERA3_BUFFER_STATUS_ERROR;
389 returnBufferLocked(mPreparedBuffers[i], 0);
390 }
391 mPreparedBuffers.clear();
392 mPreparedBufferIdx = 0;
393
394 mState = STATE_CONFIGURED;
395
396 return res;
397}
398
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -0700399status_t Camera3Stream::tearDown() {
400 ATRACE_CALL();
401 Mutex::Autolock l(mLock);
402
403 status_t res = OK;
404
405 // This function should be only called when the stream is configured.
406 if (mState != STATE_CONFIGURED) {
407 ALOGE("%s: Stream %d: Can't tear down stream if stream is not in "
408 "CONFIGURED state %d", __FUNCTION__, mId, mState);
409 return INVALID_OPERATION;
410 }
411
412 // If any buffers have been handed to the HAL, the stream cannot be torn down.
413 if (getHandoutOutputBufferCountLocked() > 0) {
414 ALOGE("%s: Stream %d: Can't tear down a stream that has outstanding buffers",
415 __FUNCTION__, mId);
416 return INVALID_OPERATION;
417 }
418
419 // Free buffers by disconnecting and then reconnecting to the buffer queue
420 // Only unused buffers will be dropped immediately; buffers that have been filled
421 // and are waiting to be acquired by the consumer and buffers that are currently
422 // acquired will be freed once they are released by the consumer.
423
424 res = disconnectLocked();
425 if (res != OK) {
426 if (res == -ENOTCONN) {
427 // queue has been disconnected, nothing left to do, so exit with success
428 return OK;
429 }
430 ALOGE("%s: Stream %d: Unable to disconnect to tear down buffers: %s (%d)",
431 __FUNCTION__, mId, strerror(-res), res);
432 return res;
433 }
434
435 mState = STATE_IN_CONFIG;
436
437 res = configureQueueLocked();
438 if (res != OK) {
439 ALOGE("%s: Unable to configure stream %d queue: %s (%d)",
440 __FUNCTION__, mId, strerror(-res), res);
441 mState = STATE_ERROR;
442 return res;
443 }
444
445 // Reset prepared state, since we've reconnected to the queue and can prepare again.
446 mPrepared = false;
447 mStreamUnpreparable = false;
448
449 mState = STATE_CONFIGURED;
450
451 return OK;
452}
453
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800454status_t Camera3Stream::getBuffer(camera3_stream_buffer *buffer) {
455 ATRACE_CALL();
456 Mutex::Autolock l(mLock);
Zhijun He6adc9cc2014-04-15 14:09:55 -0700457 status_t res = OK;
Igor Murashkin2fba5842013-04-22 14:03:54 -0700458
Zhijun He6adc9cc2014-04-15 14:09:55 -0700459 // This function should be only called when the stream is configured already.
460 if (mState != STATE_CONFIGURED) {
461 ALOGE("%s: Stream %d: Can't get buffers if stream is not in CONFIGURED state %d",
462 __FUNCTION__, mId, mState);
463 return INVALID_OPERATION;
464 }
465
466 // Wait for new buffer returned back if we are running into the limit.
467 if (getHandoutOutputBufferCountLocked() == camera3_stream::max_buffers) {
468 ALOGV("%s: Already dequeued max output buffers (%d), wait for next returned one.",
469 __FUNCTION__, camera3_stream::max_buffers);
470 res = mOutputBufferReturnedSignal.waitRelative(mLock, kWaitForBufferDuration);
471 if (res != OK) {
472 if (res == TIMED_OUT) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -0700473 ALOGE("%s: wait for output buffer return timed out after %lldms (max_buffers %d)",
474 __FUNCTION__, kWaitForBufferDuration / 1000000LL,
475 camera3_stream::max_buffers);
Zhijun He6adc9cc2014-04-15 14:09:55 -0700476 }
477 return res;
478 }
479 }
480
481 res = getBufferLocked(buffer);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700482 if (res == OK) {
483 fireBufferListenersLocked(*buffer, /*acquired*/true, /*output*/true);
Chien-Yu Chene02e9322016-04-11 16:59:33 -0700484 if (buffer->buffer) {
485 mOutstandingBuffers.push_back(*buffer->buffer);
486 }
Igor Murashkin2fba5842013-04-22 14:03:54 -0700487 }
488
489 return res;
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800490}
491
Chien-Yu Chene02e9322016-04-11 16:59:33 -0700492bool Camera3Stream::isOutstandingBuffer(const camera3_stream_buffer &buffer) {
493 if (buffer.buffer == nullptr) {
494 return false;
495 }
496
497 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
510 for (auto b = mOutstandingBuffers.begin(); b != mOutstandingBuffers.end(); b++) {
511 if (*b == *buffer.buffer) {
512 mOutstandingBuffers.erase(b);
513 return;
514 }
515 }
516}
517
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800518status_t Camera3Stream::returnBuffer(const camera3_stream_buffer &buffer,
519 nsecs_t timestamp) {
520 ATRACE_CALL();
521 Mutex::Autolock l(mLock);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700522
Chien-Yu Chene02e9322016-04-11 16:59:33 -0700523 // Check if this buffer is outstanding.
524 if (!isOutstandingBuffer(buffer)) {
525 ALOGE("%s: Stream %d: Returning an unknown buffer.", __FUNCTION__, mId);
526 return BAD_VALUE;
527 }
528
Igor Murashkin13d315e2014-04-03 18:09:04 -0700529 /**
530 * TODO: Check that the state is valid first.
531 *
532 * <HAL3.2 IN_CONFIG and IN_RECONFIG in addition to CONFIGURED.
533 * >= HAL3.2 CONFIGURED only
534 *
535 * Do this for getBuffer as well.
536 */
Igor Murashkin2fba5842013-04-22 14:03:54 -0700537 status_t res = returnBufferLocked(buffer, timestamp);
538 if (res == OK) {
539 fireBufferListenersLocked(buffer, /*acquired*/false, /*output*/true);
540 }
541
Chien-Yu Chenb83c1fe2015-09-10 16:15:21 -0700542 // Even if returning the buffer failed, we still want to signal whoever is waiting for the
543 // buffer to be returned.
544 mOutputBufferReturnedSignal.signal();
545
Chien-Yu Chene02e9322016-04-11 16:59:33 -0700546 removeOutstandingBuffer(buffer);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700547 return res;
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800548}
549
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700550status_t Camera3Stream::getInputBuffer(camera3_stream_buffer *buffer) {
551 ATRACE_CALL();
552 Mutex::Autolock l(mLock);
Zhijun He6adc9cc2014-04-15 14:09:55 -0700553 status_t res = OK;
Igor Murashkin2fba5842013-04-22 14:03:54 -0700554
Zhijun He6adc9cc2014-04-15 14:09:55 -0700555 // This function should be only called when the stream is configured already.
556 if (mState != STATE_CONFIGURED) {
557 ALOGE("%s: Stream %d: Can't get input buffers if stream is not in CONFIGURED state %d",
558 __FUNCTION__, mId, mState);
559 return INVALID_OPERATION;
560 }
561
562 // Wait for new buffer returned back if we are running into the limit.
563 if (getHandoutInputBufferCountLocked() == camera3_stream::max_buffers) {
564 ALOGV("%s: Already dequeued max input buffers (%d), wait for next returned one.",
565 __FUNCTION__, camera3_stream::max_buffers);
566 res = mInputBufferReturnedSignal.waitRelative(mLock, kWaitForBufferDuration);
567 if (res != OK) {
568 if (res == TIMED_OUT) {
569 ALOGE("%s: wait for input buffer return timed out after %lldms", __FUNCTION__,
570 kWaitForBufferDuration / 1000000LL);
571 }
572 return res;
573 }
574 }
575
576 res = getInputBufferLocked(buffer);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700577 if (res == OK) {
578 fireBufferListenersLocked(*buffer, /*acquired*/true, /*output*/false);
Chien-Yu Chene02e9322016-04-11 16:59:33 -0700579 if (buffer->buffer) {
580 mOutstandingBuffers.push_back(*buffer->buffer);
581 }
Igor Murashkin2fba5842013-04-22 14:03:54 -0700582 }
583
584 return res;
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700585}
586
587status_t Camera3Stream::returnInputBuffer(const camera3_stream_buffer &buffer) {
588 ATRACE_CALL();
589 Mutex::Autolock l(mLock);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700590
Chien-Yu Chene02e9322016-04-11 16:59:33 -0700591 // Check if this buffer is outstanding.
592 if (!isOutstandingBuffer(buffer)) {
593 ALOGE("%s: Stream %d: Returning an unknown buffer.", __FUNCTION__, mId);
594 return BAD_VALUE;
595 }
596
Igor Murashkin2fba5842013-04-22 14:03:54 -0700597 status_t res = returnInputBufferLocked(buffer);
598 if (res == OK) {
599 fireBufferListenersLocked(buffer, /*acquired*/false, /*output*/false);
Zhijun He6adc9cc2014-04-15 14:09:55 -0700600 mInputBufferReturnedSignal.signal();
Igor Murashkin2fba5842013-04-22 14:03:54 -0700601 }
Chien-Yu Chene02e9322016-04-11 16:59:33 -0700602
603 removeOutstandingBuffer(buffer);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700604 return res;
605}
606
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700607status_t Camera3Stream::getInputBufferProducer(sp<IGraphicBufferProducer> *producer) {
608 ATRACE_CALL();
609 Mutex::Autolock l(mLock);
610
611 return getInputBufferProducerLocked(producer);
612}
613
Igor Murashkin2fba5842013-04-22 14:03:54 -0700614void Camera3Stream::fireBufferListenersLocked(
Shuzhen Wangb0fdc1e2016-03-20 23:21:39 -0700615 const camera3_stream_buffer& buffer, bool acquired, bool output) {
Igor Murashkin2fba5842013-04-22 14:03:54 -0700616 List<wp<Camera3StreamBufferListener> >::iterator it, end;
617
618 // TODO: finish implementing
619
620 Camera3StreamBufferListener::BufferInfo info =
621 Camera3StreamBufferListener::BufferInfo();
622 info.mOutput = output;
Shuzhen Wangb0fdc1e2016-03-20 23:21:39 -0700623 info.mError = (buffer.status == CAMERA3_BUFFER_STATUS_ERROR);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700624 // TODO: rest of fields
625
626 for (it = mBufferListenerList.begin(), end = mBufferListenerList.end();
627 it != end;
628 ++it) {
629
630 sp<Camera3StreamBufferListener> listener = it->promote();
631 if (listener != 0) {
632 if (acquired) {
633 listener->onBufferAcquired(info);
634 } else {
635 listener->onBufferReleased(info);
636 }
637 }
638 }
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700639}
640
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800641bool Camera3Stream::hasOutstandingBuffers() const {
642 ATRACE_CALL();
643 Mutex::Autolock l(mLock);
644 return hasOutstandingBuffersLocked();
645}
646
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700647status_t Camera3Stream::setStatusTracker(sp<StatusTracker> statusTracker) {
648 Mutex::Autolock l(mLock);
649 sp<StatusTracker> oldTracker = mStatusTracker.promote();
650 if (oldTracker != 0 && mStatusId != StatusTracker::NO_STATUS_ID) {
651 oldTracker->removeComponent(mStatusId);
652 }
653 mStatusId = StatusTracker::NO_STATUS_ID;
654 mStatusTracker = statusTracker;
655
656 return OK;
657}
658
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800659status_t Camera3Stream::disconnect() {
660 ATRACE_CALL();
661 Mutex::Autolock l(mLock);
Igor Murashkine2172be2013-05-28 15:31:39 -0700662 ALOGV("%s: Stream %d: Disconnecting...", __FUNCTION__, mId);
663 status_t res = disconnectLocked();
664
665 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
673status_t Camera3Stream::registerBuffersLocked(camera3_device *hal3Device) {
674 ATRACE_CALL();
Igor Murashkin13d315e2014-04-03 18:09:04 -0700675
676 /**
677 * >= CAMERA_DEVICE_API_VERSION_3_2:
678 *
679 * camera3_device_t->ops->register_stream_buffers() is not called and must
680 * be NULL.
681 */
682 if (hal3Device->common.version >= CAMERA_DEVICE_API_VERSION_3_2) {
683 ALOGV("%s: register_stream_buffers unused as of HAL3.2", __FUNCTION__);
684
Igor Murashkinc758f222014-08-19 15:14:29 -0700685 if (hal3Device->ops->register_stream_buffers != NULL) {
Igor Murashkin13d315e2014-04-03 18:09:04 -0700686 ALOGE("%s: register_stream_buffers is deprecated in HAL3.2; "
687 "must be set to NULL in camera3_device::ops", __FUNCTION__);
688 return INVALID_OPERATION;
Igor Murashkin13d315e2014-04-03 18:09:04 -0700689 }
690
691 return OK;
Igor Murashkin13d315e2014-04-03 18:09:04 -0700692 }
693
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700694 ALOGV("%s: register_stream_buffers using deprecated code path", __FUNCTION__);
695
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800696 status_t res;
697
698 size_t bufferCount = getBufferCountLocked();
699
700 Vector<buffer_handle_t*> buffers;
Igor Murashkin13d315e2014-04-03 18:09:04 -0700701 buffers.insertAt(/*prototype_item*/NULL, /*index*/0, bufferCount);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800702
703 camera3_stream_buffer_set bufferSet = camera3_stream_buffer_set();
704 bufferSet.stream = this;
705 bufferSet.num_buffers = bufferCount;
706 bufferSet.buffers = buffers.editArray();
707
708 Vector<camera3_stream_buffer_t> streamBuffers;
Igor Murashkin13d315e2014-04-03 18:09:04 -0700709 streamBuffers.insertAt(camera3_stream_buffer_t(), /*index*/0, bufferCount);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800710
711 // Register all buffers with the HAL. This means getting all the buffers
712 // from the stream, providing them to the HAL with the
713 // register_stream_buffers() method, and then returning them back to the
714 // stream in the error state, since they won't have valid data.
715 //
716 // Only registered buffers can be sent to the HAL.
717
718 uint32_t bufferIdx = 0;
719 for (; bufferIdx < bufferCount; bufferIdx++) {
720 res = getBufferLocked( &streamBuffers.editItemAt(bufferIdx) );
721 if (res != OK) {
722 ALOGE("%s: Unable to get buffer %d for registration with HAL",
723 __FUNCTION__, bufferIdx);
724 // Skip registering, go straight to cleanup
725 break;
726 }
727
728 sp<Fence> fence = new Fence(streamBuffers[bufferIdx].acquire_fence);
Mathias Agopiand7644242013-05-16 18:07:35 -0700729 fence->waitForever("Camera3Stream::registerBuffers");
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800730
731 buffers.editItemAt(bufferIdx) = streamBuffers[bufferIdx].buffer;
732 }
733 if (bufferIdx == bufferCount) {
734 // Got all buffers, register with HAL
Colin Crosse5729fa2014-03-21 15:04:25 -0700735 ALOGV("%s: Registering %zu buffers with camera HAL",
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800736 __FUNCTION__, bufferCount);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -0700737 ATRACE_BEGIN("camera3->register_stream_buffers");
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800738 res = hal3Device->ops->register_stream_buffers(hal3Device,
739 &bufferSet);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -0700740 ATRACE_END();
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800741 }
742
743 // Return all valid buffers to stream, in ERROR state to indicate
744 // they weren't filled.
745 for (size_t i = 0; i < bufferIdx; i++) {
746 streamBuffers.editItemAt(i).release_fence = -1;
747 streamBuffers.editItemAt(i).status = CAMERA3_BUFFER_STATUS_ERROR;
748 returnBufferLocked(streamBuffers[i], 0);
749 }
750
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700751 mPrepared = true;
752
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800753 return res;
754}
755
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700756status_t Camera3Stream::getBufferLocked(camera3_stream_buffer *) {
757 ALOGE("%s: This type of stream does not support output", __FUNCTION__);
758 return INVALID_OPERATION;
759}
760status_t Camera3Stream::returnBufferLocked(const camera3_stream_buffer &,
761 nsecs_t) {
762 ALOGE("%s: This type of stream does not support output", __FUNCTION__);
763 return INVALID_OPERATION;
764}
765status_t Camera3Stream::getInputBufferLocked(camera3_stream_buffer *) {
766 ALOGE("%s: This type of stream does not support input", __FUNCTION__);
767 return INVALID_OPERATION;
768}
769status_t Camera3Stream::returnInputBufferLocked(
770 const camera3_stream_buffer &) {
771 ALOGE("%s: This type of stream does not support input", __FUNCTION__);
772 return INVALID_OPERATION;
773}
Eino-Ville Talvala02bf0322016-02-18 12:41:10 -0800774status_t Camera3Stream::getInputBufferProducerLocked(sp<IGraphicBufferProducer>*) {
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700775 ALOGE("%s: This type of stream does not support input", __FUNCTION__);
776 return INVALID_OPERATION;
777}
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700778
Igor Murashkin2fba5842013-04-22 14:03:54 -0700779void Camera3Stream::addBufferListener(
780 wp<Camera3StreamBufferListener> listener) {
781 Mutex::Autolock l(mLock);
Zhijun Hef0d962a2014-06-30 10:24:11 -0700782
783 List<wp<Camera3StreamBufferListener> >::iterator it, end;
784 for (it = mBufferListenerList.begin(), end = mBufferListenerList.end();
785 it != end;
786 ) {
787 if (*it == listener) {
788 ALOGE("%s: Try to add the same listener twice, ignoring...", __FUNCTION__);
789 return;
790 }
791 it++;
792 }
793
Igor Murashkin2fba5842013-04-22 14:03:54 -0700794 mBufferListenerList.push_back(listener);
795}
796
797void Camera3Stream::removeBufferListener(
798 const sp<Camera3StreamBufferListener>& listener) {
799 Mutex::Autolock l(mLock);
800
801 bool erased = true;
802 List<wp<Camera3StreamBufferListener> >::iterator it, end;
803 for (it = mBufferListenerList.begin(), end = mBufferListenerList.end();
804 it != end;
805 ) {
806
807 if (*it == listener) {
808 it = mBufferListenerList.erase(it);
809 erased = true;
810 } else {
811 ++it;
812 }
813 }
814
815 if (!erased) {
816 ALOGW("%s: Could not find listener to remove, already removed",
817 __FUNCTION__);
818 }
819}
820
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800821}; // namespace camera3
822
823}; // namespace android