blob: 103d90b76a8c0a1ed3db224de339223af9008e40 [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,
37 android_dataspace dataSpace) :
Igor Murashkine3a9f962013-05-08 18:03:15 -070038 Camera3IOStreamBase(id, CAMERA3_STREAM_OUTPUT, width, height,
Eino-Ville Talvala3d82c0d2015-02-23 15:19:19 -080039 /*maxSize*/0, format, dataSpace),
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,
53 android_dataspace dataSpace) :
Igor Murashkine3a9f962013-05-08 18:03:15 -070054 Camera3IOStreamBase(id, CAMERA3_STREAM_OUTPUT, width, height, maxSize,
Eino-Ville Talvala3d82c0d2015-02-23 15:19:19 -080055 format, dataSpace),
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,
75 android_dataspace dataSpace) :
Igor Murashkine3a9f962013-05-08 18:03:15 -070076 Camera3IOStreamBase(id, type, width, height,
77 /*maxSize*/0,
Eino-Ville Talvala3d82c0d2015-02-23 15:19:19 -080078 format, dataSpace),
Igor Murashkine3a9f962013-05-08 18:03:15 -070079 mTransform(0) {
80
81 // Subclasses expected to initialize mConsumer themselves
82}
83
84
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -080085Camera3OutputStream::~Camera3OutputStream() {
86 disconnectLocked();
87}
88
89status_t Camera3OutputStream::getBufferLocked(camera3_stream_buffer *buffer) {
90 ATRACE_CALL();
91 status_t res;
92
Igor Murashkine3a9f962013-05-08 18:03:15 -070093 if ((res = getBufferPreconditionCheckLocked()) != OK) {
94 return res;
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -080095 }
96
97 ANativeWindowBuffer* anb;
98 int fenceFd;
99
Zhijun He15ad2472013-10-11 16:21:11 -0700100 /**
101 * Release the lock briefly to avoid deadlock for below scenario:
102 * Thread 1: StreamingProcessor::startStream -> Camera3Stream::isConfiguring().
103 * This thread acquired StreamingProcessor lock and try to lock Camera3Stream lock.
104 * Thread 2: Camera3Stream::returnBuffer->StreamingProcessor::onFrameAvailable().
105 * This thread acquired Camera3Stream lock and bufferQueue lock, and try to lock
106 * StreamingProcessor lock.
107 * Thread 3: Camera3Stream::getBuffer(). This thread acquired Camera3Stream lock
108 * and try to lock bufferQueue lock.
109 * Then there is circular locking dependency.
110 */
111 sp<ANativeWindow> currentConsumer = mConsumer;
112 mLock.unlock();
113
114 res = currentConsumer->dequeueBuffer(currentConsumer.get(), &anb, &fenceFd);
115 mLock.lock();
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800116 if (res != OK) {
117 ALOGE("%s: Stream %d: Can't dequeue next output buffer: %s (%d)",
118 __FUNCTION__, mId, strerror(-res), res);
119 return res;
120 }
121
Igor Murashkine3a9f962013-05-08 18:03:15 -0700122 /**
123 * FenceFD now owned by HAL except in case of error,
124 * in which case we reassign it to acquire_fence
125 */
126 handoutBufferLocked(*buffer, &(anb->handle), /*acquireFence*/fenceFd,
Zhijun He6adc9cc2014-04-15 14:09:55 -0700127 /*releaseFence*/-1, CAMERA3_BUFFER_STATUS_OK, /*output*/true);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800128
129 return OK;
130}
131
132status_t Camera3OutputStream::returnBufferLocked(
133 const camera3_stream_buffer &buffer,
134 nsecs_t timestamp) {
135 ATRACE_CALL();
Igor Murashkine3a9f962013-05-08 18:03:15 -0700136
137 status_t res = returnAnyBufferLocked(buffer, timestamp, /*output*/true);
138
139 if (res != OK) {
140 return res;
141 }
142
143 mLastTimestamp = timestamp;
144
145 return OK;
146}
147
148status_t Camera3OutputStream::returnBufferCheckedLocked(
149 const camera3_stream_buffer &buffer,
150 nsecs_t timestamp,
151 bool output,
152 /*out*/
153 sp<Fence> *releaseFenceOut) {
154
155 (void)output;
156 ALOG_ASSERT(output, "Expected output to be true");
157
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800158 status_t res;
Igor Murashkin5a1798a2013-05-07 10:58:13 -0700159 sp<Fence> releaseFence;
160
161 /**
162 * Fence management - calculate Release Fence
163 */
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800164 if (buffer.status == CAMERA3_BUFFER_STATUS_ERROR) {
Igor Murashkin5a1798a2013-05-07 10:58:13 -0700165 if (buffer.release_fence != -1) {
166 ALOGE("%s: Stream %d: HAL should not set release_fence(%d) when "
167 "there is an error", __FUNCTION__, mId, buffer.release_fence);
168 close(buffer.release_fence);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800169 }
Igor Murashkin5a1798a2013-05-07 10:58:13 -0700170
171 /**
172 * Reassign release fence as the acquire fence in case of error
173 */
174 releaseFence = new Fence(buffer.acquire_fence);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800175 } else {
176 res = native_window_set_buffers_timestamp(mConsumer.get(), timestamp);
177 if (res != OK) {
178 ALOGE("%s: Stream %d: Error setting timestamp: %s (%d)",
Igor Murashkine3a9f962013-05-08 18:03:15 -0700179 __FUNCTION__, mId, strerror(-res), res);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800180 return res;
181 }
182
Igor Murashkin5a1798a2013-05-07 10:58:13 -0700183 releaseFence = new Fence(buffer.release_fence);
184 }
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800185
Igor Murashkin5a1798a2013-05-07 10:58:13 -0700186 int anwReleaseFence = releaseFence->dup();
187
188 /**
Zhijun He124ccf42013-05-22 14:01:30 -0700189 * Release the lock briefly to avoid deadlock with
190 * StreamingProcessor::startStream -> Camera3Stream::isConfiguring (this
191 * thread will go into StreamingProcessor::onFrameAvailable) during
192 * queueBuffer
193 */
194 sp<ANativeWindow> currentConsumer = mConsumer;
195 mLock.unlock();
196
197 /**
Igor Murashkin5a1798a2013-05-07 10:58:13 -0700198 * Return buffer back to ANativeWindow
199 */
200 if (buffer.status == CAMERA3_BUFFER_STATUS_ERROR) {
201 // Cancel buffer
Zhijun He124ccf42013-05-22 14:01:30 -0700202 res = currentConsumer->cancelBuffer(currentConsumer.get(),
Igor Murashkin5a1798a2013-05-07 10:58:13 -0700203 container_of(buffer.buffer, ANativeWindowBuffer, handle),
204 anwReleaseFence);
205 if (res != OK) {
206 ALOGE("%s: Stream %d: Error cancelling buffer to native window:"
Igor Murashkine3a9f962013-05-08 18:03:15 -0700207 " %s (%d)", __FUNCTION__, mId, strerror(-res), res);
Igor Murashkin5a1798a2013-05-07 10:58:13 -0700208 }
209 } else {
Ruchit Sharmae0711f22014-08-18 13:48:24 -0400210 if (mTraceFirstBuffer && (stream_type == CAMERA3_STREAM_OUTPUT)) {
211 {
212 char traceLog[48];
213 snprintf(traceLog, sizeof(traceLog), "Stream %d: first full buffer\n", mId);
214 ATRACE_NAME(traceLog);
215 }
216 mTraceFirstBuffer = false;
217 }
218
Zhijun He124ccf42013-05-22 14:01:30 -0700219 res = currentConsumer->queueBuffer(currentConsumer.get(),
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800220 container_of(buffer.buffer, ANativeWindowBuffer, handle),
221 anwReleaseFence);
222 if (res != OK) {
Igor Murashkine3a9f962013-05-08 18:03:15 -0700223 ALOGE("%s: Stream %d: Error queueing buffer to native window: "
224 "%s (%d)", __FUNCTION__, mId, strerror(-res), res);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800225 }
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800226 }
Zhijun He124ccf42013-05-22 14:01:30 -0700227 mLock.lock();
Igor Murashkin5a1798a2013-05-07 10:58:13 -0700228 if (res != OK) {
229 close(anwReleaseFence);
Igor Murashkin5a1798a2013-05-07 10:58:13 -0700230 }
231
Igor Murashkine3a9f962013-05-08 18:03:15 -0700232 *releaseFenceOut = releaseFence;
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800233
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700234 return res;
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800235}
236
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800237void Camera3OutputStream::dump(int fd, const Vector<String16> &args) const {
238 (void) args;
239 String8 lines;
240 lines.appendFormat(" Stream[%d]: Output\n", mId);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800241 write(fd, lines.string(), lines.size());
Igor Murashkine3a9f962013-05-08 18:03:15 -0700242
243 Camera3IOStreamBase::dump(fd, args);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800244}
245
246status_t Camera3OutputStream::setTransform(int transform) {
247 ATRACE_CALL();
248 Mutex::Autolock l(mLock);
249 return setTransformLocked(transform);
250}
251
252status_t Camera3OutputStream::setTransformLocked(int transform) {
253 status_t res = OK;
254 if (mState == STATE_ERROR) {
255 ALOGE("%s: Stream in error state", __FUNCTION__);
256 return INVALID_OPERATION;
257 }
258
259 mTransform = transform;
260 if (mState == STATE_CONFIGURED) {
261 res = native_window_set_buffers_transform(mConsumer.get(),
262 transform);
263 if (res != OK) {
264 ALOGE("%s: Unable to configure stream transform to %x: %s (%d)",
265 __FUNCTION__, transform, strerror(-res), res);
266 }
267 }
268 return res;
269}
270
271status_t Camera3OutputStream::configureQueueLocked() {
272 status_t res;
273
Ruchit Sharmae0711f22014-08-18 13:48:24 -0400274 mTraceFirstBuffer = true;
Igor Murashkine3a9f962013-05-08 18:03:15 -0700275 if ((res = Camera3IOStreamBase::configureQueueLocked()) != OK) {
276 return res;
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800277 }
278
Igor Murashkine3a9f962013-05-08 18:03:15 -0700279 ALOG_ASSERT(mConsumer != 0, "mConsumer should never be NULL");
280
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800281 // Configure consumer-side ANativeWindow interface
282 res = native_window_api_connect(mConsumer.get(),
283 NATIVE_WINDOW_API_CAMERA);
284 if (res != OK) {
285 ALOGE("%s: Unable to connect to native window for stream %d",
286 __FUNCTION__, mId);
287 return res;
288 }
289
290 res = native_window_set_usage(mConsumer.get(), camera3_stream::usage);
291 if (res != OK) {
292 ALOGE("%s: Unable to configure usage %08x for stream %d",
293 __FUNCTION__, camera3_stream::usage, mId);
294 return res;
295 }
296
297 res = native_window_set_scaling_mode(mConsumer.get(),
298 NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
299 if (res != OK) {
300 ALOGE("%s: Unable to configure stream scaling: %s (%d)",
301 __FUNCTION__, strerror(-res), res);
302 return res;
303 }
304
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800305 if (mMaxSize == 0) {
306 // For buffers of known size
Eino-Ville Talvala7d70c5e2014-07-24 18:10:23 -0700307 res = native_window_set_buffers_dimensions(mConsumer.get(),
308 camera3_stream::width, camera3_stream::height);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800309 } else {
310 // For buffers with bounded size
Eino-Ville Talvala7d70c5e2014-07-24 18:10:23 -0700311 res = native_window_set_buffers_dimensions(mConsumer.get(),
312 mMaxSize, 1);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800313 }
314 if (res != OK) {
Eino-Ville Talvala7d70c5e2014-07-24 18:10:23 -0700315 ALOGE("%s: Unable to configure stream buffer dimensions"
316 " %d x %d (maxSize %zu) for stream %d",
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800317 __FUNCTION__, camera3_stream::width, camera3_stream::height,
Eino-Ville Talvala7d70c5e2014-07-24 18:10:23 -0700318 mMaxSize, mId);
319 return res;
320 }
321 res = native_window_set_buffers_format(mConsumer.get(),
322 camera3_stream::format);
323 if (res != OK) {
324 ALOGE("%s: Unable to configure stream buffer format %#x for stream %d",
325 __FUNCTION__, camera3_stream::format, mId);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800326 return res;
327 }
328
Eino-Ville Talvala3d82c0d2015-02-23 15:19:19 -0800329 res = native_window_set_buffers_data_space(mConsumer.get(),
330 camera3_stream::data_space);
331 if (res != OK) {
332 ALOGE("%s: Unable to configure stream dataspace %#x for stream %d",
333 __FUNCTION__, camera3_stream::data_space, mId);
334 return res;
335 }
336
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800337 int maxConsumerBuffers;
338 res = mConsumer->query(mConsumer.get(),
339 NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS, &maxConsumerBuffers);
340 if (res != OK) {
341 ALOGE("%s: Unable to query consumer undequeued"
342 " buffer count for stream %d", __FUNCTION__, mId);
343 return res;
344 }
345
Alex Ray20cb3002013-05-28 20:18:22 -0700346 ALOGV("%s: Consumer wants %d buffers, HAL wants %d", __FUNCTION__,
347 maxConsumerBuffers, camera3_stream::max_buffers);
348 if (camera3_stream::max_buffers == 0) {
Zhijun He2ab500c2013-07-23 08:02:53 -0700349 ALOGE("%s: Camera HAL requested max_buffer count: %d, requires at least 1",
Alex Ray20cb3002013-05-28 20:18:22 -0700350 __FUNCTION__, camera3_stream::max_buffers);
351 return INVALID_OPERATION;
352 }
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800353
354 mTotalBufferCount = maxConsumerBuffers + camera3_stream::max_buffers;
Zhijun He6adc9cc2014-04-15 14:09:55 -0700355 mHandoutTotalBufferCount = 0;
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800356 mFrameCount = 0;
357 mLastTimestamp = 0;
358
359 res = native_window_set_buffer_count(mConsumer.get(),
360 mTotalBufferCount);
361 if (res != OK) {
362 ALOGE("%s: Unable to set buffer count for stream %d",
363 __FUNCTION__, mId);
364 return res;
365 }
366
367 res = native_window_set_buffers_transform(mConsumer.get(),
368 mTransform);
369 if (res != OK) {
370 ALOGE("%s: Unable to configure stream transform to %x: %s (%d)",
371 __FUNCTION__, mTransform, strerror(-res), res);
372 }
373
374 return OK;
375}
376
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800377status_t Camera3OutputStream::disconnectLocked() {
378 status_t res;
379
Igor Murashkine3a9f962013-05-08 18:03:15 -0700380 if ((res = Camera3IOStreamBase::disconnectLocked()) != OK) {
381 return res;
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800382 }
383
Igor Murashkine3a9f962013-05-08 18:03:15 -0700384 res = native_window_api_disconnect(mConsumer.get(),
385 NATIVE_WINDOW_API_CAMERA);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800386
387 /**
388 * This is not an error. if client calling process dies, the window will
389 * also die and all calls to it will return DEAD_OBJECT, thus it's already
390 * "disconnected"
391 */
392 if (res == DEAD_OBJECT) {
393 ALOGW("%s: While disconnecting stream %d from native window, the"
394 " native window died from under us", __FUNCTION__, mId);
395 }
396 else if (res != OK) {
Igor Murashkine3a9f962013-05-08 18:03:15 -0700397 ALOGE("%s: Unable to disconnect stream %d from native window "
398 "(error %d %s)",
399 __FUNCTION__, mId, res, strerror(-res));
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800400 mState = STATE_ERROR;
401 return res;
402 }
403
Igor Murashkine3a9f962013-05-08 18:03:15 -0700404 mState = (mState == STATE_IN_RECONFIG) ? STATE_IN_CONFIG
405 : STATE_CONSTRUCTED;
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800406 return OK;
407}
408
Eino-Ville Talvalab2f5b192013-07-30 14:36:03 -0700409status_t Camera3OutputStream::getEndpointUsage(uint32_t *usage) {
410
411 status_t res;
412 int32_t u = 0;
413 res = mConsumer->query(mConsumer.get(),
414 NATIVE_WINDOW_CONSUMER_USAGE_BITS, &u);
415 *usage = u;
416
417 return res;
418}
419
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800420}; // namespace camera3
421
422}; // namespace android