blob: 055913a140def7af207d81acd9ea4215b50d5e91 [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
21// This is needed for stdint.h to define INT64_MAX in C++
22#define __STDC_LIMIT_MACROS
23
24#include <utils/Log.h>
25#include <utils/Trace.h>
26#include "Camera3OutputStream.h"
27
28#ifndef container_of
29#define container_of(ptr, type, member) \
30 (type *)((char*)(ptr) - offsetof(type, member))
31#endif
32
33namespace android {
34
35namespace camera3 {
36
37Camera3OutputStream::Camera3OutputStream(int id,
38 sp<ANativeWindow> consumer,
39 uint32_t width, uint32_t height, int format) :
40 Camera3Stream(id, CAMERA3_STREAM_OUTPUT, width, height, 0, format),
41 mConsumer(consumer),
42 mTransform(0),
43 mTotalBufferCount(0),
44 mDequeuedBufferCount(0),
45 mFrameCount(0),
46 mLastTimestamp(0) {
47
48 mCombinedFence = new Fence();
49 if (mConsumer == NULL) {
50 ALOGE("%s: Consumer is NULL!", __FUNCTION__);
51 mState = STATE_ERROR;
52 }
53}
54
55Camera3OutputStream::Camera3OutputStream(int id,
56 sp<ANativeWindow> consumer,
57 uint32_t width, uint32_t height, size_t maxSize, int format) :
58 Camera3Stream(id, CAMERA3_STREAM_OUTPUT,
59 width, height, maxSize, format),
Igor Murashkina55b5452013-04-02 16:36:33 -070060 mConsumer(consumer),
61 mTransform(0),
62 mTotalBufferCount(0),
63 mDequeuedBufferCount(0),
64 mFrameCount(0),
65 mLastTimestamp(0) {
66
67 mCombinedFence = new Fence();
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -080068
69 if (format != HAL_PIXEL_FORMAT_BLOB) {
70 ALOGE("%s: Bad format for size-only stream: %d", __FUNCTION__,
71 format);
72 mState = STATE_ERROR;
73 }
74
75 if (mConsumer == NULL) {
76 ALOGE("%s: Consumer is NULL!", __FUNCTION__);
77 mState = STATE_ERROR;
78 }
79}
80
81Camera3OutputStream::~Camera3OutputStream() {
82 disconnectLocked();
83}
84
85status_t Camera3OutputStream::getBufferLocked(camera3_stream_buffer *buffer) {
86 ATRACE_CALL();
87 status_t res;
88
89 // Allow dequeue during IN_[RE]CONFIG for registration
90 if (mState != STATE_CONFIGURED &&
91 mState != STATE_IN_CONFIG && mState != STATE_IN_RECONFIG) {
92 ALOGE("%s: Stream %d: Can't get buffers in unconfigured state %d",
93 __FUNCTION__, mId, mState);
94 return INVALID_OPERATION;
95 }
96
97 // Only limit dequeue amount when fully configured
98 if (mState == STATE_CONFIGURED &&
99 mDequeuedBufferCount == camera3_stream::max_buffers) {
100 ALOGE("%s: Stream %d: Already dequeued maximum number of simultaneous"
101 " buffers (%d)", __FUNCTION__, mId,
102 camera3_stream::max_buffers);
103 return INVALID_OPERATION;
104 }
105
106 ANativeWindowBuffer* anb;
107 int fenceFd;
108
109 res = mConsumer->dequeueBuffer(mConsumer.get(), &anb, &fenceFd);
110 if (res != OK) {
111 ALOGE("%s: Stream %d: Can't dequeue next output buffer: %s (%d)",
112 __FUNCTION__, mId, strerror(-res), res);
113 return res;
114 }
115
116 // Handing out a raw pointer to this object. Increment internal refcount.
117 incStrong(this);
118 buffer->stream = this;
119 buffer->buffer = &(anb->handle);
120 buffer->acquire_fence = fenceFd;
121 buffer->release_fence = -1;
122 buffer->status = CAMERA3_BUFFER_STATUS_OK;
123
124 mDequeuedBufferCount++;
125
126 return OK;
127}
128
129status_t Camera3OutputStream::returnBufferLocked(
130 const camera3_stream_buffer &buffer,
131 nsecs_t timestamp) {
132 ATRACE_CALL();
133 status_t res;
134
135 // returnBuffer may be called from a raw pointer, not a sp<>, and we'll be
136 // decrementing the internal refcount next. In case this is the last ref, we
137 // might get destructed on the decStrong(), so keep an sp around until the
138 // end of the call - otherwise have to sprinkle the decStrong on all exit
139 // points.
140 sp<Camera3OutputStream> keepAlive(this);
141 decStrong(this);
142
143 // Allow buffers to be returned in the error state, to allow for disconnect
144 // and in the in-config states for registration
145 if (mState == STATE_CONSTRUCTED) {
146 ALOGE("%s: Stream %d: Can't return buffers in unconfigured state %d",
147 __FUNCTION__, mId, mState);
148 return INVALID_OPERATION;
149 }
150 if (mDequeuedBufferCount == 0) {
151 ALOGE("%s: Stream %d: No buffers outstanding to return", __FUNCTION__,
152 mId);
153 return INVALID_OPERATION;
154 }
Igor Murashkin5a1798a2013-05-07 10:58:13 -0700155
156 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)",
176 __FUNCTION__, mId, strerror(-res), res);
177 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 /**
186 * Return buffer back to ANativeWindow
187 */
188 if (buffer.status == CAMERA3_BUFFER_STATUS_ERROR) {
189 // Cancel buffer
190 res = mConsumer->cancelBuffer(mConsumer.get(),
191 container_of(buffer.buffer, ANativeWindowBuffer, handle),
192 anwReleaseFence);
193 if (res != OK) {
194 ALOGE("%s: Stream %d: Error cancelling buffer to native window:"
195 " %s (%d)", __FUNCTION__, mId, strerror(-res), res);
196 }
197 } else {
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800198 res = mConsumer->queueBuffer(mConsumer.get(),
199 container_of(buffer.buffer, ANativeWindowBuffer, handle),
200 anwReleaseFence);
201 if (res != OK) {
202 ALOGE("%s: Stream %d: Error queueing buffer to native window: %s (%d)",
203 __FUNCTION__, mId, strerror(-res), res);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800204 }
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800205 }
206
Igor Murashkin5a1798a2013-05-07 10:58:13 -0700207 if (res != OK) {
208 close(anwReleaseFence);
209 return res;
210 }
211
212 mCombinedFence = Fence::merge(mName, mCombinedFence, releaseFence);
213
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800214 mDequeuedBufferCount--;
215 mBufferReturnedSignal.signal();
216 mLastTimestamp = timestamp;
217
218 return OK;
219}
220
221bool Camera3OutputStream::hasOutstandingBuffersLocked() const {
222 nsecs_t signalTime = mCombinedFence->getSignalTime();
223 ALOGV("%s: Stream %d: Has %d outstanding buffers,"
224 " buffer signal time is %lld",
225 __FUNCTION__, mId, mDequeuedBufferCount, signalTime);
226 if (mDequeuedBufferCount > 0 || signalTime == INT64_MAX) {
227 return true;
228 }
229 return false;
230}
231
232status_t Camera3OutputStream::waitUntilIdle(nsecs_t timeout) {
233 status_t res;
234 {
235 Mutex::Autolock l(mLock);
236 while (mDequeuedBufferCount > 0) {
237 if (timeout != TIMEOUT_NEVER) {
238 nsecs_t startTime = systemTime();
239 res = mBufferReturnedSignal.waitRelative(mLock, timeout);
240 if (res == TIMED_OUT) {
241 return res;
242 } else if (res != OK) {
243 ALOGE("%s: Error waiting for outstanding buffers: %s (%d)",
244 __FUNCTION__, strerror(-res), res);
245 return res;
246 }
247 nsecs_t deltaTime = systemTime() - startTime;
248 if (timeout <= deltaTime) {
249 timeout = 0;
250 } else {
251 timeout -= deltaTime;
252 }
253 } else {
254 res = mBufferReturnedSignal.wait(mLock);
255 if (res != OK) {
256 ALOGE("%s: Error waiting for outstanding buffers: %s (%d)",
257 __FUNCTION__, strerror(-res), res);
258 return res;
259 }
260 }
261 }
262 }
263
264 // No lock
265
266 unsigned int timeoutMs;
267 if (timeout == TIMEOUT_NEVER) {
268 timeoutMs = Fence::TIMEOUT_NEVER;
269 } else if (timeout == 0) {
270 timeoutMs = 0;
271 } else {
272 // Round up to wait at least 1 ms
273 timeoutMs = (timeout + 999999) / 1000000;
274 }
275
276 return mCombinedFence->wait(timeoutMs);
277}
278
279void Camera3OutputStream::dump(int fd, const Vector<String16> &args) const {
280 (void) args;
281 String8 lines;
282 lines.appendFormat(" Stream[%d]: Output\n", mId);
283 lines.appendFormat(" State: %d\n", mState);
284 lines.appendFormat(" Dims: %d x %d, format 0x%x\n",
285 camera3_stream::width, camera3_stream::height,
286 camera3_stream::format);
287 lines.appendFormat(" Max size: %d\n", mMaxSize);
288 lines.appendFormat(" Usage: %d, max HAL buffers: %d\n",
289 camera3_stream::usage, camera3_stream::max_buffers);
290 lines.appendFormat(" Frames produced: %d, last timestamp: %lld ns\n",
291 mFrameCount, mLastTimestamp);
292 lines.appendFormat(" Total buffers: %d, currently dequeued: %d\n",
293 mTotalBufferCount, mDequeuedBufferCount);
294 write(fd, lines.string(), lines.size());
295}
296
297status_t Camera3OutputStream::setTransform(int transform) {
298 ATRACE_CALL();
299 Mutex::Autolock l(mLock);
300 return setTransformLocked(transform);
301}
302
303status_t Camera3OutputStream::setTransformLocked(int transform) {
304 status_t res = OK;
305 if (mState == STATE_ERROR) {
306 ALOGE("%s: Stream in error state", __FUNCTION__);
307 return INVALID_OPERATION;
308 }
309
310 mTransform = transform;
311 if (mState == STATE_CONFIGURED) {
312 res = native_window_set_buffers_transform(mConsumer.get(),
313 transform);
314 if (res != OK) {
315 ALOGE("%s: Unable to configure stream transform to %x: %s (%d)",
316 __FUNCTION__, transform, strerror(-res), res);
317 }
318 }
319 return res;
320}
321
322status_t Camera3OutputStream::configureQueueLocked() {
323 status_t res;
324
325 switch (mState) {
326 case STATE_IN_RECONFIG:
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700327 res = disconnectLocked();
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800328 if (res != OK) {
329 return res;
330 }
331 break;
332 case STATE_IN_CONFIG:
333 // OK
334 break;
335 default:
336 ALOGE("%s: Bad state: %d", __FUNCTION__, mState);
337 return INVALID_OPERATION;
338 }
339
340 // Configure consumer-side ANativeWindow interface
341 res = native_window_api_connect(mConsumer.get(),
342 NATIVE_WINDOW_API_CAMERA);
343 if (res != OK) {
344 ALOGE("%s: Unable to connect to native window for stream %d",
345 __FUNCTION__, mId);
346 return res;
347 }
348
349 res = native_window_set_usage(mConsumer.get(), camera3_stream::usage);
350 if (res != OK) {
351 ALOGE("%s: Unable to configure usage %08x for stream %d",
352 __FUNCTION__, camera3_stream::usage, mId);
353 return res;
354 }
355
356 res = native_window_set_scaling_mode(mConsumer.get(),
357 NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
358 if (res != OK) {
359 ALOGE("%s: Unable to configure stream scaling: %s (%d)",
360 __FUNCTION__, strerror(-res), res);
361 return res;
362 }
363
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800364 if (mMaxSize == 0) {
365 // For buffers of known size
366 res = native_window_set_buffers_geometry(mConsumer.get(),
367 camera3_stream::width, camera3_stream::height,
368 camera3_stream::format);
369 } else {
370 // For buffers with bounded size
371 res = native_window_set_buffers_geometry(mConsumer.get(),
372 mMaxSize, 1,
373 camera3_stream::format);
374 }
375 if (res != OK) {
376 ALOGE("%s: Unable to configure stream buffer geometry"
377 " %d x %d, format %x for stream %d",
378 __FUNCTION__, camera3_stream::width, camera3_stream::height,
379 camera3_stream::format, mId);
380 return res;
381 }
382
383 int maxConsumerBuffers;
384 res = mConsumer->query(mConsumer.get(),
385 NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS, &maxConsumerBuffers);
386 if (res != OK) {
387 ALOGE("%s: Unable to query consumer undequeued"
388 " buffer count for stream %d", __FUNCTION__, mId);
389 return res;
390 }
391
392 ALOGV("%s: Consumer wants %d buffers", __FUNCTION__,
393 maxConsumerBuffers);
394
395 mTotalBufferCount = maxConsumerBuffers + camera3_stream::max_buffers;
396 mDequeuedBufferCount = 0;
397 mFrameCount = 0;
398 mLastTimestamp = 0;
399
400 res = native_window_set_buffer_count(mConsumer.get(),
401 mTotalBufferCount);
402 if (res != OK) {
403 ALOGE("%s: Unable to set buffer count for stream %d",
404 __FUNCTION__, mId);
405 return res;
406 }
407
408 res = native_window_set_buffers_transform(mConsumer.get(),
409 mTransform);
410 if (res != OK) {
411 ALOGE("%s: Unable to configure stream transform to %x: %s (%d)",
412 __FUNCTION__, mTransform, strerror(-res), res);
413 }
414
415 return OK;
416}
417
418size_t Camera3OutputStream::getBufferCountLocked() {
419 return mTotalBufferCount;
420}
421
422status_t Camera3OutputStream::disconnectLocked() {
423 status_t res;
424
425 switch (mState) {
426 case STATE_IN_RECONFIG:
427 case STATE_CONFIGURED:
428 // OK
429 break;
430 default:
431 // No connection, nothing to do
432 return OK;
433 }
434
435 if (mDequeuedBufferCount > 0) {
436 ALOGE("%s: Can't disconnect with %d buffers still dequeued!",
437 __FUNCTION__, mDequeuedBufferCount);
438 return INVALID_OPERATION;
439 }
440
441 res = native_window_api_disconnect(mConsumer.get(), NATIVE_WINDOW_API_CAMERA);
442
443 /**
444 * This is not an error. if client calling process dies, the window will
445 * also die and all calls to it will return DEAD_OBJECT, thus it's already
446 * "disconnected"
447 */
448 if (res == DEAD_OBJECT) {
449 ALOGW("%s: While disconnecting stream %d from native window, the"
450 " native window died from under us", __FUNCTION__, mId);
451 }
452 else if (res != OK) {
453 ALOGE("%s: Unable to disconnect stream %d from native window (error %d %s)",
454 __FUNCTION__, mId, res, strerror(-res));
455 mState = STATE_ERROR;
456 return res;
457 }
458
459 mState = (mState == STATE_IN_RECONFIG) ? STATE_IN_CONFIG : STATE_CONSTRUCTED;
460 return OK;
461}
462
463}; // namespace camera3
464
465}; // namespace android