blob: f97111622f4a93aedd621388f280a4eb40f58393 [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-OutputStream"
18#define ATRACE_TAG ATRACE_TAG_CAMERA
19//#define LOG_NDEBUG 0
20
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -080021#include <utils/Log.h>
22#include <utils/Trace.h>
23#include "Camera3OutputStream.h"
24
25#ifndef container_of
26#define container_of(ptr, type, member) \
27 (type *)((char*)(ptr) - offsetof(type, member))
28#endif
29
30namespace android {
31
32namespace camera3 {
33
34Camera3OutputStream::Camera3OutputStream(int id,
Eino-Ville Talvala727d1722015-06-09 13:44:19 -070035 sp<Surface> consumer,
Eino-Ville Talvala3d82c0d2015-02-23 15:19:19 -080036 uint32_t width, uint32_t height, int format,
Shuzhen Wangc28dccc2016-02-11 23:48:46 -080037 android_dataspace dataSpace, camera3_stream_rotation_t rotation,
38 nsecs_t timestampOffset, int setId) :
Igor Murashkine3a9f962013-05-08 18:03:15 -070039 Camera3IOStreamBase(id, CAMERA3_STREAM_OUTPUT, width, height,
Zhijun He125684a2015-12-26 15:07:30 -080040 /*maxSize*/0, format, dataSpace, rotation, setId),
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -080041 mConsumer(consumer),
Ruchit Sharmae0711f22014-08-18 13:48:24 -040042 mTransform(0),
Zhijun He125684a2015-12-26 15:07:30 -080043 mTraceFirstBuffer(true),
Shuzhen Wangc28dccc2016-02-11 23:48:46 -080044 mUseBufferManager(false),
Zhijun He5d677d12016-05-29 16:52:39 -070045 mTimestampOffset(timestampOffset),
46 mConsumerUsage(0) {
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -080047
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -080048 if (mConsumer == NULL) {
49 ALOGE("%s: Consumer is NULL!", __FUNCTION__);
50 mState = STATE_ERROR;
51 }
Zhijun He125684a2015-12-26 15:07:30 -080052
53 if (setId > CAMERA3_STREAM_SET_ID_INVALID) {
54 mBufferReleasedListener = new BufferReleasedListener(this);
55 }
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -080056}
57
58Camera3OutputStream::Camera3OutputStream(int id,
Eino-Ville Talvala727d1722015-06-09 13:44:19 -070059 sp<Surface> consumer,
Eino-Ville Talvala3d82c0d2015-02-23 15:19:19 -080060 uint32_t width, uint32_t height, size_t maxSize, int format,
Shuzhen Wangc28dccc2016-02-11 23:48:46 -080061 android_dataspace dataSpace, camera3_stream_rotation_t rotation,
62 nsecs_t timestampOffset, int setId) :
Igor Murashkine3a9f962013-05-08 18:03:15 -070063 Camera3IOStreamBase(id, CAMERA3_STREAM_OUTPUT, width, height, maxSize,
Zhijun He125684a2015-12-26 15:07:30 -080064 format, dataSpace, rotation, setId),
Igor Murashkina55b5452013-04-02 16:36:33 -070065 mConsumer(consumer),
Ruchit Sharmae0711f22014-08-18 13:48:24 -040066 mTransform(0),
Zhijun He125684a2015-12-26 15:07:30 -080067 mTraceFirstBuffer(true),
Shuzhen Wangc28dccc2016-02-11 23:48:46 -080068 mUseMonoTimestamp(false),
69 mUseBufferManager(false),
Zhijun He5d677d12016-05-29 16:52:39 -070070 mTimestampOffset(timestampOffset),
71 mConsumerUsage(0) {
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -080072
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -080073 if (format != HAL_PIXEL_FORMAT_BLOB && format != HAL_PIXEL_FORMAT_RAW_OPAQUE) {
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -080074 ALOGE("%s: Bad format for size-only stream: %d", __FUNCTION__,
75 format);
76 mState = STATE_ERROR;
77 }
78
79 if (mConsumer == NULL) {
80 ALOGE("%s: Consumer is NULL!", __FUNCTION__);
81 mState = STATE_ERROR;
82 }
Zhijun He125684a2015-12-26 15:07:30 -080083
84 if (setId > CAMERA3_STREAM_SET_ID_INVALID) {
85 mBufferReleasedListener = new BufferReleasedListener(this);
86 }
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -080087}
88
Zhijun He5d677d12016-05-29 16:52:39 -070089Camera3OutputStream::Camera3OutputStream(int id,
90 uint32_t width, uint32_t height, int format,
91 uint32_t consumerUsage, android_dataspace dataSpace,
92 camera3_stream_rotation_t rotation, nsecs_t timestampOffset, int setId) :
93 Camera3IOStreamBase(id, CAMERA3_STREAM_OUTPUT, width, height,
94 /*maxSize*/0, format, dataSpace, rotation, setId),
95 mConsumer(nullptr),
96 mTransform(0),
97 mTraceFirstBuffer(true),
98 mUseBufferManager(false),
99 mTimestampOffset(timestampOffset),
100 mConsumerUsage(consumerUsage) {
101 // Deferred consumer only support preview surface format now.
102 if (format != HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED) {
103 ALOGE("%s: Deferred consumer only supports IMPLEMENTATION_DEFINED format now!",
104 __FUNCTION__);
105 mState = STATE_ERROR;
106 }
107
108 // Sanity check for the consumer usage flag.
109 if ((consumerUsage & GraphicBuffer::USAGE_HW_TEXTURE) == 0 &&
110 (consumerUsage & GraphicBuffer::USAGE_HW_COMPOSER) == 0) {
111 ALOGE("%s: Deferred consumer usage flag is illegal (0x%x)!", __FUNCTION__, consumerUsage);
112 mState = STATE_ERROR;
113 }
114
115 mConsumerName = String8("Deferred");
116 if (setId > CAMERA3_STREAM_SET_ID_INVALID) {
117 mBufferReleasedListener = new BufferReleasedListener(this);
118 }
119
120}
121
Igor Murashkine3a9f962013-05-08 18:03:15 -0700122Camera3OutputStream::Camera3OutputStream(int id, camera3_stream_type_t type,
123 uint32_t width, uint32_t height,
Eino-Ville Talvala3d82c0d2015-02-23 15:19:19 -0800124 int format,
Yin-Chia Yehb97babb2015-03-12 13:42:44 -0700125 android_dataspace dataSpace,
Zhijun He125684a2015-12-26 15:07:30 -0800126 camera3_stream_rotation_t rotation,
Shuzhen Wang0129d522016-10-30 22:43:41 -0700127 uint32_t consumerUsage, nsecs_t timestampOffset,
Zhijun He125684a2015-12-26 15:07:30 -0800128 int setId) :
Igor Murashkine3a9f962013-05-08 18:03:15 -0700129 Camera3IOStreamBase(id, type, width, height,
130 /*maxSize*/0,
Zhijun He125684a2015-12-26 15:07:30 -0800131 format, dataSpace, rotation, setId),
132 mTransform(0),
133 mTraceFirstBuffer(true),
Shuzhen Wangc28dccc2016-02-11 23:48:46 -0800134 mUseMonoTimestamp(false),
Zhijun He5d677d12016-05-29 16:52:39 -0700135 mUseBufferManager(false),
Shuzhen Wang0129d522016-10-30 22:43:41 -0700136 mTimestampOffset(timestampOffset),
137 mConsumerUsage(consumerUsage) {
Zhijun He125684a2015-12-26 15:07:30 -0800138
139 if (setId > CAMERA3_STREAM_SET_ID_INVALID) {
140 mBufferReleasedListener = new BufferReleasedListener(this);
141 }
Igor Murashkine3a9f962013-05-08 18:03:15 -0700142
143 // Subclasses expected to initialize mConsumer themselves
144}
145
146
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800147Camera3OutputStream::~Camera3OutputStream() {
148 disconnectLocked();
149}
150
151status_t Camera3OutputStream::getBufferLocked(camera3_stream_buffer *buffer) {
152 ATRACE_CALL();
153 status_t res;
154
Igor Murashkine3a9f962013-05-08 18:03:15 -0700155 if ((res = getBufferPreconditionCheckLocked()) != OK) {
156 return res;
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800157 }
158
159 ANativeWindowBuffer* anb;
Zhijun He125684a2015-12-26 15:07:30 -0800160 int fenceFd = -1;
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -0700161 bool gotBufferFromManager = false;
162
Zhijun He125684a2015-12-26 15:07:30 -0800163 if (mUseBufferManager) {
164 sp<GraphicBuffer> gb;
165 res = mBufferManager->getBufferForStream(getId(), getStreamSetId(), &gb, &fenceFd);
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -0700166 if (res == OK) {
167 // Attach this buffer to the bufferQueue: the buffer will be in dequeue state after a
168 // successful return.
169 anb = gb.get();
170 res = mConsumer->attachBuffer(anb);
171 if (res != OK) {
172 ALOGE("%s: Stream %d: Can't attach the output buffer to this surface: %s (%d)",
173 __FUNCTION__, mId, strerror(-res), res);
174 return res;
175 }
176 gotBufferFromManager = true;
177 ALOGV("Stream %d: Attached new buffer", getId());
178 } else if (res == ALREADY_EXISTS) {
179 // Have sufficient free buffers already attached, can just
180 // dequeue from buffer queue
181 ALOGV("Stream %d: Reusing attached buffer", getId());
182 gotBufferFromManager = false;
183 } else if (res != OK) {
Zhijun He125684a2015-12-26 15:07:30 -0800184 ALOGE("%s: Stream %d: Can't get next output buffer from buffer manager: %s (%d)",
185 __FUNCTION__, mId, strerror(-res), res);
186 return res;
187 }
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -0700188 }
189 if (!gotBufferFromManager) {
Zhijun He125684a2015-12-26 15:07:30 -0800190 /**
191 * Release the lock briefly to avoid deadlock for below scenario:
192 * Thread 1: StreamingProcessor::startStream -> Camera3Stream::isConfiguring().
193 * This thread acquired StreamingProcessor lock and try to lock Camera3Stream lock.
194 * Thread 2: Camera3Stream::returnBuffer->StreamingProcessor::onFrameAvailable().
195 * This thread acquired Camera3Stream lock and bufferQueue lock, and try to lock
196 * StreamingProcessor lock.
197 * Thread 3: Camera3Stream::getBuffer(). This thread acquired Camera3Stream lock
198 * and try to lock bufferQueue lock.
199 * Then there is circular locking dependency.
200 */
201 sp<ANativeWindow> currentConsumer = mConsumer;
202 mLock.unlock();
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800203
Zhijun He125684a2015-12-26 15:07:30 -0800204 res = currentConsumer->dequeueBuffer(currentConsumer.get(), &anb, &fenceFd);
205 mLock.lock();
206 if (res != OK) {
207 ALOGE("%s: Stream %d: Can't dequeue next output buffer: %s (%d)",
208 __FUNCTION__, mId, strerror(-res), res);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -0700209
210 // Only transition to STATE_ABANDONED from STATE_CONFIGURED. (If it is STATE_PREPARING,
211 // let prepareNextBuffer handle the error.)
212 if (res == NO_INIT && mState == STATE_CONFIGURED) {
213 mState = STATE_ABANDONED;
214 }
215
Zhijun He125684a2015-12-26 15:07:30 -0800216 return res;
217 }
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800218 }
219
Igor Murashkine3a9f962013-05-08 18:03:15 -0700220 /**
221 * FenceFD now owned by HAL except in case of error,
222 * in which case we reassign it to acquire_fence
223 */
224 handoutBufferLocked(*buffer, &(anb->handle), /*acquireFence*/fenceFd,
Zhijun He6adc9cc2014-04-15 14:09:55 -0700225 /*releaseFence*/-1, CAMERA3_BUFFER_STATUS_OK, /*output*/true);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800226
227 return OK;
228}
229
230status_t Camera3OutputStream::returnBufferLocked(
231 const camera3_stream_buffer &buffer,
232 nsecs_t timestamp) {
233 ATRACE_CALL();
Igor Murashkine3a9f962013-05-08 18:03:15 -0700234
235 status_t res = returnAnyBufferLocked(buffer, timestamp, /*output*/true);
236
237 if (res != OK) {
238 return res;
239 }
240
241 mLastTimestamp = timestamp;
Eino-Ville Talvalac31dc7e2017-01-31 17:35:41 -0800242 mFrameCount++;
Igor Murashkine3a9f962013-05-08 18:03:15 -0700243
244 return OK;
245}
246
247status_t Camera3OutputStream::returnBufferCheckedLocked(
248 const camera3_stream_buffer &buffer,
249 nsecs_t timestamp,
250 bool output,
251 /*out*/
252 sp<Fence> *releaseFenceOut) {
253
254 (void)output;
255 ALOG_ASSERT(output, "Expected output to be true");
256
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800257 status_t res;
Igor Murashkin5a1798a2013-05-07 10:58:13 -0700258
Yin-Chia Yeh4c9736f2015-03-05 15:01:36 -0800259 // Fence management - always honor release fence from HAL
260 sp<Fence> releaseFence = new Fence(buffer.release_fence);
Igor Murashkin5a1798a2013-05-07 10:58:13 -0700261 int anwReleaseFence = releaseFence->dup();
262
263 /**
Zhijun He124ccf42013-05-22 14:01:30 -0700264 * Release the lock briefly to avoid deadlock with
265 * StreamingProcessor::startStream -> Camera3Stream::isConfiguring (this
266 * thread will go into StreamingProcessor::onFrameAvailable) during
267 * queueBuffer
268 */
269 sp<ANativeWindow> currentConsumer = mConsumer;
270 mLock.unlock();
271
272 /**
Igor Murashkin5a1798a2013-05-07 10:58:13 -0700273 * Return buffer back to ANativeWindow
274 */
275 if (buffer.status == CAMERA3_BUFFER_STATUS_ERROR) {
276 // Cancel buffer
Zhijun He7b171a92016-06-24 16:05:52 -0700277 ALOGW("A frame is dropped for stream %d", mId);
Zhijun He124ccf42013-05-22 14:01:30 -0700278 res = currentConsumer->cancelBuffer(currentConsumer.get(),
Igor Murashkin5a1798a2013-05-07 10:58:13 -0700279 container_of(buffer.buffer, ANativeWindowBuffer, handle),
280 anwReleaseFence);
281 if (res != OK) {
282 ALOGE("%s: Stream %d: Error cancelling buffer to native window:"
Igor Murashkine3a9f962013-05-08 18:03:15 -0700283 " %s (%d)", __FUNCTION__, mId, strerror(-res), res);
Igor Murashkin5a1798a2013-05-07 10:58:13 -0700284 }
Zhijun He1ff811b2016-01-26 14:39:51 -0800285
286 if (mUseBufferManager) {
287 // Return this buffer back to buffer manager.
288 mBufferReleasedListener->onBufferReleased();
289 }
Igor Murashkin5a1798a2013-05-07 10:58:13 -0700290 } else {
Ruchit Sharmae0711f22014-08-18 13:48:24 -0400291 if (mTraceFirstBuffer && (stream_type == CAMERA3_STREAM_OUTPUT)) {
292 {
293 char traceLog[48];
294 snprintf(traceLog, sizeof(traceLog), "Stream %d: first full buffer\n", mId);
295 ATRACE_NAME(traceLog);
296 }
297 mTraceFirstBuffer = false;
298 }
299
Shuzhen Wang13a69632016-01-26 09:51:07 -0800300 /* Certain consumers (such as AudioSource or HardwareComposer) use
301 * MONOTONIC time, causing time misalignment if camera timestamp is
Shuzhen Wangc28dccc2016-02-11 23:48:46 -0800302 * in BOOTTIME. Do the conversion if necessary. */
303 res = native_window_set_buffers_timestamp(mConsumer.get(),
304 mUseMonoTimestamp ? timestamp - mTimestampOffset : timestamp);
305 if (res != OK) {
306 ALOGE("%s: Stream %d: Error setting timestamp: %s (%d)",
307 __FUNCTION__, mId, strerror(-res), res);
308 return res;
Yin-Chia Yeh4c9736f2015-03-05 15:01:36 -0800309 }
310
Zhijun He124ccf42013-05-22 14:01:30 -0700311 res = currentConsumer->queueBuffer(currentConsumer.get(),
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800312 container_of(buffer.buffer, ANativeWindowBuffer, handle),
313 anwReleaseFence);
314 if (res != OK) {
Igor Murashkine3a9f962013-05-08 18:03:15 -0700315 ALOGE("%s: Stream %d: Error queueing buffer to native window: "
316 "%s (%d)", __FUNCTION__, mId, strerror(-res), res);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800317 }
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800318 }
Zhijun He124ccf42013-05-22 14:01:30 -0700319 mLock.lock();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700320
321 // Once a valid buffer has been returned to the queue, can no longer
322 // dequeue all buffers for preallocation.
323 if (buffer.status != CAMERA3_BUFFER_STATUS_ERROR) {
324 mStreamUnpreparable = true;
325 }
326
Igor Murashkin5a1798a2013-05-07 10:58:13 -0700327 if (res != OK) {
328 close(anwReleaseFence);
Igor Murashkin5a1798a2013-05-07 10:58:13 -0700329 }
330
Igor Murashkine3a9f962013-05-08 18:03:15 -0700331 *releaseFenceOut = releaseFence;
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800332
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700333 return res;
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800334}
335
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800336void Camera3OutputStream::dump(int fd, const Vector<String16> &args) const {
337 (void) args;
338 String8 lines;
339 lines.appendFormat(" Stream[%d]: Output\n", mId);
Eino-Ville Talvala727d1722015-06-09 13:44:19 -0700340 lines.appendFormat(" Consumer name: %s\n", mConsumerName.string());
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800341 write(fd, lines.string(), lines.size());
Igor Murashkine3a9f962013-05-08 18:03:15 -0700342
343 Camera3IOStreamBase::dump(fd, args);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800344}
345
346status_t Camera3OutputStream::setTransform(int transform) {
347 ATRACE_CALL();
348 Mutex::Autolock l(mLock);
349 return setTransformLocked(transform);
350}
351
352status_t Camera3OutputStream::setTransformLocked(int transform) {
353 status_t res = OK;
354 if (mState == STATE_ERROR) {
355 ALOGE("%s: Stream in error state", __FUNCTION__);
356 return INVALID_OPERATION;
357 }
358
359 mTransform = transform;
360 if (mState == STATE_CONFIGURED) {
361 res = native_window_set_buffers_transform(mConsumer.get(),
362 transform);
363 if (res != OK) {
364 ALOGE("%s: Unable to configure stream transform to %x: %s (%d)",
365 __FUNCTION__, transform, strerror(-res), res);
366 }
367 }
368 return res;
369}
370
371status_t Camera3OutputStream::configureQueueLocked() {
372 status_t res;
373
Ruchit Sharmae0711f22014-08-18 13:48:24 -0400374 mTraceFirstBuffer = true;
Igor Murashkine3a9f962013-05-08 18:03:15 -0700375 if ((res = Camera3IOStreamBase::configureQueueLocked()) != OK) {
376 return res;
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800377 }
378
Shuzhen Wang0129d522016-10-30 22:43:41 -0700379 if ((res = configureConsumerQueueLocked()) != OK) {
380 return res;
381 }
382
383 // Set dequeueBuffer/attachBuffer timeout if the consumer is not hw composer or hw texture.
384 // We need skip these cases as timeout will disable the non-blocking (async) mode.
385 if (!(isConsumedByHWComposer() || isConsumedByHWTexture())) {
386 mConsumer->setDequeueTimeout(kDequeueBufferTimeout);
387 }
388
389 return OK;
390}
391
392status_t Camera3OutputStream::configureConsumerQueueLocked() {
393 status_t res;
394
395 mTraceFirstBuffer = true;
396
Igor Murashkine3a9f962013-05-08 18:03:15 -0700397 ALOG_ASSERT(mConsumer != 0, "mConsumer should never be NULL");
398
Zhijun He125684a2015-12-26 15:07:30 -0800399 // Configure consumer-side ANativeWindow interface. The listener may be used
400 // to notify buffer manager (if it is used) of the returned buffers.
401 res = mConsumer->connect(NATIVE_WINDOW_API_CAMERA, /*listener*/mBufferReleasedListener);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800402 if (res != OK) {
403 ALOGE("%s: Unable to connect to native window for stream %d",
404 __FUNCTION__, mId);
405 return res;
406 }
407
Eino-Ville Talvala727d1722015-06-09 13:44:19 -0700408 mConsumerName = mConsumer->getConsumerName();
409
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800410 res = native_window_set_usage(mConsumer.get(), camera3_stream::usage);
411 if (res != OK) {
412 ALOGE("%s: Unable to configure usage %08x for stream %d",
413 __FUNCTION__, camera3_stream::usage, mId);
414 return res;
415 }
416
417 res = native_window_set_scaling_mode(mConsumer.get(),
418 NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
419 if (res != OK) {
420 ALOGE("%s: Unable to configure stream scaling: %s (%d)",
421 __FUNCTION__, strerror(-res), res);
422 return res;
423 }
424
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800425 if (mMaxSize == 0) {
426 // For buffers of known size
Eino-Ville Talvala7d70c5e2014-07-24 18:10:23 -0700427 res = native_window_set_buffers_dimensions(mConsumer.get(),
428 camera3_stream::width, camera3_stream::height);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800429 } else {
430 // For buffers with bounded size
Eino-Ville Talvala7d70c5e2014-07-24 18:10:23 -0700431 res = native_window_set_buffers_dimensions(mConsumer.get(),
432 mMaxSize, 1);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800433 }
434 if (res != OK) {
Eino-Ville Talvala7d70c5e2014-07-24 18:10:23 -0700435 ALOGE("%s: Unable to configure stream buffer dimensions"
436 " %d x %d (maxSize %zu) for stream %d",
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800437 __FUNCTION__, camera3_stream::width, camera3_stream::height,
Eino-Ville Talvala7d70c5e2014-07-24 18:10:23 -0700438 mMaxSize, mId);
439 return res;
440 }
441 res = native_window_set_buffers_format(mConsumer.get(),
442 camera3_stream::format);
443 if (res != OK) {
444 ALOGE("%s: Unable to configure stream buffer format %#x for stream %d",
445 __FUNCTION__, camera3_stream::format, mId);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800446 return res;
447 }
448
Eino-Ville Talvala3d82c0d2015-02-23 15:19:19 -0800449 res = native_window_set_buffers_data_space(mConsumer.get(),
450 camera3_stream::data_space);
451 if (res != OK) {
452 ALOGE("%s: Unable to configure stream dataspace %#x for stream %d",
453 __FUNCTION__, camera3_stream::data_space, mId);
454 return res;
455 }
456
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800457 int maxConsumerBuffers;
Eino-Ville Talvala727d1722015-06-09 13:44:19 -0700458 res = static_cast<ANativeWindow*>(mConsumer.get())->query(
459 mConsumer.get(),
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800460 NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS, &maxConsumerBuffers);
461 if (res != OK) {
462 ALOGE("%s: Unable to query consumer undequeued"
463 " buffer count for stream %d", __FUNCTION__, mId);
464 return res;
465 }
466
Alex Ray20cb3002013-05-28 20:18:22 -0700467 ALOGV("%s: Consumer wants %d buffers, HAL wants %d", __FUNCTION__,
468 maxConsumerBuffers, camera3_stream::max_buffers);
469 if (camera3_stream::max_buffers == 0) {
Zhijun He2ab500c2013-07-23 08:02:53 -0700470 ALOGE("%s: Camera HAL requested max_buffer count: %d, requires at least 1",
Alex Ray20cb3002013-05-28 20:18:22 -0700471 __FUNCTION__, camera3_stream::max_buffers);
472 return INVALID_OPERATION;
473 }
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800474
475 mTotalBufferCount = maxConsumerBuffers + camera3_stream::max_buffers;
Zhijun He6adc9cc2014-04-15 14:09:55 -0700476 mHandoutTotalBufferCount = 0;
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800477 mFrameCount = 0;
478 mLastTimestamp = 0;
Shuzhen Wangc28dccc2016-02-11 23:48:46 -0800479 mUseMonoTimestamp = (isConsumedByHWComposer() | isVideoStream());
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800480
481 res = native_window_set_buffer_count(mConsumer.get(),
482 mTotalBufferCount);
483 if (res != OK) {
484 ALOGE("%s: Unable to set buffer count for stream %d",
485 __FUNCTION__, mId);
486 return res;
487 }
488
489 res = native_window_set_buffers_transform(mConsumer.get(),
490 mTransform);
491 if (res != OK) {
492 ALOGE("%s: Unable to configure stream transform to %x: %s (%d)",
493 __FUNCTION__, mTransform, strerror(-res), res);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700494 return res;
Zhijun Hef0645c12016-08-02 00:58:11 -0700495 }
496
Zhijun He125684a2015-12-26 15:07:30 -0800497 /**
Zhijun Heedd41ae2016-02-03 14:45:53 -0800498 * Camera3 Buffer manager is only supported by HAL3.3 onwards, as the older HALs requires
Zhijun He125684a2015-12-26 15:07:30 -0800499 * buffers to be statically allocated for internal static buffer registration, while the
500 * buffers provided by buffer manager are really dynamically allocated. Camera3Device only
Zhijun Heedd41ae2016-02-03 14:45:53 -0800501 * sets the mBufferManager if device version is > HAL3.2, which guarantees that the buffer
502 * manager setup is skipped in below code. Note that HAL3.2 is also excluded here, as some
503 * HAL3.2 devices may not support the dynamic buffer registeration.
Zhijun He125684a2015-12-26 15:07:30 -0800504 */
505 if (mBufferManager != 0 && mSetId > CAMERA3_STREAM_SET_ID_INVALID) {
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -0700506 uint32_t consumerUsage = 0;
507 getEndpointUsage(&consumerUsage);
Zhijun He125684a2015-12-26 15:07:30 -0800508 StreamInfo streamInfo(
509 getId(), getStreamSetId(), getWidth(), getHeight(), getFormat(), getDataSpace(),
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -0700510 camera3_stream::usage | consumerUsage, mTotalBufferCount,
511 /*isConfigured*/true);
512 wp<Camera3OutputStream> weakThis(this);
513 res = mBufferManager->registerStream(weakThis,
514 streamInfo);
Zhijun He125684a2015-12-26 15:07:30 -0800515 if (res == OK) {
516 // Disable buffer allocation for this BufferQueue, buffer manager will take over
517 // the buffer allocation responsibility.
518 mConsumer->getIGraphicBufferProducer()->allowAllocation(false);
519 mUseBufferManager = true;
520 } else {
521 ALOGE("%s: Unable to register stream %d to camera3 buffer manager, "
522 "(error %d %s), fall back to BufferQueue for buffer management!",
523 __FUNCTION__, mId, res, strerror(-res));
524 }
525 }
526
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800527 return OK;
528}
529
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800530status_t Camera3OutputStream::disconnectLocked() {
531 status_t res;
532
Igor Murashkine3a9f962013-05-08 18:03:15 -0700533 if ((res = Camera3IOStreamBase::disconnectLocked()) != OK) {
534 return res;
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800535 }
536
Zhijun He5d677d12016-05-29 16:52:39 -0700537 // Stream configuration was not finished (can only be in STATE_IN_CONFIG or STATE_CONSTRUCTED
538 // state), don't need change the stream state, return OK.
539 if (mConsumer == nullptr) {
540 return OK;
541 }
542
Zhijun He125684a2015-12-26 15:07:30 -0800543 ALOGV("%s: disconnecting stream %d from native window", __FUNCTION__, getId());
544
Igor Murashkine3a9f962013-05-08 18:03:15 -0700545 res = native_window_api_disconnect(mConsumer.get(),
546 NATIVE_WINDOW_API_CAMERA);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800547 /**
548 * This is not an error. if client calling process dies, the window will
549 * also die and all calls to it will return DEAD_OBJECT, thus it's already
550 * "disconnected"
551 */
552 if (res == DEAD_OBJECT) {
553 ALOGW("%s: While disconnecting stream %d from native window, the"
554 " native window died from under us", __FUNCTION__, mId);
555 }
556 else if (res != OK) {
Igor Murashkine3a9f962013-05-08 18:03:15 -0700557 ALOGE("%s: Unable to disconnect stream %d from native window "
558 "(error %d %s)",
559 __FUNCTION__, mId, res, strerror(-res));
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800560 mState = STATE_ERROR;
561 return res;
562 }
563
Zhijun He125684a2015-12-26 15:07:30 -0800564 // Since device is already idle, there is no getBuffer call to buffer manager, unregister the
565 // stream at this point should be safe.
566 if (mUseBufferManager) {
567 res = mBufferManager->unregisterStream(getId(), getStreamSetId());
568 if (res != OK) {
569 ALOGE("%s: Unable to unregister stream %d from buffer manager "
570 "(error %d %s)", __FUNCTION__, mId, res, strerror(-res));
571 mState = STATE_ERROR;
572 return res;
573 }
574 // Note that, to make prepare/teardown case work, we must not mBufferManager.clear(), as
575 // the stream is still in usable state after this call.
576 mUseBufferManager = false;
577 }
578
Igor Murashkine3a9f962013-05-08 18:03:15 -0700579 mState = (mState == STATE_IN_RECONFIG) ? STATE_IN_CONFIG
580 : STATE_CONSTRUCTED;
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800581 return OK;
582}
583
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700584status_t Camera3OutputStream::getEndpointUsage(uint32_t *usage) const {
Eino-Ville Talvalab2f5b192013-07-30 14:36:03 -0700585
586 status_t res;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700587
Zhijun He5d677d12016-05-29 16:52:39 -0700588 if (mConsumer == nullptr) {
589 // mConsumerUsage was sanitized before the Camera3OutputStream was constructed.
590 *usage = mConsumerUsage;
591 return OK;
592 }
593
Shuzhen Wang0129d522016-10-30 22:43:41 -0700594 res = getEndpointUsageForSurface(usage, mConsumer);
595
596 return res;
597}
598
599status_t Camera3OutputStream::getEndpointUsageForSurface(uint32_t *usage,
600 const sp<Surface>& surface) const {
601 status_t res;
602 int32_t u = 0;
603
604 res = static_cast<ANativeWindow*>(surface.get())->query(surface.get(),
Eino-Ville Talvalab2f5b192013-07-30 14:36:03 -0700605 NATIVE_WINDOW_CONSUMER_USAGE_BITS, &u);
Eino-Ville Talvalab2f5b192013-07-30 14:36:03 -0700606
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700607 // If an opaque output stream's endpoint is ImageReader, add
608 // GRALLOC_USAGE_HW_CAMERA_ZSL to the usage so HAL knows it will be used
609 // for the ZSL use case.
610 // Assume it's for ImageReader if the consumer usage doesn't have any of these bits set:
611 // 1. GRALLOC_USAGE_HW_TEXTURE
612 // 2. GRALLOC_USAGE_HW_RENDER
613 // 3. GRALLOC_USAGE_HW_COMPOSER
614 // 4. GRALLOC_USAGE_HW_VIDEO_ENCODER
615 if (camera3_stream::format == HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED &&
Shuzhen Wang0129d522016-10-30 22:43:41 -0700616 (u & (GRALLOC_USAGE_HW_TEXTURE | GRALLOC_USAGE_HW_RENDER |
617 GRALLOC_USAGE_HW_COMPOSER | GRALLOC_USAGE_HW_VIDEO_ENCODER)) == 0) {
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700618 u |= GRALLOC_USAGE_HW_CAMERA_ZSL;
619 }
620
621 *usage = u;
Eino-Ville Talvalab2f5b192013-07-30 14:36:03 -0700622 return res;
623}
624
Chien-Yu Chen85a64552015-08-28 15:46:12 -0700625bool Camera3OutputStream::isVideoStream() const {
626 uint32_t usage = 0;
627 status_t res = getEndpointUsage(&usage);
628 if (res != OK) {
629 ALOGE("%s: getting end point usage failed: %s (%d).", __FUNCTION__, strerror(-res), res);
630 return false;
631 }
632
633 return (usage & GRALLOC_USAGE_HW_VIDEO_ENCODER) != 0;
634}
635
Zhijun He125684a2015-12-26 15:07:30 -0800636status_t Camera3OutputStream::setBufferManager(sp<Camera3BufferManager> bufferManager) {
637 Mutex::Autolock l(mLock);
638 if (mState != STATE_CONSTRUCTED) {
Zhijun He5d677d12016-05-29 16:52:39 -0700639 ALOGE("%s: this method can only be called when stream in CONSTRUCTED state.",
Zhijun He125684a2015-12-26 15:07:30 -0800640 __FUNCTION__);
641 return INVALID_OPERATION;
642 }
643 mBufferManager = bufferManager;
644
645 return OK;
646}
647
648void Camera3OutputStream::BufferReleasedListener::onBufferReleased() {
649 sp<Camera3OutputStream> stream = mParent.promote();
650 if (stream == nullptr) {
651 ALOGV("%s: Parent camera3 output stream was destroyed", __FUNCTION__);
652 return;
653 }
654
655 Mutex::Autolock l(stream->mLock);
656 if (!(stream->mUseBufferManager)) {
657 return;
658 }
659
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -0700660 ALOGV("Stream %d: Buffer released", stream->getId());
661 status_t res = stream->mBufferManager->onBufferReleased(
662 stream->getId(), stream->getStreamSetId());
663 if (res != OK) {
664 ALOGE("%s: signaling buffer release to buffer manager failed: %s (%d).", __FUNCTION__,
665 strerror(-res), res);
666 stream->mState = STATE_ERROR;
667 }
668}
669
670status_t Camera3OutputStream::detachBuffer(sp<GraphicBuffer>* buffer, int* fenceFd) {
671 Mutex::Autolock l(mLock);
672
673 ALOGV("Stream %d: detachBuffer", getId());
674 if (buffer == nullptr) {
675 return BAD_VALUE;
676 }
677
Zhijun He125684a2015-12-26 15:07:30 -0800678 sp<Fence> fence;
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -0700679 status_t res = mConsumer->detachNextBuffer(buffer, &fence);
Zhijun He125684a2015-12-26 15:07:30 -0800680 if (res == NO_MEMORY) {
681 // This may rarely happen, which indicates that the released buffer was freed by other
682 // call (e.g., attachBuffer, dequeueBuffer etc.) before reaching here. We should notify the
683 // buffer manager that this buffer has been freed. It's not fatal, but should be avoided,
684 // therefore log a warning.
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -0700685 *buffer = 0;
Zhijun He125684a2015-12-26 15:07:30 -0800686 ALOGW("%s: the released buffer has already been freed by the buffer queue!", __FUNCTION__);
687 } else if (res != OK) {
Eino-Ville Talvalaff51b472016-06-28 15:26:19 -0700688 // Treat other errors as abandonment
Zhijun He125684a2015-12-26 15:07:30 -0800689 ALOGE("%s: detach next buffer failed: %s (%d).", __FUNCTION__, strerror(-res), res);
Eino-Ville Talvalaff51b472016-06-28 15:26:19 -0700690 mState = STATE_ABANDONED;
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -0700691 return res;
Zhijun He125684a2015-12-26 15:07:30 -0800692 }
693
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -0700694 if (fenceFd != nullptr) {
695 if (fence!= 0 && fence->isValid()) {
696 *fenceFd = fence->dup();
697 } else {
698 *fenceFd = -1;
699 }
Zhijun He125684a2015-12-26 15:07:30 -0800700 }
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -0700701
702 return OK;
Zhijun He125684a2015-12-26 15:07:30 -0800703}
Shuzhen Wang13a69632016-01-26 09:51:07 -0800704
Shuzhen Wang0129d522016-10-30 22:43:41 -0700705status_t Camera3OutputStream::notifyRequestedSurfaces(uint32_t /*frame_number*/,
706 const std::vector<size_t>& /*surface_ids*/) {
707 return OK;
708}
709
710bool Camera3OutputStream::isConsumerConfigurationDeferred(size_t surface_id) const {
Zhijun He5d677d12016-05-29 16:52:39 -0700711 Mutex::Autolock l(mLock);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700712
713 if (surface_id != 0) {
Shuzhen Wang758c2152017-01-10 18:26:18 -0800714 ALOGE("%s: surface_id %zu for Camera3OutputStream should be 0!", __FUNCTION__, surface_id);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700715 }
Zhijun He5d677d12016-05-29 16:52:39 -0700716 return mConsumer == nullptr;
717}
718
Shuzhen Wang758c2152017-01-10 18:26:18 -0800719status_t Camera3OutputStream::setConsumers(const std::vector<sp<Surface>>& consumers) {
720 if (consumers.size() != 1) {
721 ALOGE("%s: it's illegal to set %zu consumer surfaces!",
722 __FUNCTION__, consumers.size());
723 return INVALID_OPERATION;
724 }
725 if (consumers[0] == nullptr) {
726 ALOGE("%s: it's illegal to set null consumer surface!", __FUNCTION__);
Zhijun He5d677d12016-05-29 16:52:39 -0700727 return INVALID_OPERATION;
728 }
729
730 if (mConsumer != nullptr) {
731 ALOGE("%s: consumer surface was already set!", __FUNCTION__);
732 return INVALID_OPERATION;
733 }
734
Shuzhen Wang758c2152017-01-10 18:26:18 -0800735 mConsumer = consumers[0];
Zhijun He5d677d12016-05-29 16:52:39 -0700736 return OK;
737}
738
Shuzhen Wang13a69632016-01-26 09:51:07 -0800739bool Camera3OutputStream::isConsumedByHWComposer() const {
740 uint32_t usage = 0;
741 status_t res = getEndpointUsage(&usage);
742 if (res != OK) {
743 ALOGE("%s: getting end point usage failed: %s (%d).", __FUNCTION__, strerror(-res), res);
744 return false;
745 }
746
747 return (usage & GRALLOC_USAGE_HW_COMPOSER) != 0;
748}
749
Zhijun Hef0645c12016-08-02 00:58:11 -0700750bool Camera3OutputStream::isConsumedByHWTexture() const {
751 uint32_t usage = 0;
752 status_t res = getEndpointUsage(&usage);
753 if (res != OK) {
754 ALOGE("%s: getting end point usage failed: %s (%d).", __FUNCTION__, strerror(-res), res);
755 return false;
756 }
757
758 return (usage & GRALLOC_USAGE_HW_TEXTURE) != 0;
759}
760
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800761}; // namespace camera3
762
763}; // namespace android