blob: 7ebc299a76c5ef6ed102c793556cbb11824f53fd [file] [log] [blame]
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -08001/*
Shuzhen Wangc28189a2017-11-27 23:05:10 -08002 * Copyright (C) 2013-2018 The Android Open Source Project
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -08003 *
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,
Shuzhen Wangc28189a2017-11-27 23:05:10 -080050 android_dataspace dataSpace, camera3_stream_rotation_t rotation,
51 const String8& physicalCameraId, int setId) :
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -080052 camera3_stream(),
53 mId(id),
Zhijun He125684a2015-12-26 15:07:30 -080054 mSetId(setId),
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -080055 mName(String8::format("Camera3Stream[%d]", id)),
56 mMaxSize(maxSize),
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -070057 mState(STATE_CONSTRUCTED),
Ruben Brunkc78ac262015-08-13 17:58:46 -070058 mStatusId(StatusTracker::NO_STATUS_ID),
Zhijun He5d677d12016-05-29 16:52:39 -070059 mStreamUnpreparable(true),
Emilian Peev050f5dc2017-05-18 14:43:56 +010060 mUsage(0),
Eino-Ville Talvala02bf0322016-02-18 12:41:10 -080061 mOldUsage(0),
62 mOldMaxBuffers(0),
Zhijun He125684a2015-12-26 15:07:30 -080063 mPrepared(false),
Shuzhen Wangb3a0fb52018-09-13 17:24:08 -070064 mPrepareBlockRequest(true),
Zhijun He125684a2015-12-26 15:07:30 -080065 mPreparedBufferIdx(0),
Shuzhen Wang686f6442017-06-20 16:16:04 -070066 mLastMaxCount(Camera3StreamInterface::ALLOCATE_PIPELINE_MAX),
Emilian Peev710c1422017-08-30 11:19:38 +010067 mBufferLimitLatency(kBufferLimitLatencyBinSize),
68 mFormatOverridden(false),
Shuzhen Wangc28189a2017-11-27 23:05:10 -080069 mOriginalFormat(-1),
70 mPhysicalCameraId(physicalCameraId) {
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -080071
72 camera3_stream::stream_type = type;
73 camera3_stream::width = width;
74 camera3_stream::height = height;
75 camera3_stream::format = format;
Eino-Ville Talvala3d82c0d2015-02-23 15:19:19 -080076 camera3_stream::data_space = dataSpace;
Yin-Chia Yehb97babb2015-03-12 13:42:44 -070077 camera3_stream::rotation = rotation;
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -080078 camera3_stream::max_buffers = 0;
79 camera3_stream::priv = NULL;
Shuzhen Wangc28189a2017-11-27 23:05:10 -080080 camera3_stream::physical_camera_id = mPhysicalCameraId.string();
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -080081
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -080082 if ((format == HAL_PIXEL_FORMAT_BLOB || format == HAL_PIXEL_FORMAT_RAW_OPAQUE) &&
83 maxSize == 0) {
84 ALOGE("%s: BLOB or RAW_OPAQUE format with size == 0", __FUNCTION__);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -080085 mState = STATE_ERROR;
86 }
87}
88
89int Camera3Stream::getId() const {
90 return mId;
91}
92
Zhijun He125684a2015-12-26 15:07:30 -080093int Camera3Stream::getStreamSetId() const {
94 return mSetId;
95}
96
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -080097uint32_t Camera3Stream::getWidth() const {
98 return camera3_stream::width;
99}
100
101uint32_t Camera3Stream::getHeight() const {
102 return camera3_stream::height;
103}
104
105int Camera3Stream::getFormat() const {
106 return camera3_stream::format;
107}
108
Eino-Ville Talvala3d82c0d2015-02-23 15:19:19 -0800109android_dataspace Camera3Stream::getDataSpace() const {
110 return camera3_stream::data_space;
111}
112
Emilian Peev050f5dc2017-05-18 14:43:56 +0100113uint64_t Camera3Stream::getUsage() const {
114 return mUsage;
115}
116
117void Camera3Stream::setUsage(uint64_t usage) {
118 mUsage = usage;
119}
120
Emilian Peev710c1422017-08-30 11:19:38 +0100121void Camera3Stream::setFormatOverride(bool formatOverridden) {
122 mFormatOverridden = formatOverridden;
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -0700123 if (formatOverridden) mOriginalFormat = camera3_stream::format;
Emilian Peev710c1422017-08-30 11:19:38 +0100124}
125
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -0700126bool Camera3Stream::isFormatOverridden() const {
Emilian Peev710c1422017-08-30 11:19:38 +0100127 return mFormatOverridden;
128}
129
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -0700130int Camera3Stream::getOriginalFormat() const {
131 return mOriginalFormat;
Emilian Peev710c1422017-08-30 11:19:38 +0100132}
133
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -0700134void Camera3Stream::setDataSpaceOverride(bool dataSpaceOverridden) {
135 mDataSpaceOverridden = dataSpaceOverridden;
136 if (dataSpaceOverridden) mOriginalDataSpace = camera3_stream::data_space;
137}
138
139bool Camera3Stream::isDataSpaceOverridden() const {
140 return mDataSpaceOverridden;
141}
142
143android_dataspace Camera3Stream::getOriginalDataSpace() const {
144 return mOriginalDataSpace;
Emilian Peev710c1422017-08-30 11:19:38 +0100145}
146
Shuzhen Wang5c22c152017-12-31 17:12:25 -0800147const String8& Camera3Stream::physicalCameraId() const {
148 return mPhysicalCameraId;
149}
150
Emilian Peevac3ce6c2017-12-12 15:27:02 +0000151status_t Camera3Stream::forceToIdle() {
152 ATRACE_CALL();
153 Mutex::Autolock l(mLock);
154 status_t res;
155
156 switch (mState) {
157 case STATE_ERROR:
158 case STATE_CONSTRUCTED:
159 case STATE_IN_CONFIG:
160 case STATE_PREPARING:
161 case STATE_IN_RECONFIG:
162 ALOGE("%s: Invalid state: %d", __FUNCTION__, mState);
163 res = NO_INIT;
164 break;
165 case STATE_CONFIGURED:
166 if (hasOutstandingBuffersLocked()) {
167 sp<StatusTracker> statusTracker = mStatusTracker.promote();
168 if (statusTracker != 0) {
169 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
170 }
171 }
172
173 mState = STATE_IN_IDLE;
174 res = OK;
175
176 break;
177 default:
178 ALOGE("%s: Unknown state %d", __FUNCTION__, mState);
179 res = NO_INIT;
180 }
181
182 return res;
183}
184
185status_t Camera3Stream::restoreConfiguredState() {
186 ATRACE_CALL();
187 Mutex::Autolock l(mLock);
188 status_t res;
189
190 switch (mState) {
191 case STATE_ERROR:
192 case STATE_CONSTRUCTED:
193 case STATE_IN_CONFIG:
194 case STATE_PREPARING:
195 case STATE_IN_RECONFIG:
196 case STATE_CONFIGURED:
197 ALOGE("%s: Invalid state: %d", __FUNCTION__, mState);
198 res = NO_INIT;
199 break;
200 case STATE_IN_IDLE:
201 if (hasOutstandingBuffersLocked()) {
202 sp<StatusTracker> statusTracker = mStatusTracker.promote();
203 if (statusTracker != 0) {
204 statusTracker->markComponentActive(mStatusId);
205 }
206 }
207
208 mState = STATE_CONFIGURED;
209 res = OK;
210
211 break;
212 default:
213 ALOGE("%s: Unknown state %d", __FUNCTION__, mState);
214 res = NO_INIT;
215 }
216
217 return res;
218}
219
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800220camera3_stream* Camera3Stream::startConfiguration() {
Eino-Ville Talvalab2f5b192013-07-30 14:36:03 -0700221 ATRACE_CALL();
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800222 Mutex::Autolock l(mLock);
Eino-Ville Talvalab2f5b192013-07-30 14:36:03 -0700223 status_t res;
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800224
225 switch (mState) {
226 case STATE_ERROR:
227 ALOGE("%s: In error state", __FUNCTION__);
228 return NULL;
229 case STATE_CONSTRUCTED:
Emilian Peevac3ce6c2017-12-12 15:27:02 +0000230 case STATE_IN_IDLE:
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800231 // OK
232 break;
233 case STATE_IN_CONFIG:
234 case STATE_IN_RECONFIG:
235 // Can start config again with no trouble; but don't redo
Eino-Ville Talvala02bf0322016-02-18 12:41:10 -0800236 // mOldUsage/mOldMaxBuffers
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800237 return this;
238 case STATE_CONFIGURED:
Chien-Yu Chen90746f42015-04-15 13:50:13 -0700239 if (hasOutstandingBuffersLocked()) {
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800240 ALOGE("%s: Cannot configure stream; has outstanding buffers",
241 __FUNCTION__);
242 return NULL;
243 }
244 break;
245 default:
246 ALOGE("%s: Unknown state %d", __FUNCTION__, mState);
247 return NULL;
248 }
249
Emilian Peev050f5dc2017-05-18 14:43:56 +0100250 mOldUsage = mUsage;
Eino-Ville Talvala02bf0322016-02-18 12:41:10 -0800251 mOldMaxBuffers = camera3_stream::max_buffers;
Eino-Ville Talvalab2f5b192013-07-30 14:36:03 -0700252
Emilian Peev050f5dc2017-05-18 14:43:56 +0100253 res = getEndpointUsage(&mUsage);
Eino-Ville Talvalab2f5b192013-07-30 14:36:03 -0700254 if (res != OK) {
255 ALOGE("%s: Cannot query consumer endpoint usage!",
256 __FUNCTION__);
257 return NULL;
258 }
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800259
Emilian Peevac3ce6c2017-12-12 15:27:02 +0000260 if (mState == STATE_IN_IDLE) {
261 // Skip configuration.
262 return this;
263 }
264
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700265 // Stop tracking if currently doing so
266 if (mStatusId != StatusTracker::NO_STATUS_ID) {
267 sp<StatusTracker> statusTracker = mStatusTracker.promote();
268 if (statusTracker != 0) {
269 statusTracker->removeComponent(mStatusId);
270 }
271 mStatusId = StatusTracker::NO_STATUS_ID;
272 }
273
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800274 if (mState == STATE_CONSTRUCTED) {
275 mState = STATE_IN_CONFIG;
276 } else { // mState == STATE_CONFIGURED
Igor Murashkin13d315e2014-04-03 18:09:04 -0700277 LOG_ALWAYS_FATAL_IF(mState != STATE_CONFIGURED, "Invalid state: 0x%x", mState);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800278 mState = STATE_IN_RECONFIG;
279 }
280
281 return this;
282}
283
284bool Camera3Stream::isConfiguring() const {
285 Mutex::Autolock l(mLock);
286 return (mState == STATE_IN_CONFIG) || (mState == STATE_IN_RECONFIG);
287}
288
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800289status_t Camera3Stream::finishConfiguration() {
Eino-Ville Talvalab2f5b192013-07-30 14:36:03 -0700290 ATRACE_CALL();
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800291 Mutex::Autolock l(mLock);
292 switch (mState) {
293 case STATE_ERROR:
294 ALOGE("%s: In error state", __FUNCTION__);
295 return INVALID_OPERATION;
296 case STATE_IN_CONFIG:
297 case STATE_IN_RECONFIG:
298 // OK
299 break;
300 case STATE_CONSTRUCTED:
301 case STATE_CONFIGURED:
302 ALOGE("%s: Cannot finish configuration that hasn't been started",
303 __FUNCTION__);
304 return INVALID_OPERATION;
Emilian Peevac3ce6c2017-12-12 15:27:02 +0000305 case STATE_IN_IDLE:
306 //Skip configuration in this state
307 return OK;
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800308 default:
309 ALOGE("%s: Unknown state", __FUNCTION__);
310 return INVALID_OPERATION;
311 }
312
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700313 // Register for idle tracking
314 sp<StatusTracker> statusTracker = mStatusTracker.promote();
315 if (statusTracker != 0) {
316 mStatusId = statusTracker->addComponent();
317 }
318
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800319 // Check if the stream configuration is unchanged, and skip reallocation if
320 // so. As documented in hardware/camera3.h:configure_streams().
321 if (mState == STATE_IN_RECONFIG &&
Emilian Peev050f5dc2017-05-18 14:43:56 +0100322 mOldUsage == mUsage &&
Eino-Ville Talvala02bf0322016-02-18 12:41:10 -0800323 mOldMaxBuffers == camera3_stream::max_buffers) {
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800324 mState = STATE_CONFIGURED;
325 return OK;
326 }
327
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700328 // Reset prepared state, since buffer config has changed, and existing
329 // allocations are no longer valid
330 mPrepared = false;
Shuzhen Wangb3a0fb52018-09-13 17:24:08 -0700331 mPrepareBlockRequest = true;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700332 mStreamUnpreparable = false;
333
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800334 status_t res;
335 res = configureQueueLocked();
Shuzhen Wang210ba5c2018-07-25 16:47:40 -0700336 // configureQueueLocked could return error in case of abandoned surface.
337 // Treat as non-fatal error.
338 if (res == NO_INIT || res == DEAD_OBJECT) {
339 ALOGE("%s: Unable to configure stream %d queue (non-fatal): %s (%d)",
340 __FUNCTION__, mId, strerror(-res), res);
341 mState = STATE_ABANDONED;
342 return res;
343 } else if (res != OK) {
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800344 ALOGE("%s: Unable to configure stream %d queue: %s (%d)",
345 __FUNCTION__, mId, strerror(-res), res);
346 mState = STATE_ERROR;
347 return res;
348 }
349
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800350 mState = STATE_CONFIGURED;
351
352 return res;
353}
354
Eino-Ville Talvala17543512014-08-06 14:32:02 -0700355status_t Camera3Stream::cancelConfiguration() {
356 ATRACE_CALL();
357 Mutex::Autolock l(mLock);
358 switch (mState) {
359 case STATE_ERROR:
360 ALOGE("%s: In error state", __FUNCTION__);
361 return INVALID_OPERATION;
362 case STATE_IN_CONFIG:
363 case STATE_IN_RECONFIG:
Emilian Peevac3ce6c2017-12-12 15:27:02 +0000364 case STATE_IN_IDLE:
Eino-Ville Talvala17543512014-08-06 14:32:02 -0700365 // OK
366 break;
367 case STATE_CONSTRUCTED:
368 case STATE_CONFIGURED:
369 ALOGE("%s: Cannot cancel configuration that hasn't been started",
370 __FUNCTION__);
371 return INVALID_OPERATION;
372 default:
373 ALOGE("%s: Unknown state", __FUNCTION__);
374 return INVALID_OPERATION;
375 }
376
Emilian Peev050f5dc2017-05-18 14:43:56 +0100377 mUsage = mOldUsage;
Eino-Ville Talvala02bf0322016-02-18 12:41:10 -0800378 camera3_stream::max_buffers = mOldMaxBuffers;
Eino-Ville Talvala17543512014-08-06 14:32:02 -0700379
Emilian Peevac3ce6c2017-12-12 15:27:02 +0000380 mState = ((mState == STATE_IN_RECONFIG) || (mState == STATE_IN_IDLE)) ? STATE_CONFIGURED :
381 STATE_CONSTRUCTED;
382
Eino-Ville Talvala17543512014-08-06 14:32:02 -0700383 return OK;
384}
385
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700386bool Camera3Stream::isUnpreparable() {
387 ATRACE_CALL();
388
389 Mutex::Autolock l(mLock);
390 return mStreamUnpreparable;
391}
392
Shuzhen Wangb3a0fb52018-09-13 17:24:08 -0700393status_t Camera3Stream::startPrepare(int maxCount, bool blockRequest) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700394 ATRACE_CALL();
395
396 Mutex::Autolock l(mLock);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700397
Ruben Brunkc78ac262015-08-13 17:58:46 -0700398 if (maxCount < 0) {
399 ALOGE("%s: Stream %d: Can't prepare stream if max buffer count (%d) is < 0",
400 __FUNCTION__, mId, maxCount);
401 return BAD_VALUE;
402 }
403
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700404 // This function should be only called when the stream is configured already.
405 if (mState != STATE_CONFIGURED) {
406 ALOGE("%s: Stream %d: Can't prepare stream if stream is not in CONFIGURED "
407 "state %d", __FUNCTION__, mId, mState);
408 return INVALID_OPERATION;
409 }
410
411 // This function can't be called if the stream has already received filled
412 // buffers
413 if (mStreamUnpreparable) {
414 ALOGE("%s: Stream %d: Can't prepare stream that's already in use",
415 __FUNCTION__, mId);
416 return INVALID_OPERATION;
417 }
418
419 if (getHandoutOutputBufferCountLocked() > 0) {
420 ALOGE("%s: Stream %d: Can't prepare stream that has outstanding buffers",
421 __FUNCTION__, mId);
422 return INVALID_OPERATION;
423 }
424
Ruben Brunkc78ac262015-08-13 17:58:46 -0700425 size_t pipelineMax = getBufferCountLocked();
426 size_t clampedCount = (pipelineMax < static_cast<size_t>(maxCount)) ?
427 pipelineMax : static_cast<size_t>(maxCount);
428 size_t bufferCount = (maxCount == Camera3StreamInterface::ALLOCATE_PIPELINE_MAX) ?
429 pipelineMax : clampedCount;
430
431 mPrepared = bufferCount <= mLastMaxCount;
Shuzhen Wangb3a0fb52018-09-13 17:24:08 -0700432 mPrepareBlockRequest = blockRequest;
Ruben Brunkc78ac262015-08-13 17:58:46 -0700433
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700434 if (mPrepared) return OK;
435
Ruben Brunkc78ac262015-08-13 17:58:46 -0700436 mLastMaxCount = bufferCount;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700437
438 mPreparedBuffers.insertAt(camera3_stream_buffer_t(), /*index*/0, bufferCount);
439 mPreparedBufferIdx = 0;
440
441 mState = STATE_PREPARING;
442
443 return NOT_ENOUGH_DATA;
444}
445
Shuzhen Wangb3a0fb52018-09-13 17:24:08 -0700446bool Camera3Stream::isBlockedByPrepare() const {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700447 Mutex::Autolock l(mLock);
Shuzhen Wangb3a0fb52018-09-13 17:24:08 -0700448 return mState == STATE_PREPARING && mPrepareBlockRequest;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700449}
450
Chien-Yu Chene8c535e2016-04-14 12:18:26 -0700451bool Camera3Stream::isAbandoned() const {
452 Mutex::Autolock l(mLock);
453 return mState == STATE_ABANDONED;
454}
455
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700456status_t Camera3Stream::prepareNextBuffer() {
457 ATRACE_CALL();
458
459 Mutex::Autolock l(mLock);
460 status_t res = OK;
461
462 // This function should be only called when the stream is preparing
463 if (mState != STATE_PREPARING) {
464 ALOGE("%s: Stream %d: Can't prepare buffer if stream is not in PREPARING "
465 "state %d", __FUNCTION__, mId, mState);
466 return INVALID_OPERATION;
467 }
468
469 // Get next buffer - this may allocate, and take a while for large buffers
470 res = getBufferLocked( &mPreparedBuffers.editItemAt(mPreparedBufferIdx) );
471 if (res != OK) {
Eino-Ville Talvala02bf0322016-02-18 12:41:10 -0800472 ALOGE("%s: Stream %d: Unable to allocate buffer %zu during preparation",
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700473 __FUNCTION__, mId, mPreparedBufferIdx);
474 return NO_INIT;
475 }
476
477 mPreparedBufferIdx++;
478
479 // Check if we still have buffers left to allocate
480 if (mPreparedBufferIdx < mPreparedBuffers.size()) {
481 return NOT_ENOUGH_DATA;
482 }
483
484 // Done with prepare - mark stream as such, and return all buffers
485 // via cancelPrepare
486 mPrepared = true;
487
488 return cancelPrepareLocked();
489}
490
491status_t Camera3Stream::cancelPrepare() {
492 ATRACE_CALL();
493
494 Mutex::Autolock l(mLock);
495
496 return cancelPrepareLocked();
497}
498
499status_t Camera3Stream::cancelPrepareLocked() {
500 status_t res = OK;
501
502 // This function should be only called when the stream is mid-preparing.
503 if (mState != STATE_PREPARING) {
504 ALOGE("%s: Stream %d: Can't cancel prepare stream if stream is not in "
505 "PREPARING state %d", __FUNCTION__, mId, mState);
506 return INVALID_OPERATION;
507 }
508
509 // Return all valid buffers to stream, in ERROR state to indicate
510 // they weren't filled.
511 for (size_t i = 0; i < mPreparedBufferIdx; i++) {
512 mPreparedBuffers.editItemAt(i).release_fence = -1;
513 mPreparedBuffers.editItemAt(i).status = CAMERA3_BUFFER_STATUS_ERROR;
514 returnBufferLocked(mPreparedBuffers[i], 0);
515 }
516 mPreparedBuffers.clear();
517 mPreparedBufferIdx = 0;
518
519 mState = STATE_CONFIGURED;
520
521 return res;
522}
523
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -0700524status_t Camera3Stream::tearDown() {
525 ATRACE_CALL();
526 Mutex::Autolock l(mLock);
527
528 status_t res = OK;
529
530 // This function should be only called when the stream is configured.
531 if (mState != STATE_CONFIGURED) {
532 ALOGE("%s: Stream %d: Can't tear down stream if stream is not in "
533 "CONFIGURED state %d", __FUNCTION__, mId, mState);
534 return INVALID_OPERATION;
535 }
536
537 // If any buffers have been handed to the HAL, the stream cannot be torn down.
538 if (getHandoutOutputBufferCountLocked() > 0) {
539 ALOGE("%s: Stream %d: Can't tear down a stream that has outstanding buffers",
540 __FUNCTION__, mId);
541 return INVALID_OPERATION;
542 }
543
544 // Free buffers by disconnecting and then reconnecting to the buffer queue
545 // Only unused buffers will be dropped immediately; buffers that have been filled
546 // and are waiting to be acquired by the consumer and buffers that are currently
547 // acquired will be freed once they are released by the consumer.
548
549 res = disconnectLocked();
550 if (res != OK) {
551 if (res == -ENOTCONN) {
552 // queue has been disconnected, nothing left to do, so exit with success
553 return OK;
554 }
555 ALOGE("%s: Stream %d: Unable to disconnect to tear down buffers: %s (%d)",
556 __FUNCTION__, mId, strerror(-res), res);
557 return res;
558 }
559
560 mState = STATE_IN_CONFIG;
561
562 res = configureQueueLocked();
563 if (res != OK) {
564 ALOGE("%s: Unable to configure stream %d queue: %s (%d)",
565 __FUNCTION__, mId, strerror(-res), res);
566 mState = STATE_ERROR;
567 return res;
568 }
569
570 // Reset prepared state, since we've reconnected to the queue and can prepare again.
571 mPrepared = false;
572 mStreamUnpreparable = false;
573
574 mState = STATE_CONFIGURED;
575
576 return OK;
577}
578
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800579status_t Camera3Stream::getBuffer(camera3_stream_buffer *buffer,
Yin-Chia Yehb3a80b12018-09-04 12:13:05 -0700580 nsecs_t waitBufferTimeout,
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800581 const std::vector<size_t>& surface_ids) {
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800582 ATRACE_CALL();
583 Mutex::Autolock l(mLock);
Zhijun He6adc9cc2014-04-15 14:09:55 -0700584 status_t res = OK;
Igor Murashkin2fba5842013-04-22 14:03:54 -0700585
Zhijun He6adc9cc2014-04-15 14:09:55 -0700586 // This function should be only called when the stream is configured already.
587 if (mState != STATE_CONFIGURED) {
588 ALOGE("%s: Stream %d: Can't get buffers if stream is not in CONFIGURED state %d",
589 __FUNCTION__, mId, mState);
590 return INVALID_OPERATION;
591 }
592
593 // Wait for new buffer returned back if we are running into the limit.
594 if (getHandoutOutputBufferCountLocked() == camera3_stream::max_buffers) {
595 ALOGV("%s: Already dequeued max output buffers (%d), wait for next returned one.",
Shuzhen Wang686f6442017-06-20 16:16:04 -0700596 __FUNCTION__, camera3_stream::max_buffers);
597 nsecs_t waitStart = systemTime(SYSTEM_TIME_MONOTONIC);
Yin-Chia Yehb3a80b12018-09-04 12:13:05 -0700598 if (waitBufferTimeout < kWaitForBufferDuration) {
599 waitBufferTimeout = kWaitForBufferDuration;
600 }
601 res = mOutputBufferReturnedSignal.waitRelative(mLock, waitBufferTimeout);
Shuzhen Wang686f6442017-06-20 16:16:04 -0700602 nsecs_t waitEnd = systemTime(SYSTEM_TIME_MONOTONIC);
603 mBufferLimitLatency.add(waitStart, waitEnd);
Zhijun He6adc9cc2014-04-15 14:09:55 -0700604 if (res != OK) {
605 if (res == TIMED_OUT) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -0700606 ALOGE("%s: wait for output buffer return timed out after %lldms (max_buffers %d)",
Yin-Chia Yehb3a80b12018-09-04 12:13:05 -0700607 __FUNCTION__, waitBufferTimeout / 1000000LL,
Chien-Yu Chen85a64552015-08-28 15:46:12 -0700608 camera3_stream::max_buffers);
Zhijun He6adc9cc2014-04-15 14:09:55 -0700609 }
610 return res;
611 }
612 }
613
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800614 res = getBufferLocked(buffer, surface_ids);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700615 if (res == OK) {
616 fireBufferListenersLocked(*buffer, /*acquired*/true, /*output*/true);
Chien-Yu Chene02e9322016-04-11 16:59:33 -0700617 if (buffer->buffer) {
Emilian Peev889234d2017-07-18 18:21:26 -0700618 Mutex::Autolock l(mOutstandingBuffersLock);
Chien-Yu Chene02e9322016-04-11 16:59:33 -0700619 mOutstandingBuffers.push_back(*buffer->buffer);
620 }
Igor Murashkin2fba5842013-04-22 14:03:54 -0700621 }
622
623 return res;
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800624}
625
Emilian Peev889234d2017-07-18 18:21:26 -0700626bool Camera3Stream::isOutstandingBuffer(const camera3_stream_buffer &buffer) const{
Chien-Yu Chene02e9322016-04-11 16:59:33 -0700627 if (buffer.buffer == nullptr) {
628 return false;
629 }
630
Emilian Peev889234d2017-07-18 18:21:26 -0700631 Mutex::Autolock l(mOutstandingBuffersLock);
632
Chien-Yu Chene02e9322016-04-11 16:59:33 -0700633 for (auto b : mOutstandingBuffers) {
634 if (b == *buffer.buffer) {
635 return true;
636 }
637 }
638 return false;
639}
640
641void Camera3Stream::removeOutstandingBuffer(const camera3_stream_buffer &buffer) {
642 if (buffer.buffer == nullptr) {
643 return;
644 }
645
Emilian Peev889234d2017-07-18 18:21:26 -0700646 Mutex::Autolock l(mOutstandingBuffersLock);
647
Chien-Yu Chene02e9322016-04-11 16:59:33 -0700648 for (auto b = mOutstandingBuffers.begin(); b != mOutstandingBuffers.end(); b++) {
649 if (*b == *buffer.buffer) {
650 mOutstandingBuffers.erase(b);
651 return;
652 }
653 }
654}
655
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800656status_t Camera3Stream::returnBuffer(const camera3_stream_buffer &buffer,
657 nsecs_t timestamp) {
658 ATRACE_CALL();
659 Mutex::Autolock l(mLock);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700660
Chien-Yu Chene02e9322016-04-11 16:59:33 -0700661 // Check if this buffer is outstanding.
662 if (!isOutstandingBuffer(buffer)) {
663 ALOGE("%s: Stream %d: Returning an unknown buffer.", __FUNCTION__, mId);
664 return BAD_VALUE;
665 }
666
Shuzhen Wang1c484a62017-07-14 15:14:19 -0700667 removeOutstandingBuffer(buffer);
668
Igor Murashkin13d315e2014-04-03 18:09:04 -0700669 /**
670 * TODO: Check that the state is valid first.
671 *
672 * <HAL3.2 IN_CONFIG and IN_RECONFIG in addition to CONFIGURED.
673 * >= HAL3.2 CONFIGURED only
674 *
675 * Do this for getBuffer as well.
676 */
Igor Murashkin2fba5842013-04-22 14:03:54 -0700677 status_t res = returnBufferLocked(buffer, timestamp);
678 if (res == OK) {
679 fireBufferListenersLocked(buffer, /*acquired*/false, /*output*/true);
680 }
681
Chien-Yu Chenb83c1fe2015-09-10 16:15:21 -0700682 // Even if returning the buffer failed, we still want to signal whoever is waiting for the
683 // buffer to be returned.
684 mOutputBufferReturnedSignal.signal();
685
Igor Murashkin2fba5842013-04-22 14:03:54 -0700686 return res;
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800687}
688
Eino-Ville Talvalaba435252017-06-21 16:07:25 -0700689status_t Camera3Stream::getInputBuffer(camera3_stream_buffer *buffer, bool respectHalLimit) {
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700690 ATRACE_CALL();
691 Mutex::Autolock l(mLock);
Zhijun He6adc9cc2014-04-15 14:09:55 -0700692 status_t res = OK;
Igor Murashkin2fba5842013-04-22 14:03:54 -0700693
Zhijun He6adc9cc2014-04-15 14:09:55 -0700694 // This function should be only called when the stream is configured already.
695 if (mState != STATE_CONFIGURED) {
696 ALOGE("%s: Stream %d: Can't get input buffers if stream is not in CONFIGURED state %d",
697 __FUNCTION__, mId, mState);
698 return INVALID_OPERATION;
699 }
700
701 // Wait for new buffer returned back if we are running into the limit.
Eino-Ville Talvalaba435252017-06-21 16:07:25 -0700702 if (getHandoutInputBufferCountLocked() == camera3_stream::max_buffers && respectHalLimit) {
Zhijun He6adc9cc2014-04-15 14:09:55 -0700703 ALOGV("%s: Already dequeued max input buffers (%d), wait for next returned one.",
704 __FUNCTION__, camera3_stream::max_buffers);
705 res = mInputBufferReturnedSignal.waitRelative(mLock, kWaitForBufferDuration);
706 if (res != OK) {
707 if (res == TIMED_OUT) {
708 ALOGE("%s: wait for input buffer return timed out after %lldms", __FUNCTION__,
709 kWaitForBufferDuration / 1000000LL);
710 }
711 return res;
712 }
713 }
714
715 res = getInputBufferLocked(buffer);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700716 if (res == OK) {
717 fireBufferListenersLocked(*buffer, /*acquired*/true, /*output*/false);
Chien-Yu Chene02e9322016-04-11 16:59:33 -0700718 if (buffer->buffer) {
Emilian Peev889234d2017-07-18 18:21:26 -0700719 Mutex::Autolock l(mOutstandingBuffersLock);
Chien-Yu Chene02e9322016-04-11 16:59:33 -0700720 mOutstandingBuffers.push_back(*buffer->buffer);
721 }
Igor Murashkin2fba5842013-04-22 14:03:54 -0700722 }
723
724 return res;
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700725}
726
727status_t Camera3Stream::returnInputBuffer(const camera3_stream_buffer &buffer) {
728 ATRACE_CALL();
729 Mutex::Autolock l(mLock);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700730
Chien-Yu Chene02e9322016-04-11 16:59:33 -0700731 // Check if this buffer is outstanding.
732 if (!isOutstandingBuffer(buffer)) {
733 ALOGE("%s: Stream %d: Returning an unknown buffer.", __FUNCTION__, mId);
734 return BAD_VALUE;
735 }
736
Shuzhen Wang1c484a62017-07-14 15:14:19 -0700737 removeOutstandingBuffer(buffer);
738
Igor Murashkin2fba5842013-04-22 14:03:54 -0700739 status_t res = returnInputBufferLocked(buffer);
740 if (res == OK) {
741 fireBufferListenersLocked(buffer, /*acquired*/false, /*output*/false);
Zhijun He6adc9cc2014-04-15 14:09:55 -0700742 mInputBufferReturnedSignal.signal();
Igor Murashkin2fba5842013-04-22 14:03:54 -0700743 }
Chien-Yu Chene02e9322016-04-11 16:59:33 -0700744
Igor Murashkin2fba5842013-04-22 14:03:54 -0700745 return res;
746}
747
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700748status_t Camera3Stream::getInputBufferProducer(sp<IGraphicBufferProducer> *producer) {
749 ATRACE_CALL();
750 Mutex::Autolock l(mLock);
751
752 return getInputBufferProducerLocked(producer);
753}
754
Igor Murashkin2fba5842013-04-22 14:03:54 -0700755void Camera3Stream::fireBufferListenersLocked(
Shuzhen Wangb0fdc1e2016-03-20 23:21:39 -0700756 const camera3_stream_buffer& buffer, bool acquired, bool output) {
Igor Murashkin2fba5842013-04-22 14:03:54 -0700757 List<wp<Camera3StreamBufferListener> >::iterator it, end;
758
759 // TODO: finish implementing
760
761 Camera3StreamBufferListener::BufferInfo info =
762 Camera3StreamBufferListener::BufferInfo();
763 info.mOutput = output;
Shuzhen Wangb0fdc1e2016-03-20 23:21:39 -0700764 info.mError = (buffer.status == CAMERA3_BUFFER_STATUS_ERROR);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700765 // TODO: rest of fields
766
767 for (it = mBufferListenerList.begin(), end = mBufferListenerList.end();
768 it != end;
769 ++it) {
770
771 sp<Camera3StreamBufferListener> listener = it->promote();
772 if (listener != 0) {
773 if (acquired) {
774 listener->onBufferAcquired(info);
775 } else {
776 listener->onBufferReleased(info);
777 }
778 }
779 }
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700780}
781
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800782bool Camera3Stream::hasOutstandingBuffers() const {
783 ATRACE_CALL();
784 Mutex::Autolock l(mLock);
785 return hasOutstandingBuffersLocked();
786}
787
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700788status_t Camera3Stream::setStatusTracker(sp<StatusTracker> statusTracker) {
789 Mutex::Autolock l(mLock);
790 sp<StatusTracker> oldTracker = mStatusTracker.promote();
791 if (oldTracker != 0 && mStatusId != StatusTracker::NO_STATUS_ID) {
792 oldTracker->removeComponent(mStatusId);
793 }
794 mStatusId = StatusTracker::NO_STATUS_ID;
795 mStatusTracker = statusTracker;
796
797 return OK;
798}
799
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800800status_t Camera3Stream::disconnect() {
801 ATRACE_CALL();
802 Mutex::Autolock l(mLock);
Igor Murashkine2172be2013-05-28 15:31:39 -0700803 ALOGV("%s: Stream %d: Disconnecting...", __FUNCTION__, mId);
804 status_t res = disconnectLocked();
805
Shuzhen Wang686f6442017-06-20 16:16:04 -0700806 mBufferLimitLatency.log("Stream %d latency histogram for wait on max_buffers", mId);
807 mBufferLimitLatency.reset();
808
Igor Murashkine2172be2013-05-28 15:31:39 -0700809 if (res == -ENOTCONN) {
810 // "Already disconnected" -- not an error
811 return OK;
812 } else {
813 return res;
814 }
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800815}
816
Shuzhen Wang686f6442017-06-20 16:16:04 -0700817void Camera3Stream::dump(int fd, const Vector<String16> &args) const
818{
819 (void)args;
820 mBufferLimitLatency.dump(fd,
821 " Latency histogram for wait on max_buffers");
822}
823
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800824status_t Camera3Stream::getBufferLocked(camera3_stream_buffer *,
825 const std::vector<size_t>&) {
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700826 ALOGE("%s: This type of stream does not support output", __FUNCTION__);
827 return INVALID_OPERATION;
828}
829status_t Camera3Stream::returnBufferLocked(const camera3_stream_buffer &,
830 nsecs_t) {
831 ALOGE("%s: This type of stream does not support output", __FUNCTION__);
832 return INVALID_OPERATION;
833}
834status_t Camera3Stream::getInputBufferLocked(camera3_stream_buffer *) {
835 ALOGE("%s: This type of stream does not support input", __FUNCTION__);
836 return INVALID_OPERATION;
837}
838status_t Camera3Stream::returnInputBufferLocked(
839 const camera3_stream_buffer &) {
840 ALOGE("%s: This type of stream does not support input", __FUNCTION__);
841 return INVALID_OPERATION;
842}
Eino-Ville Talvala02bf0322016-02-18 12:41:10 -0800843status_t Camera3Stream::getInputBufferProducerLocked(sp<IGraphicBufferProducer>*) {
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700844 ALOGE("%s: This type of stream does not support input", __FUNCTION__);
845 return INVALID_OPERATION;
846}
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700847
Igor Murashkin2fba5842013-04-22 14:03:54 -0700848void Camera3Stream::addBufferListener(
849 wp<Camera3StreamBufferListener> listener) {
850 Mutex::Autolock l(mLock);
Zhijun Hef0d962a2014-06-30 10:24:11 -0700851
852 List<wp<Camera3StreamBufferListener> >::iterator it, end;
853 for (it = mBufferListenerList.begin(), end = mBufferListenerList.end();
854 it != end;
855 ) {
856 if (*it == listener) {
857 ALOGE("%s: Try to add the same listener twice, ignoring...", __FUNCTION__);
858 return;
859 }
860 it++;
861 }
862
Igor Murashkin2fba5842013-04-22 14:03:54 -0700863 mBufferListenerList.push_back(listener);
864}
865
866void Camera3Stream::removeBufferListener(
867 const sp<Camera3StreamBufferListener>& listener) {
868 Mutex::Autolock l(mLock);
869
870 bool erased = true;
871 List<wp<Camera3StreamBufferListener> >::iterator it, end;
872 for (it = mBufferListenerList.begin(), end = mBufferListenerList.end();
873 it != end;
874 ) {
875
876 if (*it == listener) {
877 it = mBufferListenerList.erase(it);
878 erased = true;
879 } else {
880 ++it;
881 }
882 }
883
884 if (!erased) {
885 ALOGW("%s: Could not find listener to remove, already removed",
886 __FUNCTION__);
887 }
888}
889
Yin-Chia Yehbe83fa72017-03-30 13:35:36 -0700890void Camera3Stream::setBufferFreedListener(
Yin-Chia Yehdb1e8642017-07-14 15:19:30 -0700891 wp<Camera3StreamBufferFreedListener> listener) {
Yin-Chia Yehbe83fa72017-03-30 13:35:36 -0700892 Mutex::Autolock l(mLock);
893 // Only allow set listener during stream configuration because stream is guaranteed to be IDLE
894 // at this state, so setBufferFreedListener won't collide with onBufferFreed callbacks
895 if (mState != STATE_IN_CONFIG && mState != STATE_IN_RECONFIG) {
896 ALOGE("%s: listener must be set during stream configuration!",__FUNCTION__);
897 return;
898 }
899 mBufferFreedListener = listener;
900}
901
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800902}; // namespace camera3
903
904}; // namespace android