blob: 04deac5fd9d2686bcf1374ebeaf9aa99baf46ef3 [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-ZslStream"
18#define ATRACE_TAG ATRACE_TAG_CAMERA
19//#define LOG_NDEBUG 0
20
21#include <utils/Log.h>
22#include <utils/Trace.h>
23#include "Camera3ZslStream.h"
24
Igor Murashkinae500e52013-04-22 14:03:54 -070025typedef android::RingBufferConsumer::PinnedBufferItem PinnedBufferItem;
26
Eino-Ville Talvala8be20f52013-03-06 16:20:06 -080027namespace android {
28
29namespace camera3 {
30
Igor Murashkinae500e52013-04-22 14:03:54 -070031namespace {
32struct TimestampFinder : public RingBufferConsumer::RingBufferComparator {
33 typedef RingBufferConsumer::BufferInfo BufferInfo;
34
35 enum {
36 SELECT_I1 = -1,
37 SELECT_I2 = 1,
38 SELECT_NEITHER = 0,
39 };
40
41 TimestampFinder(nsecs_t timestamp) : mTimestamp(timestamp) {}
42 ~TimestampFinder() {}
43
44 template <typename T>
45 static void swap(T& a, T& b) {
46 T tmp = a;
47 a = b;
48 b = tmp;
49 }
50
51 /**
52 * Try to find the best candidate for a ZSL buffer.
53 * Match priority from best to worst:
54 * 1) Timestamps match.
55 * 2) Timestamp is closest to the needle (and lower).
56 * 3) Timestamp is closest to the needle (and higher).
57 *
58 */
59 virtual int compare(const BufferInfo *i1,
60 const BufferInfo *i2) const {
61 // Try to select non-null object first.
62 if (i1 == NULL) {
63 return SELECT_I2;
64 } else if (i2 == NULL) {
65 return SELECT_I1;
66 }
67
68 // Best result: timestamp is identical
69 if (i1->mTimestamp == mTimestamp) {
70 return SELECT_I1;
71 } else if (i2->mTimestamp == mTimestamp) {
72 return SELECT_I2;
73 }
74
75 const BufferInfo* infoPtrs[2] = {
76 i1,
77 i2
78 };
79 int infoSelectors[2] = {
80 SELECT_I1,
81 SELECT_I2
82 };
83
84 // Order i1,i2 so that always i1.timestamp < i2.timestamp
85 if (i1->mTimestamp > i2->mTimestamp) {
86 swap(infoPtrs[0], infoPtrs[1]);
87 swap(infoSelectors[0], infoSelectors[1]);
88 }
89
90 // Second best: closest (lower) timestamp
91 if (infoPtrs[1]->mTimestamp < mTimestamp) {
92 return infoSelectors[1];
93 } else if (infoPtrs[0]->mTimestamp < mTimestamp) {
94 return infoSelectors[0];
95 }
96
97 // Worst: closest (higher) timestamp
98 return infoSelectors[0];
99
100 /**
101 * The above cases should cover all the possibilities,
102 * and we get an 'empty' result only if the ring buffer
103 * was empty itself
104 */
105 }
106
107 const nsecs_t mTimestamp;
108}; // struct TimestampFinder
109} // namespace anonymous
110
Eino-Ville Talvala8be20f52013-03-06 16:20:06 -0800111Camera3ZslStream::Camera3ZslStream(int id, uint32_t width, uint32_t height,
Igor Murashkin054aab32013-11-18 11:39:27 -0800112 int bufferCount) :
Igor Murashkinae3d0ba2013-05-08 18:03:15 -0700113 Camera3OutputStream(id, CAMERA3_STREAM_BIDIRECTIONAL,
114 width, height,
115 HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED),
Igor Murashkin054aab32013-11-18 11:39:27 -0800116 mDepth(bufferCount) {
Igor Murashkinae3d0ba2013-05-08 18:03:15 -0700117
Dan Stoza8aa0f062014-03-12 14:31:05 -0700118 sp<IGraphicBufferProducer> producer;
119 sp<IGraphicBufferConsumer> consumer;
120 BufferQueue::createBufferQueue(&producer, &consumer);
121 mProducer = new RingBufferConsumer(consumer, GRALLOC_USAGE_HW_CAMERA_ZSL, bufferCount);
122 mConsumer = new Surface(producer);
Igor Murashkinae500e52013-04-22 14:03:54 -0700123}
124
125Camera3ZslStream::~Camera3ZslStream() {
Eino-Ville Talvala8be20f52013-03-06 16:20:06 -0800126}
127
Igor Murashkinae500e52013-04-22 14:03:54 -0700128status_t Camera3ZslStream::getInputBufferLocked(camera3_stream_buffer *buffer) {
129 ATRACE_CALL();
130
Igor Murashkinae3d0ba2013-05-08 18:03:15 -0700131 status_t res;
132
Igor Murashkinae500e52013-04-22 14:03:54 -0700133 // TODO: potentially register from inputBufferLocked
134 // this should be ok, registerBuffersLocked only calls getBuffer for now
135 // register in output mode instead of input mode for ZSL streams.
136 if (mState == STATE_IN_CONFIG || mState == STATE_IN_RECONFIG) {
137 ALOGE("%s: Stream %d: Buffer registration for input streams"
138 " not implemented (state %d)",
139 __FUNCTION__, mId, mState);
140 return INVALID_OPERATION;
141 }
142
Igor Murashkinae3d0ba2013-05-08 18:03:15 -0700143 if ((res = getBufferPreconditionCheckLocked()) != OK) {
144 return res;
Igor Murashkinae500e52013-04-22 14:03:54 -0700145 }
146
147 ANativeWindowBuffer* anb;
148 int fenceFd;
149
150 assert(mProducer != 0);
151
152 sp<PinnedBufferItem> bufferItem;
153 {
154 List<sp<RingBufferConsumer::PinnedBufferItem> >::iterator it, end;
155 it = mInputBufferQueue.begin();
156 end = mInputBufferQueue.end();
157
158 // Need to call enqueueInputBufferByTimestamp as a prerequisite
159 if (it == end) {
160 ALOGE("%s: Stream %d: No input buffer was queued",
161 __FUNCTION__, mId);
162 return INVALID_OPERATION;
163 }
164 bufferItem = *it;
165 mInputBufferQueue.erase(it);
166 }
167
168 anb = bufferItem->getBufferItem().mGraphicBuffer->getNativeBuffer();
169 assert(anb != NULL);
170 fenceFd = bufferItem->getBufferItem().mFence->dup();
171
172 /**
173 * FenceFD now owned by HAL except in case of error,
174 * in which case we reassign it to acquire_fence
175 */
Igor Murashkinae3d0ba2013-05-08 18:03:15 -0700176 handoutBufferLocked(*buffer, &(anb->handle), /*acquireFence*/fenceFd,
177 /*releaseFence*/-1, CAMERA3_BUFFER_STATUS_OK);
Igor Murashkinae500e52013-04-22 14:03:54 -0700178
179 mBuffersInFlight.push_back(bufferItem);
180
181 return OK;
Eino-Ville Talvala8be20f52013-03-06 16:20:06 -0800182}
183
Igor Murashkinae3d0ba2013-05-08 18:03:15 -0700184status_t Camera3ZslStream::returnBufferCheckedLocked(
185 const camera3_stream_buffer &buffer,
186 nsecs_t timestamp,
187 bool output,
188 /*out*/
189 sp<Fence> *releaseFenceOut) {
Igor Murashkinae500e52013-04-22 14:03:54 -0700190
Igor Murashkinae3d0ba2013-05-08 18:03:15 -0700191 if (output) {
192 // Output stream path
193 return Camera3OutputStream::returnBufferCheckedLocked(buffer,
194 timestamp,
195 output,
196 releaseFenceOut);
Igor Murashkinae500e52013-04-22 14:03:54 -0700197 }
198
Igor Murashkinae3d0ba2013-05-08 18:03:15 -0700199 /**
200 * Input stream path
201 */
Igor Murashkinae500e52013-04-22 14:03:54 -0700202 bool bufferFound = false;
203 sp<PinnedBufferItem> bufferItem;
204 {
205 // Find the buffer we are returning
206 Vector<sp<PinnedBufferItem> >::iterator it, end;
207 for (it = mBuffersInFlight.begin(), end = mBuffersInFlight.end();
208 it != end;
209 ++it) {
210
211 const sp<PinnedBufferItem>& tmp = *it;
212 ANativeWindowBuffer *anb =
213 tmp->getBufferItem().mGraphicBuffer->getNativeBuffer();
214 if (anb != NULL && &(anb->handle) == buffer.buffer) {
215 bufferFound = true;
216 bufferItem = tmp;
217 mBuffersInFlight.erase(it);
Igor Murashkinae3d0ba2013-05-08 18:03:15 -0700218 break;
Igor Murashkinae500e52013-04-22 14:03:54 -0700219 }
220 }
221 }
222 if (!bufferFound) {
223 ALOGE("%s: Stream %d: Can't return buffer that wasn't sent to HAL",
224 __FUNCTION__, mId);
225 return INVALID_OPERATION;
226 }
227
228 int releaseFenceFd = buffer.release_fence;
229
230 if (buffer.status == CAMERA3_BUFFER_STATUS_ERROR) {
231 if (buffer.release_fence != -1) {
232 ALOGE("%s: Stream %d: HAL should not set release_fence(%d) when "
233 "there is an error", __FUNCTION__, mId, buffer.release_fence);
234 close(buffer.release_fence);
235 }
236
237 /**
238 * Reassign release fence as the acquire fence incase of error
239 */
240 releaseFenceFd = buffer.acquire_fence;
241 }
242
243 /**
244 * Unconditionally return buffer to the buffer queue.
245 * - Fwk takes over the release_fence ownership
246 */
247 sp<Fence> releaseFence = new Fence(releaseFenceFd);
248 bufferItem->getBufferItem().mFence = releaseFence;
249 bufferItem.clear(); // dropping last reference unpins buffer
250
Igor Murashkinae3d0ba2013-05-08 18:03:15 -0700251 *releaseFenceOut = releaseFence;
Igor Murashkinae500e52013-04-22 14:03:54 -0700252
253 return OK;
Igor Murashkinae3d0ba2013-05-08 18:03:15 -0700254}
Igor Murashkinae500e52013-04-22 14:03:54 -0700255
Igor Murashkinae3d0ba2013-05-08 18:03:15 -0700256status_t Camera3ZslStream::returnInputBufferLocked(
257 const camera3_stream_buffer &buffer) {
258 ATRACE_CALL();
259
260 status_t res = returnAnyBufferLocked(buffer, /*timestamp*/0,
261 /*output*/false);
262
263 return res;
Eino-Ville Talvala8be20f52013-03-06 16:20:06 -0800264}
265
266void Camera3ZslStream::dump(int fd, const Vector<String16> &args) const {
Eino-Ville Talvala8be20f52013-03-06 16:20:06 -0800267 (void) args;
Igor Murashkinae500e52013-04-22 14:03:54 -0700268
269 String8 lines;
270 lines.appendFormat(" Stream[%d]: ZSL\n", mId);
Igor Murashkinae3d0ba2013-05-08 18:03:15 -0700271 write(fd, lines.string(), lines.size());
272
273 Camera3IOStreamBase::dump(fd, args);
274
275 lines = String8();
Kévin PETIT377b2ec2014-02-03 12:35:36 +0000276 lines.appendFormat(" Input buffers pending: %zu, in flight %zu\n",
Igor Murashkinae500e52013-04-22 14:03:54 -0700277 mInputBufferQueue.size(), mBuffersInFlight.size());
278 write(fd, lines.string(), lines.size());
279}
280
281status_t Camera3ZslStream::enqueueInputBufferByTimestamp(
282 nsecs_t timestamp,
283 nsecs_t* actualTimestamp) {
284
285 Mutex::Autolock l(mLock);
286
287 TimestampFinder timestampFinder = TimestampFinder(timestamp);
288
289 sp<RingBufferConsumer::PinnedBufferItem> pinnedBuffer =
290 mProducer->pinSelectedBuffer(timestampFinder,
291 /*waitForFence*/false);
292
293 if (pinnedBuffer == 0) {
294 ALOGE("%s: No ZSL buffers were available yet", __FUNCTION__);
295 return NO_BUFFER_AVAILABLE;
296 }
297
298 nsecs_t actual = pinnedBuffer->getBufferItem().mTimestamp;
299
300 if (actual != timestamp) {
301 ALOGW("%s: ZSL buffer candidate search didn't find an exact match --"
302 " requested timestamp = %lld, actual timestamp = %lld",
303 __FUNCTION__, timestamp, actual);
304 }
305
306 mInputBufferQueue.push_back(pinnedBuffer);
307
308 if (actualTimestamp != NULL) {
309 *actualTimestamp = actual;
310 }
311
312 return OK;
313}
314
315status_t Camera3ZslStream::clearInputRingBuffer() {
316 Mutex::Autolock l(mLock);
317
318 mInputBufferQueue.clear();
319
320 return mProducer->clear();
321}
322
323status_t Camera3ZslStream::setTransform(int /*transform*/) {
324 ALOGV("%s: Not implemented", __FUNCTION__);
325 return INVALID_OPERATION;
Eino-Ville Talvala8be20f52013-03-06 16:20:06 -0800326}
327
328}; // namespace camera3
329
330}; // namespace android