blob: 1e6452fcd4fd6755f322d2c1e3b104d4f9975512 [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),
45 mTimestampOffset(timestampOffset) {
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -080046
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -080047 if (mConsumer == NULL) {
48 ALOGE("%s: Consumer is NULL!", __FUNCTION__);
49 mState = STATE_ERROR;
50 }
Zhijun He125684a2015-12-26 15:07:30 -080051
52 if (setId > CAMERA3_STREAM_SET_ID_INVALID) {
53 mBufferReleasedListener = new BufferReleasedListener(this);
54 }
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -080055}
56
57Camera3OutputStream::Camera3OutputStream(int id,
Eino-Ville Talvala727d1722015-06-09 13:44:19 -070058 sp<Surface> consumer,
Eino-Ville Talvala3d82c0d2015-02-23 15:19:19 -080059 uint32_t width, uint32_t height, size_t maxSize, int format,
Shuzhen Wangc28dccc2016-02-11 23:48:46 -080060 android_dataspace dataSpace, camera3_stream_rotation_t rotation,
61 nsecs_t timestampOffset, int setId) :
Igor Murashkine3a9f962013-05-08 18:03:15 -070062 Camera3IOStreamBase(id, CAMERA3_STREAM_OUTPUT, width, height, maxSize,
Zhijun He125684a2015-12-26 15:07:30 -080063 format, dataSpace, rotation, setId),
Igor Murashkina55b5452013-04-02 16:36:33 -070064 mConsumer(consumer),
Ruchit Sharmae0711f22014-08-18 13:48:24 -040065 mTransform(0),
Zhijun He125684a2015-12-26 15:07:30 -080066 mTraceFirstBuffer(true),
Shuzhen Wangc28dccc2016-02-11 23:48:46 -080067 mUseMonoTimestamp(false),
68 mUseBufferManager(false),
69 mTimestampOffset(timestampOffset) {
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -080070
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -080071 if (format != HAL_PIXEL_FORMAT_BLOB && format != HAL_PIXEL_FORMAT_RAW_OPAQUE) {
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -080072 ALOGE("%s: Bad format for size-only stream: %d", __FUNCTION__,
73 format);
74 mState = STATE_ERROR;
75 }
76
77 if (mConsumer == NULL) {
78 ALOGE("%s: Consumer is NULL!", __FUNCTION__);
79 mState = STATE_ERROR;
80 }
Zhijun He125684a2015-12-26 15:07:30 -080081
82 if (setId > CAMERA3_STREAM_SET_ID_INVALID) {
83 mBufferReleasedListener = new BufferReleasedListener(this);
84 }
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -080085}
86
Igor Murashkine3a9f962013-05-08 18:03:15 -070087Camera3OutputStream::Camera3OutputStream(int id, camera3_stream_type_t type,
88 uint32_t width, uint32_t height,
Eino-Ville Talvala3d82c0d2015-02-23 15:19:19 -080089 int format,
Yin-Chia Yehb97babb2015-03-12 13:42:44 -070090 android_dataspace dataSpace,
Zhijun He125684a2015-12-26 15:07:30 -080091 camera3_stream_rotation_t rotation,
92 int setId) :
Igor Murashkine3a9f962013-05-08 18:03:15 -070093 Camera3IOStreamBase(id, type, width, height,
94 /*maxSize*/0,
Zhijun He125684a2015-12-26 15:07:30 -080095 format, dataSpace, rotation, setId),
96 mTransform(0),
97 mTraceFirstBuffer(true),
Shuzhen Wangc28dccc2016-02-11 23:48:46 -080098 mUseMonoTimestamp(false),
Zhijun He125684a2015-12-26 15:07:30 -080099 mUseBufferManager(false) {
100
101 if (setId > CAMERA3_STREAM_SET_ID_INVALID) {
102 mBufferReleasedListener = new BufferReleasedListener(this);
103 }
Igor Murashkine3a9f962013-05-08 18:03:15 -0700104
105 // Subclasses expected to initialize mConsumer themselves
106}
107
108
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800109Camera3OutputStream::~Camera3OutputStream() {
110 disconnectLocked();
111}
112
113status_t Camera3OutputStream::getBufferLocked(camera3_stream_buffer *buffer) {
114 ATRACE_CALL();
115 status_t res;
116
Igor Murashkine3a9f962013-05-08 18:03:15 -0700117 if ((res = getBufferPreconditionCheckLocked()) != OK) {
118 return res;
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800119 }
120
121 ANativeWindowBuffer* anb;
Zhijun He125684a2015-12-26 15:07:30 -0800122 int fenceFd = -1;
123 if (mUseBufferManager) {
124 sp<GraphicBuffer> gb;
125 res = mBufferManager->getBufferForStream(getId(), getStreamSetId(), &gb, &fenceFd);
126 if (res != OK) {
127 ALOGE("%s: Stream %d: Can't get next output buffer from buffer manager: %s (%d)",
128 __FUNCTION__, mId, strerror(-res), res);
129 return res;
130 }
131 // Attach this buffer to the bufferQueue: the buffer will be in dequeue state after a
132 // successful return.
133 anb = gb.get();
134 res = mConsumer->attachBuffer(anb);
135 if (res != OK) {
136 ALOGE("%s: Stream %d: Can't attach the output buffer to this surface: %s (%d)",
137 __FUNCTION__, mId, strerror(-res), res);
138 return res;
139 }
140 } else {
141 /**
142 * Release the lock briefly to avoid deadlock for below scenario:
143 * Thread 1: StreamingProcessor::startStream -> Camera3Stream::isConfiguring().
144 * This thread acquired StreamingProcessor lock and try to lock Camera3Stream lock.
145 * Thread 2: Camera3Stream::returnBuffer->StreamingProcessor::onFrameAvailable().
146 * This thread acquired Camera3Stream lock and bufferQueue lock, and try to lock
147 * StreamingProcessor lock.
148 * Thread 3: Camera3Stream::getBuffer(). This thread acquired Camera3Stream lock
149 * and try to lock bufferQueue lock.
150 * Then there is circular locking dependency.
151 */
152 sp<ANativeWindow> currentConsumer = mConsumer;
153 mLock.unlock();
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800154
Zhijun He125684a2015-12-26 15:07:30 -0800155 res = currentConsumer->dequeueBuffer(currentConsumer.get(), &anb, &fenceFd);
156 mLock.lock();
157 if (res != OK) {
158 ALOGE("%s: Stream %d: Can't dequeue next output buffer: %s (%d)",
159 __FUNCTION__, mId, strerror(-res), res);
160 return res;
161 }
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800162 }
163
Igor Murashkine3a9f962013-05-08 18:03:15 -0700164 /**
165 * FenceFD now owned by HAL except in case of error,
166 * in which case we reassign it to acquire_fence
167 */
168 handoutBufferLocked(*buffer, &(anb->handle), /*acquireFence*/fenceFd,
Zhijun He6adc9cc2014-04-15 14:09:55 -0700169 /*releaseFence*/-1, CAMERA3_BUFFER_STATUS_OK, /*output*/true);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800170
171 return OK;
172}
173
174status_t Camera3OutputStream::returnBufferLocked(
175 const camera3_stream_buffer &buffer,
176 nsecs_t timestamp) {
177 ATRACE_CALL();
Igor Murashkine3a9f962013-05-08 18:03:15 -0700178
179 status_t res = returnAnyBufferLocked(buffer, timestamp, /*output*/true);
180
181 if (res != OK) {
182 return res;
183 }
184
185 mLastTimestamp = timestamp;
186
187 return OK;
188}
189
190status_t Camera3OutputStream::returnBufferCheckedLocked(
191 const camera3_stream_buffer &buffer,
192 nsecs_t timestamp,
193 bool output,
194 /*out*/
195 sp<Fence> *releaseFenceOut) {
196
197 (void)output;
198 ALOG_ASSERT(output, "Expected output to be true");
199
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800200 status_t res;
Igor Murashkin5a1798a2013-05-07 10:58:13 -0700201
Yin-Chia Yeh4c9736f2015-03-05 15:01:36 -0800202 // Fence management - always honor release fence from HAL
203 sp<Fence> releaseFence = new Fence(buffer.release_fence);
Igor Murashkin5a1798a2013-05-07 10:58:13 -0700204 int anwReleaseFence = releaseFence->dup();
205
206 /**
Zhijun He124ccf42013-05-22 14:01:30 -0700207 * Release the lock briefly to avoid deadlock with
208 * StreamingProcessor::startStream -> Camera3Stream::isConfiguring (this
209 * thread will go into StreamingProcessor::onFrameAvailable) during
210 * queueBuffer
211 */
212 sp<ANativeWindow> currentConsumer = mConsumer;
213 mLock.unlock();
214
215 /**
Igor Murashkin5a1798a2013-05-07 10:58:13 -0700216 * Return buffer back to ANativeWindow
217 */
218 if (buffer.status == CAMERA3_BUFFER_STATUS_ERROR) {
219 // Cancel buffer
Zhijun He124ccf42013-05-22 14:01:30 -0700220 res = currentConsumer->cancelBuffer(currentConsumer.get(),
Igor Murashkin5a1798a2013-05-07 10:58:13 -0700221 container_of(buffer.buffer, ANativeWindowBuffer, handle),
222 anwReleaseFence);
223 if (res != OK) {
224 ALOGE("%s: Stream %d: Error cancelling buffer to native window:"
Igor Murashkine3a9f962013-05-08 18:03:15 -0700225 " %s (%d)", __FUNCTION__, mId, strerror(-res), res);
Igor Murashkin5a1798a2013-05-07 10:58:13 -0700226 }
Zhijun He1ff811b2016-01-26 14:39:51 -0800227
228 if (mUseBufferManager) {
229 // Return this buffer back to buffer manager.
230 mBufferReleasedListener->onBufferReleased();
231 }
Igor Murashkin5a1798a2013-05-07 10:58:13 -0700232 } else {
Ruchit Sharmae0711f22014-08-18 13:48:24 -0400233 if (mTraceFirstBuffer && (stream_type == CAMERA3_STREAM_OUTPUT)) {
234 {
235 char traceLog[48];
236 snprintf(traceLog, sizeof(traceLog), "Stream %d: first full buffer\n", mId);
237 ATRACE_NAME(traceLog);
238 }
239 mTraceFirstBuffer = false;
240 }
241
Shuzhen Wang13a69632016-01-26 09:51:07 -0800242 /* Certain consumers (such as AudioSource or HardwareComposer) use
243 * MONOTONIC time, causing time misalignment if camera timestamp is
Shuzhen Wangc28dccc2016-02-11 23:48:46 -0800244 * in BOOTTIME. Do the conversion if necessary. */
245 res = native_window_set_buffers_timestamp(mConsumer.get(),
246 mUseMonoTimestamp ? timestamp - mTimestampOffset : timestamp);
247 if (res != OK) {
248 ALOGE("%s: Stream %d: Error setting timestamp: %s (%d)",
249 __FUNCTION__, mId, strerror(-res), res);
250 return res;
Yin-Chia Yeh4c9736f2015-03-05 15:01:36 -0800251 }
252
Zhijun He124ccf42013-05-22 14:01:30 -0700253 res = currentConsumer->queueBuffer(currentConsumer.get(),
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800254 container_of(buffer.buffer, ANativeWindowBuffer, handle),
255 anwReleaseFence);
256 if (res != OK) {
Igor Murashkine3a9f962013-05-08 18:03:15 -0700257 ALOGE("%s: Stream %d: Error queueing buffer to native window: "
258 "%s (%d)", __FUNCTION__, mId, strerror(-res), res);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800259 }
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800260 }
Zhijun He124ccf42013-05-22 14:01:30 -0700261 mLock.lock();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700262
263 // Once a valid buffer has been returned to the queue, can no longer
264 // dequeue all buffers for preallocation.
265 if (buffer.status != CAMERA3_BUFFER_STATUS_ERROR) {
266 mStreamUnpreparable = true;
267 }
268
Igor Murashkin5a1798a2013-05-07 10:58:13 -0700269 if (res != OK) {
270 close(anwReleaseFence);
Igor Murashkin5a1798a2013-05-07 10:58:13 -0700271 }
272
Igor Murashkine3a9f962013-05-08 18:03:15 -0700273 *releaseFenceOut = releaseFence;
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800274
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700275 return res;
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800276}
277
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800278void Camera3OutputStream::dump(int fd, const Vector<String16> &args) const {
279 (void) args;
280 String8 lines;
281 lines.appendFormat(" Stream[%d]: Output\n", mId);
Eino-Ville Talvala727d1722015-06-09 13:44:19 -0700282 lines.appendFormat(" Consumer name: %s\n", mConsumerName.string());
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800283 write(fd, lines.string(), lines.size());
Igor Murashkine3a9f962013-05-08 18:03:15 -0700284
285 Camera3IOStreamBase::dump(fd, args);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800286}
287
288status_t Camera3OutputStream::setTransform(int transform) {
289 ATRACE_CALL();
290 Mutex::Autolock l(mLock);
291 return setTransformLocked(transform);
292}
293
294status_t Camera3OutputStream::setTransformLocked(int transform) {
295 status_t res = OK;
296 if (mState == STATE_ERROR) {
297 ALOGE("%s: Stream in error state", __FUNCTION__);
298 return INVALID_OPERATION;
299 }
300
301 mTransform = transform;
302 if (mState == STATE_CONFIGURED) {
303 res = native_window_set_buffers_transform(mConsumer.get(),
304 transform);
305 if (res != OK) {
306 ALOGE("%s: Unable to configure stream transform to %x: %s (%d)",
307 __FUNCTION__, transform, strerror(-res), res);
308 }
309 }
310 return res;
311}
312
313status_t Camera3OutputStream::configureQueueLocked() {
314 status_t res;
315
Ruchit Sharmae0711f22014-08-18 13:48:24 -0400316 mTraceFirstBuffer = true;
Igor Murashkine3a9f962013-05-08 18:03:15 -0700317 if ((res = Camera3IOStreamBase::configureQueueLocked()) != OK) {
318 return res;
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800319 }
320
Igor Murashkine3a9f962013-05-08 18:03:15 -0700321 ALOG_ASSERT(mConsumer != 0, "mConsumer should never be NULL");
322
Zhijun He125684a2015-12-26 15:07:30 -0800323 // Configure consumer-side ANativeWindow interface. The listener may be used
324 // to notify buffer manager (if it is used) of the returned buffers.
325 res = mConsumer->connect(NATIVE_WINDOW_API_CAMERA, /*listener*/mBufferReleasedListener);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800326 if (res != OK) {
327 ALOGE("%s: Unable to connect to native window for stream %d",
328 __FUNCTION__, mId);
329 return res;
330 }
331
Eino-Ville Talvala727d1722015-06-09 13:44:19 -0700332 mConsumerName = mConsumer->getConsumerName();
333
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800334 res = native_window_set_usage(mConsumer.get(), camera3_stream::usage);
335 if (res != OK) {
336 ALOGE("%s: Unable to configure usage %08x for stream %d",
337 __FUNCTION__, camera3_stream::usage, mId);
338 return res;
339 }
340
341 res = native_window_set_scaling_mode(mConsumer.get(),
342 NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
343 if (res != OK) {
344 ALOGE("%s: Unable to configure stream scaling: %s (%d)",
345 __FUNCTION__, strerror(-res), res);
346 return res;
347 }
348
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800349 if (mMaxSize == 0) {
350 // For buffers of known size
Eino-Ville Talvala7d70c5e2014-07-24 18:10:23 -0700351 res = native_window_set_buffers_dimensions(mConsumer.get(),
352 camera3_stream::width, camera3_stream::height);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800353 } else {
354 // For buffers with bounded size
Eino-Ville Talvala7d70c5e2014-07-24 18:10:23 -0700355 res = native_window_set_buffers_dimensions(mConsumer.get(),
356 mMaxSize, 1);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800357 }
358 if (res != OK) {
Eino-Ville Talvala7d70c5e2014-07-24 18:10:23 -0700359 ALOGE("%s: Unable to configure stream buffer dimensions"
360 " %d x %d (maxSize %zu) for stream %d",
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800361 __FUNCTION__, camera3_stream::width, camera3_stream::height,
Eino-Ville Talvala7d70c5e2014-07-24 18:10:23 -0700362 mMaxSize, mId);
363 return res;
364 }
365 res = native_window_set_buffers_format(mConsumer.get(),
366 camera3_stream::format);
367 if (res != OK) {
368 ALOGE("%s: Unable to configure stream buffer format %#x for stream %d",
369 __FUNCTION__, camera3_stream::format, mId);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800370 return res;
371 }
372
Eino-Ville Talvala3d82c0d2015-02-23 15:19:19 -0800373 res = native_window_set_buffers_data_space(mConsumer.get(),
374 camera3_stream::data_space);
375 if (res != OK) {
376 ALOGE("%s: Unable to configure stream dataspace %#x for stream %d",
377 __FUNCTION__, camera3_stream::data_space, mId);
378 return res;
379 }
380
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800381 int maxConsumerBuffers;
Eino-Ville Talvala727d1722015-06-09 13:44:19 -0700382 res = static_cast<ANativeWindow*>(mConsumer.get())->query(
383 mConsumer.get(),
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800384 NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS, &maxConsumerBuffers);
385 if (res != OK) {
386 ALOGE("%s: Unable to query consumer undequeued"
387 " buffer count for stream %d", __FUNCTION__, mId);
388 return res;
389 }
390
Alex Ray20cb3002013-05-28 20:18:22 -0700391 ALOGV("%s: Consumer wants %d buffers, HAL wants %d", __FUNCTION__,
392 maxConsumerBuffers, camera3_stream::max_buffers);
393 if (camera3_stream::max_buffers == 0) {
Zhijun He2ab500c2013-07-23 08:02:53 -0700394 ALOGE("%s: Camera HAL requested max_buffer count: %d, requires at least 1",
Alex Ray20cb3002013-05-28 20:18:22 -0700395 __FUNCTION__, camera3_stream::max_buffers);
396 return INVALID_OPERATION;
397 }
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800398
399 mTotalBufferCount = maxConsumerBuffers + camera3_stream::max_buffers;
Zhijun He6adc9cc2014-04-15 14:09:55 -0700400 mHandoutTotalBufferCount = 0;
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800401 mFrameCount = 0;
402 mLastTimestamp = 0;
Shuzhen Wangc28dccc2016-02-11 23:48:46 -0800403 mUseMonoTimestamp = (isConsumedByHWComposer() | isVideoStream());
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800404
405 res = native_window_set_buffer_count(mConsumer.get(),
406 mTotalBufferCount);
407 if (res != OK) {
408 ALOGE("%s: Unable to set buffer count for stream %d",
409 __FUNCTION__, mId);
410 return res;
411 }
412
413 res = native_window_set_buffers_transform(mConsumer.get(),
414 mTransform);
415 if (res != OK) {
416 ALOGE("%s: Unable to configure stream transform to %x: %s (%d)",
417 __FUNCTION__, mTransform, strerror(-res), res);
418 }
419
Zhijun He125684a2015-12-26 15:07:30 -0800420 /**
Zhijun Heedd41ae2016-02-03 14:45:53 -0800421 * Camera3 Buffer manager is only supported by HAL3.3 onwards, as the older HALs requires
Zhijun He125684a2015-12-26 15:07:30 -0800422 * buffers to be statically allocated for internal static buffer registration, while the
423 * buffers provided by buffer manager are really dynamically allocated. Camera3Device only
Zhijun Heedd41ae2016-02-03 14:45:53 -0800424 * sets the mBufferManager if device version is > HAL3.2, which guarantees that the buffer
425 * manager setup is skipped in below code. Note that HAL3.2 is also excluded here, as some
426 * HAL3.2 devices may not support the dynamic buffer registeration.
Zhijun He125684a2015-12-26 15:07:30 -0800427 */
428 if (mBufferManager != 0 && mSetId > CAMERA3_STREAM_SET_ID_INVALID) {
429 StreamInfo streamInfo(
430 getId(), getStreamSetId(), getWidth(), getHeight(), getFormat(), getDataSpace(),
431 camera3_stream::usage, mTotalBufferCount, /*isConfigured*/true);
432 res = mBufferManager->registerStream(streamInfo);
433 if (res == OK) {
434 // Disable buffer allocation for this BufferQueue, buffer manager will take over
435 // the buffer allocation responsibility.
436 mConsumer->getIGraphicBufferProducer()->allowAllocation(false);
437 mUseBufferManager = true;
438 } else {
439 ALOGE("%s: Unable to register stream %d to camera3 buffer manager, "
440 "(error %d %s), fall back to BufferQueue for buffer management!",
441 __FUNCTION__, mId, res, strerror(-res));
442 }
443 }
444
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800445 return OK;
446}
447
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800448status_t Camera3OutputStream::disconnectLocked() {
449 status_t res;
450
Igor Murashkine3a9f962013-05-08 18:03:15 -0700451 if ((res = Camera3IOStreamBase::disconnectLocked()) != OK) {
452 return res;
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800453 }
454
Zhijun He125684a2015-12-26 15:07:30 -0800455 ALOGV("%s: disconnecting stream %d from native window", __FUNCTION__, getId());
456
Igor Murashkine3a9f962013-05-08 18:03:15 -0700457 res = native_window_api_disconnect(mConsumer.get(),
458 NATIVE_WINDOW_API_CAMERA);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800459
460 /**
461 * This is not an error. if client calling process dies, the window will
462 * also die and all calls to it will return DEAD_OBJECT, thus it's already
463 * "disconnected"
464 */
465 if (res == DEAD_OBJECT) {
466 ALOGW("%s: While disconnecting stream %d from native window, the"
467 " native window died from under us", __FUNCTION__, mId);
468 }
469 else if (res != OK) {
Igor Murashkine3a9f962013-05-08 18:03:15 -0700470 ALOGE("%s: Unable to disconnect stream %d from native window "
471 "(error %d %s)",
472 __FUNCTION__, mId, res, strerror(-res));
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800473 mState = STATE_ERROR;
474 return res;
475 }
476
Zhijun He125684a2015-12-26 15:07:30 -0800477 // Since device is already idle, there is no getBuffer call to buffer manager, unregister the
478 // stream at this point should be safe.
479 if (mUseBufferManager) {
480 res = mBufferManager->unregisterStream(getId(), getStreamSetId());
481 if (res != OK) {
482 ALOGE("%s: Unable to unregister stream %d from buffer manager "
483 "(error %d %s)", __FUNCTION__, mId, res, strerror(-res));
484 mState = STATE_ERROR;
485 return res;
486 }
487 // Note that, to make prepare/teardown case work, we must not mBufferManager.clear(), as
488 // the stream is still in usable state after this call.
489 mUseBufferManager = false;
490 }
491
Igor Murashkine3a9f962013-05-08 18:03:15 -0700492 mState = (mState == STATE_IN_RECONFIG) ? STATE_IN_CONFIG
493 : STATE_CONSTRUCTED;
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800494 return OK;
495}
496
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700497status_t Camera3OutputStream::getEndpointUsage(uint32_t *usage) const {
Eino-Ville Talvalab2f5b192013-07-30 14:36:03 -0700498
499 status_t res;
500 int32_t u = 0;
Eino-Ville Talvala727d1722015-06-09 13:44:19 -0700501 res = static_cast<ANativeWindow*>(mConsumer.get())->query(mConsumer.get(),
Eino-Ville Talvalab2f5b192013-07-30 14:36:03 -0700502 NATIVE_WINDOW_CONSUMER_USAGE_BITS, &u);
Eino-Ville Talvalab2f5b192013-07-30 14:36:03 -0700503
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700504 // If an opaque output stream's endpoint is ImageReader, add
505 // GRALLOC_USAGE_HW_CAMERA_ZSL to the usage so HAL knows it will be used
506 // for the ZSL use case.
507 // Assume it's for ImageReader if the consumer usage doesn't have any of these bits set:
508 // 1. GRALLOC_USAGE_HW_TEXTURE
509 // 2. GRALLOC_USAGE_HW_RENDER
510 // 3. GRALLOC_USAGE_HW_COMPOSER
511 // 4. GRALLOC_USAGE_HW_VIDEO_ENCODER
512 if (camera3_stream::format == HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED &&
513 (u & (GRALLOC_USAGE_HW_TEXTURE | GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_HW_COMPOSER |
514 GRALLOC_USAGE_HW_VIDEO_ENCODER)) == 0) {
515 u |= GRALLOC_USAGE_HW_CAMERA_ZSL;
516 }
517
518 *usage = u;
Eino-Ville Talvalab2f5b192013-07-30 14:36:03 -0700519 return res;
520}
521
Chien-Yu Chen85a64552015-08-28 15:46:12 -0700522bool Camera3OutputStream::isVideoStream() const {
523 uint32_t usage = 0;
524 status_t res = getEndpointUsage(&usage);
525 if (res != OK) {
526 ALOGE("%s: getting end point usage failed: %s (%d).", __FUNCTION__, strerror(-res), res);
527 return false;
528 }
529
530 return (usage & GRALLOC_USAGE_HW_VIDEO_ENCODER) != 0;
531}
532
Zhijun He125684a2015-12-26 15:07:30 -0800533status_t Camera3OutputStream::setBufferManager(sp<Camera3BufferManager> bufferManager) {
534 Mutex::Autolock l(mLock);
535 if (mState != STATE_CONSTRUCTED) {
536 ALOGE("%s: this method can only be called when stream in in CONSTRUCTED state.",
537 __FUNCTION__);
538 return INVALID_OPERATION;
539 }
540 mBufferManager = bufferManager;
541
542 return OK;
543}
544
545void Camera3OutputStream::BufferReleasedListener::onBufferReleased() {
546 sp<Camera3OutputStream> stream = mParent.promote();
547 if (stream == nullptr) {
548 ALOGV("%s: Parent camera3 output stream was destroyed", __FUNCTION__);
549 return;
550 }
551
552 Mutex::Autolock l(stream->mLock);
553 if (!(stream->mUseBufferManager)) {
554 return;
555 }
556
557 sp<Fence> fence;
558 sp<GraphicBuffer> buffer;
559 int fenceFd = -1;
560 status_t res = stream->mConsumer->detachNextBuffer(&buffer, &fence);
561 if (res == NO_MEMORY) {
562 // This may rarely happen, which indicates that the released buffer was freed by other
563 // call (e.g., attachBuffer, dequeueBuffer etc.) before reaching here. We should notify the
564 // buffer manager that this buffer has been freed. It's not fatal, but should be avoided,
565 // therefore log a warning.
566 buffer = 0;
567 ALOGW("%s: the released buffer has already been freed by the buffer queue!", __FUNCTION__);
568 } else if (res != OK) {
569 // Other errors are fatal.
570 ALOGE("%s: detach next buffer failed: %s (%d).", __FUNCTION__, strerror(-res), res);
571 stream->mState = STATE_ERROR;
572 return;
573 }
574
575 if (fence!= 0 && fence->isValid()) {
576 fenceFd = fence->dup();
577 }
578 res = stream->mBufferManager->returnBufferForStream(stream->getId(), stream->getStreamSetId(),
579 buffer, fenceFd);
580 if (res != OK) {
581 ALOGE("%s: return buffer to buffer manager failed: %s (%d).", __FUNCTION__,
582 strerror(-res), res);
583 stream->mState = STATE_ERROR;
584 }
585}
Shuzhen Wang13a69632016-01-26 09:51:07 -0800586
587bool Camera3OutputStream::isConsumedByHWComposer() const {
588 uint32_t usage = 0;
589 status_t res = getEndpointUsage(&usage);
590 if (res != OK) {
591 ALOGE("%s: getting end point usage failed: %s (%d).", __FUNCTION__, strerror(-res), res);
592 return false;
593 }
594
595 return (usage & GRALLOC_USAGE_HW_COMPOSER) != 0;
596}
597
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800598}; // namespace camera3
599
600}; // namespace android