Eino-Ville Talvala | 8be20f5 | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 1 | /* |
| 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-InputStream" |
| 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 "Camera3InputStream.h" |
| 24 | |
| 25 | namespace android { |
| 26 | |
| 27 | namespace camera3 { |
| 28 | |
| 29 | Camera3InputStream::Camera3InputStream(int id, |
| 30 | uint32_t width, uint32_t height, int format) : |
Igor Murashkin | ae3d0ba | 2013-05-08 18:03:15 -0700 | [diff] [blame] | 31 | Camera3IOStreamBase(id, CAMERA3_STREAM_INPUT, width, height, |
| 32 | /*maxSize*/0, format) { |
Igor Murashkin | 0776a14 | 2013-04-15 14:59:22 -0700 | [diff] [blame] | 33 | |
| 34 | if (format == HAL_PIXEL_FORMAT_BLOB) { |
| 35 | ALOGE("%s: Bad format, BLOB not supported", __FUNCTION__); |
| 36 | mState = STATE_ERROR; |
| 37 | } |
Eino-Ville Talvala | 8be20f5 | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 38 | } |
| 39 | |
Igor Murashkin | 0776a14 | 2013-04-15 14:59:22 -0700 | [diff] [blame] | 40 | Camera3InputStream::~Camera3InputStream() { |
| 41 | disconnectLocked(); |
Eino-Ville Talvala | 8be20f5 | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 42 | } |
| 43 | |
Igor Murashkin | 0776a14 | 2013-04-15 14:59:22 -0700 | [diff] [blame] | 44 | status_t Camera3InputStream::getInputBufferLocked( |
| 45 | camera3_stream_buffer *buffer) { |
| 46 | ATRACE_CALL(); |
| 47 | status_t res; |
| 48 | |
| 49 | // FIXME: will not work in (re-)registration |
| 50 | if (mState == STATE_IN_CONFIG || mState == STATE_IN_RECONFIG) { |
| 51 | ALOGE("%s: Stream %d: Buffer registration for input streams" |
| 52 | " not implemented (state %d)", |
| 53 | __FUNCTION__, mId, mState); |
| 54 | return INVALID_OPERATION; |
| 55 | } |
| 56 | |
Igor Murashkin | ae3d0ba | 2013-05-08 18:03:15 -0700 | [diff] [blame] | 57 | if ((res = getBufferPreconditionCheckLocked()) != OK) { |
| 58 | return res; |
Igor Murashkin | 0776a14 | 2013-04-15 14:59:22 -0700 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | ANativeWindowBuffer* anb; |
| 62 | int fenceFd; |
| 63 | |
| 64 | assert(mConsumer != 0); |
| 65 | |
| 66 | BufferItem bufferItem; |
| 67 | res = mConsumer->acquireBuffer(&bufferItem, /*waitForFence*/false); |
| 68 | |
| 69 | if (res != OK) { |
| 70 | ALOGE("%s: Stream %d: Can't acquire next output buffer: %s (%d)", |
| 71 | __FUNCTION__, mId, strerror(-res), res); |
| 72 | return res; |
| 73 | } |
| 74 | |
| 75 | anb = bufferItem.mGraphicBuffer->getNativeBuffer(); |
| 76 | assert(anb != NULL); |
| 77 | fenceFd = bufferItem.mFence->dup(); |
Igor Murashkin | ae3d0ba | 2013-05-08 18:03:15 -0700 | [diff] [blame] | 78 | |
Igor Murashkin | 0776a14 | 2013-04-15 14:59:22 -0700 | [diff] [blame] | 79 | /** |
| 80 | * FenceFD now owned by HAL except in case of error, |
| 81 | * in which case we reassign it to acquire_fence |
| 82 | */ |
Igor Murashkin | ae3d0ba | 2013-05-08 18:03:15 -0700 | [diff] [blame] | 83 | handoutBufferLocked(*buffer, &(anb->handle), /*acquireFence*/fenceFd, |
| 84 | /*releaseFence*/-1, CAMERA3_BUFFER_STATUS_OK); |
Igor Murashkin | 0776a14 | 2013-04-15 14:59:22 -0700 | [diff] [blame] | 85 | mBuffersInFlight.push_back(bufferItem); |
| 86 | |
| 87 | return OK; |
| 88 | } |
| 89 | |
Igor Murashkin | ae3d0ba | 2013-05-08 18:03:15 -0700 | [diff] [blame] | 90 | status_t Camera3InputStream::returnBufferCheckedLocked( |
| 91 | const camera3_stream_buffer &buffer, |
| 92 | nsecs_t timestamp, |
| 93 | bool output, |
| 94 | /*out*/ |
| 95 | sp<Fence> *releaseFenceOut) { |
| 96 | |
| 97 | (void)timestamp; |
| 98 | (void)output; |
| 99 | ALOG_ASSERT(!output, "Expected output to be false"); |
| 100 | |
Igor Murashkin | 0776a14 | 2013-04-15 14:59:22 -0700 | [diff] [blame] | 101 | status_t res; |
| 102 | |
Igor Murashkin | 0776a14 | 2013-04-15 14:59:22 -0700 | [diff] [blame] | 103 | bool bufferFound = false; |
| 104 | BufferItem bufferItem; |
| 105 | { |
| 106 | // Find the buffer we are returning |
| 107 | Vector<BufferItem>::iterator it, end; |
| 108 | for (it = mBuffersInFlight.begin(), end = mBuffersInFlight.end(); |
| 109 | it != end; |
| 110 | ++it) { |
| 111 | |
| 112 | const BufferItem& tmp = *it; |
| 113 | ANativeWindowBuffer *anb = tmp.mGraphicBuffer->getNativeBuffer(); |
| 114 | if (anb != NULL && &(anb->handle) == buffer.buffer) { |
| 115 | bufferFound = true; |
| 116 | bufferItem = tmp; |
| 117 | mBuffersInFlight.erase(it); |
| 118 | mDequeuedBufferCount--; |
| 119 | } |
| 120 | } |
| 121 | } |
| 122 | if (!bufferFound) { |
| 123 | ALOGE("%s: Stream %d: Can't return buffer that wasn't sent to HAL", |
| 124 | __FUNCTION__, mId); |
| 125 | return INVALID_OPERATION; |
| 126 | } |
| 127 | |
| 128 | if (buffer.status == CAMERA3_BUFFER_STATUS_ERROR) { |
| 129 | if (buffer.release_fence != -1) { |
| 130 | ALOGE("%s: Stream %d: HAL should not set release_fence(%d) when " |
| 131 | "there is an error", __FUNCTION__, mId, buffer.release_fence); |
| 132 | close(buffer.release_fence); |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * Reassign release fence as the acquire fence incase of error |
| 137 | */ |
| 138 | const_cast<camera3_stream_buffer*>(&buffer)->release_fence = |
| 139 | buffer.acquire_fence; |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * Unconditionally return buffer to the buffer queue. |
| 144 | * - Fwk takes over the release_fence ownership |
| 145 | */ |
| 146 | sp<Fence> releaseFence = new Fence(buffer.release_fence); |
| 147 | res = mConsumer->releaseBuffer(bufferItem, releaseFence); |
| 148 | if (res != OK) { |
| 149 | ALOGE("%s: Stream %d: Error releasing buffer back to buffer queue:" |
| 150 | " %s (%d)", __FUNCTION__, mId, strerror(-res), res); |
| 151 | return res; |
| 152 | } |
| 153 | |
Igor Murashkin | ae3d0ba | 2013-05-08 18:03:15 -0700 | [diff] [blame] | 154 | *releaseFenceOut = releaseFence; |
Igor Murashkin | 0776a14 | 2013-04-15 14:59:22 -0700 | [diff] [blame] | 155 | |
| 156 | return OK; |
Eino-Ville Talvala | 8be20f5 | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 157 | } |
| 158 | |
Igor Murashkin | ae3d0ba | 2013-05-08 18:03:15 -0700 | [diff] [blame] | 159 | status_t Camera3InputStream::returnInputBufferLocked( |
| 160 | const camera3_stream_buffer &buffer) { |
| 161 | ATRACE_CALL(); |
Eino-Ville Talvala | 8be20f5 | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 162 | |
Igor Murashkin | ae3d0ba | 2013-05-08 18:03:15 -0700 | [diff] [blame] | 163 | return returnAnyBufferLocked(buffer, /*timestamp*/0, /*output*/false); |
Eino-Ville Talvala | 8be20f5 | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | status_t Camera3InputStream::disconnectLocked() { |
Igor Murashkin | 0776a14 | 2013-04-15 14:59:22 -0700 | [diff] [blame] | 167 | |
Igor Murashkin | ae3d0ba | 2013-05-08 18:03:15 -0700 | [diff] [blame] | 168 | status_t res; |
| 169 | |
| 170 | if ((res = Camera3IOStreamBase::disconnectLocked()) != OK) { |
| 171 | return res; |
Igor Murashkin | 0776a14 | 2013-04-15 14:59:22 -0700 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | assert(mBuffersInFlight.size() == 0); |
| 175 | |
| 176 | /** |
| 177 | * no-op since we can't disconnect the producer from the consumer-side |
| 178 | */ |
| 179 | |
Igor Murashkin | ae3d0ba | 2013-05-08 18:03:15 -0700 | [diff] [blame] | 180 | mState = (mState == STATE_IN_RECONFIG) ? STATE_IN_CONFIG |
| 181 | : STATE_CONSTRUCTED; |
Igor Murashkin | 0776a14 | 2013-04-15 14:59:22 -0700 | [diff] [blame] | 182 | return OK; |
Eino-Ville Talvala | 8be20f5 | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | sp<IGraphicBufferProducer> Camera3InputStream::getProducerInterface() const { |
| 186 | return mConsumer->getProducerInterface(); |
| 187 | } |
| 188 | |
| 189 | void Camera3InputStream::dump(int fd, const Vector<String16> &args) const { |
Eino-Ville Talvala | 8be20f5 | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 190 | (void) args; |
Igor Murashkin | 0776a14 | 2013-04-15 14:59:22 -0700 | [diff] [blame] | 191 | String8 lines; |
| 192 | lines.appendFormat(" Stream[%d]: Input\n", mId); |
Igor Murashkin | 0776a14 | 2013-04-15 14:59:22 -0700 | [diff] [blame] | 193 | write(fd, lines.string(), lines.size()); |
Igor Murashkin | ae3d0ba | 2013-05-08 18:03:15 -0700 | [diff] [blame] | 194 | |
| 195 | Camera3IOStreamBase::dump(fd, args); |
Igor Murashkin | 0776a14 | 2013-04-15 14:59:22 -0700 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | status_t Camera3InputStream::configureQueueLocked() { |
| 199 | status_t res; |
| 200 | |
Igor Murashkin | ae3d0ba | 2013-05-08 18:03:15 -0700 | [diff] [blame] | 201 | if ((res = Camera3IOStreamBase::configureQueueLocked()) != OK) { |
| 202 | return res; |
Igor Murashkin | 0776a14 | 2013-04-15 14:59:22 -0700 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | assert(mMaxSize == 0); |
| 206 | assert(camera3_stream::format != HAL_PIXEL_FORMAT_BLOB); |
| 207 | |
| 208 | mTotalBufferCount = BufferQueue::MIN_UNDEQUEUED_BUFFERS + |
| 209 | camera3_stream::max_buffers; |
| 210 | mDequeuedBufferCount = 0; |
| 211 | mFrameCount = 0; |
| 212 | |
| 213 | if (mConsumer.get() == 0) { |
Mathias Agopian | 8d764bf | 2013-07-12 22:06:20 -0700 | [diff] [blame^] | 214 | sp<BufferQueue> bq = new BufferQueue(); |
| 215 | mConsumer = new BufferItemConsumer(bq, camera3_stream::usage, |
Igor Murashkin | 0776a14 | 2013-04-15 14:59:22 -0700 | [diff] [blame] | 216 | mTotalBufferCount, |
| 217 | /*synchronousMode*/true); |
| 218 | mConsumer->setName(String8::format("Camera3-InputStream-%d", mId)); |
| 219 | } |
| 220 | |
| 221 | res = mConsumer->setDefaultBufferSize(camera3_stream::width, |
| 222 | camera3_stream::height); |
| 223 | if (res != OK) { |
| 224 | ALOGE("%s: Stream %d: Could not set buffer dimensions %dx%d", |
| 225 | __FUNCTION__, mId, camera3_stream::width, camera3_stream::height); |
| 226 | return res; |
| 227 | } |
| 228 | res = mConsumer->setDefaultBufferFormat(camera3_stream::format); |
| 229 | if (res != OK) { |
| 230 | ALOGE("%s: Stream %d: Could not set buffer format %d", |
| 231 | __FUNCTION__, mId, camera3_stream::format); |
| 232 | return res; |
| 233 | } |
| 234 | |
| 235 | return OK; |
Eino-Ville Talvala | 8be20f5 | 2013-03-06 16:20:06 -0800 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | }; // namespace camera3 |
| 239 | |
| 240 | }; // namespace android |