StreamSplitter.cpp revision 5b75a513e431c097ae704cba2f7affa6bfaecec9
1/* 2 * Copyright 2014 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#include <inttypes.h> 18 19#define LOG_TAG "StreamSplitter" 20#define ATRACE_TAG ATRACE_TAG_GRAPHICS 21//#define LOG_NDEBUG 0 22 23#include <gui/IGraphicBufferConsumer.h> 24#include <gui/IGraphicBufferProducer.h> 25#include <gui/StreamSplitter.h> 26 27#include <ui/GraphicBuffer.h> 28 29#include <binder/ProcessState.h> 30 31#include <utils/Trace.h> 32 33namespace android { 34 35status_t StreamSplitter::createSplitter( 36 const sp<IGraphicBufferConsumer>& inputQueue, 37 sp<StreamSplitter>* outSplitter) { 38 if (inputQueue == NULL) { 39 ALOGE("createSplitter: inputQueue must not be NULL"); 40 return BAD_VALUE; 41 } 42 if (outSplitter == NULL) { 43 ALOGE("createSplitter: outSplitter must not be NULL"); 44 return BAD_VALUE; 45 } 46 47 sp<StreamSplitter> splitter(new StreamSplitter(inputQueue)); 48 status_t status = splitter->mInput->consumerConnect(splitter, false); 49 if (status == NO_ERROR) { 50 splitter->mInput->setConsumerName(String8("StreamSplitter")); 51 *outSplitter = splitter; 52 } 53 return status; 54} 55 56StreamSplitter::StreamSplitter(const sp<IGraphicBufferConsumer>& inputQueue) 57 : mIsAbandoned(false), mMutex(), mReleaseCondition(), 58 mOutstandingBuffers(0), mInput(inputQueue), mOutputs(), mBuffers() {} 59 60StreamSplitter::~StreamSplitter() { 61 mInput->consumerDisconnect(); 62 Vector<sp<IGraphicBufferProducer> >::iterator output = mOutputs.begin(); 63 for (; output != mOutputs.end(); ++output) { 64 (*output)->disconnect(NATIVE_WINDOW_API_CPU); 65 } 66 67 if (mBuffers.size() > 0) { 68 ALOGE("%zu buffers still being tracked", mBuffers.size()); 69 } 70} 71 72status_t StreamSplitter::addOutput( 73 const sp<IGraphicBufferProducer>& outputQueue) { 74 if (outputQueue == NULL) { 75 ALOGE("addOutput: outputQueue must not be NULL"); 76 return BAD_VALUE; 77 } 78 79 Mutex::Autolock lock(mMutex); 80 81 IGraphicBufferProducer::QueueBufferOutput queueBufferOutput; 82 sp<OutputListener> listener(new OutputListener(this, outputQueue)); 83 IInterface::asBinder(outputQueue)->linkToDeath(listener); 84 status_t status = outputQueue->connect(listener, NATIVE_WINDOW_API_CPU, 85 /* producerControlledByApp */ false, &queueBufferOutput); 86 if (status != NO_ERROR) { 87 ALOGE("addOutput: failed to connect (%d)", status); 88 return status; 89 } 90 91 mOutputs.push_back(outputQueue); 92 93 return NO_ERROR; 94} 95 96void StreamSplitter::setName(const String8 &name) { 97 Mutex::Autolock lock(mMutex); 98 mInput->setConsumerName(name); 99} 100 101void StreamSplitter::onFrameAvailable(const BufferItem& /* item */) { 102 ATRACE_CALL(); 103 Mutex::Autolock lock(mMutex); 104 105 // The current policy is that if any one consumer is consuming buffers too 106 // slowly, the splitter will stall the rest of the outputs by not acquiring 107 // any more buffers from the input. This will cause back pressure on the 108 // input queue, slowing down its producer. 109 110 // If there are too many outstanding buffers, we block until a buffer is 111 // released back to the input in onBufferReleased 112 while (mOutstandingBuffers >= MAX_OUTSTANDING_BUFFERS) { 113 mReleaseCondition.wait(mMutex); 114 115 // If the splitter is abandoned while we are waiting, the release 116 // condition variable will be broadcast, and we should just return 117 // without attempting to do anything more (since the input queue will 118 // also be abandoned). 119 if (mIsAbandoned) { 120 return; 121 } 122 } 123 ++mOutstandingBuffers; 124 125 // Acquire and detach the buffer from the input 126 IGraphicBufferConsumer::BufferItem bufferItem; 127 status_t status = mInput->acquireBuffer(&bufferItem, /* presentWhen */ 0); 128 LOG_ALWAYS_FATAL_IF(status != NO_ERROR, 129 "acquiring buffer from input failed (%d)", status); 130 131 ALOGV("acquired buffer %#" PRIx64 " from input", 132 bufferItem.mGraphicBuffer->getId()); 133 134 status = mInput->detachBuffer(bufferItem.mBuf); 135 LOG_ALWAYS_FATAL_IF(status != NO_ERROR, 136 "detaching buffer from input failed (%d)", status); 137 138 // Initialize our reference count for this buffer 139 mBuffers.add(bufferItem.mGraphicBuffer->getId(), 140 new BufferTracker(bufferItem.mGraphicBuffer)); 141 142 IGraphicBufferProducer::QueueBufferInput queueInput( 143 bufferItem.mTimestamp, bufferItem.mIsAutoTimestamp, 144 bufferItem.mDataSpace, bufferItem.mCrop, 145 static_cast<int32_t>(bufferItem.mScalingMode), 146 bufferItem.mTransform, bufferItem.mIsDroppable, 147 bufferItem.mFence); 148 149 // Attach and queue the buffer to each of the outputs 150 Vector<sp<IGraphicBufferProducer> >::iterator output = mOutputs.begin(); 151 for (; output != mOutputs.end(); ++output) { 152 int slot; 153 status = (*output)->attachBuffer(&slot, bufferItem.mGraphicBuffer); 154 if (status == NO_INIT) { 155 // If we just discovered that this output has been abandoned, note 156 // that, increment the release count so that we still release this 157 // buffer eventually, and move on to the next output 158 onAbandonedLocked(); 159 mBuffers.editValueFor(bufferItem.mGraphicBuffer->getId())-> 160 incrementReleaseCountLocked(); 161 continue; 162 } else { 163 LOG_ALWAYS_FATAL_IF(status != NO_ERROR, 164 "attaching buffer to output failed (%d)", status); 165 } 166 167 IGraphicBufferProducer::QueueBufferOutput queueOutput; 168 status = (*output)->queueBuffer(slot, queueInput, &queueOutput); 169 if (status == NO_INIT) { 170 // If we just discovered that this output has been abandoned, note 171 // that, increment the release count so that we still release this 172 // buffer eventually, and move on to the next output 173 onAbandonedLocked(); 174 mBuffers.editValueFor(bufferItem.mGraphicBuffer->getId())-> 175 incrementReleaseCountLocked(); 176 continue; 177 } else { 178 LOG_ALWAYS_FATAL_IF(status != NO_ERROR, 179 "queueing buffer to output failed (%d)", status); 180 } 181 182 ALOGV("queued buffer %#" PRIx64 " to output %p", 183 bufferItem.mGraphicBuffer->getId(), output->get()); 184 } 185} 186 187void StreamSplitter::onBufferReleasedByOutput( 188 const sp<IGraphicBufferProducer>& from) { 189 ATRACE_CALL(); 190 Mutex::Autolock lock(mMutex); 191 192 sp<GraphicBuffer> buffer; 193 sp<Fence> fence; 194 status_t status = from->detachNextBuffer(&buffer, &fence); 195 if (status == NO_INIT) { 196 // If we just discovered that this output has been abandoned, note that, 197 // but we can't do anything else, since buffer is invalid 198 onAbandonedLocked(); 199 return; 200 } else { 201 LOG_ALWAYS_FATAL_IF(status != NO_ERROR, 202 "detaching buffer from output failed (%d)", status); 203 } 204 205 ALOGV("detached buffer %#" PRIx64 " from output %p", 206 buffer->getId(), from.get()); 207 208 const sp<BufferTracker>& tracker = mBuffers.editValueFor(buffer->getId()); 209 210 // Merge the release fence of the incoming buffer so that the fence we send 211 // back to the input includes all of the outputs' fences 212 tracker->mergeFence(fence); 213 214 // Check to see if this is the last outstanding reference to this buffer 215 size_t releaseCount = tracker->incrementReleaseCountLocked(); 216 ALOGV("buffer %#" PRIx64 " reference count %zu (of %zu)", buffer->getId(), 217 releaseCount, mOutputs.size()); 218 if (releaseCount < mOutputs.size()) { 219 return; 220 } 221 222 // If we've been abandoned, we can't return the buffer to the input, so just 223 // stop tracking it and move on 224 if (mIsAbandoned) { 225 mBuffers.removeItem(buffer->getId()); 226 return; 227 } 228 229 // Attach and release the buffer back to the input 230 int consumerSlot; 231 status = mInput->attachBuffer(&consumerSlot, tracker->getBuffer()); 232 LOG_ALWAYS_FATAL_IF(status != NO_ERROR, 233 "attaching buffer to input failed (%d)", status); 234 235 status = mInput->releaseBuffer(consumerSlot, /* frameNumber */ 0, 236 EGL_NO_DISPLAY, EGL_NO_SYNC_KHR, tracker->getMergedFence()); 237 LOG_ALWAYS_FATAL_IF(status != NO_ERROR, 238 "releasing buffer to input failed (%d)", status); 239 240 ALOGV("released buffer %#" PRIx64 " to input", buffer->getId()); 241 242 // We no longer need to track the buffer once it has been returned to the 243 // input 244 mBuffers.removeItem(buffer->getId()); 245 246 // Notify any waiting onFrameAvailable calls 247 --mOutstandingBuffers; 248 mReleaseCondition.signal(); 249} 250 251void StreamSplitter::onAbandonedLocked() { 252 ALOGE("one of my outputs has abandoned me"); 253 if (!mIsAbandoned) { 254 mInput->consumerDisconnect(); 255 } 256 mIsAbandoned = true; 257 mReleaseCondition.broadcast(); 258} 259 260StreamSplitter::OutputListener::OutputListener( 261 const sp<StreamSplitter>& splitter, 262 const sp<IGraphicBufferProducer>& output) 263 : mSplitter(splitter), mOutput(output) {} 264 265StreamSplitter::OutputListener::~OutputListener() {} 266 267void StreamSplitter::OutputListener::onBufferReleased() { 268 mSplitter->onBufferReleasedByOutput(mOutput); 269} 270 271void StreamSplitter::OutputListener::binderDied(const wp<IBinder>& /* who */) { 272 Mutex::Autolock lock(mSplitter->mMutex); 273 mSplitter->onAbandonedLocked(); 274} 275 276StreamSplitter::BufferTracker::BufferTracker(const sp<GraphicBuffer>& buffer) 277 : mBuffer(buffer), mMergedFence(Fence::NO_FENCE), mReleaseCount(0) {} 278 279StreamSplitter::BufferTracker::~BufferTracker() {} 280 281void StreamSplitter::BufferTracker::mergeFence(const sp<Fence>& with) { 282 mMergedFence = Fence::merge(String8("StreamSplitter"), mMergedFence, with); 283} 284 285} // namespace android 286