blob: e42d83994ea113ffd5da882ec4c03b160243c786 [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 }
223 } else {
Ruchit Sharmae0711f22014-08-18 13:48:24 -0400224 if (mTraceFirstBuffer && (stream_type == CAMERA3_STREAM_OUTPUT)) {
225 {
226 char traceLog[48];
227 snprintf(traceLog, sizeof(traceLog), "Stream %d: first full buffer\n", mId);
228 ATRACE_NAME(traceLog);
229 }
230 mTraceFirstBuffer = false;
231 }
232
Shuzhen Wang13a69632016-01-26 09:51:07 -0800233 /* Certain consumers (such as AudioSource or HardwareComposer) use
234 * MONOTONIC time, causing time misalignment if camera timestamp is
235 * in BOOTTIME. Avoid setting timestamp, and let BufferQueue generate it
236 * instead. */
237 if (mTimestampBuffer) {
238 res = native_window_set_buffers_timestamp(mConsumer.get(), timestamp);
239 if (res != OK) {
240 ALOGE("%s: Stream %d: Error setting timestamp: %s (%d)",
241 __FUNCTION__, mId, strerror(-res), res);
242 return res;
243 }
Yin-Chia Yeh4c9736f2015-03-05 15:01:36 -0800244 }
245
Zhijun He124ccf42013-05-22 14:01:30 -0700246 res = currentConsumer->queueBuffer(currentConsumer.get(),
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800247 container_of(buffer.buffer, ANativeWindowBuffer, handle),
248 anwReleaseFence);
249 if (res != OK) {
Igor Murashkine3a9f962013-05-08 18:03:15 -0700250 ALOGE("%s: Stream %d: Error queueing buffer to native window: "
251 "%s (%d)", __FUNCTION__, mId, strerror(-res), res);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800252 }
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800253 }
Zhijun He124ccf42013-05-22 14:01:30 -0700254 mLock.lock();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700255
256 // Once a valid buffer has been returned to the queue, can no longer
257 // dequeue all buffers for preallocation.
258 if (buffer.status != CAMERA3_BUFFER_STATUS_ERROR) {
259 mStreamUnpreparable = true;
260 }
261
Igor Murashkin5a1798a2013-05-07 10:58:13 -0700262 if (res != OK) {
263 close(anwReleaseFence);
Igor Murashkin5a1798a2013-05-07 10:58:13 -0700264 }
265
Igor Murashkine3a9f962013-05-08 18:03:15 -0700266 *releaseFenceOut = releaseFence;
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800267
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700268 return res;
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800269}
270
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800271void Camera3OutputStream::dump(int fd, const Vector<String16> &args) const {
272 (void) args;
273 String8 lines;
274 lines.appendFormat(" Stream[%d]: Output\n", mId);
Eino-Ville Talvala727d1722015-06-09 13:44:19 -0700275 lines.appendFormat(" Consumer name: %s\n", mConsumerName.string());
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800276 write(fd, lines.string(), lines.size());
Igor Murashkine3a9f962013-05-08 18:03:15 -0700277
278 Camera3IOStreamBase::dump(fd, args);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800279}
280
281status_t Camera3OutputStream::setTransform(int transform) {
282 ATRACE_CALL();
283 Mutex::Autolock l(mLock);
284 return setTransformLocked(transform);
285}
286
287status_t Camera3OutputStream::setTransformLocked(int transform) {
288 status_t res = OK;
289 if (mState == STATE_ERROR) {
290 ALOGE("%s: Stream in error state", __FUNCTION__);
291 return INVALID_OPERATION;
292 }
293
294 mTransform = transform;
295 if (mState == STATE_CONFIGURED) {
296 res = native_window_set_buffers_transform(mConsumer.get(),
297 transform);
298 if (res != OK) {
299 ALOGE("%s: Unable to configure stream transform to %x: %s (%d)",
300 __FUNCTION__, transform, strerror(-res), res);
301 }
302 }
303 return res;
304}
305
306status_t Camera3OutputStream::configureQueueLocked() {
307 status_t res;
308
Ruchit Sharmae0711f22014-08-18 13:48:24 -0400309 mTraceFirstBuffer = true;
Igor Murashkine3a9f962013-05-08 18:03:15 -0700310 if ((res = Camera3IOStreamBase::configureQueueLocked()) != OK) {
311 return res;
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800312 }
313
Igor Murashkine3a9f962013-05-08 18:03:15 -0700314 ALOG_ASSERT(mConsumer != 0, "mConsumer should never be NULL");
315
Zhijun He125684a2015-12-26 15:07:30 -0800316 // Configure consumer-side ANativeWindow interface. The listener may be used
317 // to notify buffer manager (if it is used) of the returned buffers.
318 res = mConsumer->connect(NATIVE_WINDOW_API_CAMERA, /*listener*/mBufferReleasedListener);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800319 if (res != OK) {
320 ALOGE("%s: Unable to connect to native window for stream %d",
321 __FUNCTION__, mId);
322 return res;
323 }
324
Eino-Ville Talvala727d1722015-06-09 13:44:19 -0700325 mConsumerName = mConsumer->getConsumerName();
326
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800327 res = native_window_set_usage(mConsumer.get(), camera3_stream::usage);
328 if (res != OK) {
329 ALOGE("%s: Unable to configure usage %08x for stream %d",
330 __FUNCTION__, camera3_stream::usage, mId);
331 return res;
332 }
333
334 res = native_window_set_scaling_mode(mConsumer.get(),
335 NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
336 if (res != OK) {
337 ALOGE("%s: Unable to configure stream scaling: %s (%d)",
338 __FUNCTION__, strerror(-res), res);
339 return res;
340 }
341
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800342 if (mMaxSize == 0) {
343 // For buffers of known size
Eino-Ville Talvala7d70c5e2014-07-24 18:10:23 -0700344 res = native_window_set_buffers_dimensions(mConsumer.get(),
345 camera3_stream::width, camera3_stream::height);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800346 } else {
347 // For buffers with bounded size
Eino-Ville Talvala7d70c5e2014-07-24 18:10:23 -0700348 res = native_window_set_buffers_dimensions(mConsumer.get(),
349 mMaxSize, 1);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800350 }
351 if (res != OK) {
Eino-Ville Talvala7d70c5e2014-07-24 18:10:23 -0700352 ALOGE("%s: Unable to configure stream buffer dimensions"
353 " %d x %d (maxSize %zu) for stream %d",
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800354 __FUNCTION__, camera3_stream::width, camera3_stream::height,
Eino-Ville Talvala7d70c5e2014-07-24 18:10:23 -0700355 mMaxSize, mId);
356 return res;
357 }
358 res = native_window_set_buffers_format(mConsumer.get(),
359 camera3_stream::format);
360 if (res != OK) {
361 ALOGE("%s: Unable to configure stream buffer format %#x for stream %d",
362 __FUNCTION__, camera3_stream::format, mId);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800363 return res;
364 }
365
Eino-Ville Talvala3d82c0d2015-02-23 15:19:19 -0800366 res = native_window_set_buffers_data_space(mConsumer.get(),
367 camera3_stream::data_space);
368 if (res != OK) {
369 ALOGE("%s: Unable to configure stream dataspace %#x for stream %d",
370 __FUNCTION__, camera3_stream::data_space, mId);
371 return res;
372 }
373
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800374 int maxConsumerBuffers;
Eino-Ville Talvala727d1722015-06-09 13:44:19 -0700375 res = static_cast<ANativeWindow*>(mConsumer.get())->query(
376 mConsumer.get(),
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800377 NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS, &maxConsumerBuffers);
378 if (res != OK) {
379 ALOGE("%s: Unable to query consumer undequeued"
380 " buffer count for stream %d", __FUNCTION__, mId);
381 return res;
382 }
383
Alex Ray20cb3002013-05-28 20:18:22 -0700384 ALOGV("%s: Consumer wants %d buffers, HAL wants %d", __FUNCTION__,
385 maxConsumerBuffers, camera3_stream::max_buffers);
386 if (camera3_stream::max_buffers == 0) {
Zhijun He2ab500c2013-07-23 08:02:53 -0700387 ALOGE("%s: Camera HAL requested max_buffer count: %d, requires at least 1",
Alex Ray20cb3002013-05-28 20:18:22 -0700388 __FUNCTION__, camera3_stream::max_buffers);
389 return INVALID_OPERATION;
390 }
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800391
392 mTotalBufferCount = maxConsumerBuffers + camera3_stream::max_buffers;
Zhijun He6adc9cc2014-04-15 14:09:55 -0700393 mHandoutTotalBufferCount = 0;
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800394 mFrameCount = 0;
395 mLastTimestamp = 0;
Shuzhen Wang13a69632016-01-26 09:51:07 -0800396 mTimestampBuffer = !(isConsumedByHWComposer() | isVideoStream());
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800397
398 res = native_window_set_buffer_count(mConsumer.get(),
399 mTotalBufferCount);
400 if (res != OK) {
401 ALOGE("%s: Unable to set buffer count for stream %d",
402 __FUNCTION__, mId);
403 return res;
404 }
405
406 res = native_window_set_buffers_transform(mConsumer.get(),
407 mTransform);
408 if (res != OK) {
409 ALOGE("%s: Unable to configure stream transform to %x: %s (%d)",
410 __FUNCTION__, mTransform, strerror(-res), res);
411 }
412
Zhijun He125684a2015-12-26 15:07:30 -0800413 /**
414 * Camera3 Buffer manager is only supported by HAL3.2 onwards, as the older HALs requires
415 * buffers to be statically allocated for internal static buffer registration, while the
416 * buffers provided by buffer manager are really dynamically allocated. Camera3Device only
417 * sets the mBufferManager if device version is >= HAL3.2, which guarantees that the buffer
418 * manager setup is skipped in below code.
419 */
420 if (mBufferManager != 0 && mSetId > CAMERA3_STREAM_SET_ID_INVALID) {
421 StreamInfo streamInfo(
422 getId(), getStreamSetId(), getWidth(), getHeight(), getFormat(), getDataSpace(),
423 camera3_stream::usage, mTotalBufferCount, /*isConfigured*/true);
424 res = mBufferManager->registerStream(streamInfo);
425 if (res == OK) {
426 // Disable buffer allocation for this BufferQueue, buffer manager will take over
427 // the buffer allocation responsibility.
428 mConsumer->getIGraphicBufferProducer()->allowAllocation(false);
429 mUseBufferManager = true;
430 } else {
431 ALOGE("%s: Unable to register stream %d to camera3 buffer manager, "
432 "(error %d %s), fall back to BufferQueue for buffer management!",
433 __FUNCTION__, mId, res, strerror(-res));
434 }
435 }
436
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800437 return OK;
438}
439
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800440status_t Camera3OutputStream::disconnectLocked() {
441 status_t res;
442
Igor Murashkine3a9f962013-05-08 18:03:15 -0700443 if ((res = Camera3IOStreamBase::disconnectLocked()) != OK) {
444 return res;
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800445 }
446
Zhijun He125684a2015-12-26 15:07:30 -0800447 ALOGV("%s: disconnecting stream %d from native window", __FUNCTION__, getId());
448
Igor Murashkine3a9f962013-05-08 18:03:15 -0700449 res = native_window_api_disconnect(mConsumer.get(),
450 NATIVE_WINDOW_API_CAMERA);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800451
452 /**
453 * This is not an error. if client calling process dies, the window will
454 * also die and all calls to it will return DEAD_OBJECT, thus it's already
455 * "disconnected"
456 */
457 if (res == DEAD_OBJECT) {
458 ALOGW("%s: While disconnecting stream %d from native window, the"
459 " native window died from under us", __FUNCTION__, mId);
460 }
461 else if (res != OK) {
Igor Murashkine3a9f962013-05-08 18:03:15 -0700462 ALOGE("%s: Unable to disconnect stream %d from native window "
463 "(error %d %s)",
464 __FUNCTION__, mId, res, strerror(-res));
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800465 mState = STATE_ERROR;
466 return res;
467 }
468
Zhijun He125684a2015-12-26 15:07:30 -0800469 // Since device is already idle, there is no getBuffer call to buffer manager, unregister the
470 // stream at this point should be safe.
471 if (mUseBufferManager) {
472 res = mBufferManager->unregisterStream(getId(), getStreamSetId());
473 if (res != OK) {
474 ALOGE("%s: Unable to unregister stream %d from buffer manager "
475 "(error %d %s)", __FUNCTION__, mId, res, strerror(-res));
476 mState = STATE_ERROR;
477 return res;
478 }
479 // Note that, to make prepare/teardown case work, we must not mBufferManager.clear(), as
480 // the stream is still in usable state after this call.
481 mUseBufferManager = false;
482 }
483
Igor Murashkine3a9f962013-05-08 18:03:15 -0700484 mState = (mState == STATE_IN_RECONFIG) ? STATE_IN_CONFIG
485 : STATE_CONSTRUCTED;
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800486 return OK;
487}
488
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700489status_t Camera3OutputStream::getEndpointUsage(uint32_t *usage) const {
Eino-Ville Talvalab2f5b192013-07-30 14:36:03 -0700490
491 status_t res;
492 int32_t u = 0;
Eino-Ville Talvala727d1722015-06-09 13:44:19 -0700493 res = static_cast<ANativeWindow*>(mConsumer.get())->query(mConsumer.get(),
Eino-Ville Talvalab2f5b192013-07-30 14:36:03 -0700494 NATIVE_WINDOW_CONSUMER_USAGE_BITS, &u);
Eino-Ville Talvalab2f5b192013-07-30 14:36:03 -0700495
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700496 // If an opaque output stream's endpoint is ImageReader, add
497 // GRALLOC_USAGE_HW_CAMERA_ZSL to the usage so HAL knows it will be used
498 // for the ZSL use case.
499 // Assume it's for ImageReader if the consumer usage doesn't have any of these bits set:
500 // 1. GRALLOC_USAGE_HW_TEXTURE
501 // 2. GRALLOC_USAGE_HW_RENDER
502 // 3. GRALLOC_USAGE_HW_COMPOSER
503 // 4. GRALLOC_USAGE_HW_VIDEO_ENCODER
504 if (camera3_stream::format == HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED &&
505 (u & (GRALLOC_USAGE_HW_TEXTURE | GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_HW_COMPOSER |
506 GRALLOC_USAGE_HW_VIDEO_ENCODER)) == 0) {
507 u |= GRALLOC_USAGE_HW_CAMERA_ZSL;
508 }
509
510 *usage = u;
Eino-Ville Talvalab2f5b192013-07-30 14:36:03 -0700511 return res;
512}
513
Chien-Yu Chen85a64552015-08-28 15:46:12 -0700514bool Camera3OutputStream::isVideoStream() const {
515 uint32_t usage = 0;
516 status_t res = getEndpointUsage(&usage);
517 if (res != OK) {
518 ALOGE("%s: getting end point usage failed: %s (%d).", __FUNCTION__, strerror(-res), res);
519 return false;
520 }
521
522 return (usage & GRALLOC_USAGE_HW_VIDEO_ENCODER) != 0;
523}
524
Zhijun He125684a2015-12-26 15:07:30 -0800525status_t Camera3OutputStream::setBufferManager(sp<Camera3BufferManager> bufferManager) {
526 Mutex::Autolock l(mLock);
527 if (mState != STATE_CONSTRUCTED) {
528 ALOGE("%s: this method can only be called when stream in in CONSTRUCTED state.",
529 __FUNCTION__);
530 return INVALID_OPERATION;
531 }
532 mBufferManager = bufferManager;
533
534 return OK;
535}
536
537void Camera3OutputStream::BufferReleasedListener::onBufferReleased() {
538 sp<Camera3OutputStream> stream = mParent.promote();
539 if (stream == nullptr) {
540 ALOGV("%s: Parent camera3 output stream was destroyed", __FUNCTION__);
541 return;
542 }
543
544 Mutex::Autolock l(stream->mLock);
545 if (!(stream->mUseBufferManager)) {
546 return;
547 }
548
549 sp<Fence> fence;
550 sp<GraphicBuffer> buffer;
551 int fenceFd = -1;
552 status_t res = stream->mConsumer->detachNextBuffer(&buffer, &fence);
553 if (res == NO_MEMORY) {
554 // This may rarely happen, which indicates that the released buffer was freed by other
555 // call (e.g., attachBuffer, dequeueBuffer etc.) before reaching here. We should notify the
556 // buffer manager that this buffer has been freed. It's not fatal, but should be avoided,
557 // therefore log a warning.
558 buffer = 0;
559 ALOGW("%s: the released buffer has already been freed by the buffer queue!", __FUNCTION__);
560 } else if (res != OK) {
561 // Other errors are fatal.
562 ALOGE("%s: detach next buffer failed: %s (%d).", __FUNCTION__, strerror(-res), res);
563 stream->mState = STATE_ERROR;
564 return;
565 }
566
567 if (fence!= 0 && fence->isValid()) {
568 fenceFd = fence->dup();
569 }
570 res = stream->mBufferManager->returnBufferForStream(stream->getId(), stream->getStreamSetId(),
571 buffer, fenceFd);
572 if (res != OK) {
573 ALOGE("%s: return buffer to buffer manager failed: %s (%d).", __FUNCTION__,
574 strerror(-res), res);
575 stream->mState = STATE_ERROR;
576 }
577}
Shuzhen Wang13a69632016-01-26 09:51:07 -0800578
579bool Camera3OutputStream::isConsumedByHWComposer() const {
580 uint32_t usage = 0;
581 status_t res = getEndpointUsage(&usage);
582 if (res != OK) {
583 ALOGE("%s: getting end point usage failed: %s (%d).", __FUNCTION__, strerror(-res), res);
584 return false;
585 }
586
587 return (usage & GRALLOC_USAGE_HW_COMPOSER) != 0;
588}
589
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800590}; // namespace camera3
591
592}; // namespace android