blob: d753b7108081fe029ad023818a9fe5a3993cb286 [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
Colin Crosse5729fa2014-03-21 15:04:25 -070021#include <inttypes.h>
22
Eino-Ville Talvala8be20f52013-03-06 16:20:06 -080023#include <utils/Log.h>
24#include <utils/Trace.h>
25#include "Camera3ZslStream.h"
26
Igor Murashkinae500e52013-04-22 14:03:54 -070027typedef android::RingBufferConsumer::PinnedBufferItem PinnedBufferItem;
28
Eino-Ville Talvala8be20f52013-03-06 16:20:06 -080029namespace android {
30
31namespace camera3 {
32
Igor Murashkinae500e52013-04-22 14:03:54 -070033namespace {
34struct TimestampFinder : public RingBufferConsumer::RingBufferComparator {
35 typedef RingBufferConsumer::BufferInfo BufferInfo;
36
37 enum {
38 SELECT_I1 = -1,
39 SELECT_I2 = 1,
40 SELECT_NEITHER = 0,
41 };
42
Chih-Hung Hsieh090ef602016-04-27 10:39:54 -070043 explicit TimestampFinder(nsecs_t timestamp) : mTimestamp(timestamp) {}
Igor Murashkinae500e52013-04-22 14:03:54 -070044 ~TimestampFinder() {}
45
46 template <typename T>
47 static void swap(T& a, T& b) {
48 T tmp = a;
49 a = b;
50 b = tmp;
51 }
52
53 /**
54 * Try to find the best candidate for a ZSL buffer.
55 * Match priority from best to worst:
56 * 1) Timestamps match.
57 * 2) Timestamp is closest to the needle (and lower).
58 * 3) Timestamp is closest to the needle (and higher).
59 *
60 */
61 virtual int compare(const BufferInfo *i1,
62 const BufferInfo *i2) const {
63 // Try to select non-null object first.
64 if (i1 == NULL) {
65 return SELECT_I2;
66 } else if (i2 == NULL) {
67 return SELECT_I1;
68 }
69
70 // Best result: timestamp is identical
71 if (i1->mTimestamp == mTimestamp) {
72 return SELECT_I1;
73 } else if (i2->mTimestamp == mTimestamp) {
74 return SELECT_I2;
75 }
76
77 const BufferInfo* infoPtrs[2] = {
78 i1,
79 i2
80 };
81 int infoSelectors[2] = {
82 SELECT_I1,
83 SELECT_I2
84 };
85
86 // Order i1,i2 so that always i1.timestamp < i2.timestamp
87 if (i1->mTimestamp > i2->mTimestamp) {
88 swap(infoPtrs[0], infoPtrs[1]);
89 swap(infoSelectors[0], infoSelectors[1]);
90 }
91
92 // Second best: closest (lower) timestamp
93 if (infoPtrs[1]->mTimestamp < mTimestamp) {
94 return infoSelectors[1];
95 } else if (infoPtrs[0]->mTimestamp < mTimestamp) {
96 return infoSelectors[0];
97 }
98
99 // Worst: closest (higher) timestamp
100 return infoSelectors[0];
101
102 /**
103 * The above cases should cover all the possibilities,
104 * and we get an 'empty' result only if the ring buffer
105 * was empty itself
106 */
107 }
108
109 const nsecs_t mTimestamp;
110}; // struct TimestampFinder
111} // namespace anonymous
112
Eino-Ville Talvala8be20f52013-03-06 16:20:06 -0800113Camera3ZslStream::Camera3ZslStream(int id, uint32_t width, uint32_t height,
Igor Murashkin054aab32013-11-18 11:39:27 -0800114 int bufferCount) :
Igor Murashkinae3d0ba2013-05-08 18:03:15 -0700115 Camera3OutputStream(id, CAMERA3_STREAM_BIDIRECTIONAL,
116 width, height,
Eino-Ville Talvala3d82c0d2015-02-23 15:19:19 -0800117 HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED,
Yin-Chia Yehb97babb2015-03-12 13:42:44 -0700118 HAL_DATASPACE_UNKNOWN, CAMERA3_STREAM_ROTATION_0),
Igor Murashkin054aab32013-11-18 11:39:27 -0800119 mDepth(bufferCount) {
Igor Murashkinae3d0ba2013-05-08 18:03:15 -0700120
Dan Stoza8aa0f062014-03-12 14:31:05 -0700121 sp<IGraphicBufferProducer> producer;
122 sp<IGraphicBufferConsumer> consumer;
123 BufferQueue::createBufferQueue(&producer, &consumer);
124 mProducer = new RingBufferConsumer(consumer, GRALLOC_USAGE_HW_CAMERA_ZSL, bufferCount);
Eino-Ville Talvala727d1722015-06-09 13:44:19 -0700125 mProducer->setName(String8("Camera2-ZslRingBufferConsumer"));
Dan Stoza8aa0f062014-03-12 14:31:05 -0700126 mConsumer = new Surface(producer);
Igor Murashkinae500e52013-04-22 14:03:54 -0700127}
128
129Camera3ZslStream::~Camera3ZslStream() {
Eino-Ville Talvala8be20f52013-03-06 16:20:06 -0800130}
131
Igor Murashkinae500e52013-04-22 14:03:54 -0700132status_t Camera3ZslStream::getInputBufferLocked(camera3_stream_buffer *buffer) {
133 ATRACE_CALL();
134
Igor Murashkinae3d0ba2013-05-08 18:03:15 -0700135 status_t res;
136
Igor Murashkinae500e52013-04-22 14:03:54 -0700137 // TODO: potentially register from inputBufferLocked
138 // this should be ok, registerBuffersLocked only calls getBuffer for now
139 // register in output mode instead of input mode for ZSL streams.
140 if (mState == STATE_IN_CONFIG || mState == STATE_IN_RECONFIG) {
141 ALOGE("%s: Stream %d: Buffer registration for input streams"
142 " not implemented (state %d)",
143 __FUNCTION__, mId, mState);
144 return INVALID_OPERATION;
145 }
146
Igor Murashkinae3d0ba2013-05-08 18:03:15 -0700147 if ((res = getBufferPreconditionCheckLocked()) != OK) {
148 return res;
Igor Murashkinae500e52013-04-22 14:03:54 -0700149 }
150
151 ANativeWindowBuffer* anb;
152 int fenceFd;
153
154 assert(mProducer != 0);
155
156 sp<PinnedBufferItem> bufferItem;
157 {
158 List<sp<RingBufferConsumer::PinnedBufferItem> >::iterator it, end;
159 it = mInputBufferQueue.begin();
160 end = mInputBufferQueue.end();
161
162 // Need to call enqueueInputBufferByTimestamp as a prerequisite
163 if (it == end) {
164 ALOGE("%s: Stream %d: No input buffer was queued",
165 __FUNCTION__, mId);
166 return INVALID_OPERATION;
167 }
168 bufferItem = *it;
169 mInputBufferQueue.erase(it);
170 }
171
172 anb = bufferItem->getBufferItem().mGraphicBuffer->getNativeBuffer();
173 assert(anb != NULL);
174 fenceFd = bufferItem->getBufferItem().mFence->dup();
175
176 /**
177 * FenceFD now owned by HAL except in case of error,
178 * in which case we reassign it to acquire_fence
179 */
Igor Murashkinae3d0ba2013-05-08 18:03:15 -0700180 handoutBufferLocked(*buffer, &(anb->handle), /*acquireFence*/fenceFd,
Zhijun He6adc9cc2014-04-15 14:09:55 -0700181 /*releaseFence*/-1, CAMERA3_BUFFER_STATUS_OK, /*output*/false);
Igor Murashkinae500e52013-04-22 14:03:54 -0700182
183 mBuffersInFlight.push_back(bufferItem);
184
185 return OK;
Eino-Ville Talvala8be20f52013-03-06 16:20:06 -0800186}
187
Igor Murashkinae3d0ba2013-05-08 18:03:15 -0700188status_t Camera3ZslStream::returnBufferCheckedLocked(
189 const camera3_stream_buffer &buffer,
190 nsecs_t timestamp,
191 bool output,
192 /*out*/
193 sp<Fence> *releaseFenceOut) {
Igor Murashkinae500e52013-04-22 14:03:54 -0700194
Igor Murashkinae3d0ba2013-05-08 18:03:15 -0700195 if (output) {
196 // Output stream path
197 return Camera3OutputStream::returnBufferCheckedLocked(buffer,
198 timestamp,
199 output,
200 releaseFenceOut);
Igor Murashkinae500e52013-04-22 14:03:54 -0700201 }
202
Igor Murashkinae3d0ba2013-05-08 18:03:15 -0700203 /**
204 * Input stream path
205 */
Igor Murashkinae500e52013-04-22 14:03:54 -0700206 bool bufferFound = false;
207 sp<PinnedBufferItem> bufferItem;
208 {
209 // Find the buffer we are returning
210 Vector<sp<PinnedBufferItem> >::iterator it, end;
211 for (it = mBuffersInFlight.begin(), end = mBuffersInFlight.end();
212 it != end;
213 ++it) {
214
215 const sp<PinnedBufferItem>& tmp = *it;
216 ANativeWindowBuffer *anb =
217 tmp->getBufferItem().mGraphicBuffer->getNativeBuffer();
218 if (anb != NULL && &(anb->handle) == buffer.buffer) {
219 bufferFound = true;
220 bufferItem = tmp;
221 mBuffersInFlight.erase(it);
Igor Murashkinae3d0ba2013-05-08 18:03:15 -0700222 break;
Igor Murashkinae500e52013-04-22 14:03:54 -0700223 }
224 }
225 }
226 if (!bufferFound) {
227 ALOGE("%s: Stream %d: Can't return buffer that wasn't sent to HAL",
228 __FUNCTION__, mId);
229 return INVALID_OPERATION;
230 }
231
232 int releaseFenceFd = buffer.release_fence;
233
234 if (buffer.status == CAMERA3_BUFFER_STATUS_ERROR) {
235 if (buffer.release_fence != -1) {
236 ALOGE("%s: Stream %d: HAL should not set release_fence(%d) when "
237 "there is an error", __FUNCTION__, mId, buffer.release_fence);
238 close(buffer.release_fence);
239 }
240
241 /**
242 * Reassign release fence as the acquire fence incase of error
243 */
244 releaseFenceFd = buffer.acquire_fence;
245 }
246
247 /**
248 * Unconditionally return buffer to the buffer queue.
249 * - Fwk takes over the release_fence ownership
250 */
251 sp<Fence> releaseFence = new Fence(releaseFenceFd);
252 bufferItem->getBufferItem().mFence = releaseFence;
253 bufferItem.clear(); // dropping last reference unpins buffer
254
Igor Murashkinae3d0ba2013-05-08 18:03:15 -0700255 *releaseFenceOut = releaseFence;
Igor Murashkinae500e52013-04-22 14:03:54 -0700256
257 return OK;
Igor Murashkinae3d0ba2013-05-08 18:03:15 -0700258}
Igor Murashkinae500e52013-04-22 14:03:54 -0700259
Igor Murashkinae3d0ba2013-05-08 18:03:15 -0700260status_t Camera3ZslStream::returnInputBufferLocked(
261 const camera3_stream_buffer &buffer) {
262 ATRACE_CALL();
263
264 status_t res = returnAnyBufferLocked(buffer, /*timestamp*/0,
265 /*output*/false);
266
267 return res;
Eino-Ville Talvala8be20f52013-03-06 16:20:06 -0800268}
269
270void Camera3ZslStream::dump(int fd, const Vector<String16> &args) const {
Eino-Ville Talvala8be20f52013-03-06 16:20:06 -0800271 (void) args;
Igor Murashkinae500e52013-04-22 14:03:54 -0700272
273 String8 lines;
274 lines.appendFormat(" Stream[%d]: ZSL\n", mId);
Igor Murashkinae3d0ba2013-05-08 18:03:15 -0700275 write(fd, lines.string(), lines.size());
276
277 Camera3IOStreamBase::dump(fd, args);
278
279 lines = String8();
Kévin PETIT377b2ec2014-02-03 12:35:36 +0000280 lines.appendFormat(" Input buffers pending: %zu, in flight %zu\n",
Igor Murashkinae500e52013-04-22 14:03:54 -0700281 mInputBufferQueue.size(), mBuffersInFlight.size());
282 write(fd, lines.string(), lines.size());
283}
284
285status_t Camera3ZslStream::enqueueInputBufferByTimestamp(
286 nsecs_t timestamp,
287 nsecs_t* actualTimestamp) {
288
289 Mutex::Autolock l(mLock);
290
291 TimestampFinder timestampFinder = TimestampFinder(timestamp);
292
293 sp<RingBufferConsumer::PinnedBufferItem> pinnedBuffer =
294 mProducer->pinSelectedBuffer(timestampFinder,
295 /*waitForFence*/false);
296
297 if (pinnedBuffer == 0) {
298 ALOGE("%s: No ZSL buffers were available yet", __FUNCTION__);
299 return NO_BUFFER_AVAILABLE;
300 }
301
302 nsecs_t actual = pinnedBuffer->getBufferItem().mTimestamp;
303
304 if (actual != timestamp) {
Zhijun Hef0d962a2014-06-30 10:24:11 -0700305 // TODO: this is problematic, we'll end up with using wrong result for this pinned buffer.
Igor Murashkinae500e52013-04-22 14:03:54 -0700306 ALOGW("%s: ZSL buffer candidate search didn't find an exact match --"
Colin Crosse5729fa2014-03-21 15:04:25 -0700307 " requested timestamp = %" PRId64 ", actual timestamp = %" PRId64,
Igor Murashkinae500e52013-04-22 14:03:54 -0700308 __FUNCTION__, timestamp, actual);
309 }
310
311 mInputBufferQueue.push_back(pinnedBuffer);
312
313 if (actualTimestamp != NULL) {
314 *actualTimestamp = actual;
315 }
316
317 return OK;
318}
319
Yin-Chia Yeh6b7a2292014-09-09 13:31:46 -0700320status_t Camera3ZslStream::clearInputRingBuffer(nsecs_t* latestTimestamp) {
Igor Murashkinae500e52013-04-22 14:03:54 -0700321 Mutex::Autolock l(mLock);
322
Yin-Chia Yeh6b7a2292014-09-09 13:31:46 -0700323 return clearInputRingBufferLocked(latestTimestamp);
Zhijun He0a210512014-07-24 13:45:15 -0700324}
325
Yin-Chia Yeh6b7a2292014-09-09 13:31:46 -0700326status_t Camera3ZslStream::clearInputRingBufferLocked(nsecs_t* latestTimestamp) {
327
328 if (latestTimestamp) {
329 *latestTimestamp = mProducer->getLatestTimestamp();
330 }
Igor Murashkinae500e52013-04-22 14:03:54 -0700331 mInputBufferQueue.clear();
332
333 return mProducer->clear();
334}
335
Zhijun He0a210512014-07-24 13:45:15 -0700336status_t Camera3ZslStream::disconnectLocked() {
Yin-Chia Yeh6b7a2292014-09-09 13:31:46 -0700337 clearInputRingBufferLocked(NULL);
Zhijun He0a210512014-07-24 13:45:15 -0700338
339 return Camera3OutputStream::disconnectLocked();
340}
341
Igor Murashkinae500e52013-04-22 14:03:54 -0700342status_t Camera3ZslStream::setTransform(int /*transform*/) {
343 ALOGV("%s: Not implemented", __FUNCTION__);
344 return INVALID_OPERATION;
Eino-Ville Talvala8be20f52013-03-06 16:20:06 -0800345}
346
347}; // namespace camera3
348
349}; // namespace android