Jesse Hall | 80e0a39 | 2013-03-15 12:32:10 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 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 | #undef LOG_TAG |
| 18 | #define LOG_TAG "BQInterposer" |
Jesse Hall | 7414965 | 2013-03-19 17:18:09 -0700 | [diff] [blame] | 19 | //#define LOG_NDEBUG 0 |
Jesse Hall | 80e0a39 | 2013-03-15 12:32:10 -0700 | [diff] [blame] | 20 | |
| 21 | #include "BufferQueueInterposer.h" |
| 22 | |
| 23 | // --------------------------------------------------------------------------- |
| 24 | namespace android { |
| 25 | // --------------------------------------------------------------------------- |
| 26 | |
| 27 | #define BQI_LOGV(x, ...) ALOGV("[%s] "x, mName.string(), ##__VA_ARGS__) |
| 28 | #define BQI_LOGD(x, ...) ALOGD("[%s] "x, mName.string(), ##__VA_ARGS__) |
| 29 | #define BQI_LOGI(x, ...) ALOGI("[%s] "x, mName.string(), ##__VA_ARGS__) |
| 30 | #define BQI_LOGW(x, ...) ALOGW("[%s] "x, mName.string(), ##__VA_ARGS__) |
| 31 | #define BQI_LOGE(x, ...) ALOGE("[%s] "x, mName.string(), ##__VA_ARGS__) |
| 32 | |
| 33 | // Get an ID that's unique within this process. |
| 34 | static int32_t createProcessUniqueId() { |
| 35 | static volatile int32_t globalCounter = 0; |
| 36 | return android_atomic_inc(&globalCounter); |
| 37 | } |
| 38 | |
| 39 | BufferQueueInterposer::BufferQueueInterposer( |
| 40 | const sp<IGraphicBufferProducer>& sink, const String8& name) |
| 41 | : mSink(sink), |
| 42 | mName(name), |
| 43 | mAcquired(false) |
| 44 | { |
| 45 | BQI_LOGV("BufferQueueInterposer sink=%p", sink.get()); |
Jesse Hall | 80e0a39 | 2013-03-15 12:32:10 -0700 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | BufferQueueInterposer::~BufferQueueInterposer() { |
| 49 | Mutex::Autolock lock(mMutex); |
| 50 | flushQueuedBuffersLocked(); |
| 51 | BQI_LOGV("~BufferQueueInterposer"); |
| 52 | } |
| 53 | |
| 54 | status_t BufferQueueInterposer::requestBuffer(int slot, |
| 55 | sp<GraphicBuffer>* outBuf) { |
| 56 | BQI_LOGV("requestBuffer slot=%d", slot); |
| 57 | Mutex::Autolock lock(mMutex); |
| 58 | |
| 59 | if (size_t(slot) >= mBuffers.size()) { |
| 60 | size_t size = mBuffers.size(); |
| 61 | mBuffers.insertAt(size, size - slot + 1); |
| 62 | } |
| 63 | sp<GraphicBuffer>& buf = mBuffers.editItemAt(slot); |
| 64 | |
| 65 | status_t result = mSink->requestBuffer(slot, &buf); |
| 66 | *outBuf = buf; |
| 67 | return result; |
| 68 | } |
| 69 | |
| 70 | status_t BufferQueueInterposer::setBufferCount(int bufferCount) { |
| 71 | BQI_LOGV("setBufferCount count=%d", bufferCount); |
| 72 | Mutex::Autolock lock(mMutex); |
| 73 | |
| 74 | bufferCount += 1; |
| 75 | |
| 76 | status_t result = flushQueuedBuffersLocked(); |
| 77 | if (result != NO_ERROR) |
| 78 | return result; |
| 79 | |
| 80 | result = mSink->setBufferCount(bufferCount); |
| 81 | if (result != NO_ERROR) |
| 82 | return result; |
| 83 | |
| 84 | for (size_t i = 0; i < mBuffers.size(); i++) |
| 85 | mBuffers.editItemAt(i).clear(); |
| 86 | ssize_t n = mBuffers.resize(bufferCount); |
| 87 | result = (n < 0) ? n : result; |
| 88 | |
| 89 | return result; |
| 90 | } |
| 91 | |
| 92 | status_t BufferQueueInterposer::dequeueBuffer(int* slot, sp<Fence>* fence, |
| 93 | uint32_t w, uint32_t h, uint32_t format, uint32_t usage) { |
| 94 | BQI_LOGV("dequeueBuffer %ux%u fmt=%u usage=%#x", w, h, format, usage); |
| 95 | return mSink->dequeueBuffer(slot, fence, w, h, format, usage); |
| 96 | } |
| 97 | |
| 98 | status_t BufferQueueInterposer::queueBuffer(int slot, |
| 99 | const QueueBufferInput& input, QueueBufferOutput* output) { |
| 100 | BQI_LOGV("queueBuffer slot=%d", slot); |
| 101 | Mutex::Autolock lock(mMutex); |
| 102 | mQueue.push(QueuedBuffer(slot, input)); |
| 103 | *output = mQueueBufferOutput; |
| 104 | return NO_ERROR; |
| 105 | } |
| 106 | |
| 107 | void BufferQueueInterposer::cancelBuffer(int slot, const sp<Fence>& fence) { |
| 108 | BQI_LOGV("cancelBuffer slot=%d", slot); |
| 109 | mSink->cancelBuffer(slot, fence); |
| 110 | } |
| 111 | |
| 112 | int BufferQueueInterposer::query(int what, int* value) { |
| 113 | BQI_LOGV("query what=%d", what); |
| 114 | return mSink->query(what, value); |
| 115 | } |
| 116 | |
| 117 | status_t BufferQueueInterposer::setSynchronousMode(bool enabled) { |
| 118 | BQI_LOGV("setSynchronousMode %s", enabled ? "true" : "false"); |
| 119 | return mSink->setSynchronousMode(enabled); |
| 120 | } |
| 121 | |
| 122 | status_t BufferQueueInterposer::connect(int api, QueueBufferOutput* output) { |
| 123 | BQI_LOGV("connect api=%d", api); |
| 124 | Mutex::Autolock lock(mMutex); |
| 125 | status_t result = mSink->connect(api, &mQueueBufferOutput); |
| 126 | if (result == NO_ERROR) { |
| 127 | *output = mQueueBufferOutput; |
| 128 | } |
| 129 | return result; |
| 130 | } |
| 131 | |
| 132 | status_t BufferQueueInterposer::disconnect(int api) { |
| 133 | BQI_LOGV("disconnect: api=%d", api); |
| 134 | Mutex::Autolock lock(mMutex); |
| 135 | flushQueuedBuffersLocked(); |
| 136 | return mSink->disconnect(api); |
| 137 | } |
| 138 | |
| 139 | status_t BufferQueueInterposer::pullEmptyBuffer() { |
| 140 | status_t result; |
| 141 | |
| 142 | int slot; |
| 143 | sp<Fence> fence; |
| 144 | result = dequeueBuffer(&slot, &fence, 0, 0, 0, 0); |
| 145 | if (result == IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION) { |
| 146 | sp<GraphicBuffer> buffer; |
| 147 | result = requestBuffer(slot, &buffer); |
| 148 | } else if (result != NO_ERROR) { |
| 149 | return result; |
| 150 | } |
| 151 | |
| 152 | uint32_t w, h, transformHint, numPendingBuffers; |
| 153 | mQueueBufferOutput.deflate(&w, &h, &transformHint, &numPendingBuffers); |
| 154 | |
| 155 | IGraphicBufferProducer::QueueBufferInput qbi(0, Rect(w, h), |
| 156 | NATIVE_WINDOW_SCALING_MODE_FREEZE, 0, fence); |
| 157 | IGraphicBufferProducer::QueueBufferOutput qbo; |
| 158 | result = queueBuffer(slot, qbi, &qbo); |
| 159 | if (result != NO_ERROR) |
| 160 | return result; |
| 161 | |
| 162 | return NO_ERROR; |
| 163 | } |
| 164 | |
| 165 | status_t BufferQueueInterposer::acquireBuffer(sp<GraphicBuffer>* buf, |
| 166 | sp<Fence>* fence) { |
| 167 | Mutex::Autolock lock(mMutex); |
| 168 | if (mQueue.empty()) { |
| 169 | BQI_LOGV("acquireBuffer: no buffers available"); |
| 170 | return NO_BUFFER_AVAILABLE; |
| 171 | } |
| 172 | if (mAcquired) { |
| 173 | BQI_LOGE("acquireBuffer: buffer already acquired"); |
| 174 | return BUFFER_ALREADY_ACQUIRED; |
| 175 | } |
| 176 | BQI_LOGV("acquireBuffer: acquiring slot %d", mQueue[0].slot); |
| 177 | |
| 178 | *buf = mBuffers[mQueue[0].slot]; |
| 179 | *fence = mQueue[0].fence; |
| 180 | mAcquired = true; |
| 181 | return NO_ERROR; |
| 182 | } |
| 183 | |
| 184 | status_t BufferQueueInterposer::releaseBuffer(const sp<Fence>& fence) { |
| 185 | Mutex::Autolock lock(mMutex); |
| 186 | if (!mAcquired) { |
| 187 | BQI_LOGE("releaseBuffer: releasing a non-acquired buffer"); |
| 188 | return BUFFER_NOT_ACQUIRED; |
| 189 | } |
| 190 | BQI_LOGV("releaseBuffer: releasing slot %d to sink", mQueue[0].slot); |
| 191 | |
| 192 | const QueuedBuffer& b = mQueue[0]; |
| 193 | status_t result = mSink->queueBuffer(b.slot, |
| 194 | QueueBufferInput(b.timestamp, b.crop, b.scalingMode, |
| 195 | b.transform, b.fence), |
| 196 | &mQueueBufferOutput); |
| 197 | mQueue.removeAt(0); |
| 198 | mAcquired = false; |
| 199 | |
| 200 | return result; |
| 201 | } |
| 202 | |
| 203 | status_t BufferQueueInterposer::flushQueuedBuffersLocked() { |
| 204 | if (mAcquired) { |
| 205 | BQI_LOGE("flushQueuedBuffersLocked: buffer acquired, can't flush"); |
| 206 | return INVALID_OPERATION; |
| 207 | } |
| 208 | |
| 209 | status_t result = NO_ERROR; |
| 210 | for (size_t i = 0; i < mQueue.size(); i++) { |
| 211 | const QueuedBuffer& b = mQueue[i]; |
| 212 | BQI_LOGV("flushing queued slot %d to sink", b.slot); |
| 213 | status_t err = mSink->queueBuffer(b.slot, |
| 214 | QueueBufferInput(b.timestamp, b.crop, b.scalingMode, |
| 215 | b.transform, b.fence), |
| 216 | &mQueueBufferOutput); |
| 217 | if (err != NO_ERROR && result == NO_ERROR) // latch first error |
| 218 | result = err; |
| 219 | } |
| 220 | mQueue.clear(); |
| 221 | return result; |
| 222 | } |
| 223 | |
| 224 | // --------------------------------------------------------------------------- |
| 225 | } // namespace android |
| 226 | // --------------------------------------------------------------------------- |