blob: 691764fb63a18af07a0f4f374e5575a131fbd169 [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) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -0700458 ALOGE("%s: wait for output buffer return timed out after %lldms (max_buffers %d)",
459 __FUNCTION__, kWaitForBufferDuration / 1000000LL,
460 camera3_stream::max_buffers);
Zhijun He6adc9cc2014-04-15 14:09:55 -0700461 }
462 return res;
463 }
464 }
465
466 res = getBufferLocked(buffer);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700467 if (res == OK) {
468 fireBufferListenersLocked(*buffer, /*acquired*/true, /*output*/true);
469 }
470
471 return res;
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800472}
473
474status_t Camera3Stream::returnBuffer(const camera3_stream_buffer &buffer,
475 nsecs_t timestamp) {
476 ATRACE_CALL();
477 Mutex::Autolock l(mLock);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700478
Igor Murashkin13d315e2014-04-03 18:09:04 -0700479 /**
480 * TODO: Check that the state is valid first.
481 *
482 * <HAL3.2 IN_CONFIG and IN_RECONFIG in addition to CONFIGURED.
483 * >= HAL3.2 CONFIGURED only
484 *
485 * Do this for getBuffer as well.
486 */
Igor Murashkin2fba5842013-04-22 14:03:54 -0700487 status_t res = returnBufferLocked(buffer, timestamp);
488 if (res == OK) {
489 fireBufferListenersLocked(buffer, /*acquired*/false, /*output*/true);
Zhijun He6adc9cc2014-04-15 14:09:55 -0700490 mOutputBufferReturnedSignal.signal();
Igor Murashkin2fba5842013-04-22 14:03:54 -0700491 }
492
493 return res;
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800494}
495
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700496status_t Camera3Stream::getInputBuffer(camera3_stream_buffer *buffer) {
497 ATRACE_CALL();
498 Mutex::Autolock l(mLock);
Zhijun He6adc9cc2014-04-15 14:09:55 -0700499 status_t res = OK;
Igor Murashkin2fba5842013-04-22 14:03:54 -0700500
Zhijun He6adc9cc2014-04-15 14:09:55 -0700501 // This function should be only called when the stream is configured already.
502 if (mState != STATE_CONFIGURED) {
503 ALOGE("%s: Stream %d: Can't get input buffers if stream is not in CONFIGURED state %d",
504 __FUNCTION__, mId, mState);
505 return INVALID_OPERATION;
506 }
507
508 // Wait for new buffer returned back if we are running into the limit.
509 if (getHandoutInputBufferCountLocked() == camera3_stream::max_buffers) {
510 ALOGV("%s: Already dequeued max input buffers (%d), wait for next returned one.",
511 __FUNCTION__, camera3_stream::max_buffers);
512 res = mInputBufferReturnedSignal.waitRelative(mLock, kWaitForBufferDuration);
513 if (res != OK) {
514 if (res == TIMED_OUT) {
515 ALOGE("%s: wait for input buffer return timed out after %lldms", __FUNCTION__,
516 kWaitForBufferDuration / 1000000LL);
517 }
518 return res;
519 }
520 }
521
522 res = getInputBufferLocked(buffer);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700523 if (res == OK) {
524 fireBufferListenersLocked(*buffer, /*acquired*/true, /*output*/false);
525 }
526
527 return res;
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700528}
529
530status_t Camera3Stream::returnInputBuffer(const camera3_stream_buffer &buffer) {
531 ATRACE_CALL();
532 Mutex::Autolock l(mLock);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700533
534 status_t res = returnInputBufferLocked(buffer);
535 if (res == OK) {
536 fireBufferListenersLocked(buffer, /*acquired*/false, /*output*/false);
Zhijun He6adc9cc2014-04-15 14:09:55 -0700537 mInputBufferReturnedSignal.signal();
Igor Murashkin2fba5842013-04-22 14:03:54 -0700538 }
539 return res;
540}
541
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700542status_t Camera3Stream::getInputBufferProducer(sp<IGraphicBufferProducer> *producer) {
543 ATRACE_CALL();
544 Mutex::Autolock l(mLock);
545
546 return getInputBufferProducerLocked(producer);
547}
548
Igor Murashkin2fba5842013-04-22 14:03:54 -0700549void Camera3Stream::fireBufferListenersLocked(
550 const camera3_stream_buffer& /*buffer*/, bool acquired, bool output) {
551 List<wp<Camera3StreamBufferListener> >::iterator it, end;
552
553 // TODO: finish implementing
554
555 Camera3StreamBufferListener::BufferInfo info =
556 Camera3StreamBufferListener::BufferInfo();
557 info.mOutput = output;
558 // TODO: rest of fields
559
560 for (it = mBufferListenerList.begin(), end = mBufferListenerList.end();
561 it != end;
562 ++it) {
563
564 sp<Camera3StreamBufferListener> listener = it->promote();
565 if (listener != 0) {
566 if (acquired) {
567 listener->onBufferAcquired(info);
568 } else {
569 listener->onBufferReleased(info);
570 }
571 }
572 }
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700573}
574
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800575bool Camera3Stream::hasOutstandingBuffers() const {
576 ATRACE_CALL();
577 Mutex::Autolock l(mLock);
578 return hasOutstandingBuffersLocked();
579}
580
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700581status_t Camera3Stream::setStatusTracker(sp<StatusTracker> statusTracker) {
582 Mutex::Autolock l(mLock);
583 sp<StatusTracker> oldTracker = mStatusTracker.promote();
584 if (oldTracker != 0 && mStatusId != StatusTracker::NO_STATUS_ID) {
585 oldTracker->removeComponent(mStatusId);
586 }
587 mStatusId = StatusTracker::NO_STATUS_ID;
588 mStatusTracker = statusTracker;
589
590 return OK;
591}
592
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800593status_t Camera3Stream::disconnect() {
594 ATRACE_CALL();
595 Mutex::Autolock l(mLock);
Igor Murashkine2172be2013-05-28 15:31:39 -0700596 ALOGV("%s: Stream %d: Disconnecting...", __FUNCTION__, mId);
597 status_t res = disconnectLocked();
598
599 if (res == -ENOTCONN) {
600 // "Already disconnected" -- not an error
601 return OK;
602 } else {
603 return res;
604 }
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800605}
606
607status_t Camera3Stream::registerBuffersLocked(camera3_device *hal3Device) {
608 ATRACE_CALL();
Igor Murashkin13d315e2014-04-03 18:09:04 -0700609
610 /**
611 * >= CAMERA_DEVICE_API_VERSION_3_2:
612 *
613 * camera3_device_t->ops->register_stream_buffers() is not called and must
614 * be NULL.
615 */
616 if (hal3Device->common.version >= CAMERA_DEVICE_API_VERSION_3_2) {
617 ALOGV("%s: register_stream_buffers unused as of HAL3.2", __FUNCTION__);
618
Igor Murashkinc758f222014-08-19 15:14:29 -0700619 if (hal3Device->ops->register_stream_buffers != NULL) {
Igor Murashkin13d315e2014-04-03 18:09:04 -0700620 ALOGE("%s: register_stream_buffers is deprecated in HAL3.2; "
621 "must be set to NULL in camera3_device::ops", __FUNCTION__);
622 return INVALID_OPERATION;
Igor Murashkin13d315e2014-04-03 18:09:04 -0700623 }
624
625 return OK;
Igor Murashkin13d315e2014-04-03 18:09:04 -0700626 }
627
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700628 ALOGV("%s: register_stream_buffers using deprecated code path", __FUNCTION__);
629
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800630 status_t res;
631
632 size_t bufferCount = getBufferCountLocked();
633
634 Vector<buffer_handle_t*> buffers;
Igor Murashkin13d315e2014-04-03 18:09:04 -0700635 buffers.insertAt(/*prototype_item*/NULL, /*index*/0, bufferCount);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800636
637 camera3_stream_buffer_set bufferSet = camera3_stream_buffer_set();
638 bufferSet.stream = this;
639 bufferSet.num_buffers = bufferCount;
640 bufferSet.buffers = buffers.editArray();
641
642 Vector<camera3_stream_buffer_t> streamBuffers;
Igor Murashkin13d315e2014-04-03 18:09:04 -0700643 streamBuffers.insertAt(camera3_stream_buffer_t(), /*index*/0, bufferCount);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800644
645 // Register all buffers with the HAL. This means getting all the buffers
646 // from the stream, providing them to the HAL with the
647 // register_stream_buffers() method, and then returning them back to the
648 // stream in the error state, since they won't have valid data.
649 //
650 // Only registered buffers can be sent to the HAL.
651
652 uint32_t bufferIdx = 0;
653 for (; bufferIdx < bufferCount; bufferIdx++) {
654 res = getBufferLocked( &streamBuffers.editItemAt(bufferIdx) );
655 if (res != OK) {
656 ALOGE("%s: Unable to get buffer %d for registration with HAL",
657 __FUNCTION__, bufferIdx);
658 // Skip registering, go straight to cleanup
659 break;
660 }
661
662 sp<Fence> fence = new Fence(streamBuffers[bufferIdx].acquire_fence);
Mathias Agopiand7644242013-05-16 18:07:35 -0700663 fence->waitForever("Camera3Stream::registerBuffers");
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800664
665 buffers.editItemAt(bufferIdx) = streamBuffers[bufferIdx].buffer;
666 }
667 if (bufferIdx == bufferCount) {
668 // Got all buffers, register with HAL
Colin Crosse5729fa2014-03-21 15:04:25 -0700669 ALOGV("%s: Registering %zu buffers with camera HAL",
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800670 __FUNCTION__, bufferCount);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -0700671 ATRACE_BEGIN("camera3->register_stream_buffers");
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800672 res = hal3Device->ops->register_stream_buffers(hal3Device,
673 &bufferSet);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -0700674 ATRACE_END();
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800675 }
676
677 // Return all valid buffers to stream, in ERROR state to indicate
678 // they weren't filled.
679 for (size_t i = 0; i < bufferIdx; i++) {
680 streamBuffers.editItemAt(i).release_fence = -1;
681 streamBuffers.editItemAt(i).status = CAMERA3_BUFFER_STATUS_ERROR;
682 returnBufferLocked(streamBuffers[i], 0);
683 }
684
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700685 mPrepared = true;
686
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800687 return res;
688}
689
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700690status_t Camera3Stream::getBufferLocked(camera3_stream_buffer *) {
691 ALOGE("%s: This type of stream does not support output", __FUNCTION__);
692 return INVALID_OPERATION;
693}
694status_t Camera3Stream::returnBufferLocked(const camera3_stream_buffer &,
695 nsecs_t) {
696 ALOGE("%s: This type of stream does not support output", __FUNCTION__);
697 return INVALID_OPERATION;
698}
699status_t Camera3Stream::getInputBufferLocked(camera3_stream_buffer *) {
700 ALOGE("%s: This type of stream does not support input", __FUNCTION__);
701 return INVALID_OPERATION;
702}
703status_t Camera3Stream::returnInputBufferLocked(
704 const camera3_stream_buffer &) {
705 ALOGE("%s: This type of stream does not support input", __FUNCTION__);
706 return INVALID_OPERATION;
707}
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700708status_t Camera3Stream::getInputBufferProducerLocked(sp<IGraphicBufferProducer> *producer) {
709 ALOGE("%s: This type of stream does not support input", __FUNCTION__);
710 return INVALID_OPERATION;
711}
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700712
Igor Murashkin2fba5842013-04-22 14:03:54 -0700713void Camera3Stream::addBufferListener(
714 wp<Camera3StreamBufferListener> listener) {
715 Mutex::Autolock l(mLock);
Zhijun Hef0d962a2014-06-30 10:24:11 -0700716
717 List<wp<Camera3StreamBufferListener> >::iterator it, end;
718 for (it = mBufferListenerList.begin(), end = mBufferListenerList.end();
719 it != end;
720 ) {
721 if (*it == listener) {
722 ALOGE("%s: Try to add the same listener twice, ignoring...", __FUNCTION__);
723 return;
724 }
725 it++;
726 }
727
Igor Murashkin2fba5842013-04-22 14:03:54 -0700728 mBufferListenerList.push_back(listener);
729}
730
731void Camera3Stream::removeBufferListener(
732 const sp<Camera3StreamBufferListener>& listener) {
733 Mutex::Autolock l(mLock);
734
735 bool erased = true;
736 List<wp<Camera3StreamBufferListener> >::iterator it, end;
737 for (it = mBufferListenerList.begin(), end = mBufferListenerList.end();
738 it != end;
739 ) {
740
741 if (*it == listener) {
742 it = mBufferListenerList.erase(it);
743 erased = true;
744 } else {
745 ++it;
746 }
747 }
748
749 if (!erased) {
750 ALOGW("%s: Could not find listener to remove, already removed",
751 __FUNCTION__);
752 }
753}
754
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800755}; // namespace camera3
756
757}; // namespace android