blob: 50f7a91975dfb39aed0cf2a0adb0f18a2d95158b [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 He125684a2015-12-26 15:07:30 -080058 mStreamUnpreparable(false),
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
326status_t Camera3Stream::prepareNextBuffer() {
327 ATRACE_CALL();
328
329 Mutex::Autolock l(mLock);
330 status_t res = OK;
331
332 // This function should be only called when the stream is preparing
333 if (mState != STATE_PREPARING) {
334 ALOGE("%s: Stream %d: Can't prepare buffer if stream is not in PREPARING "
335 "state %d", __FUNCTION__, mId, mState);
336 return INVALID_OPERATION;
337 }
338
339 // Get next buffer - this may allocate, and take a while for large buffers
340 res = getBufferLocked( &mPreparedBuffers.editItemAt(mPreparedBufferIdx) );
341 if (res != OK) {
Eino-Ville Talvala02bf0322016-02-18 12:41:10 -0800342 ALOGE("%s: Stream %d: Unable to allocate buffer %zu during preparation",
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700343 __FUNCTION__, mId, mPreparedBufferIdx);
344 return NO_INIT;
345 }
346
347 mPreparedBufferIdx++;
348
349 // Check if we still have buffers left to allocate
350 if (mPreparedBufferIdx < mPreparedBuffers.size()) {
351 return NOT_ENOUGH_DATA;
352 }
353
354 // Done with prepare - mark stream as such, and return all buffers
355 // via cancelPrepare
356 mPrepared = true;
357
358 return cancelPrepareLocked();
359}
360
361status_t Camera3Stream::cancelPrepare() {
362 ATRACE_CALL();
363
364 Mutex::Autolock l(mLock);
365
366 return cancelPrepareLocked();
367}
368
369status_t Camera3Stream::cancelPrepareLocked() {
370 status_t res = OK;
371
372 // This function should be only called when the stream is mid-preparing.
373 if (mState != STATE_PREPARING) {
374 ALOGE("%s: Stream %d: Can't cancel prepare stream if stream is not in "
375 "PREPARING state %d", __FUNCTION__, mId, mState);
376 return INVALID_OPERATION;
377 }
378
379 // Return all valid buffers to stream, in ERROR state to indicate
380 // they weren't filled.
381 for (size_t i = 0; i < mPreparedBufferIdx; i++) {
382 mPreparedBuffers.editItemAt(i).release_fence = -1;
383 mPreparedBuffers.editItemAt(i).status = CAMERA3_BUFFER_STATUS_ERROR;
384 returnBufferLocked(mPreparedBuffers[i], 0);
385 }
386 mPreparedBuffers.clear();
387 mPreparedBufferIdx = 0;
388
389 mState = STATE_CONFIGURED;
390
391 return res;
392}
393
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -0700394status_t Camera3Stream::tearDown() {
395 ATRACE_CALL();
396 Mutex::Autolock l(mLock);
397
398 status_t res = OK;
399
400 // This function should be only called when the stream is configured.
401 if (mState != STATE_CONFIGURED) {
402 ALOGE("%s: Stream %d: Can't tear down stream if stream is not in "
403 "CONFIGURED state %d", __FUNCTION__, mId, mState);
404 return INVALID_OPERATION;
405 }
406
407 // If any buffers have been handed to the HAL, the stream cannot be torn down.
408 if (getHandoutOutputBufferCountLocked() > 0) {
409 ALOGE("%s: Stream %d: Can't tear down a stream that has outstanding buffers",
410 __FUNCTION__, mId);
411 return INVALID_OPERATION;
412 }
413
414 // Free buffers by disconnecting and then reconnecting to the buffer queue
415 // Only unused buffers will be dropped immediately; buffers that have been filled
416 // and are waiting to be acquired by the consumer and buffers that are currently
417 // acquired will be freed once they are released by the consumer.
418
419 res = disconnectLocked();
420 if (res != OK) {
421 if (res == -ENOTCONN) {
422 // queue has been disconnected, nothing left to do, so exit with success
423 return OK;
424 }
425 ALOGE("%s: Stream %d: Unable to disconnect to tear down buffers: %s (%d)",
426 __FUNCTION__, mId, strerror(-res), res);
427 return res;
428 }
429
430 mState = STATE_IN_CONFIG;
431
432 res = configureQueueLocked();
433 if (res != OK) {
434 ALOGE("%s: Unable to configure stream %d queue: %s (%d)",
435 __FUNCTION__, mId, strerror(-res), res);
436 mState = STATE_ERROR;
437 return res;
438 }
439
440 // Reset prepared state, since we've reconnected to the queue and can prepare again.
441 mPrepared = false;
442 mStreamUnpreparable = false;
443
444 mState = STATE_CONFIGURED;
445
446 return OK;
447}
448
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800449status_t Camera3Stream::getBuffer(camera3_stream_buffer *buffer) {
450 ATRACE_CALL();
451 Mutex::Autolock l(mLock);
Zhijun He6adc9cc2014-04-15 14:09:55 -0700452 status_t res = OK;
Igor Murashkin2fba5842013-04-22 14:03:54 -0700453
Zhijun He6adc9cc2014-04-15 14:09:55 -0700454 // This function should be only called when the stream is configured already.
455 if (mState != STATE_CONFIGURED) {
456 ALOGE("%s: Stream %d: Can't get buffers if stream is not in CONFIGURED state %d",
457 __FUNCTION__, mId, mState);
458 return INVALID_OPERATION;
459 }
460
461 // Wait for new buffer returned back if we are running into the limit.
462 if (getHandoutOutputBufferCountLocked() == camera3_stream::max_buffers) {
463 ALOGV("%s: Already dequeued max output buffers (%d), wait for next returned one.",
464 __FUNCTION__, camera3_stream::max_buffers);
465 res = mOutputBufferReturnedSignal.waitRelative(mLock, kWaitForBufferDuration);
466 if (res != OK) {
467 if (res == TIMED_OUT) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -0700468 ALOGE("%s: wait for output buffer return timed out after %lldms (max_buffers %d)",
469 __FUNCTION__, kWaitForBufferDuration / 1000000LL,
470 camera3_stream::max_buffers);
Zhijun He6adc9cc2014-04-15 14:09:55 -0700471 }
472 return res;
473 }
474 }
475
476 res = getBufferLocked(buffer);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700477 if (res == OK) {
478 fireBufferListenersLocked(*buffer, /*acquired*/true, /*output*/true);
479 }
480
481 return res;
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800482}
483
484status_t Camera3Stream::returnBuffer(const camera3_stream_buffer &buffer,
485 nsecs_t timestamp) {
486 ATRACE_CALL();
487 Mutex::Autolock l(mLock);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700488
Igor Murashkin13d315e2014-04-03 18:09:04 -0700489 /**
490 * TODO: Check that the state is valid first.
491 *
492 * <HAL3.2 IN_CONFIG and IN_RECONFIG in addition to CONFIGURED.
493 * >= HAL3.2 CONFIGURED only
494 *
495 * Do this for getBuffer as well.
496 */
Igor Murashkin2fba5842013-04-22 14:03:54 -0700497 status_t res = returnBufferLocked(buffer, timestamp);
498 if (res == OK) {
499 fireBufferListenersLocked(buffer, /*acquired*/false, /*output*/true);
500 }
501
Chien-Yu Chenb83c1fe2015-09-10 16:15:21 -0700502 // Even if returning the buffer failed, we still want to signal whoever is waiting for the
503 // buffer to be returned.
504 mOutputBufferReturnedSignal.signal();
505
Igor Murashkin2fba5842013-04-22 14:03:54 -0700506 return res;
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800507}
508
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700509status_t Camera3Stream::getInputBuffer(camera3_stream_buffer *buffer) {
510 ATRACE_CALL();
511 Mutex::Autolock l(mLock);
Zhijun He6adc9cc2014-04-15 14:09:55 -0700512 status_t res = OK;
Igor Murashkin2fba5842013-04-22 14:03:54 -0700513
Zhijun He6adc9cc2014-04-15 14:09:55 -0700514 // This function should be only called when the stream is configured already.
515 if (mState != STATE_CONFIGURED) {
516 ALOGE("%s: Stream %d: Can't get input buffers if stream is not in CONFIGURED state %d",
517 __FUNCTION__, mId, mState);
518 return INVALID_OPERATION;
519 }
520
521 // Wait for new buffer returned back if we are running into the limit.
522 if (getHandoutInputBufferCountLocked() == camera3_stream::max_buffers) {
523 ALOGV("%s: Already dequeued max input buffers (%d), wait for next returned one.",
524 __FUNCTION__, camera3_stream::max_buffers);
525 res = mInputBufferReturnedSignal.waitRelative(mLock, kWaitForBufferDuration);
526 if (res != OK) {
527 if (res == TIMED_OUT) {
528 ALOGE("%s: wait for input buffer return timed out after %lldms", __FUNCTION__,
529 kWaitForBufferDuration / 1000000LL);
530 }
531 return res;
532 }
533 }
534
535 res = getInputBufferLocked(buffer);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700536 if (res == OK) {
537 fireBufferListenersLocked(*buffer, /*acquired*/true, /*output*/false);
538 }
539
540 return res;
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700541}
542
543status_t Camera3Stream::returnInputBuffer(const camera3_stream_buffer &buffer) {
544 ATRACE_CALL();
545 Mutex::Autolock l(mLock);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700546
547 status_t res = returnInputBufferLocked(buffer);
548 if (res == OK) {
549 fireBufferListenersLocked(buffer, /*acquired*/false, /*output*/false);
Zhijun He6adc9cc2014-04-15 14:09:55 -0700550 mInputBufferReturnedSignal.signal();
Igor Murashkin2fba5842013-04-22 14:03:54 -0700551 }
552 return res;
553}
554
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700555status_t Camera3Stream::getInputBufferProducer(sp<IGraphicBufferProducer> *producer) {
556 ATRACE_CALL();
557 Mutex::Autolock l(mLock);
558
559 return getInputBufferProducerLocked(producer);
560}
561
Igor Murashkin2fba5842013-04-22 14:03:54 -0700562void Camera3Stream::fireBufferListenersLocked(
563 const camera3_stream_buffer& /*buffer*/, bool acquired, bool output) {
564 List<wp<Camera3StreamBufferListener> >::iterator it, end;
565
566 // TODO: finish implementing
567
568 Camera3StreamBufferListener::BufferInfo info =
569 Camera3StreamBufferListener::BufferInfo();
570 info.mOutput = output;
571 // TODO: rest of fields
572
573 for (it = mBufferListenerList.begin(), end = mBufferListenerList.end();
574 it != end;
575 ++it) {
576
577 sp<Camera3StreamBufferListener> listener = it->promote();
578 if (listener != 0) {
579 if (acquired) {
580 listener->onBufferAcquired(info);
581 } else {
582 listener->onBufferReleased(info);
583 }
584 }
585 }
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700586}
587
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800588bool Camera3Stream::hasOutstandingBuffers() const {
589 ATRACE_CALL();
590 Mutex::Autolock l(mLock);
591 return hasOutstandingBuffersLocked();
592}
593
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700594status_t Camera3Stream::setStatusTracker(sp<StatusTracker> statusTracker) {
595 Mutex::Autolock l(mLock);
596 sp<StatusTracker> oldTracker = mStatusTracker.promote();
597 if (oldTracker != 0 && mStatusId != StatusTracker::NO_STATUS_ID) {
598 oldTracker->removeComponent(mStatusId);
599 }
600 mStatusId = StatusTracker::NO_STATUS_ID;
601 mStatusTracker = statusTracker;
602
603 return OK;
604}
605
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800606status_t Camera3Stream::disconnect() {
607 ATRACE_CALL();
608 Mutex::Autolock l(mLock);
Igor Murashkine2172be2013-05-28 15:31:39 -0700609 ALOGV("%s: Stream %d: Disconnecting...", __FUNCTION__, mId);
610 status_t res = disconnectLocked();
611
612 if (res == -ENOTCONN) {
613 // "Already disconnected" -- not an error
614 return OK;
615 } else {
616 return res;
617 }
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800618}
619
620status_t Camera3Stream::registerBuffersLocked(camera3_device *hal3Device) {
621 ATRACE_CALL();
Igor Murashkin13d315e2014-04-03 18:09:04 -0700622
623 /**
624 * >= CAMERA_DEVICE_API_VERSION_3_2:
625 *
626 * camera3_device_t->ops->register_stream_buffers() is not called and must
627 * be NULL.
628 */
629 if (hal3Device->common.version >= CAMERA_DEVICE_API_VERSION_3_2) {
630 ALOGV("%s: register_stream_buffers unused as of HAL3.2", __FUNCTION__);
631
Igor Murashkinc758f222014-08-19 15:14:29 -0700632 if (hal3Device->ops->register_stream_buffers != NULL) {
Igor Murashkin13d315e2014-04-03 18:09:04 -0700633 ALOGE("%s: register_stream_buffers is deprecated in HAL3.2; "
634 "must be set to NULL in camera3_device::ops", __FUNCTION__);
635 return INVALID_OPERATION;
Igor Murashkin13d315e2014-04-03 18:09:04 -0700636 }
637
638 return OK;
Igor Murashkin13d315e2014-04-03 18:09:04 -0700639 }
640
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700641 ALOGV("%s: register_stream_buffers using deprecated code path", __FUNCTION__);
642
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800643 status_t res;
644
645 size_t bufferCount = getBufferCountLocked();
646
647 Vector<buffer_handle_t*> buffers;
Igor Murashkin13d315e2014-04-03 18:09:04 -0700648 buffers.insertAt(/*prototype_item*/NULL, /*index*/0, bufferCount);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800649
650 camera3_stream_buffer_set bufferSet = camera3_stream_buffer_set();
651 bufferSet.stream = this;
652 bufferSet.num_buffers = bufferCount;
653 bufferSet.buffers = buffers.editArray();
654
655 Vector<camera3_stream_buffer_t> streamBuffers;
Igor Murashkin13d315e2014-04-03 18:09:04 -0700656 streamBuffers.insertAt(camera3_stream_buffer_t(), /*index*/0, bufferCount);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800657
658 // Register all buffers with the HAL. This means getting all the buffers
659 // from the stream, providing them to the HAL with the
660 // register_stream_buffers() method, and then returning them back to the
661 // stream in the error state, since they won't have valid data.
662 //
663 // Only registered buffers can be sent to the HAL.
664
665 uint32_t bufferIdx = 0;
666 for (; bufferIdx < bufferCount; bufferIdx++) {
667 res = getBufferLocked( &streamBuffers.editItemAt(bufferIdx) );
668 if (res != OK) {
669 ALOGE("%s: Unable to get buffer %d for registration with HAL",
670 __FUNCTION__, bufferIdx);
671 // Skip registering, go straight to cleanup
672 break;
673 }
674
675 sp<Fence> fence = new Fence(streamBuffers[bufferIdx].acquire_fence);
Mathias Agopiand7644242013-05-16 18:07:35 -0700676 fence->waitForever("Camera3Stream::registerBuffers");
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800677
678 buffers.editItemAt(bufferIdx) = streamBuffers[bufferIdx].buffer;
679 }
680 if (bufferIdx == bufferCount) {
681 // Got all buffers, register with HAL
Colin Crosse5729fa2014-03-21 15:04:25 -0700682 ALOGV("%s: Registering %zu buffers with camera HAL",
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800683 __FUNCTION__, bufferCount);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -0700684 ATRACE_BEGIN("camera3->register_stream_buffers");
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800685 res = hal3Device->ops->register_stream_buffers(hal3Device,
686 &bufferSet);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -0700687 ATRACE_END();
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800688 }
689
690 // Return all valid buffers to stream, in ERROR state to indicate
691 // they weren't filled.
692 for (size_t i = 0; i < bufferIdx; i++) {
693 streamBuffers.editItemAt(i).release_fence = -1;
694 streamBuffers.editItemAt(i).status = CAMERA3_BUFFER_STATUS_ERROR;
695 returnBufferLocked(streamBuffers[i], 0);
696 }
697
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700698 mPrepared = true;
699
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800700 return res;
701}
702
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700703status_t Camera3Stream::getBufferLocked(camera3_stream_buffer *) {
704 ALOGE("%s: This type of stream does not support output", __FUNCTION__);
705 return INVALID_OPERATION;
706}
707status_t Camera3Stream::returnBufferLocked(const camera3_stream_buffer &,
708 nsecs_t) {
709 ALOGE("%s: This type of stream does not support output", __FUNCTION__);
710 return INVALID_OPERATION;
711}
712status_t Camera3Stream::getInputBufferLocked(camera3_stream_buffer *) {
713 ALOGE("%s: This type of stream does not support input", __FUNCTION__);
714 return INVALID_OPERATION;
715}
716status_t Camera3Stream::returnInputBufferLocked(
717 const camera3_stream_buffer &) {
718 ALOGE("%s: This type of stream does not support input", __FUNCTION__);
719 return INVALID_OPERATION;
720}
Eino-Ville Talvala02bf0322016-02-18 12:41:10 -0800721status_t Camera3Stream::getInputBufferProducerLocked(sp<IGraphicBufferProducer>*) {
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700722 ALOGE("%s: This type of stream does not support input", __FUNCTION__);
723 return INVALID_OPERATION;
724}
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700725
Igor Murashkin2fba5842013-04-22 14:03:54 -0700726void Camera3Stream::addBufferListener(
727 wp<Camera3StreamBufferListener> listener) {
728 Mutex::Autolock l(mLock);
Zhijun Hef0d962a2014-06-30 10:24:11 -0700729
730 List<wp<Camera3StreamBufferListener> >::iterator it, end;
731 for (it = mBufferListenerList.begin(), end = mBufferListenerList.end();
732 it != end;
733 ) {
734 if (*it == listener) {
735 ALOGE("%s: Try to add the same listener twice, ignoring...", __FUNCTION__);
736 return;
737 }
738 it++;
739 }
740
Igor Murashkin2fba5842013-04-22 14:03:54 -0700741 mBufferListenerList.push_back(listener);
742}
743
744void Camera3Stream::removeBufferListener(
745 const sp<Camera3StreamBufferListener>& listener) {
746 Mutex::Autolock l(mLock);
747
748 bool erased = true;
749 List<wp<Camera3StreamBufferListener> >::iterator it, end;
750 for (it = mBufferListenerList.begin(), end = mBufferListenerList.end();
751 it != end;
752 ) {
753
754 if (*it == listener) {
755 it = mBufferListenerList.erase(it);
756 erased = true;
757 } else {
758 ++it;
759 }
760 }
761
762 if (!erased) {
763 ALOGW("%s: Could not find listener to remove, already removed",
764 __FUNCTION__);
765 }
766}
767
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800768}; // namespace camera3
769
770}; // namespace android