blob: 77ad50329c36ac1223b77eb0f2bc74c7f8b661be [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,
36 uint32_t width, uint32_t height, int format) :
Igor Murashkine3a9f962013-05-08 18:03:15 -070037 Camera3IOStreamBase(id, CAMERA3_STREAM_OUTPUT, width, height,
38 /*maxSize*/0, format),
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -080039 mConsumer(consumer),
Ruchit Sharmae0711f22014-08-18 13:48:24 -040040 mTransform(0),
41 mTraceFirstBuffer(true) {
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -080042
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -080043 if (mConsumer == NULL) {
44 ALOGE("%s: Consumer is NULL!", __FUNCTION__);
45 mState = STATE_ERROR;
46 }
47}
48
49Camera3OutputStream::Camera3OutputStream(int id,
50 sp<ANativeWindow> consumer,
51 uint32_t width, uint32_t height, size_t maxSize, int format) :
Igor Murashkine3a9f962013-05-08 18:03:15 -070052 Camera3IOStreamBase(id, CAMERA3_STREAM_OUTPUT, width, height, maxSize,
53 format),
Igor Murashkina55b5452013-04-02 16:36:33 -070054 mConsumer(consumer),
Ruchit Sharmae0711f22014-08-18 13:48:24 -040055 mTransform(0),
56 mTraceFirstBuffer(true) {
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -080057
58 if (format != HAL_PIXEL_FORMAT_BLOB) {
59 ALOGE("%s: Bad format for size-only stream: %d", __FUNCTION__,
60 format);
61 mState = STATE_ERROR;
62 }
63
64 if (mConsumer == NULL) {
65 ALOGE("%s: Consumer is NULL!", __FUNCTION__);
66 mState = STATE_ERROR;
67 }
68}
69
Igor Murashkine3a9f962013-05-08 18:03:15 -070070Camera3OutputStream::Camera3OutputStream(int id, camera3_stream_type_t type,
71 uint32_t width, uint32_t height,
72 int format) :
73 Camera3IOStreamBase(id, type, width, height,
74 /*maxSize*/0,
75 format),
76 mTransform(0) {
77
78 // Subclasses expected to initialize mConsumer themselves
79}
80
81
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -080082Camera3OutputStream::~Camera3OutputStream() {
83 disconnectLocked();
84}
85
86status_t Camera3OutputStream::getBufferLocked(camera3_stream_buffer *buffer) {
87 ATRACE_CALL();
88 status_t res;
89
Igor Murashkine3a9f962013-05-08 18:03:15 -070090 if ((res = getBufferPreconditionCheckLocked()) != OK) {
91 return res;
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -080092 }
93
94 ANativeWindowBuffer* anb;
95 int fenceFd;
96
Zhijun He15ad2472013-10-11 16:21:11 -070097 /**
98 * Release the lock briefly to avoid deadlock for below scenario:
99 * Thread 1: StreamingProcessor::startStream -> Camera3Stream::isConfiguring().
100 * This thread acquired StreamingProcessor lock and try to lock Camera3Stream lock.
101 * Thread 2: Camera3Stream::returnBuffer->StreamingProcessor::onFrameAvailable().
102 * This thread acquired Camera3Stream lock and bufferQueue lock, and try to lock
103 * StreamingProcessor lock.
104 * Thread 3: Camera3Stream::getBuffer(). This thread acquired Camera3Stream lock
105 * and try to lock bufferQueue lock.
106 * Then there is circular locking dependency.
107 */
108 sp<ANativeWindow> currentConsumer = mConsumer;
109 mLock.unlock();
110
111 res = currentConsumer->dequeueBuffer(currentConsumer.get(), &anb, &fenceFd);
112 mLock.lock();
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800113 if (res != OK) {
114 ALOGE("%s: Stream %d: Can't dequeue next output buffer: %s (%d)",
115 __FUNCTION__, mId, strerror(-res), res);
116 return res;
117 }
118
Igor Murashkine3a9f962013-05-08 18:03:15 -0700119 /**
120 * FenceFD now owned by HAL except in case of error,
121 * in which case we reassign it to acquire_fence
122 */
123 handoutBufferLocked(*buffer, &(anb->handle), /*acquireFence*/fenceFd,
Zhijun He6adc9cc2014-04-15 14:09:55 -0700124 /*releaseFence*/-1, CAMERA3_BUFFER_STATUS_OK, /*output*/true);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800125
126 return OK;
127}
128
129status_t Camera3OutputStream::returnBufferLocked(
130 const camera3_stream_buffer &buffer,
131 nsecs_t timestamp) {
132 ATRACE_CALL();
Igor Murashkine3a9f962013-05-08 18:03:15 -0700133
134 status_t res = returnAnyBufferLocked(buffer, timestamp, /*output*/true);
135
136 if (res != OK) {
137 return res;
138 }
139
140 mLastTimestamp = timestamp;
141
142 return OK;
143}
144
145status_t Camera3OutputStream::returnBufferCheckedLocked(
146 const camera3_stream_buffer &buffer,
147 nsecs_t timestamp,
148 bool output,
149 /*out*/
150 sp<Fence> *releaseFenceOut) {
151
152 (void)output;
153 ALOG_ASSERT(output, "Expected output to be true");
154
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800155 status_t res;
Igor Murashkin5a1798a2013-05-07 10:58:13 -0700156 sp<Fence> releaseFence;
157
158 /**
159 * Fence management - calculate Release Fence
160 */
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800161 if (buffer.status == CAMERA3_BUFFER_STATUS_ERROR) {
Igor Murashkin5a1798a2013-05-07 10:58:13 -0700162 if (buffer.release_fence != -1) {
163 ALOGE("%s: Stream %d: HAL should not set release_fence(%d) when "
164 "there is an error", __FUNCTION__, mId, buffer.release_fence);
165 close(buffer.release_fence);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800166 }
Igor Murashkin5a1798a2013-05-07 10:58:13 -0700167
168 /**
169 * Reassign release fence as the acquire fence in case of error
170 */
171 releaseFence = new Fence(buffer.acquire_fence);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800172 } else {
173 res = native_window_set_buffers_timestamp(mConsumer.get(), timestamp);
174 if (res != OK) {
175 ALOGE("%s: Stream %d: Error setting timestamp: %s (%d)",
Igor Murashkine3a9f962013-05-08 18:03:15 -0700176 __FUNCTION__, mId, strerror(-res), res);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800177 return res;
178 }
179
Igor Murashkin5a1798a2013-05-07 10:58:13 -0700180 releaseFence = new Fence(buffer.release_fence);
181 }
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800182
Igor Murashkin5a1798a2013-05-07 10:58:13 -0700183 int anwReleaseFence = releaseFence->dup();
184
185 /**
Zhijun He124ccf42013-05-22 14:01:30 -0700186 * Release the lock briefly to avoid deadlock with
187 * StreamingProcessor::startStream -> Camera3Stream::isConfiguring (this
188 * thread will go into StreamingProcessor::onFrameAvailable) during
189 * queueBuffer
190 */
191 sp<ANativeWindow> currentConsumer = mConsumer;
192 mLock.unlock();
193
194 /**
Igor Murashkin5a1798a2013-05-07 10:58:13 -0700195 * Return buffer back to ANativeWindow
196 */
197 if (buffer.status == CAMERA3_BUFFER_STATUS_ERROR) {
198 // Cancel buffer
Zhijun He124ccf42013-05-22 14:01:30 -0700199 res = currentConsumer->cancelBuffer(currentConsumer.get(),
Igor Murashkin5a1798a2013-05-07 10:58:13 -0700200 container_of(buffer.buffer, ANativeWindowBuffer, handle),
201 anwReleaseFence);
202 if (res != OK) {
203 ALOGE("%s: Stream %d: Error cancelling buffer to native window:"
Igor Murashkine3a9f962013-05-08 18:03:15 -0700204 " %s (%d)", __FUNCTION__, mId, strerror(-res), res);
Igor Murashkin5a1798a2013-05-07 10:58:13 -0700205 }
206 } else {
Ruchit Sharmae0711f22014-08-18 13:48:24 -0400207 if (mTraceFirstBuffer && (stream_type == CAMERA3_STREAM_OUTPUT)) {
208 {
209 char traceLog[48];
210 snprintf(traceLog, sizeof(traceLog), "Stream %d: first full buffer\n", mId);
211 ATRACE_NAME(traceLog);
212 }
213 mTraceFirstBuffer = false;
214 }
215
Zhijun He124ccf42013-05-22 14:01:30 -0700216 res = currentConsumer->queueBuffer(currentConsumer.get(),
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800217 container_of(buffer.buffer, ANativeWindowBuffer, handle),
218 anwReleaseFence);
219 if (res != OK) {
Igor Murashkine3a9f962013-05-08 18:03:15 -0700220 ALOGE("%s: Stream %d: Error queueing buffer to native window: "
221 "%s (%d)", __FUNCTION__, mId, strerror(-res), res);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800222 }
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800223 }
Zhijun He124ccf42013-05-22 14:01:30 -0700224 mLock.lock();
Igor Murashkin5a1798a2013-05-07 10:58:13 -0700225 if (res != OK) {
226 close(anwReleaseFence);
Igor Murashkin5a1798a2013-05-07 10:58:13 -0700227 }
228
Igor Murashkine3a9f962013-05-08 18:03:15 -0700229 *releaseFenceOut = releaseFence;
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800230
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700231 return res;
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800232}
233
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800234void Camera3OutputStream::dump(int fd, const Vector<String16> &args) const {
235 (void) args;
236 String8 lines;
237 lines.appendFormat(" Stream[%d]: Output\n", mId);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800238 write(fd, lines.string(), lines.size());
Igor Murashkine3a9f962013-05-08 18:03:15 -0700239
240 Camera3IOStreamBase::dump(fd, args);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800241}
242
243status_t Camera3OutputStream::setTransform(int transform) {
244 ATRACE_CALL();
245 Mutex::Autolock l(mLock);
246 return setTransformLocked(transform);
247}
248
249status_t Camera3OutputStream::setTransformLocked(int transform) {
250 status_t res = OK;
251 if (mState == STATE_ERROR) {
252 ALOGE("%s: Stream in error state", __FUNCTION__);
253 return INVALID_OPERATION;
254 }
255
256 mTransform = transform;
257 if (mState == STATE_CONFIGURED) {
258 res = native_window_set_buffers_transform(mConsumer.get(),
259 transform);
260 if (res != OK) {
261 ALOGE("%s: Unable to configure stream transform to %x: %s (%d)",
262 __FUNCTION__, transform, strerror(-res), res);
263 }
264 }
265 return res;
266}
267
268status_t Camera3OutputStream::configureQueueLocked() {
269 status_t res;
270
Ruchit Sharmae0711f22014-08-18 13:48:24 -0400271 mTraceFirstBuffer = true;
Igor Murashkine3a9f962013-05-08 18:03:15 -0700272 if ((res = Camera3IOStreamBase::configureQueueLocked()) != OK) {
273 return res;
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800274 }
275
Igor Murashkine3a9f962013-05-08 18:03:15 -0700276 ALOG_ASSERT(mConsumer != 0, "mConsumer should never be NULL");
277
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800278 // Configure consumer-side ANativeWindow interface
279 res = native_window_api_connect(mConsumer.get(),
280 NATIVE_WINDOW_API_CAMERA);
281 if (res != OK) {
282 ALOGE("%s: Unable to connect to native window for stream %d",
283 __FUNCTION__, mId);
284 return res;
285 }
286
287 res = native_window_set_usage(mConsumer.get(), camera3_stream::usage);
288 if (res != OK) {
289 ALOGE("%s: Unable to configure usage %08x for stream %d",
290 __FUNCTION__, camera3_stream::usage, mId);
291 return res;
292 }
293
294 res = native_window_set_scaling_mode(mConsumer.get(),
295 NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
296 if (res != OK) {
297 ALOGE("%s: Unable to configure stream scaling: %s (%d)",
298 __FUNCTION__, strerror(-res), res);
299 return res;
300 }
301
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800302 if (mMaxSize == 0) {
303 // For buffers of known size
Eino-Ville Talvala7d70c5e2014-07-24 18:10:23 -0700304 res = native_window_set_buffers_dimensions(mConsumer.get(),
305 camera3_stream::width, camera3_stream::height);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800306 } else {
307 // For buffers with bounded size
Eino-Ville Talvala7d70c5e2014-07-24 18:10:23 -0700308 res = native_window_set_buffers_dimensions(mConsumer.get(),
309 mMaxSize, 1);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800310 }
311 if (res != OK) {
Eino-Ville Talvala7d70c5e2014-07-24 18:10:23 -0700312 ALOGE("%s: Unable to configure stream buffer dimensions"
313 " %d x %d (maxSize %zu) for stream %d",
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800314 __FUNCTION__, camera3_stream::width, camera3_stream::height,
Eino-Ville Talvala7d70c5e2014-07-24 18:10:23 -0700315 mMaxSize, mId);
316 return res;
317 }
318 res = native_window_set_buffers_format(mConsumer.get(),
319 camera3_stream::format);
320 if (res != OK) {
321 ALOGE("%s: Unable to configure stream buffer format %#x for stream %d",
322 __FUNCTION__, camera3_stream::format, mId);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800323 return res;
324 }
325
326 int maxConsumerBuffers;
327 res = mConsumer->query(mConsumer.get(),
328 NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS, &maxConsumerBuffers);
329 if (res != OK) {
330 ALOGE("%s: Unable to query consumer undequeued"
331 " buffer count for stream %d", __FUNCTION__, mId);
332 return res;
333 }
334
Alex Ray20cb3002013-05-28 20:18:22 -0700335 ALOGV("%s: Consumer wants %d buffers, HAL wants %d", __FUNCTION__,
336 maxConsumerBuffers, camera3_stream::max_buffers);
337 if (camera3_stream::max_buffers == 0) {
Zhijun He2ab500c2013-07-23 08:02:53 -0700338 ALOGE("%s: Camera HAL requested max_buffer count: %d, requires at least 1",
Alex Ray20cb3002013-05-28 20:18:22 -0700339 __FUNCTION__, camera3_stream::max_buffers);
340 return INVALID_OPERATION;
341 }
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800342
343 mTotalBufferCount = maxConsumerBuffers + camera3_stream::max_buffers;
Zhijun He6adc9cc2014-04-15 14:09:55 -0700344 mHandoutTotalBufferCount = 0;
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800345 mFrameCount = 0;
346 mLastTimestamp = 0;
347
348 res = native_window_set_buffer_count(mConsumer.get(),
349 mTotalBufferCount);
350 if (res != OK) {
351 ALOGE("%s: Unable to set buffer count for stream %d",
352 __FUNCTION__, mId);
353 return res;
354 }
355
356 res = native_window_set_buffers_transform(mConsumer.get(),
357 mTransform);
358 if (res != OK) {
359 ALOGE("%s: Unable to configure stream transform to %x: %s (%d)",
360 __FUNCTION__, mTransform, strerror(-res), res);
361 }
362
363 return OK;
364}
365
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800366status_t Camera3OutputStream::disconnectLocked() {
367 status_t res;
368
Igor Murashkine3a9f962013-05-08 18:03:15 -0700369 if ((res = Camera3IOStreamBase::disconnectLocked()) != OK) {
370 return res;
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800371 }
372
Igor Murashkine3a9f962013-05-08 18:03:15 -0700373 res = native_window_api_disconnect(mConsumer.get(),
374 NATIVE_WINDOW_API_CAMERA);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800375
376 /**
377 * This is not an error. if client calling process dies, the window will
378 * also die and all calls to it will return DEAD_OBJECT, thus it's already
379 * "disconnected"
380 */
381 if (res == DEAD_OBJECT) {
382 ALOGW("%s: While disconnecting stream %d from native window, the"
383 " native window died from under us", __FUNCTION__, mId);
384 }
385 else if (res != OK) {
Igor Murashkine3a9f962013-05-08 18:03:15 -0700386 ALOGE("%s: Unable to disconnect stream %d from native window "
387 "(error %d %s)",
388 __FUNCTION__, mId, res, strerror(-res));
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800389 mState = STATE_ERROR;
390 return res;
391 }
392
Igor Murashkine3a9f962013-05-08 18:03:15 -0700393 mState = (mState == STATE_IN_RECONFIG) ? STATE_IN_CONFIG
394 : STATE_CONSTRUCTED;
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800395 return OK;
396}
397
Eino-Ville Talvalab2f5b192013-07-30 14:36:03 -0700398status_t Camera3OutputStream::getEndpointUsage(uint32_t *usage) {
399
400 status_t res;
401 int32_t u = 0;
402 res = mConsumer->query(mConsumer.get(),
403 NATIVE_WINDOW_CONSUMER_USAGE_BITS, &u);
404 *usage = u;
405
406 return res;
407}
408
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800409}; // namespace camera3
410
411}; // namespace android