blob: 7163d62c5256dbc12e08d498a6fec5f3735a009e [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,
Yin-Chia Yehb97babb2015-03-12 13:42:44 -070050 android_dataspace dataSpace, camera3_stream_rotation_t rotation) :
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -080051 camera3_stream(),
52 mId(id),
53 mName(String8::format("Camera3Stream[%d]", id)),
54 mMaxSize(maxSize),
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -070055 mState(STATE_CONSTRUCTED),
Ruben Brunkc78ac262015-08-13 17:58:46 -070056 mStatusId(StatusTracker::NO_STATUS_ID),
57 mLastMaxCount(Camera3StreamInterface::ALLOCATE_PIPELINE_MAX) {
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -080058
59 camera3_stream::stream_type = type;
60 camera3_stream::width = width;
61 camera3_stream::height = height;
62 camera3_stream::format = format;
Eino-Ville Talvala3d82c0d2015-02-23 15:19:19 -080063 camera3_stream::data_space = dataSpace;
Yin-Chia Yehb97babb2015-03-12 13:42:44 -070064 camera3_stream::rotation = rotation;
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -080065 camera3_stream::usage = 0;
66 camera3_stream::max_buffers = 0;
67 camera3_stream::priv = NULL;
68
69 if (format == HAL_PIXEL_FORMAT_BLOB && maxSize == 0) {
70 ALOGE("%s: BLOB format with size == 0", __FUNCTION__);
71 mState = STATE_ERROR;
72 }
73}
74
75int Camera3Stream::getId() const {
76 return mId;
77}
78
79uint32_t Camera3Stream::getWidth() const {
80 return camera3_stream::width;
81}
82
83uint32_t Camera3Stream::getHeight() const {
84 return camera3_stream::height;
85}
86
87int Camera3Stream::getFormat() const {
88 return camera3_stream::format;
89}
90
Eino-Ville Talvala3d82c0d2015-02-23 15:19:19 -080091android_dataspace Camera3Stream::getDataSpace() const {
92 return camera3_stream::data_space;
93}
94
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -080095camera3_stream* Camera3Stream::startConfiguration() {
Eino-Ville Talvalab2f5b192013-07-30 14:36:03 -070096 ATRACE_CALL();
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -080097 Mutex::Autolock l(mLock);
Eino-Ville Talvalab2f5b192013-07-30 14:36:03 -070098 status_t res;
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -080099
100 switch (mState) {
101 case STATE_ERROR:
102 ALOGE("%s: In error state", __FUNCTION__);
103 return NULL;
104 case STATE_CONSTRUCTED:
105 // OK
106 break;
107 case STATE_IN_CONFIG:
108 case STATE_IN_RECONFIG:
109 // Can start config again with no trouble; but don't redo
110 // oldUsage/oldMaxBuffers
111 return this;
112 case STATE_CONFIGURED:
Chien-Yu Chen90746f42015-04-15 13:50:13 -0700113 if (hasOutstandingBuffersLocked()) {
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800114 ALOGE("%s: Cannot configure stream; has outstanding buffers",
115 __FUNCTION__);
116 return NULL;
117 }
118 break;
119 default:
120 ALOGE("%s: Unknown state %d", __FUNCTION__, mState);
121 return NULL;
122 }
123
Eino-Ville Talvalab2f5b192013-07-30 14:36:03 -0700124 oldUsage = camera3_stream::usage;
125 oldMaxBuffers = camera3_stream::max_buffers;
126
127 res = getEndpointUsage(&(camera3_stream::usage));
128 if (res != OK) {
129 ALOGE("%s: Cannot query consumer endpoint usage!",
130 __FUNCTION__);
131 return NULL;
132 }
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800133
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700134 // Stop tracking if currently doing so
135 if (mStatusId != StatusTracker::NO_STATUS_ID) {
136 sp<StatusTracker> statusTracker = mStatusTracker.promote();
137 if (statusTracker != 0) {
138 statusTracker->removeComponent(mStatusId);
139 }
140 mStatusId = StatusTracker::NO_STATUS_ID;
141 }
142
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800143 if (mState == STATE_CONSTRUCTED) {
144 mState = STATE_IN_CONFIG;
145 } else { // mState == STATE_CONFIGURED
Igor Murashkin13d315e2014-04-03 18:09:04 -0700146 LOG_ALWAYS_FATAL_IF(mState != STATE_CONFIGURED, "Invalid state: 0x%x", mState);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800147 mState = STATE_IN_RECONFIG;
148 }
149
150 return this;
151}
152
153bool Camera3Stream::isConfiguring() const {
154 Mutex::Autolock l(mLock);
155 return (mState == STATE_IN_CONFIG) || (mState == STATE_IN_RECONFIG);
156}
157
158status_t Camera3Stream::finishConfiguration(camera3_device *hal3Device) {
Eino-Ville Talvalab2f5b192013-07-30 14:36:03 -0700159 ATRACE_CALL();
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800160 Mutex::Autolock l(mLock);
161 switch (mState) {
162 case STATE_ERROR:
163 ALOGE("%s: In error state", __FUNCTION__);
164 return INVALID_OPERATION;
165 case STATE_IN_CONFIG:
166 case STATE_IN_RECONFIG:
167 // OK
168 break;
169 case STATE_CONSTRUCTED:
170 case STATE_CONFIGURED:
171 ALOGE("%s: Cannot finish configuration that hasn't been started",
172 __FUNCTION__);
173 return INVALID_OPERATION;
174 default:
175 ALOGE("%s: Unknown state", __FUNCTION__);
176 return INVALID_OPERATION;
177 }
178
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700179 // Register for idle tracking
180 sp<StatusTracker> statusTracker = mStatusTracker.promote();
181 if (statusTracker != 0) {
182 mStatusId = statusTracker->addComponent();
183 }
184
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800185 // Check if the stream configuration is unchanged, and skip reallocation if
186 // so. As documented in hardware/camera3.h:configure_streams().
187 if (mState == STATE_IN_RECONFIG &&
Eino-Ville Talvalab2f5b192013-07-30 14:36:03 -0700188 oldUsage == camera3_stream::usage &&
189 oldMaxBuffers == camera3_stream::max_buffers) {
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800190 mState = STATE_CONFIGURED;
191 return OK;
192 }
193
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700194 // Reset prepared state, since buffer config has changed, and existing
195 // allocations are no longer valid
196 mPrepared = false;
197 mStreamUnpreparable = false;
198
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800199 status_t res;
200 res = configureQueueLocked();
201 if (res != OK) {
202 ALOGE("%s: Unable to configure stream %d queue: %s (%d)",
203 __FUNCTION__, mId, strerror(-res), res);
204 mState = STATE_ERROR;
205 return res;
206 }
207
208 res = registerBuffersLocked(hal3Device);
209 if (res != OK) {
210 ALOGE("%s: Unable to register stream buffers with HAL: %s (%d)",
211 __FUNCTION__, strerror(-res), res);
212 mState = STATE_ERROR;
213 return res;
214 }
215
216 mState = STATE_CONFIGURED;
217
218 return res;
219}
220
Eino-Ville Talvala17543512014-08-06 14:32:02 -0700221status_t Camera3Stream::cancelConfiguration() {
222 ATRACE_CALL();
223 Mutex::Autolock l(mLock);
224 switch (mState) {
225 case STATE_ERROR:
226 ALOGE("%s: In error state", __FUNCTION__);
227 return INVALID_OPERATION;
228 case STATE_IN_CONFIG:
229 case STATE_IN_RECONFIG:
230 // OK
231 break;
232 case STATE_CONSTRUCTED:
233 case STATE_CONFIGURED:
234 ALOGE("%s: Cannot cancel configuration that hasn't been started",
235 __FUNCTION__);
236 return INVALID_OPERATION;
237 default:
238 ALOGE("%s: Unknown state", __FUNCTION__);
239 return INVALID_OPERATION;
240 }
241
242 camera3_stream::usage = oldUsage;
243 camera3_stream::max_buffers = oldMaxBuffers;
244
Yin-Chia Yeh3ea3fcd2014-09-05 14:14:44 -0700245 mState = (mState == STATE_IN_RECONFIG) ? STATE_CONFIGURED : STATE_CONSTRUCTED;
Eino-Ville Talvala17543512014-08-06 14:32:02 -0700246 return OK;
247}
248
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700249bool Camera3Stream::isUnpreparable() {
250 ATRACE_CALL();
251
252 Mutex::Autolock l(mLock);
253 return mStreamUnpreparable;
254}
255
Ruben Brunkc78ac262015-08-13 17:58:46 -0700256status_t Camera3Stream::startPrepare(int maxCount) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700257 ATRACE_CALL();
258
259 Mutex::Autolock l(mLock);
260 status_t res = OK;
261
Ruben Brunkc78ac262015-08-13 17:58:46 -0700262 if (maxCount < 0) {
263 ALOGE("%s: Stream %d: Can't prepare stream if max buffer count (%d) is < 0",
264 __FUNCTION__, mId, maxCount);
265 return BAD_VALUE;
266 }
267
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700268 // This function should be only called when the stream is configured already.
269 if (mState != STATE_CONFIGURED) {
270 ALOGE("%s: Stream %d: Can't prepare stream if stream is not in CONFIGURED "
271 "state %d", __FUNCTION__, mId, mState);
272 return INVALID_OPERATION;
273 }
274
275 // This function can't be called if the stream has already received filled
276 // buffers
277 if (mStreamUnpreparable) {
278 ALOGE("%s: Stream %d: Can't prepare stream that's already in use",
279 __FUNCTION__, mId);
280 return INVALID_OPERATION;
281 }
282
283 if (getHandoutOutputBufferCountLocked() > 0) {
284 ALOGE("%s: Stream %d: Can't prepare stream that has outstanding buffers",
285 __FUNCTION__, mId);
286 return INVALID_OPERATION;
287 }
288
Ruben Brunkc78ac262015-08-13 17:58:46 -0700289
290
291 size_t pipelineMax = getBufferCountLocked();
292 size_t clampedCount = (pipelineMax < static_cast<size_t>(maxCount)) ?
293 pipelineMax : static_cast<size_t>(maxCount);
294 size_t bufferCount = (maxCount == Camera3StreamInterface::ALLOCATE_PIPELINE_MAX) ?
295 pipelineMax : clampedCount;
296
297 mPrepared = bufferCount <= mLastMaxCount;
298
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700299 if (mPrepared) return OK;
300
Ruben Brunkc78ac262015-08-13 17:58:46 -0700301 mLastMaxCount = bufferCount;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700302
303 mPreparedBuffers.insertAt(camera3_stream_buffer_t(), /*index*/0, bufferCount);
304 mPreparedBufferIdx = 0;
305
306 mState = STATE_PREPARING;
307
308 return NOT_ENOUGH_DATA;
309}
310
311bool Camera3Stream::isPreparing() const {
312 Mutex::Autolock l(mLock);
313 return mState == STATE_PREPARING;
314}
315
316status_t Camera3Stream::prepareNextBuffer() {
317 ATRACE_CALL();
318
319 Mutex::Autolock l(mLock);
320 status_t res = OK;
321
322 // This function should be only called when the stream is preparing
323 if (mState != STATE_PREPARING) {
324 ALOGE("%s: Stream %d: Can't prepare buffer if stream is not in PREPARING "
325 "state %d", __FUNCTION__, mId, mState);
326 return INVALID_OPERATION;
327 }
328
329 // Get next buffer - this may allocate, and take a while for large buffers
330 res = getBufferLocked( &mPreparedBuffers.editItemAt(mPreparedBufferIdx) );
331 if (res != OK) {
332 ALOGE("%s: Stream %d: Unable to allocate buffer %d during preparation",
333 __FUNCTION__, mId, mPreparedBufferIdx);
334 return NO_INIT;
335 }
336
337 mPreparedBufferIdx++;
338
339 // Check if we still have buffers left to allocate
340 if (mPreparedBufferIdx < mPreparedBuffers.size()) {
341 return NOT_ENOUGH_DATA;
342 }
343
344 // Done with prepare - mark stream as such, and return all buffers
345 // via cancelPrepare
346 mPrepared = true;
347
348 return cancelPrepareLocked();
349}
350
351status_t Camera3Stream::cancelPrepare() {
352 ATRACE_CALL();
353
354 Mutex::Autolock l(mLock);
355
356 return cancelPrepareLocked();
357}
358
359status_t Camera3Stream::cancelPrepareLocked() {
360 status_t res = OK;
361
362 // This function should be only called when the stream is mid-preparing.
363 if (mState != STATE_PREPARING) {
364 ALOGE("%s: Stream %d: Can't cancel prepare stream if stream is not in "
365 "PREPARING state %d", __FUNCTION__, mId, mState);
366 return INVALID_OPERATION;
367 }
368
369 // Return all valid buffers to stream, in ERROR state to indicate
370 // they weren't filled.
371 for (size_t i = 0; i < mPreparedBufferIdx; i++) {
372 mPreparedBuffers.editItemAt(i).release_fence = -1;
373 mPreparedBuffers.editItemAt(i).status = CAMERA3_BUFFER_STATUS_ERROR;
374 returnBufferLocked(mPreparedBuffers[i], 0);
375 }
376 mPreparedBuffers.clear();
377 mPreparedBufferIdx = 0;
378
379 mState = STATE_CONFIGURED;
380
381 return res;
382}
383
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -0700384status_t Camera3Stream::tearDown() {
385 ATRACE_CALL();
386 Mutex::Autolock l(mLock);
387
388 status_t res = OK;
389
390 // This function should be only called when the stream is configured.
391 if (mState != STATE_CONFIGURED) {
392 ALOGE("%s: Stream %d: Can't tear down stream if stream is not in "
393 "CONFIGURED state %d", __FUNCTION__, mId, mState);
394 return INVALID_OPERATION;
395 }
396
397 // If any buffers have been handed to the HAL, the stream cannot be torn down.
398 if (getHandoutOutputBufferCountLocked() > 0) {
399 ALOGE("%s: Stream %d: Can't tear down a stream that has outstanding buffers",
400 __FUNCTION__, mId);
401 return INVALID_OPERATION;
402 }
403
404 // Free buffers by disconnecting and then reconnecting to the buffer queue
405 // Only unused buffers will be dropped immediately; buffers that have been filled
406 // and are waiting to be acquired by the consumer and buffers that are currently
407 // acquired will be freed once they are released by the consumer.
408
409 res = disconnectLocked();
410 if (res != OK) {
411 if (res == -ENOTCONN) {
412 // queue has been disconnected, nothing left to do, so exit with success
413 return OK;
414 }
415 ALOGE("%s: Stream %d: Unable to disconnect to tear down buffers: %s (%d)",
416 __FUNCTION__, mId, strerror(-res), res);
417 return res;
418 }
419
420 mState = STATE_IN_CONFIG;
421
422 res = configureQueueLocked();
423 if (res != OK) {
424 ALOGE("%s: Unable to configure stream %d queue: %s (%d)",
425 __FUNCTION__, mId, strerror(-res), res);
426 mState = STATE_ERROR;
427 return res;
428 }
429
430 // Reset prepared state, since we've reconnected to the queue and can prepare again.
431 mPrepared = false;
432 mStreamUnpreparable = false;
433
434 mState = STATE_CONFIGURED;
435
436 return OK;
437}
438
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800439status_t Camera3Stream::getBuffer(camera3_stream_buffer *buffer) {
440 ATRACE_CALL();
441 Mutex::Autolock l(mLock);
Zhijun He6adc9cc2014-04-15 14:09:55 -0700442 status_t res = OK;
Igor Murashkin2fba5842013-04-22 14:03:54 -0700443
Zhijun He6adc9cc2014-04-15 14:09:55 -0700444 // This function should be only called when the stream is configured already.
445 if (mState != STATE_CONFIGURED) {
446 ALOGE("%s: Stream %d: Can't get buffers if stream is not in CONFIGURED state %d",
447 __FUNCTION__, mId, mState);
448 return INVALID_OPERATION;
449 }
450
451 // Wait for new buffer returned back if we are running into the limit.
452 if (getHandoutOutputBufferCountLocked() == camera3_stream::max_buffers) {
453 ALOGV("%s: Already dequeued max output buffers (%d), wait for next returned one.",
454 __FUNCTION__, camera3_stream::max_buffers);
455 res = mOutputBufferReturnedSignal.waitRelative(mLock, kWaitForBufferDuration);
456 if (res != OK) {
457 if (res == TIMED_OUT) {
458 ALOGE("%s: wait for output buffer return timed out after %lldms", __FUNCTION__,
459 kWaitForBufferDuration / 1000000LL);
460 }
461 return res;
462 }
463 }
464
465 res = getBufferLocked(buffer);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700466 if (res == OK) {
467 fireBufferListenersLocked(*buffer, /*acquired*/true, /*output*/true);
468 }
469
470 return res;
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800471}
472
473status_t Camera3Stream::returnBuffer(const camera3_stream_buffer &buffer,
474 nsecs_t timestamp) {
475 ATRACE_CALL();
476 Mutex::Autolock l(mLock);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700477
Igor Murashkin13d315e2014-04-03 18:09:04 -0700478 /**
479 * TODO: Check that the state is valid first.
480 *
481 * <HAL3.2 IN_CONFIG and IN_RECONFIG in addition to CONFIGURED.
482 * >= HAL3.2 CONFIGURED only
483 *
484 * Do this for getBuffer as well.
485 */
Igor Murashkin2fba5842013-04-22 14:03:54 -0700486 status_t res = returnBufferLocked(buffer, timestamp);
487 if (res == OK) {
488 fireBufferListenersLocked(buffer, /*acquired*/false, /*output*/true);
Zhijun He6adc9cc2014-04-15 14:09:55 -0700489 mOutputBufferReturnedSignal.signal();
Igor Murashkin2fba5842013-04-22 14:03:54 -0700490 }
491
492 return res;
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800493}
494
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700495status_t Camera3Stream::getInputBuffer(camera3_stream_buffer *buffer) {
496 ATRACE_CALL();
497 Mutex::Autolock l(mLock);
Zhijun He6adc9cc2014-04-15 14:09:55 -0700498 status_t res = OK;
Igor Murashkin2fba5842013-04-22 14:03:54 -0700499
Zhijun He6adc9cc2014-04-15 14:09:55 -0700500 // This function should be only called when the stream is configured already.
501 if (mState != STATE_CONFIGURED) {
502 ALOGE("%s: Stream %d: Can't get input buffers if stream is not in CONFIGURED state %d",
503 __FUNCTION__, mId, mState);
504 return INVALID_OPERATION;
505 }
506
507 // Wait for new buffer returned back if we are running into the limit.
508 if (getHandoutInputBufferCountLocked() == camera3_stream::max_buffers) {
509 ALOGV("%s: Already dequeued max input buffers (%d), wait for next returned one.",
510 __FUNCTION__, camera3_stream::max_buffers);
511 res = mInputBufferReturnedSignal.waitRelative(mLock, kWaitForBufferDuration);
512 if (res != OK) {
513 if (res == TIMED_OUT) {
514 ALOGE("%s: wait for input buffer return timed out after %lldms", __FUNCTION__,
515 kWaitForBufferDuration / 1000000LL);
516 }
517 return res;
518 }
519 }
520
521 res = getInputBufferLocked(buffer);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700522 if (res == OK) {
523 fireBufferListenersLocked(*buffer, /*acquired*/true, /*output*/false);
524 }
525
526 return res;
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700527}
528
529status_t Camera3Stream::returnInputBuffer(const camera3_stream_buffer &buffer) {
530 ATRACE_CALL();
531 Mutex::Autolock l(mLock);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700532
533 status_t res = returnInputBufferLocked(buffer);
534 if (res == OK) {
535 fireBufferListenersLocked(buffer, /*acquired*/false, /*output*/false);
Zhijun He6adc9cc2014-04-15 14:09:55 -0700536 mInputBufferReturnedSignal.signal();
Igor Murashkin2fba5842013-04-22 14:03:54 -0700537 }
538 return res;
539}
540
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700541status_t Camera3Stream::getInputBufferProducer(sp<IGraphicBufferProducer> *producer) {
542 ATRACE_CALL();
543 Mutex::Autolock l(mLock);
544
545 return getInputBufferProducerLocked(producer);
546}
547
Igor Murashkin2fba5842013-04-22 14:03:54 -0700548void Camera3Stream::fireBufferListenersLocked(
549 const camera3_stream_buffer& /*buffer*/, bool acquired, bool output) {
550 List<wp<Camera3StreamBufferListener> >::iterator it, end;
551
552 // TODO: finish implementing
553
554 Camera3StreamBufferListener::BufferInfo info =
555 Camera3StreamBufferListener::BufferInfo();
556 info.mOutput = output;
557 // TODO: rest of fields
558
559 for (it = mBufferListenerList.begin(), end = mBufferListenerList.end();
560 it != end;
561 ++it) {
562
563 sp<Camera3StreamBufferListener> listener = it->promote();
564 if (listener != 0) {
565 if (acquired) {
566 listener->onBufferAcquired(info);
567 } else {
568 listener->onBufferReleased(info);
569 }
570 }
571 }
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700572}
573
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800574bool Camera3Stream::hasOutstandingBuffers() const {
575 ATRACE_CALL();
576 Mutex::Autolock l(mLock);
577 return hasOutstandingBuffersLocked();
578}
579
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700580status_t Camera3Stream::setStatusTracker(sp<StatusTracker> statusTracker) {
581 Mutex::Autolock l(mLock);
582 sp<StatusTracker> oldTracker = mStatusTracker.promote();
583 if (oldTracker != 0 && mStatusId != StatusTracker::NO_STATUS_ID) {
584 oldTracker->removeComponent(mStatusId);
585 }
586 mStatusId = StatusTracker::NO_STATUS_ID;
587 mStatusTracker = statusTracker;
588
589 return OK;
590}
591
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800592status_t Camera3Stream::disconnect() {
593 ATRACE_CALL();
594 Mutex::Autolock l(mLock);
Igor Murashkine2172be2013-05-28 15:31:39 -0700595 ALOGV("%s: Stream %d: Disconnecting...", __FUNCTION__, mId);
596 status_t res = disconnectLocked();
597
598 if (res == -ENOTCONN) {
599 // "Already disconnected" -- not an error
600 return OK;
601 } else {
602 return res;
603 }
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800604}
605
606status_t Camera3Stream::registerBuffersLocked(camera3_device *hal3Device) {
607 ATRACE_CALL();
Igor Murashkin13d315e2014-04-03 18:09:04 -0700608
609 /**
610 * >= CAMERA_DEVICE_API_VERSION_3_2:
611 *
612 * camera3_device_t->ops->register_stream_buffers() is not called and must
613 * be NULL.
614 */
615 if (hal3Device->common.version >= CAMERA_DEVICE_API_VERSION_3_2) {
616 ALOGV("%s: register_stream_buffers unused as of HAL3.2", __FUNCTION__);
617
Igor Murashkinc758f222014-08-19 15:14:29 -0700618 if (hal3Device->ops->register_stream_buffers != NULL) {
Igor Murashkin13d315e2014-04-03 18:09:04 -0700619 ALOGE("%s: register_stream_buffers is deprecated in HAL3.2; "
620 "must be set to NULL in camera3_device::ops", __FUNCTION__);
621 return INVALID_OPERATION;
Igor Murashkin13d315e2014-04-03 18:09:04 -0700622 }
623
624 return OK;
Igor Murashkin13d315e2014-04-03 18:09:04 -0700625 }
626
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700627 ALOGV("%s: register_stream_buffers using deprecated code path", __FUNCTION__);
628
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800629 status_t res;
630
631 size_t bufferCount = getBufferCountLocked();
632
633 Vector<buffer_handle_t*> buffers;
Igor Murashkin13d315e2014-04-03 18:09:04 -0700634 buffers.insertAt(/*prototype_item*/NULL, /*index*/0, bufferCount);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800635
636 camera3_stream_buffer_set bufferSet = camera3_stream_buffer_set();
637 bufferSet.stream = this;
638 bufferSet.num_buffers = bufferCount;
639 bufferSet.buffers = buffers.editArray();
640
641 Vector<camera3_stream_buffer_t> streamBuffers;
Igor Murashkin13d315e2014-04-03 18:09:04 -0700642 streamBuffers.insertAt(camera3_stream_buffer_t(), /*index*/0, bufferCount);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800643
644 // Register all buffers with the HAL. This means getting all the buffers
645 // from the stream, providing them to the HAL with the
646 // register_stream_buffers() method, and then returning them back to the
647 // stream in the error state, since they won't have valid data.
648 //
649 // Only registered buffers can be sent to the HAL.
650
651 uint32_t bufferIdx = 0;
652 for (; bufferIdx < bufferCount; bufferIdx++) {
653 res = getBufferLocked( &streamBuffers.editItemAt(bufferIdx) );
654 if (res != OK) {
655 ALOGE("%s: Unable to get buffer %d for registration with HAL",
656 __FUNCTION__, bufferIdx);
657 // Skip registering, go straight to cleanup
658 break;
659 }
660
661 sp<Fence> fence = new Fence(streamBuffers[bufferIdx].acquire_fence);
Mathias Agopiand7644242013-05-16 18:07:35 -0700662 fence->waitForever("Camera3Stream::registerBuffers");
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800663
664 buffers.editItemAt(bufferIdx) = streamBuffers[bufferIdx].buffer;
665 }
666 if (bufferIdx == bufferCount) {
667 // Got all buffers, register with HAL
Colin Crosse5729fa2014-03-21 15:04:25 -0700668 ALOGV("%s: Registering %zu buffers with camera HAL",
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800669 __FUNCTION__, bufferCount);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -0700670 ATRACE_BEGIN("camera3->register_stream_buffers");
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800671 res = hal3Device->ops->register_stream_buffers(hal3Device,
672 &bufferSet);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -0700673 ATRACE_END();
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800674 }
675
676 // Return all valid buffers to stream, in ERROR state to indicate
677 // they weren't filled.
678 for (size_t i = 0; i < bufferIdx; i++) {
679 streamBuffers.editItemAt(i).release_fence = -1;
680 streamBuffers.editItemAt(i).status = CAMERA3_BUFFER_STATUS_ERROR;
681 returnBufferLocked(streamBuffers[i], 0);
682 }
683
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700684 mPrepared = true;
685
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800686 return res;
687}
688
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700689status_t Camera3Stream::getBufferLocked(camera3_stream_buffer *) {
690 ALOGE("%s: This type of stream does not support output", __FUNCTION__);
691 return INVALID_OPERATION;
692}
693status_t Camera3Stream::returnBufferLocked(const camera3_stream_buffer &,
694 nsecs_t) {
695 ALOGE("%s: This type of stream does not support output", __FUNCTION__);
696 return INVALID_OPERATION;
697}
698status_t Camera3Stream::getInputBufferLocked(camera3_stream_buffer *) {
699 ALOGE("%s: This type of stream does not support input", __FUNCTION__);
700 return INVALID_OPERATION;
701}
702status_t Camera3Stream::returnInputBufferLocked(
703 const camera3_stream_buffer &) {
704 ALOGE("%s: This type of stream does not support input", __FUNCTION__);
705 return INVALID_OPERATION;
706}
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700707status_t Camera3Stream::getInputBufferProducerLocked(sp<IGraphicBufferProducer> *producer) {
708 ALOGE("%s: This type of stream does not support input", __FUNCTION__);
709 return INVALID_OPERATION;
710}
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700711
Igor Murashkin2fba5842013-04-22 14:03:54 -0700712void Camera3Stream::addBufferListener(
713 wp<Camera3StreamBufferListener> listener) {
714 Mutex::Autolock l(mLock);
Zhijun Hef0d962a2014-06-30 10:24:11 -0700715
716 List<wp<Camera3StreamBufferListener> >::iterator it, end;
717 for (it = mBufferListenerList.begin(), end = mBufferListenerList.end();
718 it != end;
719 ) {
720 if (*it == listener) {
721 ALOGE("%s: Try to add the same listener twice, ignoring...", __FUNCTION__);
722 return;
723 }
724 it++;
725 }
726
Igor Murashkin2fba5842013-04-22 14:03:54 -0700727 mBufferListenerList.push_back(listener);
728}
729
730void Camera3Stream::removeBufferListener(
731 const sp<Camera3StreamBufferListener>& listener) {
732 Mutex::Autolock l(mLock);
733
734 bool erased = true;
735 List<wp<Camera3StreamBufferListener> >::iterator it, end;
736 for (it = mBufferListenerList.begin(), end = mBufferListenerList.end();
737 it != end;
738 ) {
739
740 if (*it == listener) {
741 it = mBufferListenerList.erase(it);
742 erased = true;
743 } else {
744 ++it;
745 }
746 }
747
748 if (!erased) {
749 ALOGW("%s: Could not find listener to remove, already removed",
750 __FUNCTION__);
751 }
752}
753
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800754}; // namespace camera3
755
756}; // namespace android