blob: b29651324555da047275d5881c82e47adf4b3dc4 [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),
Shuzhen Wang26abaf42018-08-28 15:41:20 -070070 mPhysicalCameraId(physicalCameraId),
71 mLastTimestamp(0) {
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -080072
73 camera3_stream::stream_type = type;
74 camera3_stream::width = width;
75 camera3_stream::height = height;
76 camera3_stream::format = format;
Eino-Ville Talvala3d82c0d2015-02-23 15:19:19 -080077 camera3_stream::data_space = dataSpace;
Yin-Chia Yehb97babb2015-03-12 13:42:44 -070078 camera3_stream::rotation = rotation;
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -080079 camera3_stream::max_buffers = 0;
80 camera3_stream::priv = NULL;
Shuzhen Wangc28189a2017-11-27 23:05:10 -080081 camera3_stream::physical_camera_id = mPhysicalCameraId.string();
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -080082
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -080083 if ((format == HAL_PIXEL_FORMAT_BLOB || format == HAL_PIXEL_FORMAT_RAW_OPAQUE) &&
84 maxSize == 0) {
85 ALOGE("%s: BLOB or RAW_OPAQUE format with size == 0", __FUNCTION__);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -080086 mState = STATE_ERROR;
87 }
88}
89
90int Camera3Stream::getId() const {
91 return mId;
92}
93
Zhijun He125684a2015-12-26 15:07:30 -080094int Camera3Stream::getStreamSetId() const {
95 return mSetId;
96}
97
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -080098uint32_t Camera3Stream::getWidth() const {
99 return camera3_stream::width;
100}
101
102uint32_t Camera3Stream::getHeight() const {
103 return camera3_stream::height;
104}
105
106int Camera3Stream::getFormat() const {
107 return camera3_stream::format;
108}
109
Eino-Ville Talvala3d82c0d2015-02-23 15:19:19 -0800110android_dataspace Camera3Stream::getDataSpace() const {
111 return camera3_stream::data_space;
112}
113
Emilian Peev050f5dc2017-05-18 14:43:56 +0100114uint64_t Camera3Stream::getUsage() const {
115 return mUsage;
116}
117
118void Camera3Stream::setUsage(uint64_t usage) {
119 mUsage = usage;
120}
121
Emilian Peev710c1422017-08-30 11:19:38 +0100122void Camera3Stream::setFormatOverride(bool formatOverridden) {
123 mFormatOverridden = formatOverridden;
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -0700124 if (formatOverridden) mOriginalFormat = camera3_stream::format;
Emilian Peev710c1422017-08-30 11:19:38 +0100125}
126
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -0700127bool Camera3Stream::isFormatOverridden() const {
Emilian Peev710c1422017-08-30 11:19:38 +0100128 return mFormatOverridden;
129}
130
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -0700131int Camera3Stream::getOriginalFormat() const {
132 return mOriginalFormat;
Emilian Peev710c1422017-08-30 11:19:38 +0100133}
134
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -0700135void Camera3Stream::setDataSpaceOverride(bool dataSpaceOverridden) {
136 mDataSpaceOverridden = dataSpaceOverridden;
137 if (dataSpaceOverridden) mOriginalDataSpace = camera3_stream::data_space;
138}
139
140bool Camera3Stream::isDataSpaceOverridden() const {
141 return mDataSpaceOverridden;
142}
143
144android_dataspace Camera3Stream::getOriginalDataSpace() const {
145 return mOriginalDataSpace;
Emilian Peev710c1422017-08-30 11:19:38 +0100146}
147
Shuzhen Wang5c22c152017-12-31 17:12:25 -0800148const String8& Camera3Stream::physicalCameraId() const {
149 return mPhysicalCameraId;
150}
151
Emilian Peevac3ce6c2017-12-12 15:27:02 +0000152status_t Camera3Stream::forceToIdle() {
153 ATRACE_CALL();
154 Mutex::Autolock l(mLock);
155 status_t res;
156
157 switch (mState) {
158 case STATE_ERROR:
159 case STATE_CONSTRUCTED:
160 case STATE_IN_CONFIG:
161 case STATE_PREPARING:
162 case STATE_IN_RECONFIG:
163 ALOGE("%s: Invalid state: %d", __FUNCTION__, mState);
164 res = NO_INIT;
165 break;
166 case STATE_CONFIGURED:
167 if (hasOutstandingBuffersLocked()) {
168 sp<StatusTracker> statusTracker = mStatusTracker.promote();
169 if (statusTracker != 0) {
170 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
171 }
172 }
173
174 mState = STATE_IN_IDLE;
175 res = OK;
176
177 break;
178 default:
179 ALOGE("%s: Unknown state %d", __FUNCTION__, mState);
180 res = NO_INIT;
181 }
182
183 return res;
184}
185
186status_t Camera3Stream::restoreConfiguredState() {
187 ATRACE_CALL();
188 Mutex::Autolock l(mLock);
189 status_t res;
190
191 switch (mState) {
192 case STATE_ERROR:
193 case STATE_CONSTRUCTED:
194 case STATE_IN_CONFIG:
195 case STATE_PREPARING:
196 case STATE_IN_RECONFIG:
197 case STATE_CONFIGURED:
198 ALOGE("%s: Invalid state: %d", __FUNCTION__, mState);
199 res = NO_INIT;
200 break;
201 case STATE_IN_IDLE:
202 if (hasOutstandingBuffersLocked()) {
203 sp<StatusTracker> statusTracker = mStatusTracker.promote();
204 if (statusTracker != 0) {
205 statusTracker->markComponentActive(mStatusId);
206 }
207 }
208
209 mState = STATE_CONFIGURED;
210 res = OK;
211
212 break;
213 default:
214 ALOGE("%s: Unknown state %d", __FUNCTION__, mState);
215 res = NO_INIT;
216 }
217
218 return res;
219}
220
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800221camera3_stream* Camera3Stream::startConfiguration() {
Eino-Ville Talvalab2f5b192013-07-30 14:36:03 -0700222 ATRACE_CALL();
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800223 Mutex::Autolock l(mLock);
Eino-Ville Talvalab2f5b192013-07-30 14:36:03 -0700224 status_t res;
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800225
226 switch (mState) {
227 case STATE_ERROR:
228 ALOGE("%s: In error state", __FUNCTION__);
229 return NULL;
230 case STATE_CONSTRUCTED:
Emilian Peevac3ce6c2017-12-12 15:27:02 +0000231 case STATE_IN_IDLE:
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800232 // OK
233 break;
234 case STATE_IN_CONFIG:
235 case STATE_IN_RECONFIG:
236 // Can start config again with no trouble; but don't redo
Eino-Ville Talvala02bf0322016-02-18 12:41:10 -0800237 // mOldUsage/mOldMaxBuffers
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800238 return this;
239 case STATE_CONFIGURED:
Chien-Yu Chen90746f42015-04-15 13:50:13 -0700240 if (hasOutstandingBuffersLocked()) {
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800241 ALOGE("%s: Cannot configure stream; has outstanding buffers",
242 __FUNCTION__);
243 return NULL;
244 }
245 break;
246 default:
247 ALOGE("%s: Unknown state %d", __FUNCTION__, mState);
248 return NULL;
249 }
250
Emilian Peev050f5dc2017-05-18 14:43:56 +0100251 mOldUsage = mUsage;
Eino-Ville Talvala02bf0322016-02-18 12:41:10 -0800252 mOldMaxBuffers = camera3_stream::max_buffers;
Eino-Ville Talvalab2f5b192013-07-30 14:36:03 -0700253
Emilian Peev050f5dc2017-05-18 14:43:56 +0100254 res = getEndpointUsage(&mUsage);
Eino-Ville Talvalab2f5b192013-07-30 14:36:03 -0700255 if (res != OK) {
256 ALOGE("%s: Cannot query consumer endpoint usage!",
257 __FUNCTION__);
258 return NULL;
259 }
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800260
Emilian Peevac3ce6c2017-12-12 15:27:02 +0000261 if (mState == STATE_IN_IDLE) {
262 // Skip configuration.
263 return this;
264 }
265
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700266 // Stop tracking if currently doing so
267 if (mStatusId != StatusTracker::NO_STATUS_ID) {
268 sp<StatusTracker> statusTracker = mStatusTracker.promote();
269 if (statusTracker != 0) {
270 statusTracker->removeComponent(mStatusId);
271 }
272 mStatusId = StatusTracker::NO_STATUS_ID;
273 }
274
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800275 if (mState == STATE_CONSTRUCTED) {
276 mState = STATE_IN_CONFIG;
277 } else { // mState == STATE_CONFIGURED
Igor Murashkin13d315e2014-04-03 18:09:04 -0700278 LOG_ALWAYS_FATAL_IF(mState != STATE_CONFIGURED, "Invalid state: 0x%x", mState);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800279 mState = STATE_IN_RECONFIG;
280 }
281
282 return this;
283}
284
285bool Camera3Stream::isConfiguring() const {
286 Mutex::Autolock l(mLock);
287 return (mState == STATE_IN_CONFIG) || (mState == STATE_IN_RECONFIG);
288}
289
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800290status_t Camera3Stream::finishConfiguration() {
Eino-Ville Talvalab2f5b192013-07-30 14:36:03 -0700291 ATRACE_CALL();
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800292 Mutex::Autolock l(mLock);
293 switch (mState) {
294 case STATE_ERROR:
295 ALOGE("%s: In error state", __FUNCTION__);
296 return INVALID_OPERATION;
297 case STATE_IN_CONFIG:
298 case STATE_IN_RECONFIG:
299 // OK
300 break;
301 case STATE_CONSTRUCTED:
302 case STATE_CONFIGURED:
303 ALOGE("%s: Cannot finish configuration that hasn't been started",
304 __FUNCTION__);
305 return INVALID_OPERATION;
Emilian Peevac3ce6c2017-12-12 15:27:02 +0000306 case STATE_IN_IDLE:
307 //Skip configuration in this state
308 return OK;
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800309 default:
310 ALOGE("%s: Unknown state", __FUNCTION__);
311 return INVALID_OPERATION;
312 }
313
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700314 // Register for idle tracking
315 sp<StatusTracker> statusTracker = mStatusTracker.promote();
316 if (statusTracker != 0) {
317 mStatusId = statusTracker->addComponent();
318 }
319
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800320 // Check if the stream configuration is unchanged, and skip reallocation if
321 // so. As documented in hardware/camera3.h:configure_streams().
322 if (mState == STATE_IN_RECONFIG &&
Emilian Peev050f5dc2017-05-18 14:43:56 +0100323 mOldUsage == mUsage &&
Eino-Ville Talvala02bf0322016-02-18 12:41:10 -0800324 mOldMaxBuffers == camera3_stream::max_buffers) {
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800325 mState = STATE_CONFIGURED;
326 return OK;
327 }
328
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700329 // Reset prepared state, since buffer config has changed, and existing
330 // allocations are no longer valid
331 mPrepared = false;
Shuzhen Wangb3a0fb52018-09-13 17:24:08 -0700332 mPrepareBlockRequest = true;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700333 mStreamUnpreparable = false;
334
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800335 status_t res;
336 res = configureQueueLocked();
Shuzhen Wang210ba5c2018-07-25 16:47:40 -0700337 // configureQueueLocked could return error in case of abandoned surface.
338 // Treat as non-fatal error.
339 if (res == NO_INIT || res == DEAD_OBJECT) {
340 ALOGE("%s: Unable to configure stream %d queue (non-fatal): %s (%d)",
341 __FUNCTION__, mId, strerror(-res), res);
342 mState = STATE_ABANDONED;
343 return res;
344 } else if (res != OK) {
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800345 ALOGE("%s: Unable to configure stream %d queue: %s (%d)",
346 __FUNCTION__, mId, strerror(-res), res);
347 mState = STATE_ERROR;
348 return res;
349 }
350
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800351 mState = STATE_CONFIGURED;
352
353 return res;
354}
355
Eino-Ville Talvala17543512014-08-06 14:32:02 -0700356status_t Camera3Stream::cancelConfiguration() {
357 ATRACE_CALL();
358 Mutex::Autolock l(mLock);
359 switch (mState) {
360 case STATE_ERROR:
361 ALOGE("%s: In error state", __FUNCTION__);
362 return INVALID_OPERATION;
363 case STATE_IN_CONFIG:
364 case STATE_IN_RECONFIG:
Emilian Peevac3ce6c2017-12-12 15:27:02 +0000365 case STATE_IN_IDLE:
Eino-Ville Talvala17543512014-08-06 14:32:02 -0700366 // OK
367 break;
368 case STATE_CONSTRUCTED:
369 case STATE_CONFIGURED:
370 ALOGE("%s: Cannot cancel configuration that hasn't been started",
371 __FUNCTION__);
372 return INVALID_OPERATION;
373 default:
374 ALOGE("%s: Unknown state", __FUNCTION__);
375 return INVALID_OPERATION;
376 }
377
Emilian Peev050f5dc2017-05-18 14:43:56 +0100378 mUsage = mOldUsage;
Eino-Ville Talvala02bf0322016-02-18 12:41:10 -0800379 camera3_stream::max_buffers = mOldMaxBuffers;
Eino-Ville Talvala17543512014-08-06 14:32:02 -0700380
Emilian Peevac3ce6c2017-12-12 15:27:02 +0000381 mState = ((mState == STATE_IN_RECONFIG) || (mState == STATE_IN_IDLE)) ? STATE_CONFIGURED :
382 STATE_CONSTRUCTED;
383
Eino-Ville Talvala17543512014-08-06 14:32:02 -0700384 return OK;
385}
386
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700387bool Camera3Stream::isUnpreparable() {
388 ATRACE_CALL();
389
390 Mutex::Autolock l(mLock);
391 return mStreamUnpreparable;
392}
393
Shuzhen Wangb3a0fb52018-09-13 17:24:08 -0700394status_t Camera3Stream::startPrepare(int maxCount, bool blockRequest) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700395 ATRACE_CALL();
396
397 Mutex::Autolock l(mLock);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700398
Ruben Brunkc78ac262015-08-13 17:58:46 -0700399 if (maxCount < 0) {
400 ALOGE("%s: Stream %d: Can't prepare stream if max buffer count (%d) is < 0",
401 __FUNCTION__, mId, maxCount);
402 return BAD_VALUE;
403 }
404
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700405 // This function should be only called when the stream is configured already.
406 if (mState != STATE_CONFIGURED) {
407 ALOGE("%s: Stream %d: Can't prepare stream if stream is not in CONFIGURED "
408 "state %d", __FUNCTION__, mId, mState);
409 return INVALID_OPERATION;
410 }
411
412 // This function can't be called if the stream has already received filled
413 // buffers
414 if (mStreamUnpreparable) {
415 ALOGE("%s: Stream %d: Can't prepare stream that's already in use",
416 __FUNCTION__, mId);
417 return INVALID_OPERATION;
418 }
419
420 if (getHandoutOutputBufferCountLocked() > 0) {
421 ALOGE("%s: Stream %d: Can't prepare stream that has outstanding buffers",
422 __FUNCTION__, mId);
423 return INVALID_OPERATION;
424 }
425
Ruben Brunkc78ac262015-08-13 17:58:46 -0700426 size_t pipelineMax = getBufferCountLocked();
427 size_t clampedCount = (pipelineMax < static_cast<size_t>(maxCount)) ?
428 pipelineMax : static_cast<size_t>(maxCount);
429 size_t bufferCount = (maxCount == Camera3StreamInterface::ALLOCATE_PIPELINE_MAX) ?
430 pipelineMax : clampedCount;
431
432 mPrepared = bufferCount <= mLastMaxCount;
Shuzhen Wangb3a0fb52018-09-13 17:24:08 -0700433 mPrepareBlockRequest = blockRequest;
Ruben Brunkc78ac262015-08-13 17:58:46 -0700434
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700435 if (mPrepared) return OK;
436
Ruben Brunkc78ac262015-08-13 17:58:46 -0700437 mLastMaxCount = bufferCount;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700438
439 mPreparedBuffers.insertAt(camera3_stream_buffer_t(), /*index*/0, bufferCount);
440 mPreparedBufferIdx = 0;
441
442 mState = STATE_PREPARING;
443
444 return NOT_ENOUGH_DATA;
445}
446
Shuzhen Wangb3a0fb52018-09-13 17:24:08 -0700447bool Camera3Stream::isBlockedByPrepare() const {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700448 Mutex::Autolock l(mLock);
Shuzhen Wangb3a0fb52018-09-13 17:24:08 -0700449 return mState == STATE_PREPARING && mPrepareBlockRequest;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700450}
451
Chien-Yu Chene8c535e2016-04-14 12:18:26 -0700452bool Camera3Stream::isAbandoned() const {
453 Mutex::Autolock l(mLock);
454 return mState == STATE_ABANDONED;
455}
456
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700457status_t Camera3Stream::prepareNextBuffer() {
458 ATRACE_CALL();
459
460 Mutex::Autolock l(mLock);
461 status_t res = OK;
462
463 // This function should be only called when the stream is preparing
464 if (mState != STATE_PREPARING) {
465 ALOGE("%s: Stream %d: Can't prepare buffer if stream is not in PREPARING "
466 "state %d", __FUNCTION__, mId, mState);
467 return INVALID_OPERATION;
468 }
469
470 // Get next buffer - this may allocate, and take a while for large buffers
471 res = getBufferLocked( &mPreparedBuffers.editItemAt(mPreparedBufferIdx) );
472 if (res != OK) {
Eino-Ville Talvala02bf0322016-02-18 12:41:10 -0800473 ALOGE("%s: Stream %d: Unable to allocate buffer %zu during preparation",
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700474 __FUNCTION__, mId, mPreparedBufferIdx);
475 return NO_INIT;
476 }
477
478 mPreparedBufferIdx++;
479
480 // Check if we still have buffers left to allocate
481 if (mPreparedBufferIdx < mPreparedBuffers.size()) {
482 return NOT_ENOUGH_DATA;
483 }
484
485 // Done with prepare - mark stream as such, and return all buffers
486 // via cancelPrepare
487 mPrepared = true;
488
489 return cancelPrepareLocked();
490}
491
492status_t Camera3Stream::cancelPrepare() {
493 ATRACE_CALL();
494
495 Mutex::Autolock l(mLock);
496
497 return cancelPrepareLocked();
498}
499
500status_t Camera3Stream::cancelPrepareLocked() {
501 status_t res = OK;
502
503 // This function should be only called when the stream is mid-preparing.
504 if (mState != STATE_PREPARING) {
505 ALOGE("%s: Stream %d: Can't cancel prepare stream if stream is not in "
506 "PREPARING state %d", __FUNCTION__, mId, mState);
507 return INVALID_OPERATION;
508 }
509
510 // Return all valid buffers to stream, in ERROR state to indicate
511 // they weren't filled.
512 for (size_t i = 0; i < mPreparedBufferIdx; i++) {
513 mPreparedBuffers.editItemAt(i).release_fence = -1;
514 mPreparedBuffers.editItemAt(i).status = CAMERA3_BUFFER_STATUS_ERROR;
515 returnBufferLocked(mPreparedBuffers[i], 0);
516 }
517 mPreparedBuffers.clear();
518 mPreparedBufferIdx = 0;
519
520 mState = STATE_CONFIGURED;
521
522 return res;
523}
524
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -0700525status_t Camera3Stream::tearDown() {
526 ATRACE_CALL();
527 Mutex::Autolock l(mLock);
528
529 status_t res = OK;
530
531 // This function should be only called when the stream is configured.
532 if (mState != STATE_CONFIGURED) {
533 ALOGE("%s: Stream %d: Can't tear down stream if stream is not in "
534 "CONFIGURED state %d", __FUNCTION__, mId, mState);
535 return INVALID_OPERATION;
536 }
537
538 // If any buffers have been handed to the HAL, the stream cannot be torn down.
539 if (getHandoutOutputBufferCountLocked() > 0) {
540 ALOGE("%s: Stream %d: Can't tear down a stream that has outstanding buffers",
541 __FUNCTION__, mId);
542 return INVALID_OPERATION;
543 }
544
545 // Free buffers by disconnecting and then reconnecting to the buffer queue
546 // Only unused buffers will be dropped immediately; buffers that have been filled
547 // and are waiting to be acquired by the consumer and buffers that are currently
548 // acquired will be freed once they are released by the consumer.
549
550 res = disconnectLocked();
551 if (res != OK) {
552 if (res == -ENOTCONN) {
553 // queue has been disconnected, nothing left to do, so exit with success
554 return OK;
555 }
556 ALOGE("%s: Stream %d: Unable to disconnect to tear down buffers: %s (%d)",
557 __FUNCTION__, mId, strerror(-res), res);
558 return res;
559 }
560
561 mState = STATE_IN_CONFIG;
562
563 res = configureQueueLocked();
564 if (res != OK) {
565 ALOGE("%s: Unable to configure stream %d queue: %s (%d)",
566 __FUNCTION__, mId, strerror(-res), res);
567 mState = STATE_ERROR;
568 return res;
569 }
570
571 // Reset prepared state, since we've reconnected to the queue and can prepare again.
572 mPrepared = false;
573 mStreamUnpreparable = false;
574
575 mState = STATE_CONFIGURED;
576
577 return OK;
578}
579
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800580status_t Camera3Stream::getBuffer(camera3_stream_buffer *buffer,
Yin-Chia Yehb3a80b12018-09-04 12:13:05 -0700581 nsecs_t waitBufferTimeout,
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800582 const std::vector<size_t>& surface_ids) {
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800583 ATRACE_CALL();
584 Mutex::Autolock l(mLock);
Zhijun He6adc9cc2014-04-15 14:09:55 -0700585 status_t res = OK;
Igor Murashkin2fba5842013-04-22 14:03:54 -0700586
Zhijun He6adc9cc2014-04-15 14:09:55 -0700587 // This function should be only called when the stream is configured already.
588 if (mState != STATE_CONFIGURED) {
589 ALOGE("%s: Stream %d: Can't get buffers if stream is not in CONFIGURED state %d",
590 __FUNCTION__, mId, mState);
591 return INVALID_OPERATION;
592 }
593
594 // Wait for new buffer returned back if we are running into the limit.
595 if (getHandoutOutputBufferCountLocked() == camera3_stream::max_buffers) {
596 ALOGV("%s: Already dequeued max output buffers (%d), wait for next returned one.",
Shuzhen Wang686f6442017-06-20 16:16:04 -0700597 __FUNCTION__, camera3_stream::max_buffers);
598 nsecs_t waitStart = systemTime(SYSTEM_TIME_MONOTONIC);
Yin-Chia Yehb3a80b12018-09-04 12:13:05 -0700599 if (waitBufferTimeout < kWaitForBufferDuration) {
600 waitBufferTimeout = kWaitForBufferDuration;
601 }
602 res = mOutputBufferReturnedSignal.waitRelative(mLock, waitBufferTimeout);
Shuzhen Wang686f6442017-06-20 16:16:04 -0700603 nsecs_t waitEnd = systemTime(SYSTEM_TIME_MONOTONIC);
604 mBufferLimitLatency.add(waitStart, waitEnd);
Zhijun He6adc9cc2014-04-15 14:09:55 -0700605 if (res != OK) {
606 if (res == TIMED_OUT) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -0700607 ALOGE("%s: wait for output buffer return timed out after %lldms (max_buffers %d)",
Yin-Chia Yehb3a80b12018-09-04 12:13:05 -0700608 __FUNCTION__, waitBufferTimeout / 1000000LL,
Chien-Yu Chen85a64552015-08-28 15:46:12 -0700609 camera3_stream::max_buffers);
Zhijun He6adc9cc2014-04-15 14:09:55 -0700610 }
611 return res;
612 }
613 }
614
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800615 res = getBufferLocked(buffer, surface_ids);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700616 if (res == OK) {
617 fireBufferListenersLocked(*buffer, /*acquired*/true, /*output*/true);
Chien-Yu Chene02e9322016-04-11 16:59:33 -0700618 if (buffer->buffer) {
Emilian Peev889234d2017-07-18 18:21:26 -0700619 Mutex::Autolock l(mOutstandingBuffersLock);
Chien-Yu Chene02e9322016-04-11 16:59:33 -0700620 mOutstandingBuffers.push_back(*buffer->buffer);
621 }
Igor Murashkin2fba5842013-04-22 14:03:54 -0700622 }
623
624 return res;
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800625}
626
Emilian Peev889234d2017-07-18 18:21:26 -0700627bool Camera3Stream::isOutstandingBuffer(const camera3_stream_buffer &buffer) const{
Chien-Yu Chene02e9322016-04-11 16:59:33 -0700628 if (buffer.buffer == nullptr) {
629 return false;
630 }
631
Emilian Peev889234d2017-07-18 18:21:26 -0700632 Mutex::Autolock l(mOutstandingBuffersLock);
633
Chien-Yu Chene02e9322016-04-11 16:59:33 -0700634 for (auto b : mOutstandingBuffers) {
635 if (b == *buffer.buffer) {
636 return true;
637 }
638 }
639 return false;
640}
641
642void Camera3Stream::removeOutstandingBuffer(const camera3_stream_buffer &buffer) {
643 if (buffer.buffer == nullptr) {
644 return;
645 }
646
Emilian Peev889234d2017-07-18 18:21:26 -0700647 Mutex::Autolock l(mOutstandingBuffersLock);
648
Chien-Yu Chene02e9322016-04-11 16:59:33 -0700649 for (auto b = mOutstandingBuffers.begin(); b != mOutstandingBuffers.end(); b++) {
650 if (*b == *buffer.buffer) {
651 mOutstandingBuffers.erase(b);
652 return;
653 }
654 }
655}
656
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800657status_t Camera3Stream::returnBuffer(const camera3_stream_buffer &buffer,
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -0700658 nsecs_t timestamp, bool timestampIncreasing,
Emilian Peev538c90e2018-12-17 18:03:19 +0000659 const std::vector<size_t>& surface_ids, uint64_t frameNumber) {
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800660 ATRACE_CALL();
661 Mutex::Autolock l(mLock);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700662
Chien-Yu Chene02e9322016-04-11 16:59:33 -0700663 // Check if this buffer is outstanding.
664 if (!isOutstandingBuffer(buffer)) {
665 ALOGE("%s: Stream %d: Returning an unknown buffer.", __FUNCTION__, mId);
666 return BAD_VALUE;
667 }
668
Shuzhen Wang1c484a62017-07-14 15:14:19 -0700669 removeOutstandingBuffer(buffer);
670
Shuzhen Wangf0c4a6b2018-09-05 09:36:14 -0700671 // Buffer status may be changed, so make a copy of the stream_buffer struct.
672 camera3_stream_buffer b = buffer;
Shuzhen Wang26abaf42018-08-28 15:41:20 -0700673 if (timestampIncreasing && timestamp != 0 && timestamp <= mLastTimestamp) {
Shuzhen Wangf0c4a6b2018-09-05 09:36:14 -0700674 ALOGE("%s: Stream %d: timestamp %" PRId64 " is not increasing. Prev timestamp %" PRId64,
Shuzhen Wang26abaf42018-08-28 15:41:20 -0700675 __FUNCTION__, mId, timestamp, mLastTimestamp);
Shuzhen Wangf0c4a6b2018-09-05 09:36:14 -0700676 b.status = CAMERA3_BUFFER_STATUS_ERROR;
Shuzhen Wang26abaf42018-08-28 15:41:20 -0700677 }
678 mLastTimestamp = timestamp;
679
Igor Murashkin13d315e2014-04-03 18:09:04 -0700680 /**
681 * TODO: Check that the state is valid first.
682 *
683 * <HAL3.2 IN_CONFIG and IN_RECONFIG in addition to CONFIGURED.
684 * >= HAL3.2 CONFIGURED only
685 *
686 * Do this for getBuffer as well.
687 */
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -0700688 status_t res = returnBufferLocked(b, timestamp, surface_ids);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700689 if (res == OK) {
Emilian Peev538c90e2018-12-17 18:03:19 +0000690 fireBufferListenersLocked(b, /*acquired*/false, /*output*/true, timestamp, frameNumber);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700691 }
692
Chien-Yu Chenb83c1fe2015-09-10 16:15:21 -0700693 // Even if returning the buffer failed, we still want to signal whoever is waiting for the
694 // buffer to be returned.
695 mOutputBufferReturnedSignal.signal();
696
Igor Murashkin2fba5842013-04-22 14:03:54 -0700697 return res;
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800698}
699
Eino-Ville Talvalaba435252017-06-21 16:07:25 -0700700status_t Camera3Stream::getInputBuffer(camera3_stream_buffer *buffer, bool respectHalLimit) {
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700701 ATRACE_CALL();
702 Mutex::Autolock l(mLock);
Zhijun He6adc9cc2014-04-15 14:09:55 -0700703 status_t res = OK;
Igor Murashkin2fba5842013-04-22 14:03:54 -0700704
Zhijun He6adc9cc2014-04-15 14:09:55 -0700705 // This function should be only called when the stream is configured already.
706 if (mState != STATE_CONFIGURED) {
707 ALOGE("%s: Stream %d: Can't get input buffers if stream is not in CONFIGURED state %d",
708 __FUNCTION__, mId, mState);
709 return INVALID_OPERATION;
710 }
711
712 // Wait for new buffer returned back if we are running into the limit.
Eino-Ville Talvalaba435252017-06-21 16:07:25 -0700713 if (getHandoutInputBufferCountLocked() == camera3_stream::max_buffers && respectHalLimit) {
Zhijun He6adc9cc2014-04-15 14:09:55 -0700714 ALOGV("%s: Already dequeued max input buffers (%d), wait for next returned one.",
715 __FUNCTION__, camera3_stream::max_buffers);
716 res = mInputBufferReturnedSignal.waitRelative(mLock, kWaitForBufferDuration);
717 if (res != OK) {
718 if (res == TIMED_OUT) {
719 ALOGE("%s: wait for input buffer return timed out after %lldms", __FUNCTION__,
720 kWaitForBufferDuration / 1000000LL);
721 }
722 return res;
723 }
724 }
725
726 res = getInputBufferLocked(buffer);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700727 if (res == OK) {
728 fireBufferListenersLocked(*buffer, /*acquired*/true, /*output*/false);
Chien-Yu Chene02e9322016-04-11 16:59:33 -0700729 if (buffer->buffer) {
Emilian Peev889234d2017-07-18 18:21:26 -0700730 Mutex::Autolock l(mOutstandingBuffersLock);
Chien-Yu Chene02e9322016-04-11 16:59:33 -0700731 mOutstandingBuffers.push_back(*buffer->buffer);
732 }
Igor Murashkin2fba5842013-04-22 14:03:54 -0700733 }
734
735 return res;
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700736}
737
738status_t Camera3Stream::returnInputBuffer(const camera3_stream_buffer &buffer) {
739 ATRACE_CALL();
740 Mutex::Autolock l(mLock);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700741
Chien-Yu Chene02e9322016-04-11 16:59:33 -0700742 // Check if this buffer is outstanding.
743 if (!isOutstandingBuffer(buffer)) {
744 ALOGE("%s: Stream %d: Returning an unknown buffer.", __FUNCTION__, mId);
745 return BAD_VALUE;
746 }
747
Shuzhen Wang1c484a62017-07-14 15:14:19 -0700748 removeOutstandingBuffer(buffer);
749
Igor Murashkin2fba5842013-04-22 14:03:54 -0700750 status_t res = returnInputBufferLocked(buffer);
751 if (res == OK) {
752 fireBufferListenersLocked(buffer, /*acquired*/false, /*output*/false);
Zhijun He6adc9cc2014-04-15 14:09:55 -0700753 mInputBufferReturnedSignal.signal();
Igor Murashkin2fba5842013-04-22 14:03:54 -0700754 }
Chien-Yu Chene02e9322016-04-11 16:59:33 -0700755
Igor Murashkin2fba5842013-04-22 14:03:54 -0700756 return res;
757}
758
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700759status_t Camera3Stream::getInputBufferProducer(sp<IGraphicBufferProducer> *producer) {
760 ATRACE_CALL();
761 Mutex::Autolock l(mLock);
762
763 return getInputBufferProducerLocked(producer);
764}
765
Emilian Peev538c90e2018-12-17 18:03:19 +0000766void Camera3Stream::fireBufferRequestForFrameNumber(uint64_t frameNumber) {
767 ATRACE_CALL();
768 Mutex::Autolock l(mLock);
769
770 for (auto &it : mBufferListenerList) {
771 sp<Camera3StreamBufferListener> listener = it.promote();
772 if (listener.get() != nullptr) {
773 listener->onBufferRequestForFrameNumber(frameNumber, getId());
774 }
775 }
776}
777
Igor Murashkin2fba5842013-04-22 14:03:54 -0700778void Camera3Stream::fireBufferListenersLocked(
Emilian Peev538c90e2018-12-17 18:03:19 +0000779 const camera3_stream_buffer& buffer, bool acquired, bool output, nsecs_t timestamp,
780 uint64_t frameNumber) {
Igor Murashkin2fba5842013-04-22 14:03:54 -0700781 List<wp<Camera3StreamBufferListener> >::iterator it, end;
782
783 // TODO: finish implementing
784
785 Camera3StreamBufferListener::BufferInfo info =
786 Camera3StreamBufferListener::BufferInfo();
787 info.mOutput = output;
Shuzhen Wangb0fdc1e2016-03-20 23:21:39 -0700788 info.mError = (buffer.status == CAMERA3_BUFFER_STATUS_ERROR);
Emilian Peev538c90e2018-12-17 18:03:19 +0000789 info.mFrameNumber = frameNumber;
790 info.mTimestamp = timestamp;
Igor Murashkin2fba5842013-04-22 14:03:54 -0700791 // TODO: rest of fields
792
793 for (it = mBufferListenerList.begin(), end = mBufferListenerList.end();
794 it != end;
795 ++it) {
796
797 sp<Camera3StreamBufferListener> listener = it->promote();
798 if (listener != 0) {
799 if (acquired) {
800 listener->onBufferAcquired(info);
801 } else {
802 listener->onBufferReleased(info);
803 }
804 }
805 }
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700806}
807
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800808bool Camera3Stream::hasOutstandingBuffers() const {
809 ATRACE_CALL();
810 Mutex::Autolock l(mLock);
811 return hasOutstandingBuffersLocked();
812}
813
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -0700814size_t Camera3Stream::getOutstandingBuffersCount() const {
815 ATRACE_CALL();
816 Mutex::Autolock l(mLock);
817 return getHandoutOutputBufferCountLocked();
818}
819
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700820status_t Camera3Stream::setStatusTracker(sp<StatusTracker> statusTracker) {
821 Mutex::Autolock l(mLock);
822 sp<StatusTracker> oldTracker = mStatusTracker.promote();
823 if (oldTracker != 0 && mStatusId != StatusTracker::NO_STATUS_ID) {
824 oldTracker->removeComponent(mStatusId);
825 }
826 mStatusId = StatusTracker::NO_STATUS_ID;
827 mStatusTracker = statusTracker;
828
829 return OK;
830}
831
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800832status_t Camera3Stream::disconnect() {
833 ATRACE_CALL();
834 Mutex::Autolock l(mLock);
Igor Murashkine2172be2013-05-28 15:31:39 -0700835 ALOGV("%s: Stream %d: Disconnecting...", __FUNCTION__, mId);
836 status_t res = disconnectLocked();
837
Shuzhen Wang686f6442017-06-20 16:16:04 -0700838 mBufferLimitLatency.log("Stream %d latency histogram for wait on max_buffers", mId);
839 mBufferLimitLatency.reset();
840
Igor Murashkine2172be2013-05-28 15:31:39 -0700841 if (res == -ENOTCONN) {
842 // "Already disconnected" -- not an error
843 return OK;
844 } else {
845 return res;
846 }
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800847}
848
Shuzhen Wang686f6442017-06-20 16:16:04 -0700849void Camera3Stream::dump(int fd, const Vector<String16> &args) const
850{
851 (void)args;
852 mBufferLimitLatency.dump(fd,
853 " Latency histogram for wait on max_buffers");
854}
855
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800856status_t Camera3Stream::getBufferLocked(camera3_stream_buffer *,
857 const std::vector<size_t>&) {
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700858 ALOGE("%s: This type of stream does not support output", __FUNCTION__);
859 return INVALID_OPERATION;
860}
861status_t Camera3Stream::returnBufferLocked(const camera3_stream_buffer &,
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -0700862 nsecs_t, const std::vector<size_t>&) {
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700863 ALOGE("%s: This type of stream does not support output", __FUNCTION__);
864 return INVALID_OPERATION;
865}
866status_t Camera3Stream::getInputBufferLocked(camera3_stream_buffer *) {
867 ALOGE("%s: This type of stream does not support input", __FUNCTION__);
868 return INVALID_OPERATION;
869}
870status_t Camera3Stream::returnInputBufferLocked(
871 const camera3_stream_buffer &) {
872 ALOGE("%s: This type of stream does not support input", __FUNCTION__);
873 return INVALID_OPERATION;
874}
Eino-Ville Talvala02bf0322016-02-18 12:41:10 -0800875status_t Camera3Stream::getInputBufferProducerLocked(sp<IGraphicBufferProducer>*) {
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700876 ALOGE("%s: This type of stream does not support input", __FUNCTION__);
877 return INVALID_OPERATION;
878}
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700879
Igor Murashkin2fba5842013-04-22 14:03:54 -0700880void Camera3Stream::addBufferListener(
881 wp<Camera3StreamBufferListener> listener) {
882 Mutex::Autolock l(mLock);
Zhijun Hef0d962a2014-06-30 10:24:11 -0700883
884 List<wp<Camera3StreamBufferListener> >::iterator it, end;
885 for (it = mBufferListenerList.begin(), end = mBufferListenerList.end();
886 it != end;
887 ) {
888 if (*it == listener) {
889 ALOGE("%s: Try to add the same listener twice, ignoring...", __FUNCTION__);
890 return;
891 }
892 it++;
893 }
894
Igor Murashkin2fba5842013-04-22 14:03:54 -0700895 mBufferListenerList.push_back(listener);
896}
897
898void Camera3Stream::removeBufferListener(
899 const sp<Camera3StreamBufferListener>& listener) {
900 Mutex::Autolock l(mLock);
901
902 bool erased = true;
903 List<wp<Camera3StreamBufferListener> >::iterator it, end;
904 for (it = mBufferListenerList.begin(), end = mBufferListenerList.end();
905 it != end;
906 ) {
907
908 if (*it == listener) {
909 it = mBufferListenerList.erase(it);
910 erased = true;
911 } else {
912 ++it;
913 }
914 }
915
916 if (!erased) {
917 ALOGW("%s: Could not find listener to remove, already removed",
918 __FUNCTION__);
919 }
920}
921
Yin-Chia Yehbe83fa72017-03-30 13:35:36 -0700922void Camera3Stream::setBufferFreedListener(
Yin-Chia Yehdb1e8642017-07-14 15:19:30 -0700923 wp<Camera3StreamBufferFreedListener> listener) {
Yin-Chia Yehbe83fa72017-03-30 13:35:36 -0700924 Mutex::Autolock l(mLock);
925 // Only allow set listener during stream configuration because stream is guaranteed to be IDLE
926 // at this state, so setBufferFreedListener won't collide with onBufferFreed callbacks
927 if (mState != STATE_IN_CONFIG && mState != STATE_IN_RECONFIG) {
928 ALOGE("%s: listener must be set during stream configuration!",__FUNCTION__);
929 return;
930 }
931 mBufferFreedListener = listener;
932}
933
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800934}; // namespace camera3
935
936}; // namespace android