blob: 329172a4cf54e616aebad29a6369c9ee42b2612a [file] [log] [blame]
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -08001/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "Camera3-OutputStream"
18#define ATRACE_TAG ATRACE_TAG_CAMERA
19//#define LOG_NDEBUG 0
20
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -080021#include <utils/Log.h>
22#include <utils/Trace.h>
23#include "Camera3OutputStream.h"
24
25#ifndef container_of
26#define container_of(ptr, type, member) \
27 (type *)((char*)(ptr) - offsetof(type, member))
28#endif
29
30namespace android {
31
32namespace camera3 {
33
34Camera3OutputStream::Camera3OutputStream(int id,
Eino-Ville Talvala727d1722015-06-09 13:44:19 -070035 sp<Surface> consumer,
Eino-Ville Talvala3d82c0d2015-02-23 15:19:19 -080036 uint32_t width, uint32_t height, int format,
Shuzhen Wangc28dccc2016-02-11 23:48:46 -080037 android_dataspace dataSpace, camera3_stream_rotation_t rotation,
38 nsecs_t timestampOffset, int setId) :
Igor Murashkine3a9f962013-05-08 18:03:15 -070039 Camera3IOStreamBase(id, CAMERA3_STREAM_OUTPUT, width, height,
Zhijun He125684a2015-12-26 15:07:30 -080040 /*maxSize*/0, format, dataSpace, rotation, setId),
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -080041 mConsumer(consumer),
Ruchit Sharmae0711f22014-08-18 13:48:24 -040042 mTransform(0),
Zhijun He125684a2015-12-26 15:07:30 -080043 mTraceFirstBuffer(true),
Shuzhen Wangc28dccc2016-02-11 23:48:46 -080044 mUseBufferManager(false),
Zhijun He5d677d12016-05-29 16:52:39 -070045 mTimestampOffset(timestampOffset),
Shuzhen Wang686f6442017-06-20 16:16:04 -070046 mConsumerUsage(0),
47 mDequeueBufferLatency(kDequeueLatencyBinSize) {
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -080048
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -080049 if (mConsumer == NULL) {
50 ALOGE("%s: Consumer is NULL!", __FUNCTION__);
51 mState = STATE_ERROR;
52 }
Zhijun He125684a2015-12-26 15:07:30 -080053
54 if (setId > CAMERA3_STREAM_SET_ID_INVALID) {
55 mBufferReleasedListener = new BufferReleasedListener(this);
56 }
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -080057}
58
59Camera3OutputStream::Camera3OutputStream(int id,
Eino-Ville Talvala727d1722015-06-09 13:44:19 -070060 sp<Surface> consumer,
Eino-Ville Talvala3d82c0d2015-02-23 15:19:19 -080061 uint32_t width, uint32_t height, size_t maxSize, int format,
Shuzhen Wangc28dccc2016-02-11 23:48:46 -080062 android_dataspace dataSpace, camera3_stream_rotation_t rotation,
63 nsecs_t timestampOffset, int setId) :
Igor Murashkine3a9f962013-05-08 18:03:15 -070064 Camera3IOStreamBase(id, CAMERA3_STREAM_OUTPUT, width, height, maxSize,
Zhijun He125684a2015-12-26 15:07:30 -080065 format, dataSpace, rotation, setId),
Igor Murashkina55b5452013-04-02 16:36:33 -070066 mConsumer(consumer),
Ruchit Sharmae0711f22014-08-18 13:48:24 -040067 mTransform(0),
Zhijun He125684a2015-12-26 15:07:30 -080068 mTraceFirstBuffer(true),
Shuzhen Wangc28dccc2016-02-11 23:48:46 -080069 mUseMonoTimestamp(false),
70 mUseBufferManager(false),
Zhijun He5d677d12016-05-29 16:52:39 -070071 mTimestampOffset(timestampOffset),
Shuzhen Wang686f6442017-06-20 16:16:04 -070072 mConsumerUsage(0),
73 mDequeueBufferLatency(kDequeueLatencyBinSize) {
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -080074
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -080075 if (format != HAL_PIXEL_FORMAT_BLOB && format != HAL_PIXEL_FORMAT_RAW_OPAQUE) {
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -080076 ALOGE("%s: Bad format for size-only stream: %d", __FUNCTION__,
77 format);
78 mState = STATE_ERROR;
79 }
80
81 if (mConsumer == NULL) {
82 ALOGE("%s: Consumer is NULL!", __FUNCTION__);
83 mState = STATE_ERROR;
84 }
Zhijun He125684a2015-12-26 15:07:30 -080085
86 if (setId > CAMERA3_STREAM_SET_ID_INVALID) {
87 mBufferReleasedListener = new BufferReleasedListener(this);
88 }
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -080089}
90
Zhijun He5d677d12016-05-29 16:52:39 -070091Camera3OutputStream::Camera3OutputStream(int id,
92 uint32_t width, uint32_t height, int format,
Emilian Peev050f5dc2017-05-18 14:43:56 +010093 uint64_t consumerUsage, android_dataspace dataSpace,
Zhijun He5d677d12016-05-29 16:52:39 -070094 camera3_stream_rotation_t rotation, nsecs_t timestampOffset, int setId) :
95 Camera3IOStreamBase(id, CAMERA3_STREAM_OUTPUT, width, height,
96 /*maxSize*/0, format, dataSpace, rotation, setId),
97 mConsumer(nullptr),
98 mTransform(0),
99 mTraceFirstBuffer(true),
100 mUseBufferManager(false),
101 mTimestampOffset(timestampOffset),
Shuzhen Wang686f6442017-06-20 16:16:04 -0700102 mConsumerUsage(consumerUsage),
103 mDequeueBufferLatency(kDequeueLatencyBinSize) {
Zhijun He5d677d12016-05-29 16:52:39 -0700104 // Deferred consumer only support preview surface format now.
105 if (format != HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED) {
106 ALOGE("%s: Deferred consumer only supports IMPLEMENTATION_DEFINED format now!",
107 __FUNCTION__);
108 mState = STATE_ERROR;
109 }
110
111 // Sanity check for the consumer usage flag.
112 if ((consumerUsage & GraphicBuffer::USAGE_HW_TEXTURE) == 0 &&
113 (consumerUsage & GraphicBuffer::USAGE_HW_COMPOSER) == 0) {
Emilian Peev050f5dc2017-05-18 14:43:56 +0100114 ALOGE("%s: Deferred consumer usage flag is illegal %" PRIu64 "!",
115 __FUNCTION__, consumerUsage);
Zhijun He5d677d12016-05-29 16:52:39 -0700116 mState = STATE_ERROR;
117 }
118
119 mConsumerName = String8("Deferred");
120 if (setId > CAMERA3_STREAM_SET_ID_INVALID) {
121 mBufferReleasedListener = new BufferReleasedListener(this);
122 }
123
124}
125
Igor Murashkine3a9f962013-05-08 18:03:15 -0700126Camera3OutputStream::Camera3OutputStream(int id, camera3_stream_type_t type,
127 uint32_t width, uint32_t height,
Eino-Ville Talvala3d82c0d2015-02-23 15:19:19 -0800128 int format,
Yin-Chia Yehb97babb2015-03-12 13:42:44 -0700129 android_dataspace dataSpace,
Zhijun He125684a2015-12-26 15:07:30 -0800130 camera3_stream_rotation_t rotation,
Emilian Peev050f5dc2017-05-18 14:43:56 +0100131 uint64_t consumerUsage, nsecs_t timestampOffset,
Zhijun He125684a2015-12-26 15:07:30 -0800132 int setId) :
Igor Murashkine3a9f962013-05-08 18:03:15 -0700133 Camera3IOStreamBase(id, type, width, height,
134 /*maxSize*/0,
Zhijun He125684a2015-12-26 15:07:30 -0800135 format, dataSpace, rotation, setId),
136 mTransform(0),
137 mTraceFirstBuffer(true),
Shuzhen Wangc28dccc2016-02-11 23:48:46 -0800138 mUseMonoTimestamp(false),
Zhijun He5d677d12016-05-29 16:52:39 -0700139 mUseBufferManager(false),
Shuzhen Wang0129d522016-10-30 22:43:41 -0700140 mTimestampOffset(timestampOffset),
Shuzhen Wang686f6442017-06-20 16:16:04 -0700141 mConsumerUsage(consumerUsage),
142 mDequeueBufferLatency(kDequeueLatencyBinSize) {
Zhijun He125684a2015-12-26 15:07:30 -0800143
144 if (setId > CAMERA3_STREAM_SET_ID_INVALID) {
145 mBufferReleasedListener = new BufferReleasedListener(this);
146 }
Igor Murashkine3a9f962013-05-08 18:03:15 -0700147
148 // Subclasses expected to initialize mConsumer themselves
149}
150
151
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800152Camera3OutputStream::~Camera3OutputStream() {
153 disconnectLocked();
154}
155
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800156status_t Camera3OutputStream::getBufferLocked(camera3_stream_buffer *buffer,
157 const std::vector<size_t>&) {
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800158 ATRACE_CALL();
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800159
160 ANativeWindowBuffer* anb;
Zhijun He125684a2015-12-26 15:07:30 -0800161 int fenceFd = -1;
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -0700162
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800163 status_t res;
164 res = getBufferLockedCommon(&anb, &fenceFd);
165 if (res != OK) {
166 return res;
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800167 }
168
Igor Murashkine3a9f962013-05-08 18:03:15 -0700169 /**
170 * FenceFD now owned by HAL except in case of error,
171 * in which case we reassign it to acquire_fence
172 */
173 handoutBufferLocked(*buffer, &(anb->handle), /*acquireFence*/fenceFd,
Zhijun He6adc9cc2014-04-15 14:09:55 -0700174 /*releaseFence*/-1, CAMERA3_BUFFER_STATUS_OK, /*output*/true);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800175
176 return OK;
177}
178
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800179status_t Camera3OutputStream::queueBufferToConsumer(sp<ANativeWindow>& consumer,
180 ANativeWindowBuffer* buffer, int anwReleaseFence) {
181 return consumer->queueBuffer(consumer.get(), buffer, anwReleaseFence);
182}
183
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800184status_t Camera3OutputStream::returnBufferLocked(
185 const camera3_stream_buffer &buffer,
186 nsecs_t timestamp) {
187 ATRACE_CALL();
Igor Murashkine3a9f962013-05-08 18:03:15 -0700188
189 status_t res = returnAnyBufferLocked(buffer, timestamp, /*output*/true);
190
191 if (res != OK) {
192 return res;
193 }
194
195 mLastTimestamp = timestamp;
Eino-Ville Talvalac31dc7e2017-01-31 17:35:41 -0800196 mFrameCount++;
Igor Murashkine3a9f962013-05-08 18:03:15 -0700197
198 return OK;
199}
200
201status_t Camera3OutputStream::returnBufferCheckedLocked(
202 const camera3_stream_buffer &buffer,
203 nsecs_t timestamp,
204 bool output,
205 /*out*/
206 sp<Fence> *releaseFenceOut) {
207
208 (void)output;
209 ALOG_ASSERT(output, "Expected output to be true");
210
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800211 status_t res;
Igor Murashkin5a1798a2013-05-07 10:58:13 -0700212
Yin-Chia Yeh4c9736f2015-03-05 15:01:36 -0800213 // Fence management - always honor release fence from HAL
214 sp<Fence> releaseFence = new Fence(buffer.release_fence);
Igor Murashkin5a1798a2013-05-07 10:58:13 -0700215 int anwReleaseFence = releaseFence->dup();
216
217 /**
Zhijun He124ccf42013-05-22 14:01:30 -0700218 * Release the lock briefly to avoid deadlock with
219 * StreamingProcessor::startStream -> Camera3Stream::isConfiguring (this
220 * thread will go into StreamingProcessor::onFrameAvailable) during
221 * queueBuffer
222 */
223 sp<ANativeWindow> currentConsumer = mConsumer;
224 mLock.unlock();
225
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800226 ANativeWindowBuffer *anwBuffer = container_of(buffer.buffer, ANativeWindowBuffer, handle);
Zhijun He124ccf42013-05-22 14:01:30 -0700227 /**
Igor Murashkin5a1798a2013-05-07 10:58:13 -0700228 * Return buffer back to ANativeWindow
229 */
230 if (buffer.status == CAMERA3_BUFFER_STATUS_ERROR) {
231 // Cancel buffer
Zhijun He7b171a92016-06-24 16:05:52 -0700232 ALOGW("A frame is dropped for stream %d", mId);
Zhijun He124ccf42013-05-22 14:01:30 -0700233 res = currentConsumer->cancelBuffer(currentConsumer.get(),
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800234 anwBuffer,
Igor Murashkin5a1798a2013-05-07 10:58:13 -0700235 anwReleaseFence);
236 if (res != OK) {
237 ALOGE("%s: Stream %d: Error cancelling buffer to native window:"
Igor Murashkine3a9f962013-05-08 18:03:15 -0700238 " %s (%d)", __FUNCTION__, mId, strerror(-res), res);
Igor Murashkin5a1798a2013-05-07 10:58:13 -0700239 }
Zhijun He1ff811b2016-01-26 14:39:51 -0800240
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800241 notifyBufferReleased(anwBuffer);
Zhijun He1ff811b2016-01-26 14:39:51 -0800242 if (mUseBufferManager) {
243 // Return this buffer back to buffer manager.
244 mBufferReleasedListener->onBufferReleased();
245 }
Igor Murashkin5a1798a2013-05-07 10:58:13 -0700246 } else {
Ruchit Sharmae0711f22014-08-18 13:48:24 -0400247 if (mTraceFirstBuffer && (stream_type == CAMERA3_STREAM_OUTPUT)) {
248 {
249 char traceLog[48];
250 snprintf(traceLog, sizeof(traceLog), "Stream %d: first full buffer\n", mId);
251 ATRACE_NAME(traceLog);
252 }
253 mTraceFirstBuffer = false;
254 }
255
Shuzhen Wang13a69632016-01-26 09:51:07 -0800256 /* Certain consumers (such as AudioSource or HardwareComposer) use
257 * MONOTONIC time, causing time misalignment if camera timestamp is
Shuzhen Wangc28dccc2016-02-11 23:48:46 -0800258 * in BOOTTIME. Do the conversion if necessary. */
259 res = native_window_set_buffers_timestamp(mConsumer.get(),
260 mUseMonoTimestamp ? timestamp - mTimestampOffset : timestamp);
261 if (res != OK) {
262 ALOGE("%s: Stream %d: Error setting timestamp: %s (%d)",
263 __FUNCTION__, mId, strerror(-res), res);
264 return res;
Yin-Chia Yeh4c9736f2015-03-05 15:01:36 -0800265 }
266
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800267 res = queueBufferToConsumer(currentConsumer, anwBuffer, anwReleaseFence);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800268 if (res != OK) {
Igor Murashkine3a9f962013-05-08 18:03:15 -0700269 ALOGE("%s: Stream %d: Error queueing buffer to native window: "
270 "%s (%d)", __FUNCTION__, mId, strerror(-res), res);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800271 }
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800272 }
Zhijun He124ccf42013-05-22 14:01:30 -0700273 mLock.lock();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700274
275 // Once a valid buffer has been returned to the queue, can no longer
276 // dequeue all buffers for preallocation.
277 if (buffer.status != CAMERA3_BUFFER_STATUS_ERROR) {
278 mStreamUnpreparable = true;
279 }
280
Igor Murashkin5a1798a2013-05-07 10:58:13 -0700281 if (res != OK) {
282 close(anwReleaseFence);
Igor Murashkin5a1798a2013-05-07 10:58:13 -0700283 }
284
Igor Murashkine3a9f962013-05-08 18:03:15 -0700285 *releaseFenceOut = releaseFence;
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800286
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700287 return res;
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800288}
289
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800290void Camera3OutputStream::dump(int fd, const Vector<String16> &args) const {
291 (void) args;
292 String8 lines;
293 lines.appendFormat(" Stream[%d]: Output\n", mId);
Eino-Ville Talvala727d1722015-06-09 13:44:19 -0700294 lines.appendFormat(" Consumer name: %s\n", mConsumerName.string());
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800295 write(fd, lines.string(), lines.size());
Igor Murashkine3a9f962013-05-08 18:03:15 -0700296
297 Camera3IOStreamBase::dump(fd, args);
Shuzhen Wang686f6442017-06-20 16:16:04 -0700298
299 mDequeueBufferLatency.dump(fd,
300 " DequeueBuffer latency histogram:");
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800301}
302
303status_t Camera3OutputStream::setTransform(int transform) {
304 ATRACE_CALL();
305 Mutex::Autolock l(mLock);
306 return setTransformLocked(transform);
307}
308
309status_t Camera3OutputStream::setTransformLocked(int transform) {
310 status_t res = OK;
311 if (mState == STATE_ERROR) {
312 ALOGE("%s: Stream in error state", __FUNCTION__);
313 return INVALID_OPERATION;
314 }
315
316 mTransform = transform;
317 if (mState == STATE_CONFIGURED) {
318 res = native_window_set_buffers_transform(mConsumer.get(),
319 transform);
320 if (res != OK) {
321 ALOGE("%s: Unable to configure stream transform to %x: %s (%d)",
322 __FUNCTION__, transform, strerror(-res), res);
323 }
324 }
325 return res;
326}
327
328status_t Camera3OutputStream::configureQueueLocked() {
329 status_t res;
330
Ruchit Sharmae0711f22014-08-18 13:48:24 -0400331 mTraceFirstBuffer = true;
Igor Murashkine3a9f962013-05-08 18:03:15 -0700332 if ((res = Camera3IOStreamBase::configureQueueLocked()) != OK) {
333 return res;
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800334 }
335
Shuzhen Wang0129d522016-10-30 22:43:41 -0700336 if ((res = configureConsumerQueueLocked()) != OK) {
337 return res;
338 }
339
340 // Set dequeueBuffer/attachBuffer timeout if the consumer is not hw composer or hw texture.
341 // We need skip these cases as timeout will disable the non-blocking (async) mode.
342 if (!(isConsumedByHWComposer() || isConsumedByHWTexture())) {
343 mConsumer->setDequeueTimeout(kDequeueBufferTimeout);
344 }
345
346 return OK;
347}
348
349status_t Camera3OutputStream::configureConsumerQueueLocked() {
350 status_t res;
351
352 mTraceFirstBuffer = true;
353
Igor Murashkine3a9f962013-05-08 18:03:15 -0700354 ALOG_ASSERT(mConsumer != 0, "mConsumer should never be NULL");
355
Zhijun He125684a2015-12-26 15:07:30 -0800356 // Configure consumer-side ANativeWindow interface. The listener may be used
357 // to notify buffer manager (if it is used) of the returned buffers.
Yin-Chia Yeh017d49c2017-03-31 19:11:00 -0700358 res = mConsumer->connect(NATIVE_WINDOW_API_CAMERA,
359 /*listener*/mBufferReleasedListener,
360 /*reportBufferRemoval*/true);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800361 if (res != OK) {
362 ALOGE("%s: Unable to connect to native window for stream %d",
363 __FUNCTION__, mId);
364 return res;
365 }
366
Eino-Ville Talvala727d1722015-06-09 13:44:19 -0700367 mConsumerName = mConsumer->getConsumerName();
368
Emilian Peev050f5dc2017-05-18 14:43:56 +0100369 res = native_window_set_usage(mConsumer.get(), mUsage);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800370 if (res != OK) {
Emilian Peev050f5dc2017-05-18 14:43:56 +0100371 ALOGE("%s: Unable to configure usage %" PRIu64 " for stream %d",
372 __FUNCTION__, mUsage, mId);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800373 return res;
374 }
375
376 res = native_window_set_scaling_mode(mConsumer.get(),
377 NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
378 if (res != OK) {
379 ALOGE("%s: Unable to configure stream scaling: %s (%d)",
380 __FUNCTION__, strerror(-res), res);
381 return res;
382 }
383
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800384 if (mMaxSize == 0) {
385 // For buffers of known size
Eino-Ville Talvala7d70c5e2014-07-24 18:10:23 -0700386 res = native_window_set_buffers_dimensions(mConsumer.get(),
387 camera3_stream::width, camera3_stream::height);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800388 } else {
389 // For buffers with bounded size
Eino-Ville Talvala7d70c5e2014-07-24 18:10:23 -0700390 res = native_window_set_buffers_dimensions(mConsumer.get(),
391 mMaxSize, 1);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800392 }
393 if (res != OK) {
Eino-Ville Talvala7d70c5e2014-07-24 18:10:23 -0700394 ALOGE("%s: Unable to configure stream buffer dimensions"
395 " %d x %d (maxSize %zu) for stream %d",
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800396 __FUNCTION__, camera3_stream::width, camera3_stream::height,
Eino-Ville Talvala7d70c5e2014-07-24 18:10:23 -0700397 mMaxSize, mId);
398 return res;
399 }
400 res = native_window_set_buffers_format(mConsumer.get(),
401 camera3_stream::format);
402 if (res != OK) {
403 ALOGE("%s: Unable to configure stream buffer format %#x for stream %d",
404 __FUNCTION__, camera3_stream::format, mId);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800405 return res;
406 }
407
Eino-Ville Talvala3d82c0d2015-02-23 15:19:19 -0800408 res = native_window_set_buffers_data_space(mConsumer.get(),
409 camera3_stream::data_space);
410 if (res != OK) {
411 ALOGE("%s: Unable to configure stream dataspace %#x for stream %d",
412 __FUNCTION__, camera3_stream::data_space, mId);
413 return res;
414 }
415
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800416 int maxConsumerBuffers;
Eino-Ville Talvala727d1722015-06-09 13:44:19 -0700417 res = static_cast<ANativeWindow*>(mConsumer.get())->query(
418 mConsumer.get(),
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800419 NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS, &maxConsumerBuffers);
420 if (res != OK) {
421 ALOGE("%s: Unable to query consumer undequeued"
422 " buffer count for stream %d", __FUNCTION__, mId);
423 return res;
424 }
425
Alex Ray20cb3002013-05-28 20:18:22 -0700426 ALOGV("%s: Consumer wants %d buffers, HAL wants %d", __FUNCTION__,
427 maxConsumerBuffers, camera3_stream::max_buffers);
428 if (camera3_stream::max_buffers == 0) {
Zhijun He2ab500c2013-07-23 08:02:53 -0700429 ALOGE("%s: Camera HAL requested max_buffer count: %d, requires at least 1",
Alex Ray20cb3002013-05-28 20:18:22 -0700430 __FUNCTION__, camera3_stream::max_buffers);
431 return INVALID_OPERATION;
432 }
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800433
434 mTotalBufferCount = maxConsumerBuffers + camera3_stream::max_buffers;
Zhijun He6adc9cc2014-04-15 14:09:55 -0700435 mHandoutTotalBufferCount = 0;
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800436 mFrameCount = 0;
437 mLastTimestamp = 0;
Shuzhen Wangc28dccc2016-02-11 23:48:46 -0800438 mUseMonoTimestamp = (isConsumedByHWComposer() | isVideoStream());
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800439
440 res = native_window_set_buffer_count(mConsumer.get(),
441 mTotalBufferCount);
442 if (res != OK) {
443 ALOGE("%s: Unable to set buffer count for stream %d",
444 __FUNCTION__, mId);
445 return res;
446 }
447
448 res = native_window_set_buffers_transform(mConsumer.get(),
449 mTransform);
450 if (res != OK) {
451 ALOGE("%s: Unable to configure stream transform to %x: %s (%d)",
452 __FUNCTION__, mTransform, strerror(-res), res);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700453 return res;
Zhijun Hef0645c12016-08-02 00:58:11 -0700454 }
455
Zhijun He125684a2015-12-26 15:07:30 -0800456 /**
Zhijun Heedd41ae2016-02-03 14:45:53 -0800457 * Camera3 Buffer manager is only supported by HAL3.3 onwards, as the older HALs requires
Zhijun He125684a2015-12-26 15:07:30 -0800458 * buffers to be statically allocated for internal static buffer registration, while the
459 * buffers provided by buffer manager are really dynamically allocated. Camera3Device only
Zhijun Heedd41ae2016-02-03 14:45:53 -0800460 * sets the mBufferManager if device version is > HAL3.2, which guarantees that the buffer
461 * manager setup is skipped in below code. Note that HAL3.2 is also excluded here, as some
462 * HAL3.2 devices may not support the dynamic buffer registeration.
Zhijun He125684a2015-12-26 15:07:30 -0800463 */
464 if (mBufferManager != 0 && mSetId > CAMERA3_STREAM_SET_ID_INVALID) {
Emilian Peev050f5dc2017-05-18 14:43:56 +0100465 uint64_t consumerUsage = 0;
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -0700466 getEndpointUsage(&consumerUsage);
Zhijun He125684a2015-12-26 15:07:30 -0800467 StreamInfo streamInfo(
468 getId(), getStreamSetId(), getWidth(), getHeight(), getFormat(), getDataSpace(),
Emilian Peev050f5dc2017-05-18 14:43:56 +0100469 mUsage | consumerUsage, mTotalBufferCount,
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -0700470 /*isConfigured*/true);
471 wp<Camera3OutputStream> weakThis(this);
472 res = mBufferManager->registerStream(weakThis,
473 streamInfo);
Zhijun He125684a2015-12-26 15:07:30 -0800474 if (res == OK) {
475 // Disable buffer allocation for this BufferQueue, buffer manager will take over
476 // the buffer allocation responsibility.
477 mConsumer->getIGraphicBufferProducer()->allowAllocation(false);
478 mUseBufferManager = true;
479 } else {
480 ALOGE("%s: Unable to register stream %d to camera3 buffer manager, "
481 "(error %d %s), fall back to BufferQueue for buffer management!",
482 __FUNCTION__, mId, res, strerror(-res));
483 }
484 }
485
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800486 return OK;
487}
488
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800489status_t Camera3OutputStream::getBufferLockedCommon(ANativeWindowBuffer** anb, int* fenceFd) {
490 ATRACE_CALL();
491 status_t res;
492
493 if ((res = getBufferPreconditionCheckLocked()) != OK) {
494 return res;
495 }
496
497 bool gotBufferFromManager = false;
498
499 if (mUseBufferManager) {
500 sp<GraphicBuffer> gb;
501 res = mBufferManager->getBufferForStream(getId(), getStreamSetId(), &gb, fenceFd);
502 if (res == OK) {
503 // Attach this buffer to the bufferQueue: the buffer will be in dequeue state after a
504 // successful return.
505 *anb = gb.get();
506 res = mConsumer->attachBuffer(*anb);
507 if (res != OK) {
508 ALOGE("%s: Stream %d: Can't attach the output buffer to this surface: %s (%d)",
509 __FUNCTION__, mId, strerror(-res), res);
510 return res;
511 }
512 gotBufferFromManager = true;
513 ALOGV("Stream %d: Attached new buffer", getId());
514 } else if (res == ALREADY_EXISTS) {
515 // Have sufficient free buffers already attached, can just
516 // dequeue from buffer queue
517 ALOGV("Stream %d: Reusing attached buffer", getId());
518 gotBufferFromManager = false;
519 } else if (res != OK) {
520 ALOGE("%s: Stream %d: Can't get next output buffer from buffer manager: %s (%d)",
521 __FUNCTION__, mId, strerror(-res), res);
522 return res;
523 }
524 }
525 if (!gotBufferFromManager) {
526 /**
527 * Release the lock briefly to avoid deadlock for below scenario:
528 * Thread 1: StreamingProcessor::startStream -> Camera3Stream::isConfiguring().
529 * This thread acquired StreamingProcessor lock and try to lock Camera3Stream lock.
530 * Thread 2: Camera3Stream::returnBuffer->StreamingProcessor::onFrameAvailable().
531 * This thread acquired Camera3Stream lock and bufferQueue lock, and try to lock
532 * StreamingProcessor lock.
533 * Thread 3: Camera3Stream::getBuffer(). This thread acquired Camera3Stream lock
534 * and try to lock bufferQueue lock.
535 * Then there is circular locking dependency.
536 */
537 sp<ANativeWindow> currentConsumer = mConsumer;
538 mLock.unlock();
539
Shuzhen Wang686f6442017-06-20 16:16:04 -0700540 nsecs_t dequeueStart = systemTime(SYSTEM_TIME_MONOTONIC);
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800541 res = currentConsumer->dequeueBuffer(currentConsumer.get(), anb, fenceFd);
Shuzhen Wang686f6442017-06-20 16:16:04 -0700542 nsecs_t dequeueEnd = systemTime(SYSTEM_TIME_MONOTONIC);
543 mDequeueBufferLatency.add(dequeueStart, dequeueEnd);
544
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800545 mLock.lock();
546 if (res != OK) {
547 ALOGE("%s: Stream %d: Can't dequeue next output buffer: %s (%d)",
548 __FUNCTION__, mId, strerror(-res), res);
549
550 // Only transition to STATE_ABANDONED from STATE_CONFIGURED. (If it is STATE_PREPARING,
551 // let prepareNextBuffer handle the error.)
552 if (res == NO_INIT && mState == STATE_CONFIGURED) {
553 mState = STATE_ABANDONED;
554 }
555
556 return res;
557 }
558 }
559
Yin-Chia Yeh017d49c2017-03-31 19:11:00 -0700560 if (res == OK) {
561 std::vector<sp<GraphicBuffer>> removedBuffers;
562 res = mConsumer->getAndFlushRemovedBuffers(&removedBuffers);
563 if (res == OK) {
564 onBuffersRemovedLocked(removedBuffers);
Shuzhen Wangfd76cf52017-05-15 10:01:04 -0700565
566 if (mUseBufferManager && removedBuffers.size() > 0) {
567 mBufferManager->onBuffersRemoved(getId(), getStreamSetId(), removedBuffers.size());
568 }
Yin-Chia Yeh017d49c2017-03-31 19:11:00 -0700569 }
570 }
571
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800572 return res;
573}
574
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800575status_t Camera3OutputStream::disconnectLocked() {
576 status_t res;
577
Igor Murashkine3a9f962013-05-08 18:03:15 -0700578 if ((res = Camera3IOStreamBase::disconnectLocked()) != OK) {
579 return res;
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800580 }
581
Zhijun He5d677d12016-05-29 16:52:39 -0700582 // Stream configuration was not finished (can only be in STATE_IN_CONFIG or STATE_CONSTRUCTED
583 // state), don't need change the stream state, return OK.
584 if (mConsumer == nullptr) {
585 return OK;
586 }
587
Zhijun He125684a2015-12-26 15:07:30 -0800588 ALOGV("%s: disconnecting stream %d from native window", __FUNCTION__, getId());
589
Igor Murashkine3a9f962013-05-08 18:03:15 -0700590 res = native_window_api_disconnect(mConsumer.get(),
591 NATIVE_WINDOW_API_CAMERA);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800592 /**
593 * This is not an error. if client calling process dies, the window will
594 * also die and all calls to it will return DEAD_OBJECT, thus it's already
595 * "disconnected"
596 */
597 if (res == DEAD_OBJECT) {
598 ALOGW("%s: While disconnecting stream %d from native window, the"
599 " native window died from under us", __FUNCTION__, mId);
600 }
601 else if (res != OK) {
Igor Murashkine3a9f962013-05-08 18:03:15 -0700602 ALOGE("%s: Unable to disconnect stream %d from native window "
603 "(error %d %s)",
604 __FUNCTION__, mId, res, strerror(-res));
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800605 mState = STATE_ERROR;
606 return res;
607 }
608
Zhijun He125684a2015-12-26 15:07:30 -0800609 // Since device is already idle, there is no getBuffer call to buffer manager, unregister the
610 // stream at this point should be safe.
611 if (mUseBufferManager) {
612 res = mBufferManager->unregisterStream(getId(), getStreamSetId());
613 if (res != OK) {
614 ALOGE("%s: Unable to unregister stream %d from buffer manager "
615 "(error %d %s)", __FUNCTION__, mId, res, strerror(-res));
616 mState = STATE_ERROR;
617 return res;
618 }
619 // Note that, to make prepare/teardown case work, we must not mBufferManager.clear(), as
620 // the stream is still in usable state after this call.
621 mUseBufferManager = false;
622 }
623
Igor Murashkine3a9f962013-05-08 18:03:15 -0700624 mState = (mState == STATE_IN_RECONFIG) ? STATE_IN_CONFIG
625 : STATE_CONSTRUCTED;
Shuzhen Wang686f6442017-06-20 16:16:04 -0700626
627 mDequeueBufferLatency.log("Stream %d dequeueBuffer latency histogram", mId);
628 mDequeueBufferLatency.reset();
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800629 return OK;
630}
631
Emilian Peev050f5dc2017-05-18 14:43:56 +0100632status_t Camera3OutputStream::getEndpointUsage(uint64_t *usage) const {
Eino-Ville Talvalab2f5b192013-07-30 14:36:03 -0700633
634 status_t res;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700635
Zhijun He5d677d12016-05-29 16:52:39 -0700636 if (mConsumer == nullptr) {
637 // mConsumerUsage was sanitized before the Camera3OutputStream was constructed.
638 *usage = mConsumerUsage;
639 return OK;
640 }
641
Shuzhen Wang0129d522016-10-30 22:43:41 -0700642 res = getEndpointUsageForSurface(usage, mConsumer);
643
644 return res;
645}
646
Emilian Peev050f5dc2017-05-18 14:43:56 +0100647status_t Camera3OutputStream::getEndpointUsageForSurface(uint64_t *usage,
Shuzhen Wang0129d522016-10-30 22:43:41 -0700648 const sp<Surface>& surface) const {
649 status_t res;
Emilian Peev050f5dc2017-05-18 14:43:56 +0100650 uint64_t u = 0;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700651
Emilian Peev050f5dc2017-05-18 14:43:56 +0100652 res = native_window_get_consumer_usage(static_cast<ANativeWindow*>(surface.get()), &u);
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700653 // If an opaque output stream's endpoint is ImageReader, add
Yin-Chia Yeh47cf8e62017-04-04 13:00:03 -0700654 // GRALLOC_USAGE_HW_CAMERA_ZSL to the usage so HAL knows it will be used
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700655 // for the ZSL use case.
656 // Assume it's for ImageReader if the consumer usage doesn't have any of these bits set:
657 // 1. GRALLOC_USAGE_HW_TEXTURE
658 // 2. GRALLOC_USAGE_HW_RENDER
659 // 3. GRALLOC_USAGE_HW_COMPOSER
660 // 4. GRALLOC_USAGE_HW_VIDEO_ENCODER
661 if (camera3_stream::format == HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED &&
Shuzhen Wang0129d522016-10-30 22:43:41 -0700662 (u & (GRALLOC_USAGE_HW_TEXTURE | GRALLOC_USAGE_HW_RENDER |
663 GRALLOC_USAGE_HW_COMPOSER | GRALLOC_USAGE_HW_VIDEO_ENCODER)) == 0) {
Yin-Chia Yeh47cf8e62017-04-04 13:00:03 -0700664 u |= GRALLOC_USAGE_HW_CAMERA_ZSL;
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700665 }
666
667 *usage = u;
Eino-Ville Talvalab2f5b192013-07-30 14:36:03 -0700668 return res;
669}
670
Chien-Yu Chen85a64552015-08-28 15:46:12 -0700671bool Camera3OutputStream::isVideoStream() const {
Emilian Peev050f5dc2017-05-18 14:43:56 +0100672 uint64_t usage = 0;
Chien-Yu Chen85a64552015-08-28 15:46:12 -0700673 status_t res = getEndpointUsage(&usage);
674 if (res != OK) {
675 ALOGE("%s: getting end point usage failed: %s (%d).", __FUNCTION__, strerror(-res), res);
676 return false;
677 }
678
679 return (usage & GRALLOC_USAGE_HW_VIDEO_ENCODER) != 0;
680}
681
Zhijun He125684a2015-12-26 15:07:30 -0800682status_t Camera3OutputStream::setBufferManager(sp<Camera3BufferManager> bufferManager) {
683 Mutex::Autolock l(mLock);
684 if (mState != STATE_CONSTRUCTED) {
Zhijun He5d677d12016-05-29 16:52:39 -0700685 ALOGE("%s: this method can only be called when stream in CONSTRUCTED state.",
Zhijun He125684a2015-12-26 15:07:30 -0800686 __FUNCTION__);
687 return INVALID_OPERATION;
688 }
689 mBufferManager = bufferManager;
690
691 return OK;
692}
693
Emilian Peev40ead602017-09-26 15:46:36 +0100694status_t Camera3OutputStream::updateStream(const std::vector<sp<Surface>> &/*outputSurfaces*/,
695 const std::vector<OutputStreamInfo> &/*outputInfo*/,
696 const std::vector<size_t> &/*removedSurfaceIds*/,
697 KeyedVector<sp<Surface>, size_t> * /*outputMapo*/) {
698 ALOGE("%s: this method is not supported!", __FUNCTION__);
699 return INVALID_OPERATION;
700}
701
Zhijun He125684a2015-12-26 15:07:30 -0800702void Camera3OutputStream::BufferReleasedListener::onBufferReleased() {
703 sp<Camera3OutputStream> stream = mParent.promote();
704 if (stream == nullptr) {
705 ALOGV("%s: Parent camera3 output stream was destroyed", __FUNCTION__);
706 return;
707 }
708
709 Mutex::Autolock l(stream->mLock);
710 if (!(stream->mUseBufferManager)) {
711 return;
712 }
713
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -0700714 ALOGV("Stream %d: Buffer released", stream->getId());
Yin-Chia Yeh89954d92017-05-21 17:28:53 -0700715 bool shouldFreeBuffer = false;
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -0700716 status_t res = stream->mBufferManager->onBufferReleased(
Yin-Chia Yeh89954d92017-05-21 17:28:53 -0700717 stream->getId(), stream->getStreamSetId(), &shouldFreeBuffer);
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -0700718 if (res != OK) {
719 ALOGE("%s: signaling buffer release to buffer manager failed: %s (%d).", __FUNCTION__,
720 strerror(-res), res);
721 stream->mState = STATE_ERROR;
722 }
Yin-Chia Yeh89954d92017-05-21 17:28:53 -0700723
724 if (shouldFreeBuffer) {
725 sp<GraphicBuffer> buffer;
726 // Detach and free a buffer (when buffer goes out of scope)
727 stream->detachBufferLocked(&buffer, /*fenceFd*/ nullptr);
728 if (buffer.get() != nullptr) {
729 stream->mBufferManager->notifyBufferRemoved(
730 stream->getId(), stream->getStreamSetId());
731 }
732 }
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -0700733}
734
Yin-Chia Yeh017d49c2017-03-31 19:11:00 -0700735void Camera3OutputStream::onBuffersRemovedLocked(
736 const std::vector<sp<GraphicBuffer>>& removedBuffers) {
Yin-Chia Yehdb1e8642017-07-14 15:19:30 -0700737 sp<Camera3StreamBufferFreedListener> callback = mBufferFreedListener.promote();
Yin-Chia Yeh017d49c2017-03-31 19:11:00 -0700738 if (callback != nullptr) {
Chih-Hung Hsieh48fc6192017-08-04 14:37:31 -0700739 for (const auto& gb : removedBuffers) {
Yin-Chia Yeh017d49c2017-03-31 19:11:00 -0700740 callback->onBufferFreed(mId, gb->handle);
741 }
742 }
743}
744
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -0700745status_t Camera3OutputStream::detachBuffer(sp<GraphicBuffer>* buffer, int* fenceFd) {
746 Mutex::Autolock l(mLock);
Yin-Chia Yeh89954d92017-05-21 17:28:53 -0700747 return detachBufferLocked(buffer, fenceFd);
748}
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -0700749
Yin-Chia Yeh89954d92017-05-21 17:28:53 -0700750status_t Camera3OutputStream::detachBufferLocked(sp<GraphicBuffer>* buffer, int* fenceFd) {
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -0700751 ALOGV("Stream %d: detachBuffer", getId());
752 if (buffer == nullptr) {
753 return BAD_VALUE;
754 }
755
Zhijun He125684a2015-12-26 15:07:30 -0800756 sp<Fence> fence;
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -0700757 status_t res = mConsumer->detachNextBuffer(buffer, &fence);
Zhijun He125684a2015-12-26 15:07:30 -0800758 if (res == NO_MEMORY) {
759 // This may rarely happen, which indicates that the released buffer was freed by other
760 // call (e.g., attachBuffer, dequeueBuffer etc.) before reaching here. We should notify the
761 // buffer manager that this buffer has been freed. It's not fatal, but should be avoided,
762 // therefore log a warning.
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -0700763 *buffer = 0;
Zhijun He125684a2015-12-26 15:07:30 -0800764 ALOGW("%s: the released buffer has already been freed by the buffer queue!", __FUNCTION__);
765 } else if (res != OK) {
Eino-Ville Talvalaff51b472016-06-28 15:26:19 -0700766 // Treat other errors as abandonment
Zhijun He125684a2015-12-26 15:07:30 -0800767 ALOGE("%s: detach next buffer failed: %s (%d).", __FUNCTION__, strerror(-res), res);
Eino-Ville Talvalaff51b472016-06-28 15:26:19 -0700768 mState = STATE_ABANDONED;
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -0700769 return res;
Zhijun He125684a2015-12-26 15:07:30 -0800770 }
771
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -0700772 if (fenceFd != nullptr) {
773 if (fence!= 0 && fence->isValid()) {
774 *fenceFd = fence->dup();
775 } else {
776 *fenceFd = -1;
777 }
Zhijun He125684a2015-12-26 15:07:30 -0800778 }
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -0700779
Yin-Chia Yeh017d49c2017-03-31 19:11:00 -0700780 std::vector<sp<GraphicBuffer>> removedBuffers;
781 res = mConsumer->getAndFlushRemovedBuffers(&removedBuffers);
782 if (res == OK) {
783 onBuffersRemovedLocked(removedBuffers);
784 }
785 return res;
Zhijun He125684a2015-12-26 15:07:30 -0800786}
Shuzhen Wang13a69632016-01-26 09:51:07 -0800787
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800788status_t Camera3OutputStream::notifyBufferReleased(ANativeWindowBuffer* /*anwBuffer*/) {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700789 return OK;
790}
791
792bool Camera3OutputStream::isConsumerConfigurationDeferred(size_t surface_id) const {
Zhijun He5d677d12016-05-29 16:52:39 -0700793 Mutex::Autolock l(mLock);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700794
795 if (surface_id != 0) {
Shuzhen Wang758c2152017-01-10 18:26:18 -0800796 ALOGE("%s: surface_id %zu for Camera3OutputStream should be 0!", __FUNCTION__, surface_id);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700797 }
Zhijun He5d677d12016-05-29 16:52:39 -0700798 return mConsumer == nullptr;
799}
800
Shuzhen Wang758c2152017-01-10 18:26:18 -0800801status_t Camera3OutputStream::setConsumers(const std::vector<sp<Surface>>& consumers) {
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800802 Mutex::Autolock l(mLock);
Shuzhen Wang758c2152017-01-10 18:26:18 -0800803 if (consumers.size() != 1) {
804 ALOGE("%s: it's illegal to set %zu consumer surfaces!",
805 __FUNCTION__, consumers.size());
806 return INVALID_OPERATION;
807 }
808 if (consumers[0] == nullptr) {
809 ALOGE("%s: it's illegal to set null consumer surface!", __FUNCTION__);
Zhijun He5d677d12016-05-29 16:52:39 -0700810 return INVALID_OPERATION;
811 }
812
813 if (mConsumer != nullptr) {
814 ALOGE("%s: consumer surface was already set!", __FUNCTION__);
815 return INVALID_OPERATION;
816 }
817
Shuzhen Wang758c2152017-01-10 18:26:18 -0800818 mConsumer = consumers[0];
Zhijun He5d677d12016-05-29 16:52:39 -0700819 return OK;
820}
821
Shuzhen Wang13a69632016-01-26 09:51:07 -0800822bool Camera3OutputStream::isConsumedByHWComposer() const {
Emilian Peev050f5dc2017-05-18 14:43:56 +0100823 uint64_t usage = 0;
Shuzhen Wang13a69632016-01-26 09:51:07 -0800824 status_t res = getEndpointUsage(&usage);
825 if (res != OK) {
826 ALOGE("%s: getting end point usage failed: %s (%d).", __FUNCTION__, strerror(-res), res);
827 return false;
828 }
829
830 return (usage & GRALLOC_USAGE_HW_COMPOSER) != 0;
831}
832
Zhijun Hef0645c12016-08-02 00:58:11 -0700833bool Camera3OutputStream::isConsumedByHWTexture() const {
Emilian Peev050f5dc2017-05-18 14:43:56 +0100834 uint64_t usage = 0;
Zhijun Hef0645c12016-08-02 00:58:11 -0700835 status_t res = getEndpointUsage(&usage);
836 if (res != OK) {
837 ALOGE("%s: getting end point usage failed: %s (%d).", __FUNCTION__, strerror(-res), res);
838 return false;
839 }
840
841 return (usage & GRALLOC_USAGE_HW_TEXTURE) != 0;
842}
843
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800844}; // namespace camera3
845
846}; // namespace android