blob: ec8cf0d7b018a5032a2ebdcc29340bc546de533d [file] [log] [blame]
Eino-Ville Talvala8be20f52013-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 Murashkine09f4862013-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 Talvala8be20f52013-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 }
155 if (buffer.status == CAMERA3_BUFFER_STATUS_ERROR) {
156 res = mConsumer->cancelBuffer(mConsumer.get(),
157 container_of(buffer.buffer, ANativeWindowBuffer, handle),
158 buffer.release_fence);
159 if (res != OK) {
160 ALOGE("%s: Stream %d: Error cancelling buffer to native window:"
161 " %s (%d)", __FUNCTION__, mId, strerror(-res), res);
162 return res;
163 }
164 } else {
165 res = native_window_set_buffers_timestamp(mConsumer.get(), timestamp);
166 if (res != OK) {
167 ALOGE("%s: Stream %d: Error setting timestamp: %s (%d)",
168 __FUNCTION__, mId, strerror(-res), res);
169 return res;
170 }
171
172 sp<Fence> releaseFence = new Fence(buffer.release_fence);
173 int anwReleaseFence = releaseFence->dup();
174
175 res = mConsumer->queueBuffer(mConsumer.get(),
176 container_of(buffer.buffer, ANativeWindowBuffer, handle),
177 anwReleaseFence);
178 if (res != OK) {
179 ALOGE("%s: Stream %d: Error queueing buffer to native window: %s (%d)",
180 __FUNCTION__, mId, strerror(-res), res);
181 close(anwReleaseFence);
182 return res;
183 }
184
185 mCombinedFence = Fence::merge(mName, mCombinedFence, releaseFence);
186 }
187
188 mDequeuedBufferCount--;
189 mBufferReturnedSignal.signal();
190 mLastTimestamp = timestamp;
191
192 return OK;
193}
194
195bool Camera3OutputStream::hasOutstandingBuffersLocked() const {
196 nsecs_t signalTime = mCombinedFence->getSignalTime();
197 ALOGV("%s: Stream %d: Has %d outstanding buffers,"
198 " buffer signal time is %lld",
199 __FUNCTION__, mId, mDequeuedBufferCount, signalTime);
200 if (mDequeuedBufferCount > 0 || signalTime == INT64_MAX) {
201 return true;
202 }
203 return false;
204}
205
206status_t Camera3OutputStream::waitUntilIdle(nsecs_t timeout) {
207 status_t res;
208 {
209 Mutex::Autolock l(mLock);
210 while (mDequeuedBufferCount > 0) {
211 if (timeout != TIMEOUT_NEVER) {
212 nsecs_t startTime = systemTime();
213 res = mBufferReturnedSignal.waitRelative(mLock, timeout);
214 if (res == TIMED_OUT) {
215 return res;
216 } else if (res != OK) {
217 ALOGE("%s: Error waiting for outstanding buffers: %s (%d)",
218 __FUNCTION__, strerror(-res), res);
219 return res;
220 }
221 nsecs_t deltaTime = systemTime() - startTime;
222 if (timeout <= deltaTime) {
223 timeout = 0;
224 } else {
225 timeout -= deltaTime;
226 }
227 } else {
228 res = mBufferReturnedSignal.wait(mLock);
229 if (res != OK) {
230 ALOGE("%s: Error waiting for outstanding buffers: %s (%d)",
231 __FUNCTION__, strerror(-res), res);
232 return res;
233 }
234 }
235 }
236 }
237
238 // No lock
239
240 unsigned int timeoutMs;
241 if (timeout == TIMEOUT_NEVER) {
242 timeoutMs = Fence::TIMEOUT_NEVER;
243 } else if (timeout == 0) {
244 timeoutMs = 0;
245 } else {
246 // Round up to wait at least 1 ms
247 timeoutMs = (timeout + 999999) / 1000000;
248 }
249
250 return mCombinedFence->wait(timeoutMs);
251}
252
253void Camera3OutputStream::dump(int fd, const Vector<String16> &args) const {
254 (void) args;
255 String8 lines;
256 lines.appendFormat(" Stream[%d]: Output\n", mId);
257 lines.appendFormat(" State: %d\n", mState);
258 lines.appendFormat(" Dims: %d x %d, format 0x%x\n",
259 camera3_stream::width, camera3_stream::height,
260 camera3_stream::format);
261 lines.appendFormat(" Max size: %d\n", mMaxSize);
262 lines.appendFormat(" Usage: %d, max HAL buffers: %d\n",
263 camera3_stream::usage, camera3_stream::max_buffers);
264 lines.appendFormat(" Frames produced: %d, last timestamp: %lld ns\n",
265 mFrameCount, mLastTimestamp);
266 lines.appendFormat(" Total buffers: %d, currently dequeued: %d\n",
267 mTotalBufferCount, mDequeuedBufferCount);
268 write(fd, lines.string(), lines.size());
269}
270
271status_t Camera3OutputStream::setTransform(int transform) {
272 ATRACE_CALL();
273 Mutex::Autolock l(mLock);
274 return setTransformLocked(transform);
275}
276
277status_t Camera3OutputStream::setTransformLocked(int transform) {
278 status_t res = OK;
279 if (mState == STATE_ERROR) {
280 ALOGE("%s: Stream in error state", __FUNCTION__);
281 return INVALID_OPERATION;
282 }
283
284 mTransform = transform;
285 if (mState == STATE_CONFIGURED) {
286 res = native_window_set_buffers_transform(mConsumer.get(),
287 transform);
288 if (res != OK) {
289 ALOGE("%s: Unable to configure stream transform to %x: %s (%d)",
290 __FUNCTION__, transform, strerror(-res), res);
291 }
292 }
293 return res;
294}
295
296status_t Camera3OutputStream::configureQueueLocked() {
297 status_t res;
298
299 switch (mState) {
300 case STATE_IN_RECONFIG:
Igor Murashkin0776a142013-04-15 14:59:22 -0700301 res = disconnectLocked();
Eino-Ville Talvala8be20f52013-03-06 16:20:06 -0800302 if (res != OK) {
303 return res;
304 }
305 break;
306 case STATE_IN_CONFIG:
307 // OK
308 break;
309 default:
310 ALOGE("%s: Bad state: %d", __FUNCTION__, mState);
311 return INVALID_OPERATION;
312 }
313
314 // Configure consumer-side ANativeWindow interface
315 res = native_window_api_connect(mConsumer.get(),
316 NATIVE_WINDOW_API_CAMERA);
317 if (res != OK) {
318 ALOGE("%s: Unable to connect to native window for stream %d",
319 __FUNCTION__, mId);
320 return res;
321 }
322
323 res = native_window_set_usage(mConsumer.get(), camera3_stream::usage);
324 if (res != OK) {
325 ALOGE("%s: Unable to configure usage %08x for stream %d",
326 __FUNCTION__, camera3_stream::usage, mId);
327 return res;
328 }
329
330 res = native_window_set_scaling_mode(mConsumer.get(),
331 NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
332 if (res != OK) {
333 ALOGE("%s: Unable to configure stream scaling: %s (%d)",
334 __FUNCTION__, strerror(-res), res);
335 return res;
336 }
337
338 res = setTransformLocked(0);
339 if (res != OK) {
340 return res;
341 }
342
343 if (mMaxSize == 0) {
344 // For buffers of known size
345 res = native_window_set_buffers_geometry(mConsumer.get(),
346 camera3_stream::width, camera3_stream::height,
347 camera3_stream::format);
348 } else {
349 // For buffers with bounded size
350 res = native_window_set_buffers_geometry(mConsumer.get(),
351 mMaxSize, 1,
352 camera3_stream::format);
353 }
354 if (res != OK) {
355 ALOGE("%s: Unable to configure stream buffer geometry"
356 " %d x %d, format %x for stream %d",
357 __FUNCTION__, camera3_stream::width, camera3_stream::height,
358 camera3_stream::format, mId);
359 return res;
360 }
361
362 int maxConsumerBuffers;
363 res = mConsumer->query(mConsumer.get(),
364 NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS, &maxConsumerBuffers);
365 if (res != OK) {
366 ALOGE("%s: Unable to query consumer undequeued"
367 " buffer count for stream %d", __FUNCTION__, mId);
368 return res;
369 }
370
371 ALOGV("%s: Consumer wants %d buffers", __FUNCTION__,
372 maxConsumerBuffers);
373
374 mTotalBufferCount = maxConsumerBuffers + camera3_stream::max_buffers;
375 mDequeuedBufferCount = 0;
376 mFrameCount = 0;
377 mLastTimestamp = 0;
378
379 res = native_window_set_buffer_count(mConsumer.get(),
380 mTotalBufferCount);
381 if (res != OK) {
382 ALOGE("%s: Unable to set buffer count for stream %d",
383 __FUNCTION__, mId);
384 return res;
385 }
386
387 res = native_window_set_buffers_transform(mConsumer.get(),
388 mTransform);
389 if (res != OK) {
390 ALOGE("%s: Unable to configure stream transform to %x: %s (%d)",
391 __FUNCTION__, mTransform, strerror(-res), res);
392 }
393
394 return OK;
395}
396
397size_t Camera3OutputStream::getBufferCountLocked() {
398 return mTotalBufferCount;
399}
400
401status_t Camera3OutputStream::disconnectLocked() {
402 status_t res;
403
404 switch (mState) {
405 case STATE_IN_RECONFIG:
406 case STATE_CONFIGURED:
407 // OK
408 break;
409 default:
410 // No connection, nothing to do
411 return OK;
412 }
413
414 if (mDequeuedBufferCount > 0) {
415 ALOGE("%s: Can't disconnect with %d buffers still dequeued!",
416 __FUNCTION__, mDequeuedBufferCount);
417 return INVALID_OPERATION;
418 }
419
420 res = native_window_api_disconnect(mConsumer.get(), NATIVE_WINDOW_API_CAMERA);
421
422 /**
423 * This is not an error. if client calling process dies, the window will
424 * also die and all calls to it will return DEAD_OBJECT, thus it's already
425 * "disconnected"
426 */
427 if (res == DEAD_OBJECT) {
428 ALOGW("%s: While disconnecting stream %d from native window, the"
429 " native window died from under us", __FUNCTION__, mId);
430 }
431 else if (res != OK) {
432 ALOGE("%s: Unable to disconnect stream %d from native window (error %d %s)",
433 __FUNCTION__, mId, res, strerror(-res));
434 mState = STATE_ERROR;
435 return res;
436 }
437
438 mState = (mState == STATE_IN_RECONFIG) ? STATE_IN_CONFIG : STATE_CONSTRUCTED;
439 return OK;
440}
441
442}; // namespace camera3
443
444}; // namespace android