blob: a2c97d4c78bdce96052fe464fa806412da6c548a [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),
Igor Murashkine3a9f962013-05-08 18:03:15 -070040 mTransform(0) {
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -080041
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -080042 if (mConsumer == NULL) {
43 ALOGE("%s: Consumer is NULL!", __FUNCTION__);
44 mState = STATE_ERROR;
45 }
46}
47
48Camera3OutputStream::Camera3OutputStream(int id,
49 sp<ANativeWindow> consumer,
50 uint32_t width, uint32_t height, size_t maxSize, int format) :
Igor Murashkine3a9f962013-05-08 18:03:15 -070051 Camera3IOStreamBase(id, CAMERA3_STREAM_OUTPUT, width, height, maxSize,
52 format),
Igor Murashkina55b5452013-04-02 16:36:33 -070053 mConsumer(consumer),
Igor Murashkine3a9f962013-05-08 18:03:15 -070054 mTransform(0) {
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -080055
56 if (format != HAL_PIXEL_FORMAT_BLOB) {
57 ALOGE("%s: Bad format for size-only stream: %d", __FUNCTION__,
58 format);
59 mState = STATE_ERROR;
60 }
61
62 if (mConsumer == NULL) {
63 ALOGE("%s: Consumer is NULL!", __FUNCTION__);
64 mState = STATE_ERROR;
65 }
66}
67
Igor Murashkine3a9f962013-05-08 18:03:15 -070068Camera3OutputStream::Camera3OutputStream(int id, camera3_stream_type_t type,
69 uint32_t width, uint32_t height,
70 int format) :
71 Camera3IOStreamBase(id, type, width, height,
72 /*maxSize*/0,
73 format),
74 mTransform(0) {
75
76 // Subclasses expected to initialize mConsumer themselves
77}
78
79
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -080080Camera3OutputStream::~Camera3OutputStream() {
81 disconnectLocked();
82}
83
84status_t Camera3OutputStream::getBufferLocked(camera3_stream_buffer *buffer) {
85 ATRACE_CALL();
86 status_t res;
87
Igor Murashkine3a9f962013-05-08 18:03:15 -070088 if ((res = getBufferPreconditionCheckLocked()) != OK) {
89 return res;
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -080090 }
91
92 ANativeWindowBuffer* anb;
93 int fenceFd;
94
95 res = mConsumer->dequeueBuffer(mConsumer.get(), &anb, &fenceFd);
96 if (res != OK) {
97 ALOGE("%s: Stream %d: Can't dequeue next output buffer: %s (%d)",
98 __FUNCTION__, mId, strerror(-res), res);
99 return res;
100 }
101
Igor Murashkine3a9f962013-05-08 18:03:15 -0700102 /**
103 * FenceFD now owned by HAL except in case of error,
104 * in which case we reassign it to acquire_fence
105 */
106 handoutBufferLocked(*buffer, &(anb->handle), /*acquireFence*/fenceFd,
107 /*releaseFence*/-1, CAMERA3_BUFFER_STATUS_OK);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800108
109 return OK;
110}
111
112status_t Camera3OutputStream::returnBufferLocked(
113 const camera3_stream_buffer &buffer,
114 nsecs_t timestamp) {
115 ATRACE_CALL();
Igor Murashkine3a9f962013-05-08 18:03:15 -0700116
117 status_t res = returnAnyBufferLocked(buffer, timestamp, /*output*/true);
118
119 if (res != OK) {
120 return res;
121 }
122
123 mLastTimestamp = timestamp;
124
125 return OK;
126}
127
128status_t Camera3OutputStream::returnBufferCheckedLocked(
129 const camera3_stream_buffer &buffer,
130 nsecs_t timestamp,
131 bool output,
132 /*out*/
133 sp<Fence> *releaseFenceOut) {
134
135 (void)output;
136 ALOG_ASSERT(output, "Expected output to be true");
137
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800138 status_t res;
Igor Murashkin5a1798a2013-05-07 10:58:13 -0700139 sp<Fence> releaseFence;
140
141 /**
142 * Fence management - calculate Release Fence
143 */
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800144 if (buffer.status == CAMERA3_BUFFER_STATUS_ERROR) {
Igor Murashkin5a1798a2013-05-07 10:58:13 -0700145 if (buffer.release_fence != -1) {
146 ALOGE("%s: Stream %d: HAL should not set release_fence(%d) when "
147 "there is an error", __FUNCTION__, mId, buffer.release_fence);
148 close(buffer.release_fence);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800149 }
Igor Murashkin5a1798a2013-05-07 10:58:13 -0700150
151 /**
152 * Reassign release fence as the acquire fence in case of error
153 */
154 releaseFence = new Fence(buffer.acquire_fence);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800155 } else {
156 res = native_window_set_buffers_timestamp(mConsumer.get(), timestamp);
157 if (res != OK) {
158 ALOGE("%s: Stream %d: Error setting timestamp: %s (%d)",
Igor Murashkine3a9f962013-05-08 18:03:15 -0700159 __FUNCTION__, mId, strerror(-res), res);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800160 return res;
161 }
162
Igor Murashkin5a1798a2013-05-07 10:58:13 -0700163 releaseFence = new Fence(buffer.release_fence);
164 }
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800165
Igor Murashkin5a1798a2013-05-07 10:58:13 -0700166 int anwReleaseFence = releaseFence->dup();
167
168 /**
169 * Return buffer back to ANativeWindow
170 */
171 if (buffer.status == CAMERA3_BUFFER_STATUS_ERROR) {
172 // Cancel buffer
173 res = mConsumer->cancelBuffer(mConsumer.get(),
174 container_of(buffer.buffer, ANativeWindowBuffer, handle),
175 anwReleaseFence);
176 if (res != OK) {
177 ALOGE("%s: Stream %d: Error cancelling buffer to native window:"
Igor Murashkine3a9f962013-05-08 18:03:15 -0700178 " %s (%d)", __FUNCTION__, mId, strerror(-res), res);
Igor Murashkin5a1798a2013-05-07 10:58:13 -0700179 }
180 } else {
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800181 res = mConsumer->queueBuffer(mConsumer.get(),
182 container_of(buffer.buffer, ANativeWindowBuffer, handle),
183 anwReleaseFence);
184 if (res != OK) {
Igor Murashkine3a9f962013-05-08 18:03:15 -0700185 ALOGE("%s: Stream %d: Error queueing buffer to native window: "
186 "%s (%d)", __FUNCTION__, mId, strerror(-res), res);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800187 }
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800188 }
189
Igor Murashkin5a1798a2013-05-07 10:58:13 -0700190 if (res != OK) {
191 close(anwReleaseFence);
192 return res;
193 }
194
Igor Murashkine3a9f962013-05-08 18:03:15 -0700195 *releaseFenceOut = releaseFence;
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800196
197 return OK;
198}
199
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800200void Camera3OutputStream::dump(int fd, const Vector<String16> &args) const {
201 (void) args;
202 String8 lines;
203 lines.appendFormat(" Stream[%d]: Output\n", mId);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800204 write(fd, lines.string(), lines.size());
Igor Murashkine3a9f962013-05-08 18:03:15 -0700205
206 Camera3IOStreamBase::dump(fd, args);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800207}
208
209status_t Camera3OutputStream::setTransform(int transform) {
210 ATRACE_CALL();
211 Mutex::Autolock l(mLock);
212 return setTransformLocked(transform);
213}
214
215status_t Camera3OutputStream::setTransformLocked(int transform) {
216 status_t res = OK;
217 if (mState == STATE_ERROR) {
218 ALOGE("%s: Stream in error state", __FUNCTION__);
219 return INVALID_OPERATION;
220 }
221
222 mTransform = transform;
223 if (mState == STATE_CONFIGURED) {
224 res = native_window_set_buffers_transform(mConsumer.get(),
225 transform);
226 if (res != OK) {
227 ALOGE("%s: Unable to configure stream transform to %x: %s (%d)",
228 __FUNCTION__, transform, strerror(-res), res);
229 }
230 }
231 return res;
232}
233
234status_t Camera3OutputStream::configureQueueLocked() {
235 status_t res;
236
Igor Murashkine3a9f962013-05-08 18:03:15 -0700237 if ((res = Camera3IOStreamBase::configureQueueLocked()) != OK) {
238 return res;
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800239 }
240
Igor Murashkine3a9f962013-05-08 18:03:15 -0700241 ALOG_ASSERT(mConsumer != 0, "mConsumer should never be NULL");
242
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800243 // Configure consumer-side ANativeWindow interface
244 res = native_window_api_connect(mConsumer.get(),
245 NATIVE_WINDOW_API_CAMERA);
246 if (res != OK) {
247 ALOGE("%s: Unable to connect to native window for stream %d",
248 __FUNCTION__, mId);
249 return res;
250 }
251
252 res = native_window_set_usage(mConsumer.get(), camera3_stream::usage);
253 if (res != OK) {
254 ALOGE("%s: Unable to configure usage %08x for stream %d",
255 __FUNCTION__, camera3_stream::usage, mId);
256 return res;
257 }
258
259 res = native_window_set_scaling_mode(mConsumer.get(),
260 NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
261 if (res != OK) {
262 ALOGE("%s: Unable to configure stream scaling: %s (%d)",
263 __FUNCTION__, strerror(-res), res);
264 return res;
265 }
266
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800267 if (mMaxSize == 0) {
268 // For buffers of known size
269 res = native_window_set_buffers_geometry(mConsumer.get(),
270 camera3_stream::width, camera3_stream::height,
271 camera3_stream::format);
272 } else {
273 // For buffers with bounded size
274 res = native_window_set_buffers_geometry(mConsumer.get(),
275 mMaxSize, 1,
276 camera3_stream::format);
277 }
278 if (res != OK) {
279 ALOGE("%s: Unable to configure stream buffer geometry"
280 " %d x %d, format %x for stream %d",
281 __FUNCTION__, camera3_stream::width, camera3_stream::height,
282 camera3_stream::format, mId);
283 return res;
284 }
285
286 int maxConsumerBuffers;
287 res = mConsumer->query(mConsumer.get(),
288 NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS, &maxConsumerBuffers);
289 if (res != OK) {
290 ALOGE("%s: Unable to query consumer undequeued"
291 " buffer count for stream %d", __FUNCTION__, mId);
292 return res;
293 }
294
295 ALOGV("%s: Consumer wants %d buffers", __FUNCTION__,
296 maxConsumerBuffers);
297
298 mTotalBufferCount = maxConsumerBuffers + camera3_stream::max_buffers;
299 mDequeuedBufferCount = 0;
300 mFrameCount = 0;
301 mLastTimestamp = 0;
302
303 res = native_window_set_buffer_count(mConsumer.get(),
304 mTotalBufferCount);
305 if (res != OK) {
306 ALOGE("%s: Unable to set buffer count for stream %d",
307 __FUNCTION__, mId);
308 return res;
309 }
310
311 res = native_window_set_buffers_transform(mConsumer.get(),
312 mTransform);
313 if (res != OK) {
314 ALOGE("%s: Unable to configure stream transform to %x: %s (%d)",
315 __FUNCTION__, mTransform, strerror(-res), res);
316 }
317
318 return OK;
319}
320
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800321status_t Camera3OutputStream::disconnectLocked() {
322 status_t res;
323
Igor Murashkine3a9f962013-05-08 18:03:15 -0700324 if ((res = Camera3IOStreamBase::disconnectLocked()) != OK) {
325 return res;
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800326 }
327
Igor Murashkine3a9f962013-05-08 18:03:15 -0700328 res = native_window_api_disconnect(mConsumer.get(),
329 NATIVE_WINDOW_API_CAMERA);
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800330
331 /**
332 * This is not an error. if client calling process dies, the window will
333 * also die and all calls to it will return DEAD_OBJECT, thus it's already
334 * "disconnected"
335 */
336 if (res == DEAD_OBJECT) {
337 ALOGW("%s: While disconnecting stream %d from native window, the"
338 " native window died from under us", __FUNCTION__, mId);
339 }
340 else if (res != OK) {
Igor Murashkine3a9f962013-05-08 18:03:15 -0700341 ALOGE("%s: Unable to disconnect stream %d from native window "
342 "(error %d %s)",
343 __FUNCTION__, mId, res, strerror(-res));
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800344 mState = STATE_ERROR;
345 return res;
346 }
347
Igor Murashkine3a9f962013-05-08 18:03:15 -0700348 mState = (mState == STATE_IN_RECONFIG) ? STATE_IN_CONFIG
349 : STATE_CONSTRUCTED;
Eino-Ville Talvalafd58f1a2013-03-06 16:20:06 -0800350 return OK;
351}
352
353}; // namespace camera3
354
355}; // namespace android