blob: 4e79029807e933c8fdaf1176506761e752fc0f97 [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,
Zhijun He125684a2015-12-26 15:07:30 -080037 android_dataspace dataSpace, camera3_stream_rotation_t rotation, int setId) :
Igor Murashkine3a9f962013-05-08 18:03:15 -070038 Camera3IOStreamBase(id, CAMERA3_STREAM_OUTPUT, width, height,
Zhijun He125684a2015-12-26 15:07:30 -080039 /*maxSize*/0, format, dataSpace, rotation, setId),
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -080040 mConsumer(consumer),
Ruchit Sharmae0711f22014-08-18 13:48:24 -040041 mTransform(0),
Zhijun He125684a2015-12-26 15:07:30 -080042 mTraceFirstBuffer(true),
Shuzhen Wang13a69632016-01-26 09:51:07 -080043 mTimestampBuffer(true),
Zhijun He125684a2015-12-26 15:07:30 -080044 mUseBufferManager(false) {
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -080045
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -080046 if (mConsumer == NULL) {
47 ALOGE("%s: Consumer is NULL!", __FUNCTION__);
48 mState = STATE_ERROR;
49 }
Zhijun He125684a2015-12-26 15:07:30 -080050
51 if (setId > CAMERA3_STREAM_SET_ID_INVALID) {
52 mBufferReleasedListener = new BufferReleasedListener(this);
53 }
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -080054}
55
56Camera3OutputStream::Camera3OutputStream(int id,
Eino-Ville Talvala727d1722015-06-09 13:44:19 -070057 sp<Surface> consumer,
Eino-Ville Talvala3d82c0d2015-02-23 15:19:19 -080058 uint32_t width, uint32_t height, size_t maxSize, int format,
Zhijun He125684a2015-12-26 15:07:30 -080059 android_dataspace dataSpace, camera3_stream_rotation_t rotation, int setId) :
Igor Murashkine3a9f962013-05-08 18:03:15 -070060 Camera3IOStreamBase(id, CAMERA3_STREAM_OUTPUT, width, height, maxSize,
Zhijun He125684a2015-12-26 15:07:30 -080061 format, dataSpace, rotation, setId),
Igor Murashkina55b5452013-04-02 16:36:33 -070062 mConsumer(consumer),
Ruchit Sharmae0711f22014-08-18 13:48:24 -040063 mTransform(0),
Zhijun He125684a2015-12-26 15:07:30 -080064 mTraceFirstBuffer(true),
Shuzhen Wang13a69632016-01-26 09:51:07 -080065 mTimestampBuffer(true),
Zhijun He125684a2015-12-26 15:07:30 -080066 mUseBufferManager(false) {
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -080067
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -080068 if (format != HAL_PIXEL_FORMAT_BLOB && format != HAL_PIXEL_FORMAT_RAW_OPAQUE) {
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -080069 ALOGE("%s: Bad format for size-only stream: %d", __FUNCTION__,
70 format);
71 mState = STATE_ERROR;
72 }
73
74 if (mConsumer == NULL) {
75 ALOGE("%s: Consumer is NULL!", __FUNCTION__);
76 mState = STATE_ERROR;
77 }
Zhijun He125684a2015-12-26 15:07:30 -080078
79 if (setId > CAMERA3_STREAM_SET_ID_INVALID) {
80 mBufferReleasedListener = new BufferReleasedListener(this);
81 }
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -080082}
83
Igor Murashkine3a9f962013-05-08 18:03:15 -070084Camera3OutputStream::Camera3OutputStream(int id, camera3_stream_type_t type,
85 uint32_t width, uint32_t height,
Eino-Ville Talvala3d82c0d2015-02-23 15:19:19 -080086 int format,
Yin-Chia Yehb97babb2015-03-12 13:42:44 -070087 android_dataspace dataSpace,
Zhijun He125684a2015-12-26 15:07:30 -080088 camera3_stream_rotation_t rotation,
89 int setId) :
Igor Murashkine3a9f962013-05-08 18:03:15 -070090 Camera3IOStreamBase(id, type, width, height,
91 /*maxSize*/0,
Zhijun He125684a2015-12-26 15:07:30 -080092 format, dataSpace, rotation, setId),
93 mTransform(0),
94 mTraceFirstBuffer(true),
95 mUseBufferManager(false) {
96
97 if (setId > CAMERA3_STREAM_SET_ID_INVALID) {
98 mBufferReleasedListener = new BufferReleasedListener(this);
99 }
Igor Murashkine3a9f962013-05-08 18:03:15 -0700100
101 // Subclasses expected to initialize mConsumer themselves
102}
103
104
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800105Camera3OutputStream::~Camera3OutputStream() {
106 disconnectLocked();
107}
108
109status_t Camera3OutputStream::getBufferLocked(camera3_stream_buffer *buffer) {
110 ATRACE_CALL();
111 status_t res;
112
Igor Murashkine3a9f962013-05-08 18:03:15 -0700113 if ((res = getBufferPreconditionCheckLocked()) != OK) {
114 return res;
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800115 }
116
117 ANativeWindowBuffer* anb;
Zhijun He125684a2015-12-26 15:07:30 -0800118 int fenceFd = -1;
119 if (mUseBufferManager) {
120 sp<GraphicBuffer> gb;
121 res = mBufferManager->getBufferForStream(getId(), getStreamSetId(), &gb, &fenceFd);
122 if (res != OK) {
123 ALOGE("%s: Stream %d: Can't get next output buffer from buffer manager: %s (%d)",
124 __FUNCTION__, mId, strerror(-res), res);
125 return res;
126 }
127 // Attach this buffer to the bufferQueue: the buffer will be in dequeue state after a
128 // successful return.
129 anb = gb.get();
130 res = mConsumer->attachBuffer(anb);
131 if (res != OK) {
132 ALOGE("%s: Stream %d: Can't attach the output buffer to this surface: %s (%d)",
133 __FUNCTION__, mId, strerror(-res), res);
134 return res;
135 }
136 } else {
137 /**
138 * Release the lock briefly to avoid deadlock for below scenario:
139 * Thread 1: StreamingProcessor::startStream -> Camera3Stream::isConfiguring().
140 * This thread acquired StreamingProcessor lock and try to lock Camera3Stream lock.
141 * Thread 2: Camera3Stream::returnBuffer->StreamingProcessor::onFrameAvailable().
142 * This thread acquired Camera3Stream lock and bufferQueue lock, and try to lock
143 * StreamingProcessor lock.
144 * Thread 3: Camera3Stream::getBuffer(). This thread acquired Camera3Stream lock
145 * and try to lock bufferQueue lock.
146 * Then there is circular locking dependency.
147 */
148 sp<ANativeWindow> currentConsumer = mConsumer;
149 mLock.unlock();
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800150
Zhijun He125684a2015-12-26 15:07:30 -0800151 res = currentConsumer->dequeueBuffer(currentConsumer.get(), &anb, &fenceFd);
152 mLock.lock();
153 if (res != OK) {
154 ALOGE("%s: Stream %d: Can't dequeue next output buffer: %s (%d)",
155 __FUNCTION__, mId, strerror(-res), res);
156 return res;
157 }
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800158 }
159
Igor Murashkine3a9f962013-05-08 18:03:15 -0700160 /**
161 * FenceFD now owned by HAL except in case of error,
162 * in which case we reassign it to acquire_fence
163 */
164 handoutBufferLocked(*buffer, &(anb->handle), /*acquireFence*/fenceFd,
Zhijun He6adc9cc2014-04-15 14:09:55 -0700165 /*releaseFence*/-1, CAMERA3_BUFFER_STATUS_OK, /*output*/true);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800166
167 return OK;
168}
169
170status_t Camera3OutputStream::returnBufferLocked(
171 const camera3_stream_buffer &buffer,
172 nsecs_t timestamp) {
173 ATRACE_CALL();
Igor Murashkine3a9f962013-05-08 18:03:15 -0700174
175 status_t res = returnAnyBufferLocked(buffer, timestamp, /*output*/true);
176
177 if (res != OK) {
178 return res;
179 }
180
181 mLastTimestamp = timestamp;
182
183 return OK;
184}
185
186status_t Camera3OutputStream::returnBufferCheckedLocked(
187 const camera3_stream_buffer &buffer,
188 nsecs_t timestamp,
189 bool output,
190 /*out*/
191 sp<Fence> *releaseFenceOut) {
192
193 (void)output;
194 ALOG_ASSERT(output, "Expected output to be true");
195
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800196 status_t res;
Igor Murashkin5a1798a2013-05-07 10:58:13 -0700197
Yin-Chia Yeh4c9736f2015-03-05 15:01:36 -0800198 // Fence management - always honor release fence from HAL
199 sp<Fence> releaseFence = new Fence(buffer.release_fence);
Igor Murashkin5a1798a2013-05-07 10:58:13 -0700200 int anwReleaseFence = releaseFence->dup();
201
202 /**
Zhijun He124ccf42013-05-22 14:01:30 -0700203 * Release the lock briefly to avoid deadlock with
204 * StreamingProcessor::startStream -> Camera3Stream::isConfiguring (this
205 * thread will go into StreamingProcessor::onFrameAvailable) during
206 * queueBuffer
207 */
208 sp<ANativeWindow> currentConsumer = mConsumer;
209 mLock.unlock();
210
211 /**
Igor Murashkin5a1798a2013-05-07 10:58:13 -0700212 * Return buffer back to ANativeWindow
213 */
214 if (buffer.status == CAMERA3_BUFFER_STATUS_ERROR) {
215 // Cancel buffer
Zhijun He124ccf42013-05-22 14:01:30 -0700216 res = currentConsumer->cancelBuffer(currentConsumer.get(),
Igor Murashkin5a1798a2013-05-07 10:58:13 -0700217 container_of(buffer.buffer, ANativeWindowBuffer, handle),
218 anwReleaseFence);
219 if (res != OK) {
220 ALOGE("%s: Stream %d: Error cancelling buffer to native window:"
Igor Murashkine3a9f962013-05-08 18:03:15 -0700221 " %s (%d)", __FUNCTION__, mId, strerror(-res), res);
Igor Murashkin5a1798a2013-05-07 10:58:13 -0700222 }
Zhijun He1ff811b2016-01-26 14:39:51 -0800223
224 if (mUseBufferManager) {
225 // Return this buffer back to buffer manager.
226 mBufferReleasedListener->onBufferReleased();
227 }
Igor Murashkin5a1798a2013-05-07 10:58:13 -0700228 } else {
Ruchit Sharmae0711f22014-08-18 13:48:24 -0400229 if (mTraceFirstBuffer && (stream_type == CAMERA3_STREAM_OUTPUT)) {
230 {
231 char traceLog[48];
232 snprintf(traceLog, sizeof(traceLog), "Stream %d: first full buffer\n", mId);
233 ATRACE_NAME(traceLog);
234 }
235 mTraceFirstBuffer = false;
236 }
237
Shuzhen Wang13a69632016-01-26 09:51:07 -0800238 /* Certain consumers (such as AudioSource or HardwareComposer) use
239 * MONOTONIC time, causing time misalignment if camera timestamp is
240 * in BOOTTIME. Avoid setting timestamp, and let BufferQueue generate it
241 * instead. */
242 if (mTimestampBuffer) {
243 res = native_window_set_buffers_timestamp(mConsumer.get(), timestamp);
244 if (res != OK) {
245 ALOGE("%s: Stream %d: Error setting timestamp: %s (%d)",
246 __FUNCTION__, mId, strerror(-res), res);
247 return res;
248 }
Yin-Chia Yeh4c9736f2015-03-05 15:01:36 -0800249 }
250
Zhijun He124ccf42013-05-22 14:01:30 -0700251 res = currentConsumer->queueBuffer(currentConsumer.get(),
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800252 container_of(buffer.buffer, ANativeWindowBuffer, handle),
253 anwReleaseFence);
254 if (res != OK) {
Igor Murashkine3a9f962013-05-08 18:03:15 -0700255 ALOGE("%s: Stream %d: Error queueing buffer to native window: "
256 "%s (%d)", __FUNCTION__, mId, strerror(-res), res);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800257 }
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800258 }
Zhijun He124ccf42013-05-22 14:01:30 -0700259 mLock.lock();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700260
261 // Once a valid buffer has been returned to the queue, can no longer
262 // dequeue all buffers for preallocation.
263 if (buffer.status != CAMERA3_BUFFER_STATUS_ERROR) {
264 mStreamUnpreparable = true;
265 }
266
Igor Murashkin5a1798a2013-05-07 10:58:13 -0700267 if (res != OK) {
268 close(anwReleaseFence);
Igor Murashkin5a1798a2013-05-07 10:58:13 -0700269 }
270
Igor Murashkine3a9f962013-05-08 18:03:15 -0700271 *releaseFenceOut = releaseFence;
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800272
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700273 return res;
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800274}
275
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800276void Camera3OutputStream::dump(int fd, const Vector<String16> &args) const {
277 (void) args;
278 String8 lines;
279 lines.appendFormat(" Stream[%d]: Output\n", mId);
Eino-Ville Talvala727d1722015-06-09 13:44:19 -0700280 lines.appendFormat(" Consumer name: %s\n", mConsumerName.string());
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800281 write(fd, lines.string(), lines.size());
Igor Murashkine3a9f962013-05-08 18:03:15 -0700282
283 Camera3IOStreamBase::dump(fd, args);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800284}
285
286status_t Camera3OutputStream::setTransform(int transform) {
287 ATRACE_CALL();
288 Mutex::Autolock l(mLock);
289 return setTransformLocked(transform);
290}
291
292status_t Camera3OutputStream::setTransformLocked(int transform) {
293 status_t res = OK;
294 if (mState == STATE_ERROR) {
295 ALOGE("%s: Stream in error state", __FUNCTION__);
296 return INVALID_OPERATION;
297 }
298
299 mTransform = transform;
300 if (mState == STATE_CONFIGURED) {
301 res = native_window_set_buffers_transform(mConsumer.get(),
302 transform);
303 if (res != OK) {
304 ALOGE("%s: Unable to configure stream transform to %x: %s (%d)",
305 __FUNCTION__, transform, strerror(-res), res);
306 }
307 }
308 return res;
309}
310
311status_t Camera3OutputStream::configureQueueLocked() {
312 status_t res;
313
Ruchit Sharmae0711f22014-08-18 13:48:24 -0400314 mTraceFirstBuffer = true;
Igor Murashkine3a9f962013-05-08 18:03:15 -0700315 if ((res = Camera3IOStreamBase::configureQueueLocked()) != OK) {
316 return res;
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800317 }
318
Igor Murashkine3a9f962013-05-08 18:03:15 -0700319 ALOG_ASSERT(mConsumer != 0, "mConsumer should never be NULL");
320
Zhijun He125684a2015-12-26 15:07:30 -0800321 // Configure consumer-side ANativeWindow interface. The listener may be used
322 // to notify buffer manager (if it is used) of the returned buffers.
323 res = mConsumer->connect(NATIVE_WINDOW_API_CAMERA, /*listener*/mBufferReleasedListener);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800324 if (res != OK) {
325 ALOGE("%s: Unable to connect to native window for stream %d",
326 __FUNCTION__, mId);
327 return res;
328 }
329
Eino-Ville Talvala727d1722015-06-09 13:44:19 -0700330 mConsumerName = mConsumer->getConsumerName();
331
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800332 res = native_window_set_usage(mConsumer.get(), camera3_stream::usage);
333 if (res != OK) {
334 ALOGE("%s: Unable to configure usage %08x for stream %d",
335 __FUNCTION__, camera3_stream::usage, mId);
336 return res;
337 }
338
339 res = native_window_set_scaling_mode(mConsumer.get(),
340 NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
341 if (res != OK) {
342 ALOGE("%s: Unable to configure stream scaling: %s (%d)",
343 __FUNCTION__, strerror(-res), res);
344 return res;
345 }
346
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800347 if (mMaxSize == 0) {
348 // For buffers of known size
Eino-Ville Talvala7d70c5e2014-07-24 18:10:23 -0700349 res = native_window_set_buffers_dimensions(mConsumer.get(),
350 camera3_stream::width, camera3_stream::height);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800351 } else {
352 // For buffers with bounded size
Eino-Ville Talvala7d70c5e2014-07-24 18:10:23 -0700353 res = native_window_set_buffers_dimensions(mConsumer.get(),
354 mMaxSize, 1);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800355 }
356 if (res != OK) {
Eino-Ville Talvala7d70c5e2014-07-24 18:10:23 -0700357 ALOGE("%s: Unable to configure stream buffer dimensions"
358 " %d x %d (maxSize %zu) for stream %d",
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800359 __FUNCTION__, camera3_stream::width, camera3_stream::height,
Eino-Ville Talvala7d70c5e2014-07-24 18:10:23 -0700360 mMaxSize, mId);
361 return res;
362 }
363 res = native_window_set_buffers_format(mConsumer.get(),
364 camera3_stream::format);
365 if (res != OK) {
366 ALOGE("%s: Unable to configure stream buffer format %#x for stream %d",
367 __FUNCTION__, camera3_stream::format, mId);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800368 return res;
369 }
370
Eino-Ville Talvala3d82c0d2015-02-23 15:19:19 -0800371 res = native_window_set_buffers_data_space(mConsumer.get(),
372 camera3_stream::data_space);
373 if (res != OK) {
374 ALOGE("%s: Unable to configure stream dataspace %#x for stream %d",
375 __FUNCTION__, camera3_stream::data_space, mId);
376 return res;
377 }
378
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800379 int maxConsumerBuffers;
Eino-Ville Talvala727d1722015-06-09 13:44:19 -0700380 res = static_cast<ANativeWindow*>(mConsumer.get())->query(
381 mConsumer.get(),
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800382 NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS, &maxConsumerBuffers);
383 if (res != OK) {
384 ALOGE("%s: Unable to query consumer undequeued"
385 " buffer count for stream %d", __FUNCTION__, mId);
386 return res;
387 }
388
Alex Ray20cb3002013-05-28 20:18:22 -0700389 ALOGV("%s: Consumer wants %d buffers, HAL wants %d", __FUNCTION__,
390 maxConsumerBuffers, camera3_stream::max_buffers);
391 if (camera3_stream::max_buffers == 0) {
Zhijun He2ab500c2013-07-23 08:02:53 -0700392 ALOGE("%s: Camera HAL requested max_buffer count: %d, requires at least 1",
Alex Ray20cb3002013-05-28 20:18:22 -0700393 __FUNCTION__, camera3_stream::max_buffers);
394 return INVALID_OPERATION;
395 }
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800396
397 mTotalBufferCount = maxConsumerBuffers + camera3_stream::max_buffers;
Zhijun He6adc9cc2014-04-15 14:09:55 -0700398 mHandoutTotalBufferCount = 0;
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800399 mFrameCount = 0;
400 mLastTimestamp = 0;
Shuzhen Wang13a69632016-01-26 09:51:07 -0800401 mTimestampBuffer = !(isConsumedByHWComposer() | isVideoStream());
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800402
403 res = native_window_set_buffer_count(mConsumer.get(),
404 mTotalBufferCount);
405 if (res != OK) {
406 ALOGE("%s: Unable to set buffer count for stream %d",
407 __FUNCTION__, mId);
408 return res;
409 }
410
411 res = native_window_set_buffers_transform(mConsumer.get(),
412 mTransform);
413 if (res != OK) {
414 ALOGE("%s: Unable to configure stream transform to %x: %s (%d)",
415 __FUNCTION__, mTransform, strerror(-res), res);
416 }
417
Zhijun He125684a2015-12-26 15:07:30 -0800418 /**
419 * Camera3 Buffer manager is only supported by HAL3.2 onwards, as the older HALs requires
420 * buffers to be statically allocated for internal static buffer registration, while the
421 * buffers provided by buffer manager are really dynamically allocated. Camera3Device only
422 * sets the mBufferManager if device version is >= HAL3.2, which guarantees that the buffer
423 * manager setup is skipped in below code.
424 */
425 if (mBufferManager != 0 && mSetId > CAMERA3_STREAM_SET_ID_INVALID) {
426 StreamInfo streamInfo(
427 getId(), getStreamSetId(), getWidth(), getHeight(), getFormat(), getDataSpace(),
428 camera3_stream::usage, mTotalBufferCount, /*isConfigured*/true);
429 res = mBufferManager->registerStream(streamInfo);
430 if (res == OK) {
431 // Disable buffer allocation for this BufferQueue, buffer manager will take over
432 // the buffer allocation responsibility.
433 mConsumer->getIGraphicBufferProducer()->allowAllocation(false);
434 mUseBufferManager = true;
435 } else {
436 ALOGE("%s: Unable to register stream %d to camera3 buffer manager, "
437 "(error %d %s), fall back to BufferQueue for buffer management!",
438 __FUNCTION__, mId, res, strerror(-res));
439 }
440 }
441
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800442 return OK;
443}
444
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800445status_t Camera3OutputStream::disconnectLocked() {
446 status_t res;
447
Igor Murashkine3a9f962013-05-08 18:03:15 -0700448 if ((res = Camera3IOStreamBase::disconnectLocked()) != OK) {
449 return res;
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800450 }
451
Zhijun He125684a2015-12-26 15:07:30 -0800452 ALOGV("%s: disconnecting stream %d from native window", __FUNCTION__, getId());
453
Igor Murashkine3a9f962013-05-08 18:03:15 -0700454 res = native_window_api_disconnect(mConsumer.get(),
455 NATIVE_WINDOW_API_CAMERA);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800456
457 /**
458 * This is not an error. if client calling process dies, the window will
459 * also die and all calls to it will return DEAD_OBJECT, thus it's already
460 * "disconnected"
461 */
462 if (res == DEAD_OBJECT) {
463 ALOGW("%s: While disconnecting stream %d from native window, the"
464 " native window died from under us", __FUNCTION__, mId);
465 }
466 else if (res != OK) {
Igor Murashkine3a9f962013-05-08 18:03:15 -0700467 ALOGE("%s: Unable to disconnect stream %d from native window "
468 "(error %d %s)",
469 __FUNCTION__, mId, res, strerror(-res));
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800470 mState = STATE_ERROR;
471 return res;
472 }
473
Zhijun He125684a2015-12-26 15:07:30 -0800474 // Since device is already idle, there is no getBuffer call to buffer manager, unregister the
475 // stream at this point should be safe.
476 if (mUseBufferManager) {
477 res = mBufferManager->unregisterStream(getId(), getStreamSetId());
478 if (res != OK) {
479 ALOGE("%s: Unable to unregister stream %d from buffer manager "
480 "(error %d %s)", __FUNCTION__, mId, res, strerror(-res));
481 mState = STATE_ERROR;
482 return res;
483 }
484 // Note that, to make prepare/teardown case work, we must not mBufferManager.clear(), as
485 // the stream is still in usable state after this call.
486 mUseBufferManager = false;
487 }
488
Igor Murashkine3a9f962013-05-08 18:03:15 -0700489 mState = (mState == STATE_IN_RECONFIG) ? STATE_IN_CONFIG
490 : STATE_CONSTRUCTED;
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800491 return OK;
492}
493
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700494status_t Camera3OutputStream::getEndpointUsage(uint32_t *usage) const {
Eino-Ville Talvalab2f5b192013-07-30 14:36:03 -0700495
496 status_t res;
497 int32_t u = 0;
Eino-Ville Talvala727d1722015-06-09 13:44:19 -0700498 res = static_cast<ANativeWindow*>(mConsumer.get())->query(mConsumer.get(),
Eino-Ville Talvalab2f5b192013-07-30 14:36:03 -0700499 NATIVE_WINDOW_CONSUMER_USAGE_BITS, &u);
Eino-Ville Talvalab2f5b192013-07-30 14:36:03 -0700500
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700501 // If an opaque output stream's endpoint is ImageReader, add
502 // GRALLOC_USAGE_HW_CAMERA_ZSL to the usage so HAL knows it will be used
503 // for the ZSL use case.
504 // Assume it's for ImageReader if the consumer usage doesn't have any of these bits set:
505 // 1. GRALLOC_USAGE_HW_TEXTURE
506 // 2. GRALLOC_USAGE_HW_RENDER
507 // 3. GRALLOC_USAGE_HW_COMPOSER
508 // 4. GRALLOC_USAGE_HW_VIDEO_ENCODER
509 if (camera3_stream::format == HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED &&
510 (u & (GRALLOC_USAGE_HW_TEXTURE | GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_HW_COMPOSER |
511 GRALLOC_USAGE_HW_VIDEO_ENCODER)) == 0) {
512 u |= GRALLOC_USAGE_HW_CAMERA_ZSL;
513 }
514
515 *usage = u;
Eino-Ville Talvalab2f5b192013-07-30 14:36:03 -0700516 return res;
517}
518
Chien-Yu Chen85a64552015-08-28 15:46:12 -0700519bool Camera3OutputStream::isVideoStream() const {
520 uint32_t usage = 0;
521 status_t res = getEndpointUsage(&usage);
522 if (res != OK) {
523 ALOGE("%s: getting end point usage failed: %s (%d).", __FUNCTION__, strerror(-res), res);
524 return false;
525 }
526
527 return (usage & GRALLOC_USAGE_HW_VIDEO_ENCODER) != 0;
528}
529
Zhijun He125684a2015-12-26 15:07:30 -0800530status_t Camera3OutputStream::setBufferManager(sp<Camera3BufferManager> bufferManager) {
531 Mutex::Autolock l(mLock);
532 if (mState != STATE_CONSTRUCTED) {
533 ALOGE("%s: this method can only be called when stream in in CONSTRUCTED state.",
534 __FUNCTION__);
535 return INVALID_OPERATION;
536 }
537 mBufferManager = bufferManager;
538
539 return OK;
540}
541
542void Camera3OutputStream::BufferReleasedListener::onBufferReleased() {
543 sp<Camera3OutputStream> stream = mParent.promote();
544 if (stream == nullptr) {
545 ALOGV("%s: Parent camera3 output stream was destroyed", __FUNCTION__);
546 return;
547 }
548
549 Mutex::Autolock l(stream->mLock);
550 if (!(stream->mUseBufferManager)) {
551 return;
552 }
553
554 sp<Fence> fence;
555 sp<GraphicBuffer> buffer;
556 int fenceFd = -1;
557 status_t res = stream->mConsumer->detachNextBuffer(&buffer, &fence);
558 if (res == NO_MEMORY) {
559 // This may rarely happen, which indicates that the released buffer was freed by other
560 // call (e.g., attachBuffer, dequeueBuffer etc.) before reaching here. We should notify the
561 // buffer manager that this buffer has been freed. It's not fatal, but should be avoided,
562 // therefore log a warning.
563 buffer = 0;
564 ALOGW("%s: the released buffer has already been freed by the buffer queue!", __FUNCTION__);
565 } else if (res != OK) {
566 // Other errors are fatal.
567 ALOGE("%s: detach next buffer failed: %s (%d).", __FUNCTION__, strerror(-res), res);
568 stream->mState = STATE_ERROR;
569 return;
570 }
571
572 if (fence!= 0 && fence->isValid()) {
573 fenceFd = fence->dup();
574 }
575 res = stream->mBufferManager->returnBufferForStream(stream->getId(), stream->getStreamSetId(),
576 buffer, fenceFd);
577 if (res != OK) {
578 ALOGE("%s: return buffer to buffer manager failed: %s (%d).", __FUNCTION__,
579 strerror(-res), res);
580 stream->mState = STATE_ERROR;
581 }
582}
Shuzhen Wang13a69632016-01-26 09:51:07 -0800583
584bool Camera3OutputStream::isConsumedByHWComposer() const {
585 uint32_t usage = 0;
586 status_t res = getEndpointUsage(&usage);
587 if (res != OK) {
588 ALOGE("%s: getting end point usage failed: %s (%d).", __FUNCTION__, strerror(-res), res);
589 return false;
590 }
591
592 return (usage & GRALLOC_USAGE_HW_COMPOSER) != 0;
593}
594
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800595}; // namespace camera3
596
597}; // namespace android