blob: 6dfc8383c743a330397d1d1f0e07ac57f428fa17 [file] [log] [blame]
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -08001/*
Shuzhen Wangc28189a2017-11-27 23:05:10 -08002 * Copyright (C) 2013-2018 The Android Open Source Project
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -08003 *
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
Shuzhen Wangabbcb6b2020-12-09 22:32:44 -080021#include <ctime>
22#include <fstream>
23
24#include <android-base/unique_fd.h>
25#include <ui/GraphicBuffer.h>
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -080026#include <utils/Log.h>
27#include <utils/Trace.h>
Shuzhen Wangabbcb6b2020-12-09 22:32:44 -080028
29#include "api1/client2/JpegProcessor.h"
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -080030#include "Camera3OutputStream.h"
Jayant Chowdharyd4776262020-06-23 23:45:57 -070031#include "utils/TraceHFR.h"
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -080032
33#ifndef container_of
34#define container_of(ptr, type, member) \
35 (type *)((char*)(ptr) - offsetof(type, member))
36#endif
37
38namespace android {
39
40namespace camera3 {
41
42Camera3OutputStream::Camera3OutputStream(int id,
Eino-Ville Talvala727d1722015-06-09 13:44:19 -070043 sp<Surface> consumer,
Eino-Ville Talvala3d82c0d2015-02-23 15:19:19 -080044 uint32_t width, uint32_t height, int format,
Shuzhen Wangc28dccc2016-02-11 23:48:46 -080045 android_dataspace dataSpace, camera3_stream_rotation_t rotation,
Shuzhen Wangc28189a2017-11-27 23:05:10 -080046 nsecs_t timestampOffset, const String8& physicalCameraId,
47 int setId) :
Igor Murashkine3a9f962013-05-08 18:03:15 -070048 Camera3IOStreamBase(id, CAMERA3_STREAM_OUTPUT, width, height,
Shuzhen Wangc28189a2017-11-27 23:05:10 -080049 /*maxSize*/0, format, dataSpace, rotation,
50 physicalCameraId, setId),
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -080051 mConsumer(consumer),
Ruchit Sharmae0711f22014-08-18 13:48:24 -040052 mTransform(0),
Zhijun He125684a2015-12-26 15:07:30 -080053 mTraceFirstBuffer(true),
Shuzhen Wangc28dccc2016-02-11 23:48:46 -080054 mUseBufferManager(false),
Zhijun He5d677d12016-05-29 16:52:39 -070055 mTimestampOffset(timestampOffset),
Shuzhen Wang686f6442017-06-20 16:16:04 -070056 mConsumerUsage(0),
Chien-Yu Chena936ac22017-10-23 15:59:49 -070057 mDropBuffers(false),
Shuzhen Wang686f6442017-06-20 16:16:04 -070058 mDequeueBufferLatency(kDequeueLatencyBinSize) {
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -080059
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -080060 if (mConsumer == NULL) {
61 ALOGE("%s: Consumer is NULL!", __FUNCTION__);
62 mState = STATE_ERROR;
63 }
Zhijun He125684a2015-12-26 15:07:30 -080064
Shuzhen Wang0160ddd2019-08-15 09:11:56 -070065 bool needsReleaseNotify = setId > CAMERA3_STREAM_SET_ID_INVALID;
66 mBufferProducerListener = new BufferProducerListener(this, needsReleaseNotify);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -080067}
68
69Camera3OutputStream::Camera3OutputStream(int id,
Eino-Ville Talvala727d1722015-06-09 13:44:19 -070070 sp<Surface> consumer,
Eino-Ville Talvala3d82c0d2015-02-23 15:19:19 -080071 uint32_t width, uint32_t height, size_t maxSize, int format,
Shuzhen Wangc28dccc2016-02-11 23:48:46 -080072 android_dataspace dataSpace, camera3_stream_rotation_t rotation,
Shuzhen Wangc28189a2017-11-27 23:05:10 -080073 nsecs_t timestampOffset, const String8& physicalCameraId, int setId) :
Igor Murashkine3a9f962013-05-08 18:03:15 -070074 Camera3IOStreamBase(id, CAMERA3_STREAM_OUTPUT, width, height, maxSize,
Shuzhen Wangc28189a2017-11-27 23:05:10 -080075 format, dataSpace, rotation, physicalCameraId, setId),
Igor Murashkina55b5452013-04-02 16:36:33 -070076 mConsumer(consumer),
Ruchit Sharmae0711f22014-08-18 13:48:24 -040077 mTransform(0),
Zhijun He125684a2015-12-26 15:07:30 -080078 mTraceFirstBuffer(true),
Shuzhen Wangc28dccc2016-02-11 23:48:46 -080079 mUseMonoTimestamp(false),
80 mUseBufferManager(false),
Zhijun He5d677d12016-05-29 16:52:39 -070081 mTimestampOffset(timestampOffset),
Shuzhen Wang686f6442017-06-20 16:16:04 -070082 mConsumerUsage(0),
Chien-Yu Chena936ac22017-10-23 15:59:49 -070083 mDropBuffers(false),
Shuzhen Wang686f6442017-06-20 16:16:04 -070084 mDequeueBufferLatency(kDequeueLatencyBinSize) {
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -080085
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -080086 if (format != HAL_PIXEL_FORMAT_BLOB && format != HAL_PIXEL_FORMAT_RAW_OPAQUE) {
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -080087 ALOGE("%s: Bad format for size-only stream: %d", __FUNCTION__,
88 format);
89 mState = STATE_ERROR;
90 }
91
92 if (mConsumer == NULL) {
93 ALOGE("%s: Consumer is NULL!", __FUNCTION__);
94 mState = STATE_ERROR;
95 }
Zhijun He125684a2015-12-26 15:07:30 -080096
Shuzhen Wang0160ddd2019-08-15 09:11:56 -070097 bool needsReleaseNotify = setId > CAMERA3_STREAM_SET_ID_INVALID;
98 mBufferProducerListener = new BufferProducerListener(this, needsReleaseNotify);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -080099}
100
Zhijun He5d677d12016-05-29 16:52:39 -0700101Camera3OutputStream::Camera3OutputStream(int id,
102 uint32_t width, uint32_t height, int format,
Emilian Peev050f5dc2017-05-18 14:43:56 +0100103 uint64_t consumerUsage, android_dataspace dataSpace,
Shuzhen Wangc28189a2017-11-27 23:05:10 -0800104 camera3_stream_rotation_t rotation, nsecs_t timestampOffset,
105 const String8& physicalCameraId, int setId) :
Zhijun He5d677d12016-05-29 16:52:39 -0700106 Camera3IOStreamBase(id, CAMERA3_STREAM_OUTPUT, width, height,
Shuzhen Wangc28189a2017-11-27 23:05:10 -0800107 /*maxSize*/0, format, dataSpace, rotation,
108 physicalCameraId, setId),
Zhijun He5d677d12016-05-29 16:52:39 -0700109 mConsumer(nullptr),
110 mTransform(0),
111 mTraceFirstBuffer(true),
112 mUseBufferManager(false),
113 mTimestampOffset(timestampOffset),
Shuzhen Wang686f6442017-06-20 16:16:04 -0700114 mConsumerUsage(consumerUsage),
Chien-Yu Chena936ac22017-10-23 15:59:49 -0700115 mDropBuffers(false),
Shuzhen Wang686f6442017-06-20 16:16:04 -0700116 mDequeueBufferLatency(kDequeueLatencyBinSize) {
Zhijun He5d677d12016-05-29 16:52:39 -0700117 // Deferred consumer only support preview surface format now.
118 if (format != HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED) {
119 ALOGE("%s: Deferred consumer only supports IMPLEMENTATION_DEFINED format now!",
120 __FUNCTION__);
121 mState = STATE_ERROR;
122 }
123
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -0400124 // Validation check for the consumer usage flag.
Zhijun He5d677d12016-05-29 16:52:39 -0700125 if ((consumerUsage & GraphicBuffer::USAGE_HW_TEXTURE) == 0 &&
126 (consumerUsage & GraphicBuffer::USAGE_HW_COMPOSER) == 0) {
Emilian Peev050f5dc2017-05-18 14:43:56 +0100127 ALOGE("%s: Deferred consumer usage flag is illegal %" PRIu64 "!",
128 __FUNCTION__, consumerUsage);
Zhijun He5d677d12016-05-29 16:52:39 -0700129 mState = STATE_ERROR;
130 }
131
132 mConsumerName = String8("Deferred");
Shuzhen Wang0160ddd2019-08-15 09:11:56 -0700133 bool needsReleaseNotify = setId > CAMERA3_STREAM_SET_ID_INVALID;
134 mBufferProducerListener = new BufferProducerListener(this, needsReleaseNotify);
Zhijun He5d677d12016-05-29 16:52:39 -0700135}
136
Igor Murashkine3a9f962013-05-08 18:03:15 -0700137Camera3OutputStream::Camera3OutputStream(int id, camera3_stream_type_t type,
138 uint32_t width, uint32_t height,
Eino-Ville Talvala3d82c0d2015-02-23 15:19:19 -0800139 int format,
Yin-Chia Yehb97babb2015-03-12 13:42:44 -0700140 android_dataspace dataSpace,
Zhijun He125684a2015-12-26 15:07:30 -0800141 camera3_stream_rotation_t rotation,
Shuzhen Wangc28189a2017-11-27 23:05:10 -0800142 const String8& physicalCameraId,
Emilian Peev050f5dc2017-05-18 14:43:56 +0100143 uint64_t consumerUsage, nsecs_t timestampOffset,
Zhijun He125684a2015-12-26 15:07:30 -0800144 int setId) :
Igor Murashkine3a9f962013-05-08 18:03:15 -0700145 Camera3IOStreamBase(id, type, width, height,
146 /*maxSize*/0,
Shuzhen Wangc28189a2017-11-27 23:05:10 -0800147 format, dataSpace, rotation,
148 physicalCameraId, setId),
Zhijun He125684a2015-12-26 15:07:30 -0800149 mTransform(0),
150 mTraceFirstBuffer(true),
Shuzhen Wangc28dccc2016-02-11 23:48:46 -0800151 mUseMonoTimestamp(false),
Zhijun He5d677d12016-05-29 16:52:39 -0700152 mUseBufferManager(false),
Shuzhen Wang0129d522016-10-30 22:43:41 -0700153 mTimestampOffset(timestampOffset),
Shuzhen Wang686f6442017-06-20 16:16:04 -0700154 mConsumerUsage(consumerUsage),
Chien-Yu Chena936ac22017-10-23 15:59:49 -0700155 mDropBuffers(false),
Shuzhen Wang686f6442017-06-20 16:16:04 -0700156 mDequeueBufferLatency(kDequeueLatencyBinSize) {
Zhijun He125684a2015-12-26 15:07:30 -0800157
Shuzhen Wang0160ddd2019-08-15 09:11:56 -0700158 bool needsReleaseNotify = setId > CAMERA3_STREAM_SET_ID_INVALID;
159 mBufferProducerListener = new BufferProducerListener(this, needsReleaseNotify);
Igor Murashkine3a9f962013-05-08 18:03:15 -0700160
161 // Subclasses expected to initialize mConsumer themselves
162}
163
164
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800165Camera3OutputStream::~Camera3OutputStream() {
166 disconnectLocked();
167}
168
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800169status_t Camera3OutputStream::getBufferLocked(camera3_stream_buffer *buffer,
170 const std::vector<size_t>&) {
Jayant Chowdharyd4776262020-06-23 23:45:57 -0700171 ATRACE_HFR_CALL();
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800172
173 ANativeWindowBuffer* anb;
Zhijun He125684a2015-12-26 15:07:30 -0800174 int fenceFd = -1;
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -0700175
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800176 status_t res;
177 res = getBufferLockedCommon(&anb, &fenceFd);
178 if (res != OK) {
179 return res;
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800180 }
181
Igor Murashkine3a9f962013-05-08 18:03:15 -0700182 /**
183 * FenceFD now owned by HAL except in case of error,
184 * in which case we reassign it to acquire_fence
185 */
186 handoutBufferLocked(*buffer, &(anb->handle), /*acquireFence*/fenceFd,
Zhijun He6adc9cc2014-04-15 14:09:55 -0700187 /*releaseFence*/-1, CAMERA3_BUFFER_STATUS_OK, /*output*/true);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800188
189 return OK;
190}
191
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800192status_t Camera3OutputStream::queueBufferToConsumer(sp<ANativeWindow>& consumer,
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -0700193 ANativeWindowBuffer* buffer, int anwReleaseFence,
194 const std::vector<size_t>&) {
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800195 return consumer->queueBuffer(consumer.get(), buffer, anwReleaseFence);
196}
197
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800198status_t Camera3OutputStream::returnBufferLocked(
199 const camera3_stream_buffer &buffer,
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -0700200 nsecs_t timestamp, const std::vector<size_t>& surface_ids) {
Jayant Chowdharyd4776262020-06-23 23:45:57 -0700201 ATRACE_HFR_CALL();
Igor Murashkine3a9f962013-05-08 18:03:15 -0700202
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -0700203 status_t res = returnAnyBufferLocked(buffer, timestamp, /*output*/true, surface_ids);
Igor Murashkine3a9f962013-05-08 18:03:15 -0700204
205 if (res != OK) {
206 return res;
207 }
208
209 mLastTimestamp = timestamp;
Eino-Ville Talvalac31dc7e2017-01-31 17:35:41 -0800210 mFrameCount++;
Igor Murashkine3a9f962013-05-08 18:03:15 -0700211
212 return OK;
213}
214
215status_t Camera3OutputStream::returnBufferCheckedLocked(
216 const camera3_stream_buffer &buffer,
217 nsecs_t timestamp,
218 bool output,
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -0700219 const std::vector<size_t>& surface_ids,
Igor Murashkine3a9f962013-05-08 18:03:15 -0700220 /*out*/
221 sp<Fence> *releaseFenceOut) {
222
223 (void)output;
224 ALOG_ASSERT(output, "Expected output to be true");
225
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800226 status_t res;
Igor Murashkin5a1798a2013-05-07 10:58:13 -0700227
Yin-Chia Yeh4c9736f2015-03-05 15:01:36 -0800228 // Fence management - always honor release fence from HAL
229 sp<Fence> releaseFence = new Fence(buffer.release_fence);
Igor Murashkin5a1798a2013-05-07 10:58:13 -0700230 int anwReleaseFence = releaseFence->dup();
231
232 /**
Zhijun He124ccf42013-05-22 14:01:30 -0700233 * Release the lock briefly to avoid deadlock with
234 * StreamingProcessor::startStream -> Camera3Stream::isConfiguring (this
235 * thread will go into StreamingProcessor::onFrameAvailable) during
236 * queueBuffer
237 */
238 sp<ANativeWindow> currentConsumer = mConsumer;
Yin-Chia Yeha1b56c82019-03-27 15:50:39 -0700239 StreamState state = mState;
Zhijun He124ccf42013-05-22 14:01:30 -0700240 mLock.unlock();
241
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800242 ANativeWindowBuffer *anwBuffer = container_of(buffer.buffer, ANativeWindowBuffer, handle);
Zhijun He124ccf42013-05-22 14:01:30 -0700243 /**
Igor Murashkin5a1798a2013-05-07 10:58:13 -0700244 * Return buffer back to ANativeWindow
245 */
Shuzhen Wangf0c4a6b2018-09-05 09:36:14 -0700246 if (buffer.status == CAMERA3_BUFFER_STATUS_ERROR || mDropBuffers || timestamp == 0) {
Igor Murashkin5a1798a2013-05-07 10:58:13 -0700247 // Cancel buffer
Chien-Yu Chena936ac22017-10-23 15:59:49 -0700248 if (mDropBuffers) {
249 ALOGV("%s: Dropping a frame for stream %d.", __FUNCTION__, mId);
Shuzhen Wangf0c4a6b2018-09-05 09:36:14 -0700250 } else if (buffer.status == CAMERA3_BUFFER_STATUS_ERROR) {
Yin-Chia Yeha1b56c82019-03-27 15:50:39 -0700251 ALOGV("%s: A frame is dropped for stream %d due to buffer error.", __FUNCTION__, mId);
Shuzhen Wangf0c4a6b2018-09-05 09:36:14 -0700252 } else {
253 ALOGE("%s: Stream %d: timestamp shouldn't be 0", __FUNCTION__, mId);
Chien-Yu Chena936ac22017-10-23 15:59:49 -0700254 }
255
Zhijun He124ccf42013-05-22 14:01:30 -0700256 res = currentConsumer->cancelBuffer(currentConsumer.get(),
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800257 anwBuffer,
Igor Murashkin5a1798a2013-05-07 10:58:13 -0700258 anwReleaseFence);
Yin-Chia Yeha1b56c82019-03-27 15:50:39 -0700259 if (shouldLogError(res, state)) {
Igor Murashkin5a1798a2013-05-07 10:58:13 -0700260 ALOGE("%s: Stream %d: Error cancelling buffer to native window:"
Igor Murashkine3a9f962013-05-08 18:03:15 -0700261 " %s (%d)", __FUNCTION__, mId, strerror(-res), res);
Igor Murashkin5a1798a2013-05-07 10:58:13 -0700262 }
Zhijun He1ff811b2016-01-26 14:39:51 -0800263
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800264 notifyBufferReleased(anwBuffer);
Zhijun He1ff811b2016-01-26 14:39:51 -0800265 if (mUseBufferManager) {
266 // Return this buffer back to buffer manager.
Shuzhen Wang0160ddd2019-08-15 09:11:56 -0700267 mBufferProducerListener->onBufferReleased();
Zhijun He1ff811b2016-01-26 14:39:51 -0800268 }
Igor Murashkin5a1798a2013-05-07 10:58:13 -0700269 } else {
Ruchit Sharmae0711f22014-08-18 13:48:24 -0400270 if (mTraceFirstBuffer && (stream_type == CAMERA3_STREAM_OUTPUT)) {
271 {
272 char traceLog[48];
273 snprintf(traceLog, sizeof(traceLog), "Stream %d: first full buffer\n", mId);
274 ATRACE_NAME(traceLog);
275 }
276 mTraceFirstBuffer = false;
277 }
278
Shuzhen Wang13a69632016-01-26 09:51:07 -0800279 /* Certain consumers (such as AudioSource or HardwareComposer) use
280 * MONOTONIC time, causing time misalignment if camera timestamp is
Shuzhen Wangc28dccc2016-02-11 23:48:46 -0800281 * in BOOTTIME. Do the conversion if necessary. */
282 res = native_window_set_buffers_timestamp(mConsumer.get(),
283 mUseMonoTimestamp ? timestamp - mTimestampOffset : timestamp);
284 if (res != OK) {
285 ALOGE("%s: Stream %d: Error setting timestamp: %s (%d)",
286 __FUNCTION__, mId, strerror(-res), res);
287 return res;
Yin-Chia Yeh4c9736f2015-03-05 15:01:36 -0800288 }
Shuzhen Wangabbcb6b2020-12-09 22:32:44 -0800289 // If this is a JPEG output, and image dump mask is set, save image to
290 // disk.
291 if (getFormat() == HAL_PIXEL_FORMAT_BLOB && getDataSpace() == HAL_DATASPACE_V0_JFIF &&
292 mImageDumpMask) {
293 dumpImageToDisk(timestamp, anwBuffer, anwReleaseFence);
294 }
Yin-Chia Yeh4c9736f2015-03-05 15:01:36 -0800295
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -0700296 res = queueBufferToConsumer(currentConsumer, anwBuffer, anwReleaseFence, surface_ids);
Yin-Chia Yeha1b56c82019-03-27 15:50:39 -0700297 if (shouldLogError(res, state)) {
298 ALOGE("%s: Stream %d: Error queueing buffer to native window:"
299 " %s (%d)", __FUNCTION__, mId, strerror(-res), res);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800300 }
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800301 }
Zhijun He124ccf42013-05-22 14:01:30 -0700302 mLock.lock();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700303
304 // Once a valid buffer has been returned to the queue, can no longer
305 // dequeue all buffers for preallocation.
306 if (buffer.status != CAMERA3_BUFFER_STATUS_ERROR) {
307 mStreamUnpreparable = true;
308 }
309
Igor Murashkin5a1798a2013-05-07 10:58:13 -0700310 if (res != OK) {
311 close(anwReleaseFence);
Igor Murashkin5a1798a2013-05-07 10:58:13 -0700312 }
313
Igor Murashkine3a9f962013-05-08 18:03:15 -0700314 *releaseFenceOut = releaseFence;
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800315
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700316 return res;
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800317}
318
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800319void Camera3OutputStream::dump(int fd, const Vector<String16> &args) const {
320 (void) args;
321 String8 lines;
322 lines.appendFormat(" Stream[%d]: Output\n", mId);
Eino-Ville Talvala727d1722015-06-09 13:44:19 -0700323 lines.appendFormat(" Consumer name: %s\n", mConsumerName.string());
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800324 write(fd, lines.string(), lines.size());
Igor Murashkine3a9f962013-05-08 18:03:15 -0700325
326 Camera3IOStreamBase::dump(fd, args);
Shuzhen Wang686f6442017-06-20 16:16:04 -0700327
328 mDequeueBufferLatency.dump(fd,
329 " DequeueBuffer latency histogram:");
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800330}
331
332status_t Camera3OutputStream::setTransform(int transform) {
333 ATRACE_CALL();
334 Mutex::Autolock l(mLock);
335 return setTransformLocked(transform);
336}
337
338status_t Camera3OutputStream::setTransformLocked(int transform) {
339 status_t res = OK;
340 if (mState == STATE_ERROR) {
341 ALOGE("%s: Stream in error state", __FUNCTION__);
342 return INVALID_OPERATION;
343 }
344
345 mTransform = transform;
346 if (mState == STATE_CONFIGURED) {
347 res = native_window_set_buffers_transform(mConsumer.get(),
348 transform);
349 if (res != OK) {
350 ALOGE("%s: Unable to configure stream transform to %x: %s (%d)",
351 __FUNCTION__, transform, strerror(-res), res);
352 }
353 }
354 return res;
355}
356
357status_t Camera3OutputStream::configureQueueLocked() {
358 status_t res;
359
Ruchit Sharmae0711f22014-08-18 13:48:24 -0400360 mTraceFirstBuffer = true;
Igor Murashkine3a9f962013-05-08 18:03:15 -0700361 if ((res = Camera3IOStreamBase::configureQueueLocked()) != OK) {
362 return res;
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800363 }
364
Shuzhen Wang0129d522016-10-30 22:43:41 -0700365 if ((res = configureConsumerQueueLocked()) != OK) {
366 return res;
367 }
368
369 // Set dequeueBuffer/attachBuffer timeout if the consumer is not hw composer or hw texture.
370 // We need skip these cases as timeout will disable the non-blocking (async) mode.
371 if (!(isConsumedByHWComposer() || isConsumedByHWTexture())) {
Yin-Chia Yehbf1b8b92019-03-06 14:56:08 -0800372 if (mUseBufferManager) {
373 // When buffer manager is handling the buffer, we should have available buffers in
374 // buffer queue before we calls into dequeueBuffer because buffer manager is tracking
375 // free buffers.
376 // There are however some consumer side feature (ImageReader::discardFreeBuffers) that
377 // can discard free buffers without notifying buffer manager. We want the timeout to
378 // happen immediately here so buffer manager can try to update its internal state and
379 // try to allocate a buffer instead of waiting.
380 mConsumer->setDequeueTimeout(0);
381 } else {
382 mConsumer->setDequeueTimeout(kDequeueBufferTimeout);
383 }
Shuzhen Wang0129d522016-10-30 22:43:41 -0700384 }
385
386 return OK;
387}
388
389status_t Camera3OutputStream::configureConsumerQueueLocked() {
390 status_t res;
391
392 mTraceFirstBuffer = true;
393
Igor Murashkine3a9f962013-05-08 18:03:15 -0700394 ALOG_ASSERT(mConsumer != 0, "mConsumer should never be NULL");
395
Zhijun He125684a2015-12-26 15:07:30 -0800396 // Configure consumer-side ANativeWindow interface. The listener may be used
397 // to notify buffer manager (if it is used) of the returned buffers.
Yin-Chia Yeh017d49c2017-03-31 19:11:00 -0700398 res = mConsumer->connect(NATIVE_WINDOW_API_CAMERA,
Shuzhen Wang0160ddd2019-08-15 09:11:56 -0700399 /*reportBufferRemoval*/true,
400 /*listener*/mBufferProducerListener);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800401 if (res != OK) {
402 ALOGE("%s: Unable to connect to native window for stream %d",
403 __FUNCTION__, mId);
404 return res;
405 }
406
Eino-Ville Talvala727d1722015-06-09 13:44:19 -0700407 mConsumerName = mConsumer->getConsumerName();
408
Emilian Peev050f5dc2017-05-18 14:43:56 +0100409 res = native_window_set_usage(mConsumer.get(), mUsage);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800410 if (res != OK) {
Emilian Peev050f5dc2017-05-18 14:43:56 +0100411 ALOGE("%s: Unable to configure usage %" PRIu64 " for stream %d",
412 __FUNCTION__, mUsage, mId);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800413 return res;
414 }
415
416 res = native_window_set_scaling_mode(mConsumer.get(),
417 NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
418 if (res != OK) {
419 ALOGE("%s: Unable to configure stream scaling: %s (%d)",
420 __FUNCTION__, strerror(-res), res);
421 return res;
422 }
423
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800424 if (mMaxSize == 0) {
425 // For buffers of known size
Eino-Ville Talvala7d70c5e2014-07-24 18:10:23 -0700426 res = native_window_set_buffers_dimensions(mConsumer.get(),
427 camera3_stream::width, camera3_stream::height);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800428 } else {
429 // For buffers with bounded size
Eino-Ville Talvala7d70c5e2014-07-24 18:10:23 -0700430 res = native_window_set_buffers_dimensions(mConsumer.get(),
431 mMaxSize, 1);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800432 }
433 if (res != OK) {
Eino-Ville Talvala7d70c5e2014-07-24 18:10:23 -0700434 ALOGE("%s: Unable to configure stream buffer dimensions"
435 " %d x %d (maxSize %zu) for stream %d",
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800436 __FUNCTION__, camera3_stream::width, camera3_stream::height,
Eino-Ville Talvala7d70c5e2014-07-24 18:10:23 -0700437 mMaxSize, mId);
438 return res;
439 }
440 res = native_window_set_buffers_format(mConsumer.get(),
441 camera3_stream::format);
442 if (res != OK) {
443 ALOGE("%s: Unable to configure stream buffer format %#x for stream %d",
444 __FUNCTION__, camera3_stream::format, mId);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800445 return res;
446 }
447
Eino-Ville Talvala3d82c0d2015-02-23 15:19:19 -0800448 res = native_window_set_buffers_data_space(mConsumer.get(),
449 camera3_stream::data_space);
450 if (res != OK) {
451 ALOGE("%s: Unable to configure stream dataspace %#x for stream %d",
452 __FUNCTION__, camera3_stream::data_space, mId);
453 return res;
454 }
455
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800456 int maxConsumerBuffers;
Eino-Ville Talvala727d1722015-06-09 13:44:19 -0700457 res = static_cast<ANativeWindow*>(mConsumer.get())->query(
458 mConsumer.get(),
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800459 NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS, &maxConsumerBuffers);
460 if (res != OK) {
461 ALOGE("%s: Unable to query consumer undequeued"
462 " buffer count for stream %d", __FUNCTION__, mId);
463 return res;
464 }
465
Alex Ray20cb3002013-05-28 20:18:22 -0700466 ALOGV("%s: Consumer wants %d buffers, HAL wants %d", __FUNCTION__,
467 maxConsumerBuffers, camera3_stream::max_buffers);
468 if (camera3_stream::max_buffers == 0) {
Zhijun He2ab500c2013-07-23 08:02:53 -0700469 ALOGE("%s: Camera HAL requested max_buffer count: %d, requires at least 1",
Alex Ray20cb3002013-05-28 20:18:22 -0700470 __FUNCTION__, camera3_stream::max_buffers);
471 return INVALID_OPERATION;
472 }
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800473
474 mTotalBufferCount = maxConsumerBuffers + camera3_stream::max_buffers;
Zhijun He6adc9cc2014-04-15 14:09:55 -0700475 mHandoutTotalBufferCount = 0;
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800476 mFrameCount = 0;
477 mLastTimestamp = 0;
Shuzhen Wangc28dccc2016-02-11 23:48:46 -0800478 mUseMonoTimestamp = (isConsumedByHWComposer() | isVideoStream());
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800479
480 res = native_window_set_buffer_count(mConsumer.get(),
481 mTotalBufferCount);
482 if (res != OK) {
483 ALOGE("%s: Unable to set buffer count for stream %d",
484 __FUNCTION__, mId);
485 return res;
486 }
487
488 res = native_window_set_buffers_transform(mConsumer.get(),
489 mTransform);
490 if (res != OK) {
491 ALOGE("%s: Unable to configure stream transform to %x: %s (%d)",
492 __FUNCTION__, mTransform, strerror(-res), res);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700493 return res;
Zhijun Hef0645c12016-08-02 00:58:11 -0700494 }
495
Zhijun He125684a2015-12-26 15:07:30 -0800496 /**
Zhijun Heedd41ae2016-02-03 14:45:53 -0800497 * Camera3 Buffer manager is only supported by HAL3.3 onwards, as the older HALs requires
Zhijun He125684a2015-12-26 15:07:30 -0800498 * buffers to be statically allocated for internal static buffer registration, while the
499 * buffers provided by buffer manager are really dynamically allocated. Camera3Device only
Zhijun Heedd41ae2016-02-03 14:45:53 -0800500 * sets the mBufferManager if device version is > HAL3.2, which guarantees that the buffer
501 * manager setup is skipped in below code. Note that HAL3.2 is also excluded here, as some
502 * HAL3.2 devices may not support the dynamic buffer registeration.
Yin-Chia Yehb6578902019-04-16 13:36:16 -0700503 * Also Camera3BufferManager does not support display/texture streams as they have its own
504 * buffer management logic.
Zhijun He125684a2015-12-26 15:07:30 -0800505 */
Yin-Chia Yehb6578902019-04-16 13:36:16 -0700506 if (mBufferManager != 0 && mSetId > CAMERA3_STREAM_SET_ID_INVALID &&
507 !(isConsumedByHWComposer() || isConsumedByHWTexture())) {
Emilian Peev050f5dc2017-05-18 14:43:56 +0100508 uint64_t consumerUsage = 0;
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -0700509 getEndpointUsage(&consumerUsage);
Zhijun He125684a2015-12-26 15:07:30 -0800510 StreamInfo streamInfo(
511 getId(), getStreamSetId(), getWidth(), getHeight(), getFormat(), getDataSpace(),
Emilian Peev050f5dc2017-05-18 14:43:56 +0100512 mUsage | consumerUsage, mTotalBufferCount,
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -0700513 /*isConfigured*/true);
514 wp<Camera3OutputStream> weakThis(this);
515 res = mBufferManager->registerStream(weakThis,
516 streamInfo);
Zhijun He125684a2015-12-26 15:07:30 -0800517 if (res == OK) {
518 // Disable buffer allocation for this BufferQueue, buffer manager will take over
519 // the buffer allocation responsibility.
520 mConsumer->getIGraphicBufferProducer()->allowAllocation(false);
521 mUseBufferManager = true;
522 } else {
523 ALOGE("%s: Unable to register stream %d to camera3 buffer manager, "
524 "(error %d %s), fall back to BufferQueue for buffer management!",
525 __FUNCTION__, mId, res, strerror(-res));
526 }
527 }
528
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800529 return OK;
530}
531
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800532status_t Camera3OutputStream::getBufferLockedCommon(ANativeWindowBuffer** anb, int* fenceFd) {
Jayant Chowdharyd4776262020-06-23 23:45:57 -0700533 ATRACE_HFR_CALL();
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800534 status_t res;
535
536 if ((res = getBufferPreconditionCheckLocked()) != OK) {
537 return res;
538 }
539
540 bool gotBufferFromManager = false;
541
542 if (mUseBufferManager) {
543 sp<GraphicBuffer> gb;
544 res = mBufferManager->getBufferForStream(getId(), getStreamSetId(), &gb, fenceFd);
545 if (res == OK) {
546 // Attach this buffer to the bufferQueue: the buffer will be in dequeue state after a
547 // successful return.
548 *anb = gb.get();
549 res = mConsumer->attachBuffer(*anb);
Yin-Chia Yeha1b56c82019-03-27 15:50:39 -0700550 if (shouldLogError(res, mState)) {
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800551 ALOGE("%s: Stream %d: Can't attach the output buffer to this surface: %s (%d)",
552 __FUNCTION__, mId, strerror(-res), res);
Yin-Chia Yeha1b56c82019-03-27 15:50:39 -0700553 }
554 if (res != OK) {
Yin-Chia Yehbf1b8b92019-03-06 14:56:08 -0800555 checkRetAndSetAbandonedLocked(res);
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800556 return res;
557 }
558 gotBufferFromManager = true;
559 ALOGV("Stream %d: Attached new buffer", getId());
560 } else if (res == ALREADY_EXISTS) {
561 // Have sufficient free buffers already attached, can just
562 // dequeue from buffer queue
563 ALOGV("Stream %d: Reusing attached buffer", getId());
564 gotBufferFromManager = false;
565 } else if (res != OK) {
566 ALOGE("%s: Stream %d: Can't get next output buffer from buffer manager: %s (%d)",
567 __FUNCTION__, mId, strerror(-res), res);
568 return res;
569 }
570 }
571 if (!gotBufferFromManager) {
572 /**
573 * Release the lock briefly to avoid deadlock for below scenario:
574 * Thread 1: StreamingProcessor::startStream -> Camera3Stream::isConfiguring().
575 * This thread acquired StreamingProcessor lock and try to lock Camera3Stream lock.
576 * Thread 2: Camera3Stream::returnBuffer->StreamingProcessor::onFrameAvailable().
577 * This thread acquired Camera3Stream lock and bufferQueue lock, and try to lock
578 * StreamingProcessor lock.
579 * Thread 3: Camera3Stream::getBuffer(). This thread acquired Camera3Stream lock
580 * and try to lock bufferQueue lock.
581 * Then there is circular locking dependency.
582 */
583 sp<ANativeWindow> currentConsumer = mConsumer;
584 mLock.unlock();
585
Shuzhen Wang686f6442017-06-20 16:16:04 -0700586 nsecs_t dequeueStart = systemTime(SYSTEM_TIME_MONOTONIC);
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800587 res = currentConsumer->dequeueBuffer(currentConsumer.get(), anb, fenceFd);
Shuzhen Wang686f6442017-06-20 16:16:04 -0700588 nsecs_t dequeueEnd = systemTime(SYSTEM_TIME_MONOTONIC);
589 mDequeueBufferLatency.add(dequeueStart, dequeueEnd);
590
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800591 mLock.lock();
Yin-Chia Yehbf1b8b92019-03-06 14:56:08 -0800592
593 if (mUseBufferManager && res == TIMED_OUT) {
594 checkRemovedBuffersLocked();
595
596 sp<GraphicBuffer> gb;
597 res = mBufferManager->getBufferForStream(
598 getId(), getStreamSetId(), &gb, fenceFd, /*noFreeBuffer*/true);
599
600 if (res == OK) {
601 // Attach this buffer to the bufferQueue: the buffer will be in dequeue state after
602 // a successful return.
603 *anb = gb.get();
604 res = mConsumer->attachBuffer(*anb);
605 gotBufferFromManager = true;
606 ALOGV("Stream %d: Attached new buffer", getId());
607
608 if (res != OK) {
Yin-Chia Yeha1b56c82019-03-27 15:50:39 -0700609 if (shouldLogError(res, mState)) {
610 ALOGE("%s: Stream %d: Can't attach the output buffer to this surface:"
611 " %s (%d)", __FUNCTION__, mId, strerror(-res), res);
612 }
Yin-Chia Yehbf1b8b92019-03-06 14:56:08 -0800613 checkRetAndSetAbandonedLocked(res);
614 return res;
615 }
616 } else {
617 ALOGE("%s: Stream %d: Can't get next output buffer from buffer manager:"
618 " %s (%d)", __FUNCTION__, mId, strerror(-res), res);
619 return res;
620 }
621 } else if (res != OK) {
Yin-Chia Yeha1b56c82019-03-27 15:50:39 -0700622 if (shouldLogError(res, mState)) {
623 ALOGE("%s: Stream %d: Can't dequeue next output buffer: %s (%d)",
624 __FUNCTION__, mId, strerror(-res), res);
625 }
Yin-Chia Yehbf1b8b92019-03-06 14:56:08 -0800626 checkRetAndSetAbandonedLocked(res);
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800627 return res;
628 }
629 }
630
Yin-Chia Yeh017d49c2017-03-31 19:11:00 -0700631 if (res == OK) {
Yin-Chia Yehbf1b8b92019-03-06 14:56:08 -0800632 checkRemovedBuffersLocked();
Yin-Chia Yeh017d49c2017-03-31 19:11:00 -0700633 }
634
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800635 return res;
636}
637
Yin-Chia Yehbf1b8b92019-03-06 14:56:08 -0800638void Camera3OutputStream::checkRemovedBuffersLocked(bool notifyBufferManager) {
639 std::vector<sp<GraphicBuffer>> removedBuffers;
640 status_t res = mConsumer->getAndFlushRemovedBuffers(&removedBuffers);
641 if (res == OK) {
642 onBuffersRemovedLocked(removedBuffers);
643
644 if (notifyBufferManager && mUseBufferManager && removedBuffers.size() > 0) {
645 mBufferManager->onBuffersRemoved(getId(), getStreamSetId(), removedBuffers.size());
646 }
647 }
648}
649
650void Camera3OutputStream::checkRetAndSetAbandonedLocked(status_t res) {
651 // Only transition to STATE_ABANDONED from STATE_CONFIGURED. (If it is
652 // STATE_PREPARING, let prepareNextBuffer handle the error.)
653 if ((res == NO_INIT || res == DEAD_OBJECT) && mState == STATE_CONFIGURED) {
654 mState = STATE_ABANDONED;
655 }
656}
657
Yin-Chia Yeha1b56c82019-03-27 15:50:39 -0700658bool Camera3OutputStream::shouldLogError(status_t res, StreamState state) {
659 if (res == OK) {
660 return false;
661 }
662 if ((res == DEAD_OBJECT || res == NO_INIT) && state == STATE_ABANDONED) {
663 return false;
664 }
665 return true;
666}
667
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800668status_t Camera3OutputStream::disconnectLocked() {
669 status_t res;
670
Igor Murashkine3a9f962013-05-08 18:03:15 -0700671 if ((res = Camera3IOStreamBase::disconnectLocked()) != OK) {
672 return res;
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800673 }
674
Zhijun He5d677d12016-05-29 16:52:39 -0700675 // Stream configuration was not finished (can only be in STATE_IN_CONFIG or STATE_CONSTRUCTED
676 // state), don't need change the stream state, return OK.
677 if (mConsumer == nullptr) {
678 return OK;
679 }
680
Zhijun He125684a2015-12-26 15:07:30 -0800681 ALOGV("%s: disconnecting stream %d from native window", __FUNCTION__, getId());
682
Igor Murashkine3a9f962013-05-08 18:03:15 -0700683 res = native_window_api_disconnect(mConsumer.get(),
684 NATIVE_WINDOW_API_CAMERA);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800685 /**
686 * This is not an error. if client calling process dies, the window will
687 * also die and all calls to it will return DEAD_OBJECT, thus it's already
688 * "disconnected"
689 */
690 if (res == DEAD_OBJECT) {
691 ALOGW("%s: While disconnecting stream %d from native window, the"
692 " native window died from under us", __FUNCTION__, mId);
693 }
694 else if (res != OK) {
Igor Murashkine3a9f962013-05-08 18:03:15 -0700695 ALOGE("%s: Unable to disconnect stream %d from native window "
696 "(error %d %s)",
697 __FUNCTION__, mId, res, strerror(-res));
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800698 mState = STATE_ERROR;
699 return res;
700 }
701
Zhijun He125684a2015-12-26 15:07:30 -0800702 // Since device is already idle, there is no getBuffer call to buffer manager, unregister the
703 // stream at this point should be safe.
704 if (mUseBufferManager) {
705 res = mBufferManager->unregisterStream(getId(), getStreamSetId());
706 if (res != OK) {
707 ALOGE("%s: Unable to unregister stream %d from buffer manager "
708 "(error %d %s)", __FUNCTION__, mId, res, strerror(-res));
709 mState = STATE_ERROR;
710 return res;
711 }
712 // Note that, to make prepare/teardown case work, we must not mBufferManager.clear(), as
713 // the stream is still in usable state after this call.
714 mUseBufferManager = false;
715 }
716
Igor Murashkine3a9f962013-05-08 18:03:15 -0700717 mState = (mState == STATE_IN_RECONFIG) ? STATE_IN_CONFIG
718 : STATE_CONSTRUCTED;
Shuzhen Wang686f6442017-06-20 16:16:04 -0700719
720 mDequeueBufferLatency.log("Stream %d dequeueBuffer latency histogram", mId);
721 mDequeueBufferLatency.reset();
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800722 return OK;
723}
724
Emilian Peev050f5dc2017-05-18 14:43:56 +0100725status_t Camera3OutputStream::getEndpointUsage(uint64_t *usage) const {
Eino-Ville Talvalab2f5b192013-07-30 14:36:03 -0700726
727 status_t res;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700728
Zhijun He5d677d12016-05-29 16:52:39 -0700729 if (mConsumer == nullptr) {
730 // mConsumerUsage was sanitized before the Camera3OutputStream was constructed.
731 *usage = mConsumerUsage;
732 return OK;
733 }
734
Shuzhen Wang0129d522016-10-30 22:43:41 -0700735 res = getEndpointUsageForSurface(usage, mConsumer);
736
737 return res;
738}
739
Emilian Peev35ae8262018-11-08 13:11:32 +0000740void Camera3OutputStream::applyZSLUsageQuirk(int format, uint64_t *consumerUsage /*inout*/) {
741 if (consumerUsage == nullptr) {
742 return;
743 }
Shuzhen Wang0129d522016-10-30 22:43:41 -0700744
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700745 // If an opaque output stream's endpoint is ImageReader, add
Yin-Chia Yeh47cf8e62017-04-04 13:00:03 -0700746 // GRALLOC_USAGE_HW_CAMERA_ZSL to the usage so HAL knows it will be used
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700747 // for the ZSL use case.
748 // Assume it's for ImageReader if the consumer usage doesn't have any of these bits set:
749 // 1. GRALLOC_USAGE_HW_TEXTURE
750 // 2. GRALLOC_USAGE_HW_RENDER
751 // 3. GRALLOC_USAGE_HW_COMPOSER
752 // 4. GRALLOC_USAGE_HW_VIDEO_ENCODER
Emilian Peev35ae8262018-11-08 13:11:32 +0000753 if (format == HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED &&
754 (*consumerUsage & (GRALLOC_USAGE_HW_TEXTURE | GRALLOC_USAGE_HW_RENDER |
Shuzhen Wang0129d522016-10-30 22:43:41 -0700755 GRALLOC_USAGE_HW_COMPOSER | GRALLOC_USAGE_HW_VIDEO_ENCODER)) == 0) {
Emilian Peev35ae8262018-11-08 13:11:32 +0000756 *consumerUsage |= GRALLOC_USAGE_HW_CAMERA_ZSL;
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700757 }
Emilian Peev35ae8262018-11-08 13:11:32 +0000758}
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700759
Emilian Peev35ae8262018-11-08 13:11:32 +0000760status_t Camera3OutputStream::getEndpointUsageForSurface(uint64_t *usage,
761 const sp<Surface>& surface) const {
762 status_t res;
763 uint64_t u = 0;
764
765 res = native_window_get_consumer_usage(static_cast<ANativeWindow*>(surface.get()), &u);
766 applyZSLUsageQuirk(camera3_stream::format, &u);
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700767 *usage = u;
Eino-Ville Talvalab2f5b192013-07-30 14:36:03 -0700768 return res;
769}
770
Chien-Yu Chen85a64552015-08-28 15:46:12 -0700771bool Camera3OutputStream::isVideoStream() const {
Emilian Peev050f5dc2017-05-18 14:43:56 +0100772 uint64_t usage = 0;
Chien-Yu Chen85a64552015-08-28 15:46:12 -0700773 status_t res = getEndpointUsage(&usage);
774 if (res != OK) {
775 ALOGE("%s: getting end point usage failed: %s (%d).", __FUNCTION__, strerror(-res), res);
776 return false;
777 }
778
779 return (usage & GRALLOC_USAGE_HW_VIDEO_ENCODER) != 0;
780}
781
Zhijun He125684a2015-12-26 15:07:30 -0800782status_t Camera3OutputStream::setBufferManager(sp<Camera3BufferManager> bufferManager) {
783 Mutex::Autolock l(mLock);
784 if (mState != STATE_CONSTRUCTED) {
Zhijun He5d677d12016-05-29 16:52:39 -0700785 ALOGE("%s: this method can only be called when stream in CONSTRUCTED state.",
Zhijun He125684a2015-12-26 15:07:30 -0800786 __FUNCTION__);
787 return INVALID_OPERATION;
788 }
789 mBufferManager = bufferManager;
790
791 return OK;
792}
793
Emilian Peev40ead602017-09-26 15:46:36 +0100794status_t Camera3OutputStream::updateStream(const std::vector<sp<Surface>> &/*outputSurfaces*/,
795 const std::vector<OutputStreamInfo> &/*outputInfo*/,
796 const std::vector<size_t> &/*removedSurfaceIds*/,
797 KeyedVector<sp<Surface>, size_t> * /*outputMapo*/) {
798 ALOGE("%s: this method is not supported!", __FUNCTION__);
799 return INVALID_OPERATION;
800}
801
Shuzhen Wang0160ddd2019-08-15 09:11:56 -0700802void Camera3OutputStream::BufferProducerListener::onBufferReleased() {
Zhijun He125684a2015-12-26 15:07:30 -0800803 sp<Camera3OutputStream> stream = mParent.promote();
804 if (stream == nullptr) {
805 ALOGV("%s: Parent camera3 output stream was destroyed", __FUNCTION__);
806 return;
807 }
808
809 Mutex::Autolock l(stream->mLock);
810 if (!(stream->mUseBufferManager)) {
811 return;
812 }
813
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -0700814 ALOGV("Stream %d: Buffer released", stream->getId());
Yin-Chia Yeh89954d92017-05-21 17:28:53 -0700815 bool shouldFreeBuffer = false;
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -0700816 status_t res = stream->mBufferManager->onBufferReleased(
Yin-Chia Yeh89954d92017-05-21 17:28:53 -0700817 stream->getId(), stream->getStreamSetId(), &shouldFreeBuffer);
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -0700818 if (res != OK) {
819 ALOGE("%s: signaling buffer release to buffer manager failed: %s (%d).", __FUNCTION__,
820 strerror(-res), res);
821 stream->mState = STATE_ERROR;
822 }
Yin-Chia Yeh89954d92017-05-21 17:28:53 -0700823
824 if (shouldFreeBuffer) {
825 sp<GraphicBuffer> buffer;
826 // Detach and free a buffer (when buffer goes out of scope)
827 stream->detachBufferLocked(&buffer, /*fenceFd*/ nullptr);
828 if (buffer.get() != nullptr) {
829 stream->mBufferManager->notifyBufferRemoved(
830 stream->getId(), stream->getStreamSetId());
831 }
832 }
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -0700833}
834
Shuzhen Wang0160ddd2019-08-15 09:11:56 -0700835void Camera3OutputStream::BufferProducerListener::onBuffersDiscarded(
836 const std::vector<sp<GraphicBuffer>>& buffers) {
837 sp<Camera3OutputStream> stream = mParent.promote();
838 if (stream == nullptr) {
839 ALOGV("%s: Parent camera3 output stream was destroyed", __FUNCTION__);
840 return;
841 }
842
843 if (buffers.size() > 0) {
844 Mutex::Autolock l(stream->mLock);
845 stream->onBuffersRemovedLocked(buffers);
846 if (stream->mUseBufferManager) {
847 stream->mBufferManager->onBuffersRemoved(stream->getId(),
848 stream->getStreamSetId(), buffers.size());
849 }
850 ALOGV("Stream %d: %zu Buffers discarded.", stream->getId(), buffers.size());
851 }
852}
853
Yin-Chia Yeh017d49c2017-03-31 19:11:00 -0700854void Camera3OutputStream::onBuffersRemovedLocked(
855 const std::vector<sp<GraphicBuffer>>& removedBuffers) {
Yin-Chia Yehdb1e8642017-07-14 15:19:30 -0700856 sp<Camera3StreamBufferFreedListener> callback = mBufferFreedListener.promote();
Yin-Chia Yeh017d49c2017-03-31 19:11:00 -0700857 if (callback != nullptr) {
Chih-Hung Hsieh48fc6192017-08-04 14:37:31 -0700858 for (const auto& gb : removedBuffers) {
Yin-Chia Yeh017d49c2017-03-31 19:11:00 -0700859 callback->onBufferFreed(mId, gb->handle);
860 }
861 }
862}
863
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -0700864status_t Camera3OutputStream::detachBuffer(sp<GraphicBuffer>* buffer, int* fenceFd) {
865 Mutex::Autolock l(mLock);
Yin-Chia Yeh89954d92017-05-21 17:28:53 -0700866 return detachBufferLocked(buffer, fenceFd);
867}
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -0700868
Yin-Chia Yeh89954d92017-05-21 17:28:53 -0700869status_t Camera3OutputStream::detachBufferLocked(sp<GraphicBuffer>* buffer, int* fenceFd) {
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -0700870 ALOGV("Stream %d: detachBuffer", getId());
871 if (buffer == nullptr) {
872 return BAD_VALUE;
873 }
874
Zhijun He125684a2015-12-26 15:07:30 -0800875 sp<Fence> fence;
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -0700876 status_t res = mConsumer->detachNextBuffer(buffer, &fence);
Zhijun He125684a2015-12-26 15:07:30 -0800877 if (res == NO_MEMORY) {
878 // This may rarely happen, which indicates that the released buffer was freed by other
879 // call (e.g., attachBuffer, dequeueBuffer etc.) before reaching here. We should notify the
880 // buffer manager that this buffer has been freed. It's not fatal, but should be avoided,
881 // therefore log a warning.
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -0700882 *buffer = 0;
Zhijun He125684a2015-12-26 15:07:30 -0800883 ALOGW("%s: the released buffer has already been freed by the buffer queue!", __FUNCTION__);
884 } else if (res != OK) {
Eino-Ville Talvalaff51b472016-06-28 15:26:19 -0700885 // Treat other errors as abandonment
Yin-Chia Yeha1b56c82019-03-27 15:50:39 -0700886 if (shouldLogError(res, mState)) {
887 ALOGE("%s: detach next buffer failed: %s (%d).", __FUNCTION__, strerror(-res), res);
888 }
Eino-Ville Talvalaff51b472016-06-28 15:26:19 -0700889 mState = STATE_ABANDONED;
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -0700890 return res;
Zhijun He125684a2015-12-26 15:07:30 -0800891 }
892
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -0700893 if (fenceFd != nullptr) {
894 if (fence!= 0 && fence->isValid()) {
895 *fenceFd = fence->dup();
896 } else {
897 *fenceFd = -1;
898 }
Zhijun He125684a2015-12-26 15:07:30 -0800899 }
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -0700900
Yin-Chia Yehbf1b8b92019-03-06 14:56:08 -0800901 // Here we assume detachBuffer is called by buffer manager so it doesn't need to be notified
902 checkRemovedBuffersLocked(/*notifyBufferManager*/false);
Yin-Chia Yeh017d49c2017-03-31 19:11:00 -0700903 return res;
Zhijun He125684a2015-12-26 15:07:30 -0800904}
Shuzhen Wang13a69632016-01-26 09:51:07 -0800905
Chien-Yu Chena936ac22017-10-23 15:59:49 -0700906status_t Camera3OutputStream::dropBuffers(bool dropping) {
907 Mutex::Autolock l(mLock);
908 mDropBuffers = dropping;
909 return OK;
910}
911
Shuzhen Wang5c22c152017-12-31 17:12:25 -0800912const String8& Camera3OutputStream::getPhysicalCameraId() const {
913 Mutex::Autolock l(mLock);
914 return physicalCameraId();
915}
916
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800917status_t Camera3OutputStream::notifyBufferReleased(ANativeWindowBuffer* /*anwBuffer*/) {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700918 return OK;
919}
920
921bool Camera3OutputStream::isConsumerConfigurationDeferred(size_t surface_id) const {
Zhijun He5d677d12016-05-29 16:52:39 -0700922 Mutex::Autolock l(mLock);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700923
924 if (surface_id != 0) {
Shuzhen Wang758c2152017-01-10 18:26:18 -0800925 ALOGE("%s: surface_id %zu for Camera3OutputStream should be 0!", __FUNCTION__, surface_id);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700926 }
Zhijun He5d677d12016-05-29 16:52:39 -0700927 return mConsumer == nullptr;
928}
929
Shuzhen Wang758c2152017-01-10 18:26:18 -0800930status_t Camera3OutputStream::setConsumers(const std::vector<sp<Surface>>& consumers) {
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800931 Mutex::Autolock l(mLock);
Shuzhen Wang758c2152017-01-10 18:26:18 -0800932 if (consumers.size() != 1) {
933 ALOGE("%s: it's illegal to set %zu consumer surfaces!",
934 __FUNCTION__, consumers.size());
935 return INVALID_OPERATION;
936 }
937 if (consumers[0] == nullptr) {
938 ALOGE("%s: it's illegal to set null consumer surface!", __FUNCTION__);
Zhijun He5d677d12016-05-29 16:52:39 -0700939 return INVALID_OPERATION;
940 }
941
942 if (mConsumer != nullptr) {
943 ALOGE("%s: consumer surface was already set!", __FUNCTION__);
944 return INVALID_OPERATION;
945 }
946
Shuzhen Wang758c2152017-01-10 18:26:18 -0800947 mConsumer = consumers[0];
Zhijun He5d677d12016-05-29 16:52:39 -0700948 return OK;
949}
950
Shuzhen Wang13a69632016-01-26 09:51:07 -0800951bool Camera3OutputStream::isConsumedByHWComposer() const {
Emilian Peev050f5dc2017-05-18 14:43:56 +0100952 uint64_t usage = 0;
Shuzhen Wang13a69632016-01-26 09:51:07 -0800953 status_t res = getEndpointUsage(&usage);
954 if (res != OK) {
955 ALOGE("%s: getting end point usage failed: %s (%d).", __FUNCTION__, strerror(-res), res);
956 return false;
957 }
958
959 return (usage & GRALLOC_USAGE_HW_COMPOSER) != 0;
960}
961
Zhijun Hef0645c12016-08-02 00:58:11 -0700962bool Camera3OutputStream::isConsumedByHWTexture() const {
Emilian Peev050f5dc2017-05-18 14:43:56 +0100963 uint64_t usage = 0;
Zhijun Hef0645c12016-08-02 00:58:11 -0700964 status_t res = getEndpointUsage(&usage);
965 if (res != OK) {
966 ALOGE("%s: getting end point usage failed: %s (%d).", __FUNCTION__, strerror(-res), res);
967 return false;
968 }
969
970 return (usage & GRALLOC_USAGE_HW_TEXTURE) != 0;
971}
972
Shuzhen Wangabbcb6b2020-12-09 22:32:44 -0800973void Camera3OutputStream::dumpImageToDisk(nsecs_t timestamp,
974 ANativeWindowBuffer* anwBuffer, int fence) {
975 // Deriver output file name
976 std::string fileExtension = "jpg";
977 char imageFileName[64];
978 time_t now = time(0);
979 tm *localTime = localtime(&now);
980 snprintf(imageFileName, sizeof(imageFileName), "IMG_%4d%02d%02d_%02d%02d%02d_%" PRId64 ".%s",
981 1900 + localTime->tm_year, localTime->tm_mon, localTime->tm_mday,
982 localTime->tm_hour, localTime->tm_min, localTime->tm_sec,
983 timestamp, fileExtension.c_str());
984
985 // Lock the image for CPU read
986 sp<GraphicBuffer> graphicBuffer = GraphicBuffer::from(anwBuffer);
987 void* mapped = nullptr;
988 base::unique_fd fenceFd(dup(fence));
989 status_t res = graphicBuffer->lockAsync(GraphicBuffer::USAGE_SW_READ_OFTEN, &mapped,
990 fenceFd.get());
991 if (res != OK) {
992 ALOGE("%s: Failed to lock the buffer: %s (%d)", __FUNCTION__, strerror(-res), res);
993 return;
994 }
995
996 // Figure out actual file size
997 auto actualJpegSize = android::camera2::JpegProcessor::findJpegSize((uint8_t*)mapped, mMaxSize);
998 if (actualJpegSize == 0) {
999 actualJpegSize = mMaxSize;
1000 }
1001
1002 // Output image data to file
1003 std::string filePath = "/data/misc/cameraserver/";
1004 filePath += imageFileName;
1005 std::ofstream imageFile(filePath.c_str(), std::ofstream::binary);
1006 if (!imageFile.is_open()) {
1007 ALOGE("%s: Unable to create file %s", __FUNCTION__, filePath.c_str());
1008 graphicBuffer->unlock();
1009 return;
1010 }
1011 imageFile.write((const char*)mapped, actualJpegSize);
1012
1013 graphicBuffer->unlock();
1014}
1015
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -08001016}; // namespace camera3
1017
1018}; // namespace android