blob: c3b756527685445e94267b35c700d18982b6ac2b [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
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800446status_t Camera3Stream::getBuffer(camera3_stream_buffer *buffer) {
447 ATRACE_CALL();
448 Mutex::Autolock l(mLock);
Zhijun He6adc9cc2014-04-15 14:09:55 -0700449 status_t res = OK;
Igor Murashkin2fba5842013-04-22 14:03:54 -0700450
Zhijun He6adc9cc2014-04-15 14:09:55 -0700451 // This function should be only called when the stream is configured already.
452 if (mState != STATE_CONFIGURED) {
453 ALOGE("%s: Stream %d: Can't get buffers if stream is not in CONFIGURED state %d",
454 __FUNCTION__, mId, mState);
455 return INVALID_OPERATION;
456 }
457
458 // Wait for new buffer returned back if we are running into the limit.
459 if (getHandoutOutputBufferCountLocked() == camera3_stream::max_buffers) {
460 ALOGV("%s: Already dequeued max output buffers (%d), wait for next returned one.",
461 __FUNCTION__, camera3_stream::max_buffers);
462 res = mOutputBufferReturnedSignal.waitRelative(mLock, kWaitForBufferDuration);
463 if (res != OK) {
464 if (res == TIMED_OUT) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -0700465 ALOGE("%s: wait for output buffer return timed out after %lldms (max_buffers %d)",
466 __FUNCTION__, kWaitForBufferDuration / 1000000LL,
467 camera3_stream::max_buffers);
Zhijun He6adc9cc2014-04-15 14:09:55 -0700468 }
469 return res;
470 }
471 }
472
473 res = getBufferLocked(buffer);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700474 if (res == OK) {
475 fireBufferListenersLocked(*buffer, /*acquired*/true, /*output*/true);
Chien-Yu Chene02e9322016-04-11 16:59:33 -0700476 if (buffer->buffer) {
477 mOutstandingBuffers.push_back(*buffer->buffer);
478 }
Igor Murashkin2fba5842013-04-22 14:03:54 -0700479 }
480
481 return res;
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800482}
483
Chien-Yu Chene02e9322016-04-11 16:59:33 -0700484bool Camera3Stream::isOutstandingBuffer(const camera3_stream_buffer &buffer) {
485 if (buffer.buffer == nullptr) {
486 return false;
487 }
488
489 for (auto b : mOutstandingBuffers) {
490 if (b == *buffer.buffer) {
491 return true;
492 }
493 }
494 return false;
495}
496
497void Camera3Stream::removeOutstandingBuffer(const camera3_stream_buffer &buffer) {
498 if (buffer.buffer == nullptr) {
499 return;
500 }
501
502 for (auto b = mOutstandingBuffers.begin(); b != mOutstandingBuffers.end(); b++) {
503 if (*b == *buffer.buffer) {
504 mOutstandingBuffers.erase(b);
505 return;
506 }
507 }
508}
509
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800510status_t Camera3Stream::returnBuffer(const camera3_stream_buffer &buffer,
511 nsecs_t timestamp) {
512 ATRACE_CALL();
513 Mutex::Autolock l(mLock);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700514
Chien-Yu Chene02e9322016-04-11 16:59:33 -0700515 // Check if this buffer is outstanding.
516 if (!isOutstandingBuffer(buffer)) {
517 ALOGE("%s: Stream %d: Returning an unknown buffer.", __FUNCTION__, mId);
518 return BAD_VALUE;
519 }
520
Igor Murashkin13d315e2014-04-03 18:09:04 -0700521 /**
522 * TODO: Check that the state is valid first.
523 *
524 * <HAL3.2 IN_CONFIG and IN_RECONFIG in addition to CONFIGURED.
525 * >= HAL3.2 CONFIGURED only
526 *
527 * Do this for getBuffer as well.
528 */
Igor Murashkin2fba5842013-04-22 14:03:54 -0700529 status_t res = returnBufferLocked(buffer, timestamp);
530 if (res == OK) {
531 fireBufferListenersLocked(buffer, /*acquired*/false, /*output*/true);
532 }
533
Chien-Yu Chenb83c1fe2015-09-10 16:15:21 -0700534 // Even if returning the buffer failed, we still want to signal whoever is waiting for the
535 // buffer to be returned.
536 mOutputBufferReturnedSignal.signal();
537
Chien-Yu Chene02e9322016-04-11 16:59:33 -0700538 removeOutstandingBuffer(buffer);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700539 return res;
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800540}
541
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700542status_t Camera3Stream::getInputBuffer(camera3_stream_buffer *buffer) {
543 ATRACE_CALL();
544 Mutex::Autolock l(mLock);
Zhijun He6adc9cc2014-04-15 14:09:55 -0700545 status_t res = OK;
Igor Murashkin2fba5842013-04-22 14:03:54 -0700546
Zhijun He6adc9cc2014-04-15 14:09:55 -0700547 // This function should be only called when the stream is configured already.
548 if (mState != STATE_CONFIGURED) {
549 ALOGE("%s: Stream %d: Can't get input buffers if stream is not in CONFIGURED state %d",
550 __FUNCTION__, mId, mState);
551 return INVALID_OPERATION;
552 }
553
554 // Wait for new buffer returned back if we are running into the limit.
555 if (getHandoutInputBufferCountLocked() == camera3_stream::max_buffers) {
556 ALOGV("%s: Already dequeued max input buffers (%d), wait for next returned one.",
557 __FUNCTION__, camera3_stream::max_buffers);
558 res = mInputBufferReturnedSignal.waitRelative(mLock, kWaitForBufferDuration);
559 if (res != OK) {
560 if (res == TIMED_OUT) {
561 ALOGE("%s: wait for input buffer return timed out after %lldms", __FUNCTION__,
562 kWaitForBufferDuration / 1000000LL);
563 }
564 return res;
565 }
566 }
567
568 res = getInputBufferLocked(buffer);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700569 if (res == OK) {
570 fireBufferListenersLocked(*buffer, /*acquired*/true, /*output*/false);
Chien-Yu Chene02e9322016-04-11 16:59:33 -0700571 if (buffer->buffer) {
572 mOutstandingBuffers.push_back(*buffer->buffer);
573 }
Igor Murashkin2fba5842013-04-22 14:03:54 -0700574 }
575
576 return res;
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700577}
578
579status_t Camera3Stream::returnInputBuffer(const camera3_stream_buffer &buffer) {
580 ATRACE_CALL();
581 Mutex::Autolock l(mLock);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700582
Chien-Yu Chene02e9322016-04-11 16:59:33 -0700583 // Check if this buffer is outstanding.
584 if (!isOutstandingBuffer(buffer)) {
585 ALOGE("%s: Stream %d: Returning an unknown buffer.", __FUNCTION__, mId);
586 return BAD_VALUE;
587 }
588
Igor Murashkin2fba5842013-04-22 14:03:54 -0700589 status_t res = returnInputBufferLocked(buffer);
590 if (res == OK) {
591 fireBufferListenersLocked(buffer, /*acquired*/false, /*output*/false);
Zhijun He6adc9cc2014-04-15 14:09:55 -0700592 mInputBufferReturnedSignal.signal();
Igor Murashkin2fba5842013-04-22 14:03:54 -0700593 }
Chien-Yu Chene02e9322016-04-11 16:59:33 -0700594
595 removeOutstandingBuffer(buffer);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700596 return res;
597}
598
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700599status_t Camera3Stream::getInputBufferProducer(sp<IGraphicBufferProducer> *producer) {
600 ATRACE_CALL();
601 Mutex::Autolock l(mLock);
602
603 return getInputBufferProducerLocked(producer);
604}
605
Igor Murashkin2fba5842013-04-22 14:03:54 -0700606void Camera3Stream::fireBufferListenersLocked(
Shuzhen Wangb0fdc1e2016-03-20 23:21:39 -0700607 const camera3_stream_buffer& buffer, bool acquired, bool output) {
Igor Murashkin2fba5842013-04-22 14:03:54 -0700608 List<wp<Camera3StreamBufferListener> >::iterator it, end;
609
610 // TODO: finish implementing
611
612 Camera3StreamBufferListener::BufferInfo info =
613 Camera3StreamBufferListener::BufferInfo();
614 info.mOutput = output;
Shuzhen Wangb0fdc1e2016-03-20 23:21:39 -0700615 info.mError = (buffer.status == CAMERA3_BUFFER_STATUS_ERROR);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700616 // TODO: rest of fields
617
618 for (it = mBufferListenerList.begin(), end = mBufferListenerList.end();
619 it != end;
620 ++it) {
621
622 sp<Camera3StreamBufferListener> listener = it->promote();
623 if (listener != 0) {
624 if (acquired) {
625 listener->onBufferAcquired(info);
626 } else {
627 listener->onBufferReleased(info);
628 }
629 }
630 }
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700631}
632
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800633bool Camera3Stream::hasOutstandingBuffers() const {
634 ATRACE_CALL();
635 Mutex::Autolock l(mLock);
636 return hasOutstandingBuffersLocked();
637}
638
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700639status_t Camera3Stream::setStatusTracker(sp<StatusTracker> statusTracker) {
640 Mutex::Autolock l(mLock);
641 sp<StatusTracker> oldTracker = mStatusTracker.promote();
642 if (oldTracker != 0 && mStatusId != StatusTracker::NO_STATUS_ID) {
643 oldTracker->removeComponent(mStatusId);
644 }
645 mStatusId = StatusTracker::NO_STATUS_ID;
646 mStatusTracker = statusTracker;
647
648 return OK;
649}
650
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800651status_t Camera3Stream::disconnect() {
652 ATRACE_CALL();
653 Mutex::Autolock l(mLock);
Igor Murashkine2172be2013-05-28 15:31:39 -0700654 ALOGV("%s: Stream %d: Disconnecting...", __FUNCTION__, mId);
655 status_t res = disconnectLocked();
656
657 if (res == -ENOTCONN) {
658 // "Already disconnected" -- not an error
659 return OK;
660 } else {
661 return res;
662 }
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800663}
664
665status_t Camera3Stream::registerBuffersLocked(camera3_device *hal3Device) {
666 ATRACE_CALL();
Igor Murashkin13d315e2014-04-03 18:09:04 -0700667
668 /**
669 * >= CAMERA_DEVICE_API_VERSION_3_2:
670 *
671 * camera3_device_t->ops->register_stream_buffers() is not called and must
672 * be NULL.
673 */
674 if (hal3Device->common.version >= CAMERA_DEVICE_API_VERSION_3_2) {
675 ALOGV("%s: register_stream_buffers unused as of HAL3.2", __FUNCTION__);
676
Igor Murashkinc758f222014-08-19 15:14:29 -0700677 if (hal3Device->ops->register_stream_buffers != NULL) {
Igor Murashkin13d315e2014-04-03 18:09:04 -0700678 ALOGE("%s: register_stream_buffers is deprecated in HAL3.2; "
679 "must be set to NULL in camera3_device::ops", __FUNCTION__);
680 return INVALID_OPERATION;
Igor Murashkin13d315e2014-04-03 18:09:04 -0700681 }
682
683 return OK;
Igor Murashkin13d315e2014-04-03 18:09:04 -0700684 }
685
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700686 ALOGV("%s: register_stream_buffers using deprecated code path", __FUNCTION__);
687
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800688 status_t res;
689
690 size_t bufferCount = getBufferCountLocked();
691
692 Vector<buffer_handle_t*> buffers;
Igor Murashkin13d315e2014-04-03 18:09:04 -0700693 buffers.insertAt(/*prototype_item*/NULL, /*index*/0, bufferCount);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800694
695 camera3_stream_buffer_set bufferSet = camera3_stream_buffer_set();
696 bufferSet.stream = this;
697 bufferSet.num_buffers = bufferCount;
698 bufferSet.buffers = buffers.editArray();
699
700 Vector<camera3_stream_buffer_t> streamBuffers;
Igor Murashkin13d315e2014-04-03 18:09:04 -0700701 streamBuffers.insertAt(camera3_stream_buffer_t(), /*index*/0, bufferCount);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800702
703 // Register all buffers with the HAL. This means getting all the buffers
704 // from the stream, providing them to the HAL with the
705 // register_stream_buffers() method, and then returning them back to the
706 // stream in the error state, since they won't have valid data.
707 //
708 // Only registered buffers can be sent to the HAL.
709
710 uint32_t bufferIdx = 0;
711 for (; bufferIdx < bufferCount; bufferIdx++) {
712 res = getBufferLocked( &streamBuffers.editItemAt(bufferIdx) );
713 if (res != OK) {
714 ALOGE("%s: Unable to get buffer %d for registration with HAL",
715 __FUNCTION__, bufferIdx);
716 // Skip registering, go straight to cleanup
717 break;
718 }
719
720 sp<Fence> fence = new Fence(streamBuffers[bufferIdx].acquire_fence);
Mathias Agopiand7644242013-05-16 18:07:35 -0700721 fence->waitForever("Camera3Stream::registerBuffers");
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800722
723 buffers.editItemAt(bufferIdx) = streamBuffers[bufferIdx].buffer;
724 }
725 if (bufferIdx == bufferCount) {
726 // Got all buffers, register with HAL
Colin Crosse5729fa2014-03-21 15:04:25 -0700727 ALOGV("%s: Registering %zu buffers with camera HAL",
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800728 __FUNCTION__, bufferCount);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -0700729 ATRACE_BEGIN("camera3->register_stream_buffers");
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800730 res = hal3Device->ops->register_stream_buffers(hal3Device,
731 &bufferSet);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -0700732 ATRACE_END();
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800733 }
734
735 // Return all valid buffers to stream, in ERROR state to indicate
736 // they weren't filled.
737 for (size_t i = 0; i < bufferIdx; i++) {
738 streamBuffers.editItemAt(i).release_fence = -1;
739 streamBuffers.editItemAt(i).status = CAMERA3_BUFFER_STATUS_ERROR;
740 returnBufferLocked(streamBuffers[i], 0);
741 }
742
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700743 mPrepared = true;
744
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800745 return res;
746}
747
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700748status_t Camera3Stream::getBufferLocked(camera3_stream_buffer *) {
749 ALOGE("%s: This type of stream does not support output", __FUNCTION__);
750 return INVALID_OPERATION;
751}
752status_t Camera3Stream::returnBufferLocked(const camera3_stream_buffer &,
753 nsecs_t) {
754 ALOGE("%s: This type of stream does not support output", __FUNCTION__);
755 return INVALID_OPERATION;
756}
757status_t Camera3Stream::getInputBufferLocked(camera3_stream_buffer *) {
758 ALOGE("%s: This type of stream does not support input", __FUNCTION__);
759 return INVALID_OPERATION;
760}
761status_t Camera3Stream::returnInputBufferLocked(
762 const camera3_stream_buffer &) {
763 ALOGE("%s: This type of stream does not support input", __FUNCTION__);
764 return INVALID_OPERATION;
765}
Eino-Ville Talvala02bf0322016-02-18 12:41:10 -0800766status_t Camera3Stream::getInputBufferProducerLocked(sp<IGraphicBufferProducer>*) {
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700767 ALOGE("%s: This type of stream does not support input", __FUNCTION__);
768 return INVALID_OPERATION;
769}
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700770
Igor Murashkin2fba5842013-04-22 14:03:54 -0700771void Camera3Stream::addBufferListener(
772 wp<Camera3StreamBufferListener> listener) {
773 Mutex::Autolock l(mLock);
Zhijun Hef0d962a2014-06-30 10:24:11 -0700774
775 List<wp<Camera3StreamBufferListener> >::iterator it, end;
776 for (it = mBufferListenerList.begin(), end = mBufferListenerList.end();
777 it != end;
778 ) {
779 if (*it == listener) {
780 ALOGE("%s: Try to add the same listener twice, ignoring...", __FUNCTION__);
781 return;
782 }
783 it++;
784 }
785
Igor Murashkin2fba5842013-04-22 14:03:54 -0700786 mBufferListenerList.push_back(listener);
787}
788
789void Camera3Stream::removeBufferListener(
790 const sp<Camera3StreamBufferListener>& listener) {
791 Mutex::Autolock l(mLock);
792
793 bool erased = true;
794 List<wp<Camera3StreamBufferListener> >::iterator it, end;
795 for (it = mBufferListenerList.begin(), end = mBufferListenerList.end();
796 it != end;
797 ) {
798
799 if (*it == listener) {
800 it = mBufferListenerList.erase(it);
801 erased = true;
802 } else {
803 ++it;
804 }
805 }
806
807 if (!erased) {
808 ALOGW("%s: Could not find listener to remove, already removed",
809 __FUNCTION__);
810 }
811}
812
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800813}; // namespace camera3
814
815}; // namespace android