blob: 8c611d545673c273990c394f0e9bc33fe1680c21 [file] [log] [blame]
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -08001/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "Camera3-OutputStream"
18#define ATRACE_TAG ATRACE_TAG_CAMERA
19//#define LOG_NDEBUG 0
20
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -080021#include <utils/Log.h>
22#include <utils/Trace.h>
23#include "Camera3OutputStream.h"
24
25#ifndef container_of
26#define container_of(ptr, type, member) \
27 (type *)((char*)(ptr) - offsetof(type, member))
28#endif
29
30namespace android {
31
32namespace camera3 {
33
34Camera3OutputStream::Camera3OutputStream(int id,
Eino-Ville Talvala727d1722015-06-09 13:44:19 -070035 sp<Surface> consumer,
Eino-Ville Talvala3d82c0d2015-02-23 15:19:19 -080036 uint32_t width, uint32_t height, int format,
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,
Eino-Ville Talvala727d1722015-06-09 13:44:19 -070051 sp<Surface> 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();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700212
213 // Once a valid buffer has been returned to the queue, can no longer
214 // dequeue all buffers for preallocation.
215 if (buffer.status != CAMERA3_BUFFER_STATUS_ERROR) {
216 mStreamUnpreparable = true;
217 }
218
Igor Murashkin5a1798a2013-05-07 10:58:13 -0700219 if (res != OK) {
220 close(anwReleaseFence);
Igor Murashkin5a1798a2013-05-07 10:58:13 -0700221 }
222
Igor Murashkine3a9f962013-05-08 18:03:15 -0700223 *releaseFenceOut = releaseFence;
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800224
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700225 return res;
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800226}
227
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800228void Camera3OutputStream::dump(int fd, const Vector<String16> &args) const {
229 (void) args;
230 String8 lines;
231 lines.appendFormat(" Stream[%d]: Output\n", mId);
Eino-Ville Talvala727d1722015-06-09 13:44:19 -0700232 lines.appendFormat(" Consumer name: %s\n", mConsumerName.string());
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800233 write(fd, lines.string(), lines.size());
Igor Murashkine3a9f962013-05-08 18:03:15 -0700234
235 Camera3IOStreamBase::dump(fd, args);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800236}
237
238status_t Camera3OutputStream::setTransform(int transform) {
239 ATRACE_CALL();
240 Mutex::Autolock l(mLock);
241 return setTransformLocked(transform);
242}
243
244status_t Camera3OutputStream::setTransformLocked(int transform) {
245 status_t res = OK;
246 if (mState == STATE_ERROR) {
247 ALOGE("%s: Stream in error state", __FUNCTION__);
248 return INVALID_OPERATION;
249 }
250
251 mTransform = transform;
252 if (mState == STATE_CONFIGURED) {
253 res = native_window_set_buffers_transform(mConsumer.get(),
254 transform);
255 if (res != OK) {
256 ALOGE("%s: Unable to configure stream transform to %x: %s (%d)",
257 __FUNCTION__, transform, strerror(-res), res);
258 }
259 }
260 return res;
261}
262
263status_t Camera3OutputStream::configureQueueLocked() {
264 status_t res;
265
Ruchit Sharmae0711f22014-08-18 13:48:24 -0400266 mTraceFirstBuffer = true;
Igor Murashkine3a9f962013-05-08 18:03:15 -0700267 if ((res = Camera3IOStreamBase::configureQueueLocked()) != OK) {
268 return res;
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800269 }
270
Igor Murashkine3a9f962013-05-08 18:03:15 -0700271 ALOG_ASSERT(mConsumer != 0, "mConsumer should never be NULL");
272
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800273 // Configure consumer-side ANativeWindow interface
274 res = native_window_api_connect(mConsumer.get(),
275 NATIVE_WINDOW_API_CAMERA);
276 if (res != OK) {
277 ALOGE("%s: Unable to connect to native window for stream %d",
278 __FUNCTION__, mId);
279 return res;
280 }
281
Eino-Ville Talvala727d1722015-06-09 13:44:19 -0700282 mConsumerName = mConsumer->getConsumerName();
283
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800284 res = native_window_set_usage(mConsumer.get(), camera3_stream::usage);
285 if (res != OK) {
286 ALOGE("%s: Unable to configure usage %08x for stream %d",
287 __FUNCTION__, camera3_stream::usage, mId);
288 return res;
289 }
290
291 res = native_window_set_scaling_mode(mConsumer.get(),
292 NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
293 if (res != OK) {
294 ALOGE("%s: Unable to configure stream scaling: %s (%d)",
295 __FUNCTION__, strerror(-res), res);
296 return res;
297 }
298
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800299 if (mMaxSize == 0) {
300 // For buffers of known size
Eino-Ville Talvala7d70c5e2014-07-24 18:10:23 -0700301 res = native_window_set_buffers_dimensions(mConsumer.get(),
302 camera3_stream::width, camera3_stream::height);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800303 } else {
304 // For buffers with bounded size
Eino-Ville Talvala7d70c5e2014-07-24 18:10:23 -0700305 res = native_window_set_buffers_dimensions(mConsumer.get(),
306 mMaxSize, 1);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800307 }
308 if (res != OK) {
Eino-Ville Talvala7d70c5e2014-07-24 18:10:23 -0700309 ALOGE("%s: Unable to configure stream buffer dimensions"
310 " %d x %d (maxSize %zu) for stream %d",
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800311 __FUNCTION__, camera3_stream::width, camera3_stream::height,
Eino-Ville Talvala7d70c5e2014-07-24 18:10:23 -0700312 mMaxSize, mId);
313 return res;
314 }
315 res = native_window_set_buffers_format(mConsumer.get(),
316 camera3_stream::format);
317 if (res != OK) {
318 ALOGE("%s: Unable to configure stream buffer format %#x for stream %d",
319 __FUNCTION__, camera3_stream::format, mId);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800320 return res;
321 }
322
Eino-Ville Talvala3d82c0d2015-02-23 15:19:19 -0800323 res = native_window_set_buffers_data_space(mConsumer.get(),
324 camera3_stream::data_space);
325 if (res != OK) {
326 ALOGE("%s: Unable to configure stream dataspace %#x for stream %d",
327 __FUNCTION__, camera3_stream::data_space, mId);
328 return res;
329 }
330
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800331 int maxConsumerBuffers;
Eino-Ville Talvala727d1722015-06-09 13:44:19 -0700332 res = static_cast<ANativeWindow*>(mConsumer.get())->query(
333 mConsumer.get(),
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800334 NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS, &maxConsumerBuffers);
335 if (res != OK) {
336 ALOGE("%s: Unable to query consumer undequeued"
337 " buffer count for stream %d", __FUNCTION__, mId);
338 return res;
339 }
340
Alex Ray20cb3002013-05-28 20:18:22 -0700341 ALOGV("%s: Consumer wants %d buffers, HAL wants %d", __FUNCTION__,
342 maxConsumerBuffers, camera3_stream::max_buffers);
343 if (camera3_stream::max_buffers == 0) {
Zhijun He2ab500c2013-07-23 08:02:53 -0700344 ALOGE("%s: Camera HAL requested max_buffer count: %d, requires at least 1",
Alex Ray20cb3002013-05-28 20:18:22 -0700345 __FUNCTION__, camera3_stream::max_buffers);
346 return INVALID_OPERATION;
347 }
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800348
349 mTotalBufferCount = maxConsumerBuffers + camera3_stream::max_buffers;
Zhijun He6adc9cc2014-04-15 14:09:55 -0700350 mHandoutTotalBufferCount = 0;
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800351 mFrameCount = 0;
352 mLastTimestamp = 0;
353
354 res = native_window_set_buffer_count(mConsumer.get(),
355 mTotalBufferCount);
356 if (res != OK) {
357 ALOGE("%s: Unable to set buffer count for stream %d",
358 __FUNCTION__, mId);
359 return res;
360 }
361
362 res = native_window_set_buffers_transform(mConsumer.get(),
363 mTransform);
364 if (res != OK) {
365 ALOGE("%s: Unable to configure stream transform to %x: %s (%d)",
366 __FUNCTION__, mTransform, strerror(-res), res);
367 }
368
369 return OK;
370}
371
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800372status_t Camera3OutputStream::disconnectLocked() {
373 status_t res;
374
Igor Murashkine3a9f962013-05-08 18:03:15 -0700375 if ((res = Camera3IOStreamBase::disconnectLocked()) != OK) {
376 return res;
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800377 }
378
Igor Murashkine3a9f962013-05-08 18:03:15 -0700379 res = native_window_api_disconnect(mConsumer.get(),
380 NATIVE_WINDOW_API_CAMERA);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800381
382 /**
383 * This is not an error. if client calling process dies, the window will
384 * also die and all calls to it will return DEAD_OBJECT, thus it's already
385 * "disconnected"
386 */
387 if (res == DEAD_OBJECT) {
388 ALOGW("%s: While disconnecting stream %d from native window, the"
389 " native window died from under us", __FUNCTION__, mId);
390 }
391 else if (res != OK) {
Igor Murashkine3a9f962013-05-08 18:03:15 -0700392 ALOGE("%s: Unable to disconnect stream %d from native window "
393 "(error %d %s)",
394 __FUNCTION__, mId, res, strerror(-res));
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800395 mState = STATE_ERROR;
396 return res;
397 }
398
Igor Murashkine3a9f962013-05-08 18:03:15 -0700399 mState = (mState == STATE_IN_RECONFIG) ? STATE_IN_CONFIG
400 : STATE_CONSTRUCTED;
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800401 return OK;
402}
403
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700404status_t Camera3OutputStream::getEndpointUsage(uint32_t *usage) const {
Eino-Ville Talvalab2f5b192013-07-30 14:36:03 -0700405
406 status_t res;
407 int32_t u = 0;
Eino-Ville Talvala727d1722015-06-09 13:44:19 -0700408 res = static_cast<ANativeWindow*>(mConsumer.get())->query(mConsumer.get(),
Eino-Ville Talvalab2f5b192013-07-30 14:36:03 -0700409 NATIVE_WINDOW_CONSUMER_USAGE_BITS, &u);
Eino-Ville Talvalab2f5b192013-07-30 14:36:03 -0700410
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700411 // If an opaque output stream's endpoint is ImageReader, add
412 // GRALLOC_USAGE_HW_CAMERA_ZSL to the usage so HAL knows it will be used
413 // for the ZSL use case.
414 // Assume it's for ImageReader if the consumer usage doesn't have any of these bits set:
415 // 1. GRALLOC_USAGE_HW_TEXTURE
416 // 2. GRALLOC_USAGE_HW_RENDER
417 // 3. GRALLOC_USAGE_HW_COMPOSER
418 // 4. GRALLOC_USAGE_HW_VIDEO_ENCODER
419 if (camera3_stream::format == HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED &&
420 (u & (GRALLOC_USAGE_HW_TEXTURE | GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_HW_COMPOSER |
421 GRALLOC_USAGE_HW_VIDEO_ENCODER)) == 0) {
422 u |= GRALLOC_USAGE_HW_CAMERA_ZSL;
423 }
424
425 *usage = u;
Eino-Ville Talvalab2f5b192013-07-30 14:36:03 -0700426 return res;
427}
428
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800429}; // namespace camera3
430
431}; // namespace android