• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2013-2018 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-InputStream"
18 #define ATRACE_TAG ATRACE_TAG_CAMERA
19 //#define LOG_NDEBUG 0
20 
21 #include <gui/BufferItem.h>
22 #include <utils/Log.h>
23 #include <utils/Trace.h>
24 #include "Camera3InputStream.h"
25 
26 namespace android {
27 
28 namespace camera3 {
29 
30 const String8 Camera3InputStream::FAKE_ID;
31 
Camera3InputStream(int id,uint32_t width,uint32_t height,int format)32 Camera3InputStream::Camera3InputStream(int id,
33         uint32_t width, uint32_t height, int format) :
34         Camera3IOStreamBase(id, CAMERA_STREAM_INPUT, width, height, /*maxSize*/0,
35                             format, HAL_DATASPACE_UNKNOWN, CAMERA_STREAM_ROTATION_0,
36                             FAKE_ID,
37                             std::unordered_set<int32_t>{ANDROID_SENSOR_PIXEL_MODE_DEFAULT}) {
38 
39     if (format == HAL_PIXEL_FORMAT_BLOB) {
40         ALOGE("%s: Bad format, BLOB not supported", __FUNCTION__);
41         mState = STATE_ERROR;
42     }
43 }
44 
~Camera3InputStream()45 Camera3InputStream::~Camera3InputStream() {
46     disconnectLocked();
47 }
48 
getInputBufferLocked(camera_stream_buffer * buffer,Size * size)49 status_t Camera3InputStream::getInputBufferLocked(
50         camera_stream_buffer *buffer, Size *size) {
51     ATRACE_CALL();
52     status_t res;
53 
54     if (size == nullptr) {
55         ALOGE("%s: size must not be null", __FUNCTION__);
56         return BAD_VALUE;
57     }
58     // FIXME: will not work in (re-)registration
59     if (mState == STATE_IN_CONFIG || mState == STATE_IN_RECONFIG) {
60         ALOGE("%s: Stream %d: Buffer registration for input streams"
61               " not implemented (state %d)",
62               __FUNCTION__, mId, mState);
63         return INVALID_OPERATION;
64     }
65 
66     if ((res = getBufferPreconditionCheckLocked()) != OK) {
67         return res;
68     }
69 
70     ANativeWindowBuffer* anb;
71     int fenceFd;
72 
73     assert(mConsumer != 0);
74 
75     BufferItem bufferItem;
76 
77     res = mConsumer->acquireBuffer(&bufferItem, /*waitForFence*/false);
78     if (res != OK) {
79         // This may or may not be an error condition depending on caller.
80         ALOGV("%s: Stream %d: Can't acquire next output buffer: %s (%d)",
81                 __FUNCTION__, mId, strerror(-res), res);
82         return res;
83     }
84 
85     size->width  = bufferItem.mGraphicBuffer->getWidth();
86     size->height = bufferItem.mGraphicBuffer->getHeight();
87 
88     anb = bufferItem.mGraphicBuffer->getNativeBuffer();
89     assert(anb != NULL);
90     fenceFd = bufferItem.mFence->dup();
91     /**
92      * FenceFD now owned by HAL except in case of error,
93      * in which case we reassign it to acquire_fence
94      */
95     handoutBufferLocked(*buffer, &(anb->handle), /*acquireFence*/fenceFd,
96                         /*releaseFence*/-1, CAMERA_BUFFER_STATUS_OK, /*output*/false);
97     mBuffersInFlight.push_back(bufferItem);
98 
99     mFrameCount++;
100     mLastTimestamp = bufferItem.mTimestamp;
101 
102     return OK;
103 }
104 
returnBufferCheckedLocked(const camera_stream_buffer & buffer,nsecs_t timestamp,bool output,const std::vector<size_t> &,sp<Fence> * releaseFenceOut)105 status_t Camera3InputStream::returnBufferCheckedLocked(
106             const camera_stream_buffer &buffer,
107             nsecs_t timestamp,
108             bool output,
109             const std::vector<size_t>&,
110             /*out*/
111             sp<Fence> *releaseFenceOut) {
112 
113     (void)timestamp;
114     (void)output;
115     ALOG_ASSERT(!output, "Expected output to be false");
116 
117     status_t res;
118 
119     bool bufferFound = false;
120     BufferItem bufferItem;
121     {
122         // Find the buffer we are returning
123         Vector<BufferItem>::iterator it, end;
124         for (it = mBuffersInFlight.begin(), end = mBuffersInFlight.end();
125              it != end;
126              ++it) {
127 
128             const BufferItem& tmp = *it;
129             ANativeWindowBuffer *anb = tmp.mGraphicBuffer->getNativeBuffer();
130             if (anb != NULL && &(anb->handle) == buffer.buffer) {
131                 bufferFound = true;
132                 bufferItem = tmp;
133                 mBuffersInFlight.erase(it);
134                 break;
135             }
136         }
137     }
138     if (!bufferFound) {
139         ALOGE("%s: Stream %d: Can't return buffer that wasn't sent to HAL",
140               __FUNCTION__, mId);
141         return INVALID_OPERATION;
142     }
143 
144     if (buffer.status == CAMERA_BUFFER_STATUS_ERROR) {
145         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);
149         }
150 
151         /**
152          * Reassign release fence as the acquire fence incase of error
153          */
154         const_cast<camera_stream_buffer*>(&buffer)->release_fence =
155                 buffer.acquire_fence;
156     }
157 
158     /**
159      * Unconditionally return buffer to the buffer queue.
160      * - Fwk takes over the release_fence ownership
161      */
162     sp<Fence> releaseFence = new Fence(buffer.release_fence);
163     res = mConsumer->releaseBuffer(bufferItem, releaseFence);
164     if (res != OK) {
165         ALOGE("%s: Stream %d: Error releasing buffer back to buffer queue:"
166                 " %s (%d)", __FUNCTION__, mId, strerror(-res), res);
167     }
168 
169     *releaseFenceOut = releaseFence;
170 
171     return res;
172 }
173 
returnInputBufferLocked(const camera_stream_buffer & buffer)174 status_t Camera3InputStream::returnInputBufferLocked(
175         const camera_stream_buffer &buffer) {
176     ATRACE_CALL();
177 
178     return returnAnyBufferLocked(buffer, /*timestamp*/0, /*output*/false);
179 }
180 
getInputBufferProducerLocked(sp<IGraphicBufferProducer> * producer)181 status_t Camera3InputStream::getInputBufferProducerLocked(
182             sp<IGraphicBufferProducer> *producer) {
183     ATRACE_CALL();
184 
185     if (producer == NULL) {
186         return BAD_VALUE;
187     } else if (mProducer == NULL) {
188         ALOGE("%s: No input stream is configured", __FUNCTION__);
189         return INVALID_OPERATION;
190     }
191 
192     *producer = mProducer;
193     return OK;
194 }
195 
disconnectLocked()196 status_t Camera3InputStream::disconnectLocked() {
197 
198     status_t res;
199 
200     if ((res = Camera3IOStreamBase::disconnectLocked()) != OK) {
201         return res;
202     }
203 
204     assert(mBuffersInFlight.size() == 0);
205 
206     mConsumer->abandon();
207 
208     /**
209      *  no-op since we can't disconnect the producer from the consumer-side
210      */
211 
212     mState = (mState == STATE_IN_RECONFIG) ? STATE_IN_CONFIG
213                                            : STATE_CONSTRUCTED;
214     return OK;
215 }
216 
dump(int fd,const Vector<String16> & args) const217 void Camera3InputStream::dump(int fd, const Vector<String16> &args) const {
218     (void) args;
219     String8 lines;
220     lines.appendFormat("    Stream[%d]: Input\n", mId);
221     write(fd, lines.string(), lines.size());
222 
223     Camera3IOStreamBase::dump(fd, args);
224 }
225 
configureQueueLocked()226 status_t Camera3InputStream::configureQueueLocked() {
227     status_t res;
228 
229     if ((res = Camera3IOStreamBase::configureQueueLocked()) != OK) {
230         return res;
231     }
232 
233     assert(mMaxSize == 0);
234     assert(camera_stream::format != HAL_PIXEL_FORMAT_BLOB);
235 
236     mHandoutTotalBufferCount = 0;
237     mFrameCount = 0;
238     mLastTimestamp = 0;
239 
240     if (mConsumer.get() == 0) {
241         sp<IGraphicBufferProducer> producer;
242         sp<IGraphicBufferConsumer> consumer;
243         BufferQueue::createBufferQueue(&producer, &consumer);
244 
245         int minUndequeuedBuffers = 0;
246         res = producer->query(NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS, &minUndequeuedBuffers);
247         if (res != OK || minUndequeuedBuffers < 0) {
248             ALOGE("%s: Stream %d: Could not query min undequeued buffers (error %d, bufCount %d)",
249                     __FUNCTION__, mId, res, minUndequeuedBuffers);
250             return res;
251         }
252         size_t minBufs = static_cast<size_t>(minUndequeuedBuffers);
253 
254         if (camera_stream::max_buffers == 0) {
255             ALOGE("%s: %d: HAL sets max_buffer to 0. Must be at least 1.",
256                     __FUNCTION__, __LINE__);
257             return INVALID_OPERATION;
258         }
259 
260         /*
261          * We promise never to 'acquire' more than camera_stream::max_buffers
262          * at any one time.
263          *
264          * Boost the number up to meet the minimum required buffer count.
265          *
266          * (Note that this sets consumer-side buffer count only,
267          * and not the sum of producer+consumer side as in other camera streams).
268          */
269         mTotalBufferCount = camera_stream::max_buffers > minBufs ?
270             camera_stream::max_buffers : minBufs;
271         // TODO: somehow set the total buffer count when producer connects?
272 
273         mConsumer = new BufferItemConsumer(consumer, mUsage,
274                                            mTotalBufferCount);
275         mConsumer->setName(String8::format("Camera3-InputStream-%d", mId));
276 
277         mProducer = producer;
278 
279         mConsumer->setBufferFreedListener(this);
280     }
281 
282     res = mConsumer->setDefaultBufferSize(camera_stream::width,
283                                           camera_stream::height);
284     if (res != OK) {
285         ALOGE("%s: Stream %d: Could not set buffer dimensions %dx%d",
286               __FUNCTION__, mId, camera_stream::width, camera_stream::height);
287         return res;
288     }
289     res = mConsumer->setDefaultBufferFormat(camera_stream::format);
290     if (res != OK) {
291         ALOGE("%s: Stream %d: Could not set buffer format %d",
292               __FUNCTION__, mId, camera_stream::format);
293         return res;
294     }
295 
296     return OK;
297 }
298 
getEndpointUsage(uint64_t * usage) const299 status_t Camera3InputStream::getEndpointUsage(uint64_t *usage) const {
300     // Per HAL3 spec, input streams have 0 for their initial usage field.
301     *usage = 0;
302     return OK;
303 }
304 
onBufferFreed(const wp<GraphicBuffer> & gb)305 void Camera3InputStream::onBufferFreed(const wp<GraphicBuffer>& gb) {
306     const sp<GraphicBuffer> buffer = gb.promote();
307     if (buffer != nullptr) {
308         camera_stream_buffer streamBuffer =
309                 {nullptr, &buffer->handle, CAMERA_BUFFER_STATUS_OK, -1, -1};
310         // Check if this buffer is outstanding.
311         if (isOutstandingBuffer(streamBuffer)) {
312             ALOGV("%s: Stream %d: Trying to free a buffer that is still being "
313                     "processed.", __FUNCTION__, mId);
314             return;
315         }
316 
317         sp<Camera3StreamBufferFreedListener> callback = mBufferFreedListener.promote();
318         if (callback != nullptr) {
319             callback->onBufferFreed(mId, buffer->handle);
320         }
321     } else {
322         ALOGE("%s: GraphicBuffer is freed before onBufferFreed callback finishes!", __FUNCTION__);
323     }
324 }
325 
326 }; // namespace camera3
327 
328 }; // namespace android
329