blob: 0c739e9e24de0b14719f209a476577fd0d954903 [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,
35 sp<ANativeWindow> consumer,
Eino-Ville Talvala3d82c0d2015-02-23 15:19:19 -080036 uint32_t width, uint32_t height, int format,
Yin-Chia Yehb97babb2015-03-12 13:42:44 -070037 android_dataspace dataSpace, camera3_stream_rotation_t rotation) :
Igor Murashkine3a9f962013-05-08 18:03:15 -070038 Camera3IOStreamBase(id, CAMERA3_STREAM_OUTPUT, width, height,
Yin-Chia Yehb97babb2015-03-12 13:42:44 -070039 /*maxSize*/0, format, dataSpace, rotation),
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -080040 mConsumer(consumer),
Ruchit Sharmae0711f22014-08-18 13:48:24 -040041 mTransform(0),
42 mTraceFirstBuffer(true) {
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -080043
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -080044 if (mConsumer == NULL) {
45 ALOGE("%s: Consumer is NULL!", __FUNCTION__);
46 mState = STATE_ERROR;
47 }
48}
49
50Camera3OutputStream::Camera3OutputStream(int id,
51 sp<ANativeWindow> consumer,
Eino-Ville Talvala3d82c0d2015-02-23 15:19:19 -080052 uint32_t width, uint32_t height, size_t maxSize, int format,
Yin-Chia Yehb97babb2015-03-12 13:42:44 -070053 android_dataspace dataSpace, camera3_stream_rotation_t rotation) :
Igor Murashkine3a9f962013-05-08 18:03:15 -070054 Camera3IOStreamBase(id, CAMERA3_STREAM_OUTPUT, width, height, maxSize,
Yin-Chia Yehb97babb2015-03-12 13:42:44 -070055 format, dataSpace, rotation),
Igor Murashkina55b5452013-04-02 16:36:33 -070056 mConsumer(consumer),
Ruchit Sharmae0711f22014-08-18 13:48:24 -040057 mTransform(0),
58 mTraceFirstBuffer(true) {
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -080059
60 if (format != HAL_PIXEL_FORMAT_BLOB) {
61 ALOGE("%s: Bad format for size-only stream: %d", __FUNCTION__,
62 format);
63 mState = STATE_ERROR;
64 }
65
66 if (mConsumer == NULL) {
67 ALOGE("%s: Consumer is NULL!", __FUNCTION__);
68 mState = STATE_ERROR;
69 }
70}
71
Igor Murashkine3a9f962013-05-08 18:03:15 -070072Camera3OutputStream::Camera3OutputStream(int id, camera3_stream_type_t type,
73 uint32_t width, uint32_t height,
Eino-Ville Talvala3d82c0d2015-02-23 15:19:19 -080074 int format,
Yin-Chia Yehb97babb2015-03-12 13:42:44 -070075 android_dataspace dataSpace,
76 camera3_stream_rotation_t rotation) :
Igor Murashkine3a9f962013-05-08 18:03:15 -070077 Camera3IOStreamBase(id, type, width, height,
78 /*maxSize*/0,
Yin-Chia Yehb97babb2015-03-12 13:42:44 -070079 format, dataSpace, rotation),
Igor Murashkine3a9f962013-05-08 18:03:15 -070080 mTransform(0) {
81
82 // Subclasses expected to initialize mConsumer themselves
83}
84
85
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -080086Camera3OutputStream::~Camera3OutputStream() {
87 disconnectLocked();
88}
89
90status_t Camera3OutputStream::getBufferLocked(camera3_stream_buffer *buffer) {
91 ATRACE_CALL();
92 status_t res;
93
Igor Murashkine3a9f962013-05-08 18:03:15 -070094 if ((res = getBufferPreconditionCheckLocked()) != OK) {
95 return res;
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -080096 }
97
98 ANativeWindowBuffer* anb;
99 int fenceFd;
100
Zhijun He15ad2472013-10-11 16:21:11 -0700101 /**
102 * Release the lock briefly to avoid deadlock for below scenario:
103 * Thread 1: StreamingProcessor::startStream -> Camera3Stream::isConfiguring().
104 * This thread acquired StreamingProcessor lock and try to lock Camera3Stream lock.
105 * Thread 2: Camera3Stream::returnBuffer->StreamingProcessor::onFrameAvailable().
106 * This thread acquired Camera3Stream lock and bufferQueue lock, and try to lock
107 * StreamingProcessor lock.
108 * Thread 3: Camera3Stream::getBuffer(). This thread acquired Camera3Stream lock
109 * and try to lock bufferQueue lock.
110 * Then there is circular locking dependency.
111 */
112 sp<ANativeWindow> currentConsumer = mConsumer;
113 mLock.unlock();
114
115 res = currentConsumer->dequeueBuffer(currentConsumer.get(), &anb, &fenceFd);
116 mLock.lock();
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800117 if (res != OK) {
118 ALOGE("%s: Stream %d: Can't dequeue next output buffer: %s (%d)",
119 __FUNCTION__, mId, strerror(-res), res);
120 return res;
121 }
122
Igor Murashkine3a9f962013-05-08 18:03:15 -0700123 /**
124 * FenceFD now owned by HAL except in case of error,
125 * in which case we reassign it to acquire_fence
126 */
127 handoutBufferLocked(*buffer, &(anb->handle), /*acquireFence*/fenceFd,
Zhijun He6adc9cc2014-04-15 14:09:55 -0700128 /*releaseFence*/-1, CAMERA3_BUFFER_STATUS_OK, /*output*/true);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800129
130 return OK;
131}
132
133status_t Camera3OutputStream::returnBufferLocked(
134 const camera3_stream_buffer &buffer,
135 nsecs_t timestamp) {
136 ATRACE_CALL();
Igor Murashkine3a9f962013-05-08 18:03:15 -0700137
138 status_t res = returnAnyBufferLocked(buffer, timestamp, /*output*/true);
139
140 if (res != OK) {
141 return res;
142 }
143
144 mLastTimestamp = timestamp;
145
146 return OK;
147}
148
149status_t Camera3OutputStream::returnBufferCheckedLocked(
150 const camera3_stream_buffer &buffer,
151 nsecs_t timestamp,
152 bool output,
153 /*out*/
154 sp<Fence> *releaseFenceOut) {
155
156 (void)output;
157 ALOG_ASSERT(output, "Expected output to be true");
158
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800159 status_t res;
Igor Murashkin5a1798a2013-05-07 10:58:13 -0700160
Yin-Chia Yeh4c9736f2015-03-05 15:01:36 -0800161 // Fence management - always honor release fence from HAL
162 sp<Fence> releaseFence = new Fence(buffer.release_fence);
Igor Murashkin5a1798a2013-05-07 10:58:13 -0700163 int anwReleaseFence = releaseFence->dup();
164
165 /**
Zhijun He124ccf42013-05-22 14:01:30 -0700166 * Release the lock briefly to avoid deadlock with
167 * StreamingProcessor::startStream -> Camera3Stream::isConfiguring (this
168 * thread will go into StreamingProcessor::onFrameAvailable) during
169 * queueBuffer
170 */
171 sp<ANativeWindow> currentConsumer = mConsumer;
172 mLock.unlock();
173
174 /**
Igor Murashkin5a1798a2013-05-07 10:58:13 -0700175 * Return buffer back to ANativeWindow
176 */
177 if (buffer.status == CAMERA3_BUFFER_STATUS_ERROR) {
178 // Cancel buffer
Zhijun He124ccf42013-05-22 14:01:30 -0700179 res = currentConsumer->cancelBuffer(currentConsumer.get(),
Igor Murashkin5a1798a2013-05-07 10:58:13 -0700180 container_of(buffer.buffer, ANativeWindowBuffer, handle),
181 anwReleaseFence);
182 if (res != OK) {
183 ALOGE("%s: Stream %d: Error cancelling buffer to native window:"
Igor Murashkine3a9f962013-05-08 18:03:15 -0700184 " %s (%d)", __FUNCTION__, mId, strerror(-res), res);
Igor Murashkin5a1798a2013-05-07 10:58:13 -0700185 }
186 } else {
Ruchit Sharmae0711f22014-08-18 13:48:24 -0400187 if (mTraceFirstBuffer && (stream_type == CAMERA3_STREAM_OUTPUT)) {
188 {
189 char traceLog[48];
190 snprintf(traceLog, sizeof(traceLog), "Stream %d: first full buffer\n", mId);
191 ATRACE_NAME(traceLog);
192 }
193 mTraceFirstBuffer = false;
194 }
195
Yin-Chia Yeh4c9736f2015-03-05 15:01:36 -0800196 res = native_window_set_buffers_timestamp(mConsumer.get(), timestamp);
197 if (res != OK) {
198 ALOGE("%s: Stream %d: Error setting timestamp: %s (%d)",
199 __FUNCTION__, mId, strerror(-res), res);
200 return res;
201 }
202
Zhijun He124ccf42013-05-22 14:01:30 -0700203 res = currentConsumer->queueBuffer(currentConsumer.get(),
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800204 container_of(buffer.buffer, ANativeWindowBuffer, handle),
205 anwReleaseFence);
206 if (res != OK) {
Igor Murashkine3a9f962013-05-08 18:03:15 -0700207 ALOGE("%s: Stream %d: Error queueing buffer to native window: "
208 "%s (%d)", __FUNCTION__, mId, strerror(-res), res);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800209 }
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800210 }
Zhijun He124ccf42013-05-22 14:01:30 -0700211 mLock.lock();
Igor Murashkin5a1798a2013-05-07 10:58:13 -0700212 if (res != OK) {
213 close(anwReleaseFence);
Igor Murashkin5a1798a2013-05-07 10:58:13 -0700214 }
215
Igor Murashkine3a9f962013-05-08 18:03:15 -0700216 *releaseFenceOut = releaseFence;
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800217
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700218 return res;
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800219}
220
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800221void Camera3OutputStream::dump(int fd, const Vector<String16> &args) const {
222 (void) args;
223 String8 lines;
224 lines.appendFormat(" Stream[%d]: Output\n", mId);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800225 write(fd, lines.string(), lines.size());
Igor Murashkine3a9f962013-05-08 18:03:15 -0700226
227 Camera3IOStreamBase::dump(fd, args);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800228}
229
230status_t Camera3OutputStream::setTransform(int transform) {
231 ATRACE_CALL();
232 Mutex::Autolock l(mLock);
233 return setTransformLocked(transform);
234}
235
236status_t Camera3OutputStream::setTransformLocked(int transform) {
237 status_t res = OK;
238 if (mState == STATE_ERROR) {
239 ALOGE("%s: Stream in error state", __FUNCTION__);
240 return INVALID_OPERATION;
241 }
242
243 mTransform = transform;
244 if (mState == STATE_CONFIGURED) {
245 res = native_window_set_buffers_transform(mConsumer.get(),
246 transform);
247 if (res != OK) {
248 ALOGE("%s: Unable to configure stream transform to %x: %s (%d)",
249 __FUNCTION__, transform, strerror(-res), res);
250 }
251 }
252 return res;
253}
254
255status_t Camera3OutputStream::configureQueueLocked() {
256 status_t res;
257
Ruchit Sharmae0711f22014-08-18 13:48:24 -0400258 mTraceFirstBuffer = true;
Igor Murashkine3a9f962013-05-08 18:03:15 -0700259 if ((res = Camera3IOStreamBase::configureQueueLocked()) != OK) {
260 return res;
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800261 }
262
Igor Murashkine3a9f962013-05-08 18:03:15 -0700263 ALOG_ASSERT(mConsumer != 0, "mConsumer should never be NULL");
264
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800265 // Configure consumer-side ANativeWindow interface
266 res = native_window_api_connect(mConsumer.get(),
267 NATIVE_WINDOW_API_CAMERA);
268 if (res != OK) {
269 ALOGE("%s: Unable to connect to native window for stream %d",
270 __FUNCTION__, mId);
271 return res;
272 }
273
274 res = native_window_set_usage(mConsumer.get(), camera3_stream::usage);
275 if (res != OK) {
276 ALOGE("%s: Unable to configure usage %08x for stream %d",
277 __FUNCTION__, camera3_stream::usage, mId);
278 return res;
279 }
280
281 res = native_window_set_scaling_mode(mConsumer.get(),
282 NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
283 if (res != OK) {
284 ALOGE("%s: Unable to configure stream scaling: %s (%d)",
285 __FUNCTION__, strerror(-res), res);
286 return res;
287 }
288
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800289 if (mMaxSize == 0) {
290 // For buffers of known size
Eino-Ville Talvala7d70c5e2014-07-24 18:10:23 -0700291 res = native_window_set_buffers_dimensions(mConsumer.get(),
292 camera3_stream::width, camera3_stream::height);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800293 } else {
294 // For buffers with bounded size
Eino-Ville Talvala7d70c5e2014-07-24 18:10:23 -0700295 res = native_window_set_buffers_dimensions(mConsumer.get(),
296 mMaxSize, 1);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800297 }
298 if (res != OK) {
Eino-Ville Talvala7d70c5e2014-07-24 18:10:23 -0700299 ALOGE("%s: Unable to configure stream buffer dimensions"
300 " %d x %d (maxSize %zu) for stream %d",
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800301 __FUNCTION__, camera3_stream::width, camera3_stream::height,
Eino-Ville Talvala7d70c5e2014-07-24 18:10:23 -0700302 mMaxSize, mId);
303 return res;
304 }
305 res = native_window_set_buffers_format(mConsumer.get(),
306 camera3_stream::format);
307 if (res != OK) {
308 ALOGE("%s: Unable to configure stream buffer format %#x for stream %d",
309 __FUNCTION__, camera3_stream::format, mId);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800310 return res;
311 }
312
Eino-Ville Talvala3d82c0d2015-02-23 15:19:19 -0800313 res = native_window_set_buffers_data_space(mConsumer.get(),
314 camera3_stream::data_space);
315 if (res != OK) {
316 ALOGE("%s: Unable to configure stream dataspace %#x for stream %d",
317 __FUNCTION__, camera3_stream::data_space, mId);
318 return res;
319 }
320
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800321 int maxConsumerBuffers;
322 res = mConsumer->query(mConsumer.get(),
323 NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS, &maxConsumerBuffers);
324 if (res != OK) {
325 ALOGE("%s: Unable to query consumer undequeued"
326 " buffer count for stream %d", __FUNCTION__, mId);
327 return res;
328 }
329
Alex Ray20cb3002013-05-28 20:18:22 -0700330 ALOGV("%s: Consumer wants %d buffers, HAL wants %d", __FUNCTION__,
331 maxConsumerBuffers, camera3_stream::max_buffers);
332 if (camera3_stream::max_buffers == 0) {
Zhijun He2ab500c2013-07-23 08:02:53 -0700333 ALOGE("%s: Camera HAL requested max_buffer count: %d, requires at least 1",
Alex Ray20cb3002013-05-28 20:18:22 -0700334 __FUNCTION__, camera3_stream::max_buffers);
335 return INVALID_OPERATION;
336 }
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800337
338 mTotalBufferCount = maxConsumerBuffers + camera3_stream::max_buffers;
Zhijun He6adc9cc2014-04-15 14:09:55 -0700339 mHandoutTotalBufferCount = 0;
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800340 mFrameCount = 0;
341 mLastTimestamp = 0;
342
343 res = native_window_set_buffer_count(mConsumer.get(),
344 mTotalBufferCount);
345 if (res != OK) {
346 ALOGE("%s: Unable to set buffer count for stream %d",
347 __FUNCTION__, mId);
348 return res;
349 }
350
351 res = native_window_set_buffers_transform(mConsumer.get(),
352 mTransform);
353 if (res != OK) {
354 ALOGE("%s: Unable to configure stream transform to %x: %s (%d)",
355 __FUNCTION__, mTransform, strerror(-res), res);
356 }
357
358 return OK;
359}
360
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800361status_t Camera3OutputStream::disconnectLocked() {
362 status_t res;
363
Igor Murashkine3a9f962013-05-08 18:03:15 -0700364 if ((res = Camera3IOStreamBase::disconnectLocked()) != OK) {
365 return res;
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800366 }
367
Igor Murashkine3a9f962013-05-08 18:03:15 -0700368 res = native_window_api_disconnect(mConsumer.get(),
369 NATIVE_WINDOW_API_CAMERA);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800370
371 /**
372 * This is not an error. if client calling process dies, the window will
373 * also die and all calls to it will return DEAD_OBJECT, thus it's already
374 * "disconnected"
375 */
376 if (res == DEAD_OBJECT) {
377 ALOGW("%s: While disconnecting stream %d from native window, the"
378 " native window died from under us", __FUNCTION__, mId);
379 }
380 else if (res != OK) {
Igor Murashkine3a9f962013-05-08 18:03:15 -0700381 ALOGE("%s: Unable to disconnect stream %d from native window "
382 "(error %d %s)",
383 __FUNCTION__, mId, res, strerror(-res));
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800384 mState = STATE_ERROR;
385 return res;
386 }
387
Igor Murashkine3a9f962013-05-08 18:03:15 -0700388 mState = (mState == STATE_IN_RECONFIG) ? STATE_IN_CONFIG
389 : STATE_CONSTRUCTED;
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800390 return OK;
391}
392
Eino-Ville Talvalab2f5b192013-07-30 14:36:03 -0700393status_t Camera3OutputStream::getEndpointUsage(uint32_t *usage) {
394
395 status_t res;
396 int32_t u = 0;
397 res = mConsumer->query(mConsumer.get(),
398 NATIVE_WINDOW_CONSUMER_USAGE_BITS, &u);
399 *usage = u;
400
401 return res;
402}
403
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800404}; // namespace camera3
405
406}; // namespace android