blob: 4c40bb620a2764a62bdce689d82c85e9f499d612 [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),
56 mStatusId(StatusTracker::NO_STATUS_ID) {
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -080057
58 camera3_stream::stream_type = type;
59 camera3_stream::width = width;
60 camera3_stream::height = height;
61 camera3_stream::format = format;
Eino-Ville Talvala3d82c0d2015-02-23 15:19:19 -080062 camera3_stream::data_space = dataSpace;
Yin-Chia Yehb97babb2015-03-12 13:42:44 -070063 camera3_stream::rotation = rotation;
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -080064 camera3_stream::usage = 0;
65 camera3_stream::max_buffers = 0;
66 camera3_stream::priv = NULL;
67
68 if (format == HAL_PIXEL_FORMAT_BLOB && maxSize == 0) {
69 ALOGE("%s: BLOB format with size == 0", __FUNCTION__);
70 mState = STATE_ERROR;
71 }
72}
73
74int Camera3Stream::getId() const {
75 return mId;
76}
77
78uint32_t Camera3Stream::getWidth() const {
79 return camera3_stream::width;
80}
81
82uint32_t Camera3Stream::getHeight() const {
83 return camera3_stream::height;
84}
85
86int Camera3Stream::getFormat() const {
87 return camera3_stream::format;
88}
89
Eino-Ville Talvala3d82c0d2015-02-23 15:19:19 -080090android_dataspace Camera3Stream::getDataSpace() const {
91 return camera3_stream::data_space;
92}
93
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -080094camera3_stream* Camera3Stream::startConfiguration() {
Eino-Ville Talvalab2f5b192013-07-30 14:36:03 -070095 ATRACE_CALL();
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -080096 Mutex::Autolock l(mLock);
Eino-Ville Talvalab2f5b192013-07-30 14:36:03 -070097 status_t res;
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -080098
99 switch (mState) {
100 case STATE_ERROR:
101 ALOGE("%s: In error state", __FUNCTION__);
102 return NULL;
103 case STATE_CONSTRUCTED:
104 // OK
105 break;
106 case STATE_IN_CONFIG:
107 case STATE_IN_RECONFIG:
108 // Can start config again with no trouble; but don't redo
109 // oldUsage/oldMaxBuffers
110 return this;
111 case STATE_CONFIGURED:
Chien-Yu Chen90746f42015-04-15 13:50:13 -0700112 if (hasOutstandingBuffersLocked()) {
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800113 ALOGE("%s: Cannot configure stream; has outstanding buffers",
114 __FUNCTION__);
115 return NULL;
116 }
117 break;
118 default:
119 ALOGE("%s: Unknown state %d", __FUNCTION__, mState);
120 return NULL;
121 }
122
Eino-Ville Talvalab2f5b192013-07-30 14:36:03 -0700123 oldUsage = camera3_stream::usage;
124 oldMaxBuffers = camera3_stream::max_buffers;
125
126 res = getEndpointUsage(&(camera3_stream::usage));
127 if (res != OK) {
128 ALOGE("%s: Cannot query consumer endpoint usage!",
129 __FUNCTION__);
130 return NULL;
131 }
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800132
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700133 // Stop tracking if currently doing so
134 if (mStatusId != StatusTracker::NO_STATUS_ID) {
135 sp<StatusTracker> statusTracker = mStatusTracker.promote();
136 if (statusTracker != 0) {
137 statusTracker->removeComponent(mStatusId);
138 }
139 mStatusId = StatusTracker::NO_STATUS_ID;
140 }
141
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800142 if (mState == STATE_CONSTRUCTED) {
143 mState = STATE_IN_CONFIG;
144 } else { // mState == STATE_CONFIGURED
Igor Murashkin13d315e2014-04-03 18:09:04 -0700145 LOG_ALWAYS_FATAL_IF(mState != STATE_CONFIGURED, "Invalid state: 0x%x", mState);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800146 mState = STATE_IN_RECONFIG;
147 }
148
149 return this;
150}
151
152bool Camera3Stream::isConfiguring() const {
153 Mutex::Autolock l(mLock);
154 return (mState == STATE_IN_CONFIG) || (mState == STATE_IN_RECONFIG);
155}
156
157status_t Camera3Stream::finishConfiguration(camera3_device *hal3Device) {
Eino-Ville Talvalab2f5b192013-07-30 14:36:03 -0700158 ATRACE_CALL();
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800159 Mutex::Autolock l(mLock);
160 switch (mState) {
161 case STATE_ERROR:
162 ALOGE("%s: In error state", __FUNCTION__);
163 return INVALID_OPERATION;
164 case STATE_IN_CONFIG:
165 case STATE_IN_RECONFIG:
166 // OK
167 break;
168 case STATE_CONSTRUCTED:
169 case STATE_CONFIGURED:
170 ALOGE("%s: Cannot finish configuration that hasn't been started",
171 __FUNCTION__);
172 return INVALID_OPERATION;
173 default:
174 ALOGE("%s: Unknown state", __FUNCTION__);
175 return INVALID_OPERATION;
176 }
177
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700178 // Register for idle tracking
179 sp<StatusTracker> statusTracker = mStatusTracker.promote();
180 if (statusTracker != 0) {
181 mStatusId = statusTracker->addComponent();
182 }
183
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800184 // Check if the stream configuration is unchanged, and skip reallocation if
185 // so. As documented in hardware/camera3.h:configure_streams().
186 if (mState == STATE_IN_RECONFIG &&
Eino-Ville Talvalab2f5b192013-07-30 14:36:03 -0700187 oldUsage == camera3_stream::usage &&
188 oldMaxBuffers == camera3_stream::max_buffers) {
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800189 mState = STATE_CONFIGURED;
190 return OK;
191 }
192
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700193 // Reset prepared state, since buffer config has changed, and existing
194 // allocations are no longer valid
195 mPrepared = false;
196 mStreamUnpreparable = false;
197
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800198 status_t res;
199 res = configureQueueLocked();
200 if (res != OK) {
201 ALOGE("%s: Unable to configure stream %d queue: %s (%d)",
202 __FUNCTION__, mId, strerror(-res), res);
203 mState = STATE_ERROR;
204 return res;
205 }
206
207 res = registerBuffersLocked(hal3Device);
208 if (res != OK) {
209 ALOGE("%s: Unable to register stream buffers with HAL: %s (%d)",
210 __FUNCTION__, strerror(-res), res);
211 mState = STATE_ERROR;
212 return res;
213 }
214
215 mState = STATE_CONFIGURED;
216
217 return res;
218}
219
Eino-Ville Talvala17543512014-08-06 14:32:02 -0700220status_t Camera3Stream::cancelConfiguration() {
221 ATRACE_CALL();
222 Mutex::Autolock l(mLock);
223 switch (mState) {
224 case STATE_ERROR:
225 ALOGE("%s: In error state", __FUNCTION__);
226 return INVALID_OPERATION;
227 case STATE_IN_CONFIG:
228 case STATE_IN_RECONFIG:
229 // OK
230 break;
231 case STATE_CONSTRUCTED:
232 case STATE_CONFIGURED:
233 ALOGE("%s: Cannot cancel configuration that hasn't been started",
234 __FUNCTION__);
235 return INVALID_OPERATION;
236 default:
237 ALOGE("%s: Unknown state", __FUNCTION__);
238 return INVALID_OPERATION;
239 }
240
241 camera3_stream::usage = oldUsage;
242 camera3_stream::max_buffers = oldMaxBuffers;
243
Yin-Chia Yeh3ea3fcd2014-09-05 14:14:44 -0700244 mState = (mState == STATE_IN_RECONFIG) ? STATE_CONFIGURED : STATE_CONSTRUCTED;
Eino-Ville Talvala17543512014-08-06 14:32:02 -0700245 return OK;
246}
247
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700248bool Camera3Stream::isUnpreparable() {
249 ATRACE_CALL();
250
251 Mutex::Autolock l(mLock);
252 return mStreamUnpreparable;
253}
254
255status_t Camera3Stream::startPrepare() {
256 ATRACE_CALL();
257
258 Mutex::Autolock l(mLock);
259 status_t res = OK;
260
261 // This function should be only called when the stream is configured already.
262 if (mState != STATE_CONFIGURED) {
263 ALOGE("%s: Stream %d: Can't prepare stream if stream is not in CONFIGURED "
264 "state %d", __FUNCTION__, mId, mState);
265 return INVALID_OPERATION;
266 }
267
268 // This function can't be called if the stream has already received filled
269 // buffers
270 if (mStreamUnpreparable) {
271 ALOGE("%s: Stream %d: Can't prepare stream that's already in use",
272 __FUNCTION__, mId);
273 return INVALID_OPERATION;
274 }
275
276 if (getHandoutOutputBufferCountLocked() > 0) {
277 ALOGE("%s: Stream %d: Can't prepare stream that has outstanding buffers",
278 __FUNCTION__, mId);
279 return INVALID_OPERATION;
280 }
281
282 if (mPrepared) return OK;
283
284 size_t bufferCount = getBufferCountLocked();
285
286 mPreparedBuffers.insertAt(camera3_stream_buffer_t(), /*index*/0, bufferCount);
287 mPreparedBufferIdx = 0;
288
289 mState = STATE_PREPARING;
290
291 return NOT_ENOUGH_DATA;
292}
293
294bool Camera3Stream::isPreparing() const {
295 Mutex::Autolock l(mLock);
296 return mState == STATE_PREPARING;
297}
298
299status_t Camera3Stream::prepareNextBuffer() {
300 ATRACE_CALL();
301
302 Mutex::Autolock l(mLock);
303 status_t res = OK;
304
305 // This function should be only called when the stream is preparing
306 if (mState != STATE_PREPARING) {
307 ALOGE("%s: Stream %d: Can't prepare buffer if stream is not in PREPARING "
308 "state %d", __FUNCTION__, mId, mState);
309 return INVALID_OPERATION;
310 }
311
312 // Get next buffer - this may allocate, and take a while for large buffers
313 res = getBufferLocked( &mPreparedBuffers.editItemAt(mPreparedBufferIdx) );
314 if (res != OK) {
315 ALOGE("%s: Stream %d: Unable to allocate buffer %d during preparation",
316 __FUNCTION__, mId, mPreparedBufferIdx);
317 return NO_INIT;
318 }
319
320 mPreparedBufferIdx++;
321
322 // Check if we still have buffers left to allocate
323 if (mPreparedBufferIdx < mPreparedBuffers.size()) {
324 return NOT_ENOUGH_DATA;
325 }
326
327 // Done with prepare - mark stream as such, and return all buffers
328 // via cancelPrepare
329 mPrepared = true;
330
331 return cancelPrepareLocked();
332}
333
334status_t Camera3Stream::cancelPrepare() {
335 ATRACE_CALL();
336
337 Mutex::Autolock l(mLock);
338
339 return cancelPrepareLocked();
340}
341
342status_t Camera3Stream::cancelPrepareLocked() {
343 status_t res = OK;
344
345 // This function should be only called when the stream is mid-preparing.
346 if (mState != STATE_PREPARING) {
347 ALOGE("%s: Stream %d: Can't cancel prepare stream if stream is not in "
348 "PREPARING state %d", __FUNCTION__, mId, mState);
349 return INVALID_OPERATION;
350 }
351
352 // Return all valid buffers to stream, in ERROR state to indicate
353 // they weren't filled.
354 for (size_t i = 0; i < mPreparedBufferIdx; i++) {
355 mPreparedBuffers.editItemAt(i).release_fence = -1;
356 mPreparedBuffers.editItemAt(i).status = CAMERA3_BUFFER_STATUS_ERROR;
357 returnBufferLocked(mPreparedBuffers[i], 0);
358 }
359 mPreparedBuffers.clear();
360 mPreparedBufferIdx = 0;
361
362 mState = STATE_CONFIGURED;
363
364 return res;
365}
366
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800367status_t Camera3Stream::getBuffer(camera3_stream_buffer *buffer) {
368 ATRACE_CALL();
369 Mutex::Autolock l(mLock);
Zhijun He6adc9cc2014-04-15 14:09:55 -0700370 status_t res = OK;
Igor Murashkin2fba5842013-04-22 14:03:54 -0700371
Zhijun He6adc9cc2014-04-15 14:09:55 -0700372 // This function should be only called when the stream is configured already.
373 if (mState != STATE_CONFIGURED) {
374 ALOGE("%s: Stream %d: Can't get buffers if stream is not in CONFIGURED state %d",
375 __FUNCTION__, mId, mState);
376 return INVALID_OPERATION;
377 }
378
379 // Wait for new buffer returned back if we are running into the limit.
380 if (getHandoutOutputBufferCountLocked() == camera3_stream::max_buffers) {
381 ALOGV("%s: Already dequeued max output buffers (%d), wait for next returned one.",
382 __FUNCTION__, camera3_stream::max_buffers);
383 res = mOutputBufferReturnedSignal.waitRelative(mLock, kWaitForBufferDuration);
384 if (res != OK) {
385 if (res == TIMED_OUT) {
386 ALOGE("%s: wait for output buffer return timed out after %lldms", __FUNCTION__,
387 kWaitForBufferDuration / 1000000LL);
388 }
389 return res;
390 }
391 }
392
393 res = getBufferLocked(buffer);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700394 if (res == OK) {
395 fireBufferListenersLocked(*buffer, /*acquired*/true, /*output*/true);
396 }
397
398 return res;
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800399}
400
401status_t Camera3Stream::returnBuffer(const camera3_stream_buffer &buffer,
402 nsecs_t timestamp) {
403 ATRACE_CALL();
404 Mutex::Autolock l(mLock);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700405
Igor Murashkin13d315e2014-04-03 18:09:04 -0700406 /**
407 * TODO: Check that the state is valid first.
408 *
409 * <HAL3.2 IN_CONFIG and IN_RECONFIG in addition to CONFIGURED.
410 * >= HAL3.2 CONFIGURED only
411 *
412 * Do this for getBuffer as well.
413 */
Igor Murashkin2fba5842013-04-22 14:03:54 -0700414 status_t res = returnBufferLocked(buffer, timestamp);
415 if (res == OK) {
416 fireBufferListenersLocked(buffer, /*acquired*/false, /*output*/true);
Zhijun He6adc9cc2014-04-15 14:09:55 -0700417 mOutputBufferReturnedSignal.signal();
Igor Murashkin2fba5842013-04-22 14:03:54 -0700418 }
419
420 return res;
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800421}
422
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700423status_t Camera3Stream::getInputBuffer(camera3_stream_buffer *buffer) {
424 ATRACE_CALL();
425 Mutex::Autolock l(mLock);
Zhijun He6adc9cc2014-04-15 14:09:55 -0700426 status_t res = OK;
Igor Murashkin2fba5842013-04-22 14:03:54 -0700427
Zhijun He6adc9cc2014-04-15 14:09:55 -0700428 // This function should be only called when the stream is configured already.
429 if (mState != STATE_CONFIGURED) {
430 ALOGE("%s: Stream %d: Can't get input buffers if stream is not in CONFIGURED state %d",
431 __FUNCTION__, mId, mState);
432 return INVALID_OPERATION;
433 }
434
435 // Wait for new buffer returned back if we are running into the limit.
436 if (getHandoutInputBufferCountLocked() == camera3_stream::max_buffers) {
437 ALOGV("%s: Already dequeued max input buffers (%d), wait for next returned one.",
438 __FUNCTION__, camera3_stream::max_buffers);
439 res = mInputBufferReturnedSignal.waitRelative(mLock, kWaitForBufferDuration);
440 if (res != OK) {
441 if (res == TIMED_OUT) {
442 ALOGE("%s: wait for input buffer return timed out after %lldms", __FUNCTION__,
443 kWaitForBufferDuration / 1000000LL);
444 }
445 return res;
446 }
447 }
448
449 res = getInputBufferLocked(buffer);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700450 if (res == OK) {
451 fireBufferListenersLocked(*buffer, /*acquired*/true, /*output*/false);
452 }
453
454 return res;
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700455}
456
457status_t Camera3Stream::returnInputBuffer(const camera3_stream_buffer &buffer) {
458 ATRACE_CALL();
459 Mutex::Autolock l(mLock);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700460
461 status_t res = returnInputBufferLocked(buffer);
462 if (res == OK) {
463 fireBufferListenersLocked(buffer, /*acquired*/false, /*output*/false);
Zhijun He6adc9cc2014-04-15 14:09:55 -0700464 mInputBufferReturnedSignal.signal();
Igor Murashkin2fba5842013-04-22 14:03:54 -0700465 }
466 return res;
467}
468
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700469status_t Camera3Stream::getInputBufferProducer(sp<IGraphicBufferProducer> *producer) {
470 ATRACE_CALL();
471 Mutex::Autolock l(mLock);
472
473 return getInputBufferProducerLocked(producer);
474}
475
Igor Murashkin2fba5842013-04-22 14:03:54 -0700476void Camera3Stream::fireBufferListenersLocked(
477 const camera3_stream_buffer& /*buffer*/, bool acquired, bool output) {
478 List<wp<Camera3StreamBufferListener> >::iterator it, end;
479
480 // TODO: finish implementing
481
482 Camera3StreamBufferListener::BufferInfo info =
483 Camera3StreamBufferListener::BufferInfo();
484 info.mOutput = output;
485 // TODO: rest of fields
486
487 for (it = mBufferListenerList.begin(), end = mBufferListenerList.end();
488 it != end;
489 ++it) {
490
491 sp<Camera3StreamBufferListener> listener = it->promote();
492 if (listener != 0) {
493 if (acquired) {
494 listener->onBufferAcquired(info);
495 } else {
496 listener->onBufferReleased(info);
497 }
498 }
499 }
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700500}
501
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800502bool Camera3Stream::hasOutstandingBuffers() const {
503 ATRACE_CALL();
504 Mutex::Autolock l(mLock);
505 return hasOutstandingBuffersLocked();
506}
507
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700508status_t Camera3Stream::setStatusTracker(sp<StatusTracker> statusTracker) {
509 Mutex::Autolock l(mLock);
510 sp<StatusTracker> oldTracker = mStatusTracker.promote();
511 if (oldTracker != 0 && mStatusId != StatusTracker::NO_STATUS_ID) {
512 oldTracker->removeComponent(mStatusId);
513 }
514 mStatusId = StatusTracker::NO_STATUS_ID;
515 mStatusTracker = statusTracker;
516
517 return OK;
518}
519
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800520status_t Camera3Stream::disconnect() {
521 ATRACE_CALL();
522 Mutex::Autolock l(mLock);
Igor Murashkine2172be2013-05-28 15:31:39 -0700523 ALOGV("%s: Stream %d: Disconnecting...", __FUNCTION__, mId);
524 status_t res = disconnectLocked();
525
526 if (res == -ENOTCONN) {
527 // "Already disconnected" -- not an error
528 return OK;
529 } else {
530 return res;
531 }
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800532}
533
534status_t Camera3Stream::registerBuffersLocked(camera3_device *hal3Device) {
535 ATRACE_CALL();
Igor Murashkin13d315e2014-04-03 18:09:04 -0700536
537 /**
538 * >= CAMERA_DEVICE_API_VERSION_3_2:
539 *
540 * camera3_device_t->ops->register_stream_buffers() is not called and must
541 * be NULL.
542 */
543 if (hal3Device->common.version >= CAMERA_DEVICE_API_VERSION_3_2) {
544 ALOGV("%s: register_stream_buffers unused as of HAL3.2", __FUNCTION__);
545
Igor Murashkinc758f222014-08-19 15:14:29 -0700546 if (hal3Device->ops->register_stream_buffers != NULL) {
Igor Murashkin13d315e2014-04-03 18:09:04 -0700547 ALOGE("%s: register_stream_buffers is deprecated in HAL3.2; "
548 "must be set to NULL in camera3_device::ops", __FUNCTION__);
549 return INVALID_OPERATION;
Igor Murashkin13d315e2014-04-03 18:09:04 -0700550 }
551
552 return OK;
Igor Murashkin13d315e2014-04-03 18:09:04 -0700553 }
554
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700555 ALOGV("%s: register_stream_buffers using deprecated code path", __FUNCTION__);
556
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800557 status_t res;
558
559 size_t bufferCount = getBufferCountLocked();
560
561 Vector<buffer_handle_t*> buffers;
Igor Murashkin13d315e2014-04-03 18:09:04 -0700562 buffers.insertAt(/*prototype_item*/NULL, /*index*/0, bufferCount);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800563
564 camera3_stream_buffer_set bufferSet = camera3_stream_buffer_set();
565 bufferSet.stream = this;
566 bufferSet.num_buffers = bufferCount;
567 bufferSet.buffers = buffers.editArray();
568
569 Vector<camera3_stream_buffer_t> streamBuffers;
Igor Murashkin13d315e2014-04-03 18:09:04 -0700570 streamBuffers.insertAt(camera3_stream_buffer_t(), /*index*/0, bufferCount);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800571
572 // Register all buffers with the HAL. This means getting all the buffers
573 // from the stream, providing them to the HAL with the
574 // register_stream_buffers() method, and then returning them back to the
575 // stream in the error state, since they won't have valid data.
576 //
577 // Only registered buffers can be sent to the HAL.
578
579 uint32_t bufferIdx = 0;
580 for (; bufferIdx < bufferCount; bufferIdx++) {
581 res = getBufferLocked( &streamBuffers.editItemAt(bufferIdx) );
582 if (res != OK) {
583 ALOGE("%s: Unable to get buffer %d for registration with HAL",
584 __FUNCTION__, bufferIdx);
585 // Skip registering, go straight to cleanup
586 break;
587 }
588
589 sp<Fence> fence = new Fence(streamBuffers[bufferIdx].acquire_fence);
Mathias Agopiand7644242013-05-16 18:07:35 -0700590 fence->waitForever("Camera3Stream::registerBuffers");
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800591
592 buffers.editItemAt(bufferIdx) = streamBuffers[bufferIdx].buffer;
593 }
594 if (bufferIdx == bufferCount) {
595 // Got all buffers, register with HAL
Colin Crosse5729fa2014-03-21 15:04:25 -0700596 ALOGV("%s: Registering %zu buffers with camera HAL",
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800597 __FUNCTION__, bufferCount);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -0700598 ATRACE_BEGIN("camera3->register_stream_buffers");
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800599 res = hal3Device->ops->register_stream_buffers(hal3Device,
600 &bufferSet);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -0700601 ATRACE_END();
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800602 }
603
604 // Return all valid buffers to stream, in ERROR state to indicate
605 // they weren't filled.
606 for (size_t i = 0; i < bufferIdx; i++) {
607 streamBuffers.editItemAt(i).release_fence = -1;
608 streamBuffers.editItemAt(i).status = CAMERA3_BUFFER_STATUS_ERROR;
609 returnBufferLocked(streamBuffers[i], 0);
610 }
611
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700612 mPrepared = true;
613
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800614 return res;
615}
616
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700617status_t Camera3Stream::getBufferLocked(camera3_stream_buffer *) {
618 ALOGE("%s: This type of stream does not support output", __FUNCTION__);
619 return INVALID_OPERATION;
620}
621status_t Camera3Stream::returnBufferLocked(const camera3_stream_buffer &,
622 nsecs_t) {
623 ALOGE("%s: This type of stream does not support output", __FUNCTION__);
624 return INVALID_OPERATION;
625}
626status_t Camera3Stream::getInputBufferLocked(camera3_stream_buffer *) {
627 ALOGE("%s: This type of stream does not support input", __FUNCTION__);
628 return INVALID_OPERATION;
629}
630status_t Camera3Stream::returnInputBufferLocked(
631 const camera3_stream_buffer &) {
632 ALOGE("%s: This type of stream does not support input", __FUNCTION__);
633 return INVALID_OPERATION;
634}
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700635status_t Camera3Stream::getInputBufferProducerLocked(sp<IGraphicBufferProducer> *producer) {
636 ALOGE("%s: This type of stream does not support input", __FUNCTION__);
637 return INVALID_OPERATION;
638}
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700639
Igor Murashkin2fba5842013-04-22 14:03:54 -0700640void Camera3Stream::addBufferListener(
641 wp<Camera3StreamBufferListener> listener) {
642 Mutex::Autolock l(mLock);
Zhijun Hef0d962a2014-06-30 10:24:11 -0700643
644 List<wp<Camera3StreamBufferListener> >::iterator it, end;
645 for (it = mBufferListenerList.begin(), end = mBufferListenerList.end();
646 it != end;
647 ) {
648 if (*it == listener) {
649 ALOGE("%s: Try to add the same listener twice, ignoring...", __FUNCTION__);
650 return;
651 }
652 it++;
653 }
654
Igor Murashkin2fba5842013-04-22 14:03:54 -0700655 mBufferListenerList.push_back(listener);
656}
657
658void Camera3Stream::removeBufferListener(
659 const sp<Camera3StreamBufferListener>& listener) {
660 Mutex::Autolock l(mLock);
661
662 bool erased = true;
663 List<wp<Camera3StreamBufferListener> >::iterator it, end;
664 for (it = mBufferListenerList.begin(), end = mBufferListenerList.end();
665 it != end;
666 ) {
667
668 if (*it == listener) {
669 it = mBufferListenerList.erase(it);
670 erased = true;
671 } else {
672 ++it;
673 }
674 }
675
676 if (!erased) {
677 ALOGW("%s: Could not find listener to remove, already removed",
678 __FUNCTION__);
679 }
680}
681
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800682}; // namespace camera3
683
684}; // namespace android