blob: d2b98e61f11f6d14ef0dc4f7f61e47e2b2d513c5 [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);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -0700160
161 // Only transition to STATE_ABANDONED from STATE_CONFIGURED. (If it is STATE_PREPARING,
162 // let prepareNextBuffer handle the error.)
163 if (res == NO_INIT && mState == STATE_CONFIGURED) {
164 mState = STATE_ABANDONED;
165 }
166
Zhijun He125684a2015-12-26 15:07:30 -0800167 return res;
168 }
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800169 }
170
Igor Murashkine3a9f962013-05-08 18:03:15 -0700171 /**
172 * FenceFD now owned by HAL except in case of error,
173 * in which case we reassign it to acquire_fence
174 */
175 handoutBufferLocked(*buffer, &(anb->handle), /*acquireFence*/fenceFd,
Zhijun He6adc9cc2014-04-15 14:09:55 -0700176 /*releaseFence*/-1, CAMERA3_BUFFER_STATUS_OK, /*output*/true);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800177
178 return OK;
179}
180
181status_t Camera3OutputStream::returnBufferLocked(
182 const camera3_stream_buffer &buffer,
183 nsecs_t timestamp) {
184 ATRACE_CALL();
Igor Murashkine3a9f962013-05-08 18:03:15 -0700185
186 status_t res = returnAnyBufferLocked(buffer, timestamp, /*output*/true);
187
188 if (res != OK) {
189 return res;
190 }
191
192 mLastTimestamp = timestamp;
193
194 return OK;
195}
196
197status_t Camera3OutputStream::returnBufferCheckedLocked(
198 const camera3_stream_buffer &buffer,
199 nsecs_t timestamp,
200 bool output,
201 /*out*/
202 sp<Fence> *releaseFenceOut) {
203
204 (void)output;
205 ALOG_ASSERT(output, "Expected output to be true");
206
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800207 status_t res;
Igor Murashkin5a1798a2013-05-07 10:58:13 -0700208
Yin-Chia Yeh4c9736f2015-03-05 15:01:36 -0800209 // Fence management - always honor release fence from HAL
210 sp<Fence> releaseFence = new Fence(buffer.release_fence);
Igor Murashkin5a1798a2013-05-07 10:58:13 -0700211 int anwReleaseFence = releaseFence->dup();
212
213 /**
Zhijun He124ccf42013-05-22 14:01:30 -0700214 * Release the lock briefly to avoid deadlock with
215 * StreamingProcessor::startStream -> Camera3Stream::isConfiguring (this
216 * thread will go into StreamingProcessor::onFrameAvailable) during
217 * queueBuffer
218 */
219 sp<ANativeWindow> currentConsumer = mConsumer;
220 mLock.unlock();
221
222 /**
Igor Murashkin5a1798a2013-05-07 10:58:13 -0700223 * Return buffer back to ANativeWindow
224 */
225 if (buffer.status == CAMERA3_BUFFER_STATUS_ERROR) {
226 // Cancel buffer
Zhijun He124ccf42013-05-22 14:01:30 -0700227 res = currentConsumer->cancelBuffer(currentConsumer.get(),
Igor Murashkin5a1798a2013-05-07 10:58:13 -0700228 container_of(buffer.buffer, ANativeWindowBuffer, handle),
229 anwReleaseFence);
230 if (res != OK) {
231 ALOGE("%s: Stream %d: Error cancelling buffer to native window:"
Igor Murashkine3a9f962013-05-08 18:03:15 -0700232 " %s (%d)", __FUNCTION__, mId, strerror(-res), res);
Igor Murashkin5a1798a2013-05-07 10:58:13 -0700233 }
Zhijun He1ff811b2016-01-26 14:39:51 -0800234
235 if (mUseBufferManager) {
236 // Return this buffer back to buffer manager.
237 mBufferReleasedListener->onBufferReleased();
238 }
Igor Murashkin5a1798a2013-05-07 10:58:13 -0700239 } else {
Ruchit Sharmae0711f22014-08-18 13:48:24 -0400240 if (mTraceFirstBuffer && (stream_type == CAMERA3_STREAM_OUTPUT)) {
241 {
242 char traceLog[48];
243 snprintf(traceLog, sizeof(traceLog), "Stream %d: first full buffer\n", mId);
244 ATRACE_NAME(traceLog);
245 }
246 mTraceFirstBuffer = false;
247 }
248
Shuzhen Wang13a69632016-01-26 09:51:07 -0800249 /* Certain consumers (such as AudioSource or HardwareComposer) use
250 * MONOTONIC time, causing time misalignment if camera timestamp is
Shuzhen Wangc28dccc2016-02-11 23:48:46 -0800251 * in BOOTTIME. Do the conversion if necessary. */
252 res = native_window_set_buffers_timestamp(mConsumer.get(),
253 mUseMonoTimestamp ? timestamp - mTimestampOffset : timestamp);
254 if (res != OK) {
255 ALOGE("%s: Stream %d: Error setting timestamp: %s (%d)",
256 __FUNCTION__, mId, strerror(-res), res);
257 return res;
Yin-Chia Yeh4c9736f2015-03-05 15:01:36 -0800258 }
259
Zhijun He124ccf42013-05-22 14:01:30 -0700260 res = currentConsumer->queueBuffer(currentConsumer.get(),
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800261 container_of(buffer.buffer, ANativeWindowBuffer, handle),
262 anwReleaseFence);
263 if (res != OK) {
Igor Murashkine3a9f962013-05-08 18:03:15 -0700264 ALOGE("%s: Stream %d: Error queueing buffer to native window: "
265 "%s (%d)", __FUNCTION__, mId, strerror(-res), res);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800266 }
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800267 }
Zhijun He124ccf42013-05-22 14:01:30 -0700268 mLock.lock();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700269
270 // Once a valid buffer has been returned to the queue, can no longer
271 // dequeue all buffers for preallocation.
272 if (buffer.status != CAMERA3_BUFFER_STATUS_ERROR) {
273 mStreamUnpreparable = true;
274 }
275
Igor Murashkin5a1798a2013-05-07 10:58:13 -0700276 if (res != OK) {
277 close(anwReleaseFence);
Igor Murashkin5a1798a2013-05-07 10:58:13 -0700278 }
279
Igor Murashkine3a9f962013-05-08 18:03:15 -0700280 *releaseFenceOut = releaseFence;
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800281
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700282 return res;
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800283}
284
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800285void Camera3OutputStream::dump(int fd, const Vector<String16> &args) const {
286 (void) args;
287 String8 lines;
288 lines.appendFormat(" Stream[%d]: Output\n", mId);
Eino-Ville Talvala727d1722015-06-09 13:44:19 -0700289 lines.appendFormat(" Consumer name: %s\n", mConsumerName.string());
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800290 write(fd, lines.string(), lines.size());
Igor Murashkine3a9f962013-05-08 18:03:15 -0700291
292 Camera3IOStreamBase::dump(fd, args);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800293}
294
295status_t Camera3OutputStream::setTransform(int transform) {
296 ATRACE_CALL();
297 Mutex::Autolock l(mLock);
298 return setTransformLocked(transform);
299}
300
301status_t Camera3OutputStream::setTransformLocked(int transform) {
302 status_t res = OK;
303 if (mState == STATE_ERROR) {
304 ALOGE("%s: Stream in error state", __FUNCTION__);
305 return INVALID_OPERATION;
306 }
307
308 mTransform = transform;
309 if (mState == STATE_CONFIGURED) {
310 res = native_window_set_buffers_transform(mConsumer.get(),
311 transform);
312 if (res != OK) {
313 ALOGE("%s: Unable to configure stream transform to %x: %s (%d)",
314 __FUNCTION__, transform, strerror(-res), res);
315 }
316 }
317 return res;
318}
319
320status_t Camera3OutputStream::configureQueueLocked() {
321 status_t res;
322
Ruchit Sharmae0711f22014-08-18 13:48:24 -0400323 mTraceFirstBuffer = true;
Igor Murashkine3a9f962013-05-08 18:03:15 -0700324 if ((res = Camera3IOStreamBase::configureQueueLocked()) != OK) {
325 return res;
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800326 }
327
Igor Murashkine3a9f962013-05-08 18:03:15 -0700328 ALOG_ASSERT(mConsumer != 0, "mConsumer should never be NULL");
329
Zhijun He125684a2015-12-26 15:07:30 -0800330 // Configure consumer-side ANativeWindow interface. The listener may be used
331 // to notify buffer manager (if it is used) of the returned buffers.
332 res = mConsumer->connect(NATIVE_WINDOW_API_CAMERA, /*listener*/mBufferReleasedListener);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800333 if (res != OK) {
334 ALOGE("%s: Unable to connect to native window for stream %d",
335 __FUNCTION__, mId);
336 return res;
337 }
338
Eino-Ville Talvala727d1722015-06-09 13:44:19 -0700339 mConsumerName = mConsumer->getConsumerName();
340
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800341 res = native_window_set_usage(mConsumer.get(), camera3_stream::usage);
342 if (res != OK) {
343 ALOGE("%s: Unable to configure usage %08x for stream %d",
344 __FUNCTION__, camera3_stream::usage, mId);
345 return res;
346 }
347
348 res = native_window_set_scaling_mode(mConsumer.get(),
349 NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
350 if (res != OK) {
351 ALOGE("%s: Unable to configure stream scaling: %s (%d)",
352 __FUNCTION__, strerror(-res), res);
353 return res;
354 }
355
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800356 if (mMaxSize == 0) {
357 // For buffers of known size
Eino-Ville Talvala7d70c5e2014-07-24 18:10:23 -0700358 res = native_window_set_buffers_dimensions(mConsumer.get(),
359 camera3_stream::width, camera3_stream::height);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800360 } else {
361 // For buffers with bounded size
Eino-Ville Talvala7d70c5e2014-07-24 18:10:23 -0700362 res = native_window_set_buffers_dimensions(mConsumer.get(),
363 mMaxSize, 1);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800364 }
365 if (res != OK) {
Eino-Ville Talvala7d70c5e2014-07-24 18:10:23 -0700366 ALOGE("%s: Unable to configure stream buffer dimensions"
367 " %d x %d (maxSize %zu) for stream %d",
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800368 __FUNCTION__, camera3_stream::width, camera3_stream::height,
Eino-Ville Talvala7d70c5e2014-07-24 18:10:23 -0700369 mMaxSize, mId);
370 return res;
371 }
372 res = native_window_set_buffers_format(mConsumer.get(),
373 camera3_stream::format);
374 if (res != OK) {
375 ALOGE("%s: Unable to configure stream buffer format %#x for stream %d",
376 __FUNCTION__, camera3_stream::format, mId);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800377 return res;
378 }
379
Eino-Ville Talvala3d82c0d2015-02-23 15:19:19 -0800380 res = native_window_set_buffers_data_space(mConsumer.get(),
381 camera3_stream::data_space);
382 if (res != OK) {
383 ALOGE("%s: Unable to configure stream dataspace %#x for stream %d",
384 __FUNCTION__, camera3_stream::data_space, mId);
385 return res;
386 }
387
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800388 int maxConsumerBuffers;
Eino-Ville Talvala727d1722015-06-09 13:44:19 -0700389 res = static_cast<ANativeWindow*>(mConsumer.get())->query(
390 mConsumer.get(),
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800391 NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS, &maxConsumerBuffers);
392 if (res != OK) {
393 ALOGE("%s: Unable to query consumer undequeued"
394 " buffer count for stream %d", __FUNCTION__, mId);
395 return res;
396 }
397
Alex Ray20cb3002013-05-28 20:18:22 -0700398 ALOGV("%s: Consumer wants %d buffers, HAL wants %d", __FUNCTION__,
399 maxConsumerBuffers, camera3_stream::max_buffers);
400 if (camera3_stream::max_buffers == 0) {
Zhijun He2ab500c2013-07-23 08:02:53 -0700401 ALOGE("%s: Camera HAL requested max_buffer count: %d, requires at least 1",
Alex Ray20cb3002013-05-28 20:18:22 -0700402 __FUNCTION__, camera3_stream::max_buffers);
403 return INVALID_OPERATION;
404 }
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800405
406 mTotalBufferCount = maxConsumerBuffers + camera3_stream::max_buffers;
Zhijun He6adc9cc2014-04-15 14:09:55 -0700407 mHandoutTotalBufferCount = 0;
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800408 mFrameCount = 0;
409 mLastTimestamp = 0;
Shuzhen Wangc28dccc2016-02-11 23:48:46 -0800410 mUseMonoTimestamp = (isConsumedByHWComposer() | isVideoStream());
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800411
412 res = native_window_set_buffer_count(mConsumer.get(),
413 mTotalBufferCount);
414 if (res != OK) {
415 ALOGE("%s: Unable to set buffer count for stream %d",
416 __FUNCTION__, mId);
417 return res;
418 }
419
420 res = native_window_set_buffers_transform(mConsumer.get(),
421 mTransform);
422 if (res != OK) {
423 ALOGE("%s: Unable to configure stream transform to %x: %s (%d)",
424 __FUNCTION__, mTransform, strerror(-res), res);
425 }
426
Zhijun He125684a2015-12-26 15:07:30 -0800427 /**
Zhijun Heedd41ae2016-02-03 14:45:53 -0800428 * Camera3 Buffer manager is only supported by HAL3.3 onwards, as the older HALs requires
Zhijun He125684a2015-12-26 15:07:30 -0800429 * buffers to be statically allocated for internal static buffer registration, while the
430 * buffers provided by buffer manager are really dynamically allocated. Camera3Device only
Zhijun Heedd41ae2016-02-03 14:45:53 -0800431 * sets the mBufferManager if device version is > HAL3.2, which guarantees that the buffer
432 * manager setup is skipped in below code. Note that HAL3.2 is also excluded here, as some
433 * HAL3.2 devices may not support the dynamic buffer registeration.
Zhijun He125684a2015-12-26 15:07:30 -0800434 */
435 if (mBufferManager != 0 && mSetId > CAMERA3_STREAM_SET_ID_INVALID) {
436 StreamInfo streamInfo(
437 getId(), getStreamSetId(), getWidth(), getHeight(), getFormat(), getDataSpace(),
438 camera3_stream::usage, mTotalBufferCount, /*isConfigured*/true);
439 res = mBufferManager->registerStream(streamInfo);
440 if (res == OK) {
441 // Disable buffer allocation for this BufferQueue, buffer manager will take over
442 // the buffer allocation responsibility.
443 mConsumer->getIGraphicBufferProducer()->allowAllocation(false);
444 mUseBufferManager = true;
445 } else {
446 ALOGE("%s: Unable to register stream %d to camera3 buffer manager, "
447 "(error %d %s), fall back to BufferQueue for buffer management!",
448 __FUNCTION__, mId, res, strerror(-res));
449 }
450 }
451
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800452 return OK;
453}
454
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800455status_t Camera3OutputStream::disconnectLocked() {
456 status_t res;
457
Igor Murashkine3a9f962013-05-08 18:03:15 -0700458 if ((res = Camera3IOStreamBase::disconnectLocked()) != OK) {
459 return res;
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800460 }
461
Zhijun He125684a2015-12-26 15:07:30 -0800462 ALOGV("%s: disconnecting stream %d from native window", __FUNCTION__, getId());
463
Igor Murashkine3a9f962013-05-08 18:03:15 -0700464 res = native_window_api_disconnect(mConsumer.get(),
465 NATIVE_WINDOW_API_CAMERA);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800466
467 /**
468 * This is not an error. if client calling process dies, the window will
469 * also die and all calls to it will return DEAD_OBJECT, thus it's already
470 * "disconnected"
471 */
472 if (res == DEAD_OBJECT) {
473 ALOGW("%s: While disconnecting stream %d from native window, the"
474 " native window died from under us", __FUNCTION__, mId);
475 }
476 else if (res != OK) {
Igor Murashkine3a9f962013-05-08 18:03:15 -0700477 ALOGE("%s: Unable to disconnect stream %d from native window "
478 "(error %d %s)",
479 __FUNCTION__, mId, res, strerror(-res));
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800480 mState = STATE_ERROR;
481 return res;
482 }
483
Zhijun He125684a2015-12-26 15:07:30 -0800484 // Since device is already idle, there is no getBuffer call to buffer manager, unregister the
485 // stream at this point should be safe.
486 if (mUseBufferManager) {
487 res = mBufferManager->unregisterStream(getId(), getStreamSetId());
488 if (res != OK) {
489 ALOGE("%s: Unable to unregister stream %d from buffer manager "
490 "(error %d %s)", __FUNCTION__, mId, res, strerror(-res));
491 mState = STATE_ERROR;
492 return res;
493 }
494 // Note that, to make prepare/teardown case work, we must not mBufferManager.clear(), as
495 // the stream is still in usable state after this call.
496 mUseBufferManager = false;
497 }
498
Igor Murashkine3a9f962013-05-08 18:03:15 -0700499 mState = (mState == STATE_IN_RECONFIG) ? STATE_IN_CONFIG
500 : STATE_CONSTRUCTED;
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800501 return OK;
502}
503
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700504status_t Camera3OutputStream::getEndpointUsage(uint32_t *usage) const {
Eino-Ville Talvalab2f5b192013-07-30 14:36:03 -0700505
506 status_t res;
507 int32_t u = 0;
Eino-Ville Talvala727d1722015-06-09 13:44:19 -0700508 res = static_cast<ANativeWindow*>(mConsumer.get())->query(mConsumer.get(),
Eino-Ville Talvalab2f5b192013-07-30 14:36:03 -0700509 NATIVE_WINDOW_CONSUMER_USAGE_BITS, &u);
Eino-Ville Talvalab2f5b192013-07-30 14:36:03 -0700510
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700511 // If an opaque output stream's endpoint is ImageReader, add
512 // GRALLOC_USAGE_HW_CAMERA_ZSL to the usage so HAL knows it will be used
513 // for the ZSL use case.
514 // Assume it's for ImageReader if the consumer usage doesn't have any of these bits set:
515 // 1. GRALLOC_USAGE_HW_TEXTURE
516 // 2. GRALLOC_USAGE_HW_RENDER
517 // 3. GRALLOC_USAGE_HW_COMPOSER
518 // 4. GRALLOC_USAGE_HW_VIDEO_ENCODER
519 if (camera3_stream::format == HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED &&
520 (u & (GRALLOC_USAGE_HW_TEXTURE | GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_HW_COMPOSER |
521 GRALLOC_USAGE_HW_VIDEO_ENCODER)) == 0) {
522 u |= GRALLOC_USAGE_HW_CAMERA_ZSL;
523 }
524
525 *usage = u;
Eino-Ville Talvalab2f5b192013-07-30 14:36:03 -0700526 return res;
527}
528
Chien-Yu Chen85a64552015-08-28 15:46:12 -0700529bool Camera3OutputStream::isVideoStream() const {
530 uint32_t usage = 0;
531 status_t res = getEndpointUsage(&usage);
532 if (res != OK) {
533 ALOGE("%s: getting end point usage failed: %s (%d).", __FUNCTION__, strerror(-res), res);
534 return false;
535 }
536
537 return (usage & GRALLOC_USAGE_HW_VIDEO_ENCODER) != 0;
538}
539
Zhijun He125684a2015-12-26 15:07:30 -0800540status_t Camera3OutputStream::setBufferManager(sp<Camera3BufferManager> bufferManager) {
541 Mutex::Autolock l(mLock);
542 if (mState != STATE_CONSTRUCTED) {
543 ALOGE("%s: this method can only be called when stream in in CONSTRUCTED state.",
544 __FUNCTION__);
545 return INVALID_OPERATION;
546 }
547 mBufferManager = bufferManager;
548
549 return OK;
550}
551
552void Camera3OutputStream::BufferReleasedListener::onBufferReleased() {
553 sp<Camera3OutputStream> stream = mParent.promote();
554 if (stream == nullptr) {
555 ALOGV("%s: Parent camera3 output stream was destroyed", __FUNCTION__);
556 return;
557 }
558
559 Mutex::Autolock l(stream->mLock);
560 if (!(stream->mUseBufferManager)) {
561 return;
562 }
563
564 sp<Fence> fence;
565 sp<GraphicBuffer> buffer;
566 int fenceFd = -1;
567 status_t res = stream->mConsumer->detachNextBuffer(&buffer, &fence);
568 if (res == NO_MEMORY) {
569 // This may rarely happen, which indicates that the released buffer was freed by other
570 // call (e.g., attachBuffer, dequeueBuffer etc.) before reaching here. We should notify the
571 // buffer manager that this buffer has been freed. It's not fatal, but should be avoided,
572 // therefore log a warning.
573 buffer = 0;
574 ALOGW("%s: the released buffer has already been freed by the buffer queue!", __FUNCTION__);
575 } else if (res != OK) {
576 // Other errors are fatal.
577 ALOGE("%s: detach next buffer failed: %s (%d).", __FUNCTION__, strerror(-res), res);
578 stream->mState = STATE_ERROR;
579 return;
580 }
581
582 if (fence!= 0 && fence->isValid()) {
583 fenceFd = fence->dup();
584 }
585 res = stream->mBufferManager->returnBufferForStream(stream->getId(), stream->getStreamSetId(),
586 buffer, fenceFd);
587 if (res != OK) {
588 ALOGE("%s: return buffer to buffer manager failed: %s (%d).", __FUNCTION__,
589 strerror(-res), res);
590 stream->mState = STATE_ERROR;
591 }
592}
Shuzhen Wang13a69632016-01-26 09:51:07 -0800593
594bool Camera3OutputStream::isConsumedByHWComposer() const {
595 uint32_t usage = 0;
596 status_t res = getEndpointUsage(&usage);
597 if (res != OK) {
598 ALOGE("%s: getting end point usage failed: %s (%d).", __FUNCTION__, strerror(-res), res);
599 return false;
600 }
601
602 return (usage & GRALLOC_USAGE_HW_COMPOSER) != 0;
603}
604
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800605}; // namespace camera3
606
607}; // namespace android