Home | History | Annotate | Download | only in DisplayHardware
      1 /*
      2  **
      3  ** Copyright 2012 The Android Open Source Project
      4  **
      5  ** Licensed under the Apache License Version 2.0(the "License");
      6  ** you may not use this file except in compliance with the License.
      7  ** You may obtain a copy of the License at
      8  **
      9  **     http://www.apache.org/licenses/LICENSE-2.0
     10  **
     11  ** Unless required by applicable law or agreed to in writing software
     12  ** distributed under the License is distributed on an "AS IS" BASIS
     13  ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND either express or implied.
     14  ** See the License for the specific language governing permissions and
     15  ** limitations under the License.
     16  */
     17 
     18 // #define LOG_NDEBUG 0
     19 #undef LOG_TAG
     20 #define LOG_TAG "FramebufferSurface"
     21 
     22 #include <stdlib.h>
     23 #include <stdio.h>
     24 #include <string.h>
     25 #include <errno.h>
     26 
     27 #include <cutils/log.h>
     28 
     29 #include <utils/String8.h>
     30 
     31 #include <ui/Rect.h>
     32 
     33 #include <EGL/egl.h>
     34 
     35 #include <hardware/hardware.h>
     36 #include <gui/BufferItem.h>
     37 #include <gui/GraphicBufferAlloc.h>
     38 #include <gui/Surface.h>
     39 #include <ui/GraphicBuffer.h>
     40 
     41 #include "FramebufferSurface.h"
     42 #include "HWComposer.h"
     43 
     44 #ifndef NUM_FRAMEBUFFER_SURFACE_BUFFERS
     45 #define NUM_FRAMEBUFFER_SURFACE_BUFFERS (2)
     46 #endif
     47 
     48 // ----------------------------------------------------------------------------
     49 namespace android {
     50 // ----------------------------------------------------------------------------
     51 
     52 /*
     53  * This implements the (main) framebuffer management. This class is used
     54  * mostly by SurfaceFlinger, but also by command line GL application.
     55  *
     56  */
     57 
     58 FramebufferSurface::FramebufferSurface(HWComposer& hwc, int disp,
     59         const sp<IGraphicBufferConsumer>& consumer) :
     60     ConsumerBase(consumer),
     61     mDisplayType(disp),
     62     mCurrentBufferSlot(-1),
     63     mCurrentBuffer(),
     64     mCurrentFence(Fence::NO_FENCE),
     65 #ifdef USE_HWC2
     66     mHwc(hwc),
     67     mHasPendingRelease(false),
     68     mPreviousBufferSlot(BufferQueue::INVALID_BUFFER_SLOT),
     69     mPreviousBuffer()
     70 #else
     71     mHwc(hwc)
     72 #endif
     73 {
     74 #ifdef USE_HWC2
     75     ALOGV("Creating for display %d", disp);
     76 #endif
     77 
     78     mName = "FramebufferSurface";
     79     mConsumer->setConsumerName(mName);
     80     mConsumer->setConsumerUsageBits(GRALLOC_USAGE_HW_FB |
     81                                        GRALLOC_USAGE_HW_RENDER |
     82                                        GRALLOC_USAGE_HW_COMPOSER);
     83 #ifdef USE_HWC2
     84     const auto& activeConfig = mHwc.getActiveConfig(disp);
     85     mConsumer->setDefaultBufferSize(activeConfig->getWidth(),
     86             activeConfig->getHeight());
     87 #else
     88     mConsumer->setDefaultBufferFormat(mHwc.getFormat(disp));
     89     mConsumer->setDefaultBufferSize(mHwc.getWidth(disp), mHwc.getHeight(disp));
     90 #endif
     91     mConsumer->setMaxAcquiredBufferCount(NUM_FRAMEBUFFER_SURFACE_BUFFERS - 1);
     92 }
     93 
     94 status_t FramebufferSurface::beginFrame(bool /*mustRecompose*/) {
     95     return NO_ERROR;
     96 }
     97 
     98 status_t FramebufferSurface::prepareFrame(CompositionType /*compositionType*/) {
     99     return NO_ERROR;
    100 }
    101 
    102 status_t FramebufferSurface::advanceFrame() {
    103 #ifdef USE_HWC2
    104     sp<GraphicBuffer> buf;
    105     sp<Fence> acquireFence(Fence::NO_FENCE);
    106     android_dataspace_t dataspace = HAL_DATASPACE_UNKNOWN;
    107     status_t result = nextBuffer(buf, acquireFence, dataspace);
    108     if (result != NO_ERROR) {
    109         ALOGE("error latching next FramebufferSurface buffer: %s (%d)",
    110                 strerror(-result), result);
    111         return result;
    112     }
    113     result = mHwc.setClientTarget(mDisplayType, acquireFence, buf, dataspace);
    114     if (result != NO_ERROR) {
    115         ALOGE("error posting framebuffer: %d", result);
    116     }
    117     return result;
    118 #else
    119     // Once we remove FB HAL support, we can call nextBuffer() from here
    120     // instead of using onFrameAvailable(). No real benefit, except it'll be
    121     // more like VirtualDisplaySurface.
    122     return NO_ERROR;
    123 #endif
    124 }
    125 
    126 #ifdef USE_HWC2
    127 status_t FramebufferSurface::nextBuffer(sp<GraphicBuffer>& outBuffer,
    128         sp<Fence>& outFence, android_dataspace_t& outDataspace) {
    129 #else
    130 status_t FramebufferSurface::nextBuffer(sp<GraphicBuffer>& outBuffer, sp<Fence>& outFence) {
    131 #endif
    132     Mutex::Autolock lock(mMutex);
    133 
    134     BufferItem item;
    135     status_t err = acquireBufferLocked(&item, 0);
    136     if (err == BufferQueue::NO_BUFFER_AVAILABLE) {
    137         outBuffer = mCurrentBuffer;
    138         return NO_ERROR;
    139     } else if (err != NO_ERROR) {
    140         ALOGE("error acquiring buffer: %s (%d)", strerror(-err), err);
    141         return err;
    142     }
    143 
    144     // If the BufferQueue has freed and reallocated a buffer in mCurrentSlot
    145     // then we may have acquired the slot we already own.  If we had released
    146     // our current buffer before we call acquireBuffer then that release call
    147     // would have returned STALE_BUFFER_SLOT, and we would have called
    148     // freeBufferLocked on that slot.  Because the buffer slot has already
    149     // been overwritten with the new buffer all we have to do is skip the
    150     // releaseBuffer call and we should be in the same state we'd be in if we
    151     // had released the old buffer first.
    152     if (mCurrentBufferSlot != BufferQueue::INVALID_BUFFER_SLOT &&
    153         item.mSlot != mCurrentBufferSlot) {
    154 #ifdef USE_HWC2
    155         mHasPendingRelease = true;
    156         mPreviousBufferSlot = mCurrentBufferSlot;
    157         mPreviousBuffer = mCurrentBuffer;
    158 #else
    159         // Release the previous buffer.
    160         err = releaseBufferLocked(mCurrentBufferSlot, mCurrentBuffer,
    161                 EGL_NO_DISPLAY, EGL_NO_SYNC_KHR);
    162         if (err < NO_ERROR) {
    163             ALOGE("error releasing buffer: %s (%d)", strerror(-err), err);
    164             return err;
    165         }
    166 #endif
    167     }
    168     mCurrentBufferSlot = item.mSlot;
    169     mCurrentBuffer = mSlots[mCurrentBufferSlot].mGraphicBuffer;
    170     mCurrentFence = item.mFence;
    171 
    172     outFence = item.mFence;
    173     outBuffer = mCurrentBuffer;
    174 #ifdef USE_HWC2
    175     outDataspace = item.mDataSpace;
    176 #endif
    177     return NO_ERROR;
    178 }
    179 
    180 #ifndef USE_HWC2
    181 // Overrides ConsumerBase::onFrameAvailable(), does not call base class impl.
    182 void FramebufferSurface::onFrameAvailable(const BufferItem& /* item */) {
    183     sp<GraphicBuffer> buf;
    184     sp<Fence> acquireFence;
    185     status_t err = nextBuffer(buf, acquireFence);
    186     if (err != NO_ERROR) {
    187         ALOGE("error latching nnext FramebufferSurface buffer: %s (%d)",
    188                 strerror(-err), err);
    189         return;
    190     }
    191     err = mHwc.fbPost(mDisplayType, acquireFence, buf);
    192     if (err != NO_ERROR) {
    193         ALOGE("error posting framebuffer: %d", err);
    194     }
    195 }
    196 #endif
    197 
    198 void FramebufferSurface::freeBufferLocked(int slotIndex) {
    199     ConsumerBase::freeBufferLocked(slotIndex);
    200     if (slotIndex == mCurrentBufferSlot) {
    201         mCurrentBufferSlot = BufferQueue::INVALID_BUFFER_SLOT;
    202     }
    203 }
    204 
    205 void FramebufferSurface::onFrameCommitted() {
    206 #ifdef USE_HWC2
    207     if (mHasPendingRelease) {
    208         sp<Fence> fence = mHwc.getRetireFence(mDisplayType);
    209         if (fence->isValid()) {
    210             status_t result = addReleaseFence(mPreviousBufferSlot,
    211                     mPreviousBuffer, fence);
    212             ALOGE_IF(result != NO_ERROR, "onFrameCommitted: failed to add the"
    213                     " fence: %s (%d)", strerror(-result), result);
    214         }
    215         status_t result = releaseBufferLocked(mPreviousBufferSlot,
    216                 mPreviousBuffer, EGL_NO_DISPLAY, EGL_NO_SYNC_KHR);
    217         ALOGE_IF(result != NO_ERROR, "onFrameCommitted: error releasing buffer:"
    218                 " %s (%d)", strerror(-result), result);
    219 
    220         mPreviousBuffer.clear();
    221         mHasPendingRelease = false;
    222     }
    223 #else
    224     sp<Fence> fence = mHwc.getAndResetReleaseFence(mDisplayType);
    225     if (fence->isValid() &&
    226             mCurrentBufferSlot != BufferQueue::INVALID_BUFFER_SLOT) {
    227         status_t err = addReleaseFence(mCurrentBufferSlot,
    228                 mCurrentBuffer, fence);
    229         ALOGE_IF(err, "setReleaseFenceFd: failed to add the fence: %s (%d)",
    230                 strerror(-err), err);
    231     }
    232 #endif
    233 }
    234 
    235 #ifndef USE_HWC2
    236 status_t FramebufferSurface::compositionComplete()
    237 {
    238     return mHwc.fbCompositionComplete();
    239 }
    240 #endif
    241 
    242 void FramebufferSurface::dumpAsString(String8& result) const {
    243     ConsumerBase::dump(result);
    244 }
    245 
    246 void FramebufferSurface::dumpLocked(String8& result, const char* prefix) const
    247 {
    248 #ifndef USE_HWC2
    249     mHwc.fbDump(result);
    250 #endif
    251     ConsumerBase::dumpLocked(result, prefix);
    252 }
    253 
    254 const sp<Fence>& FramebufferSurface::getClientTargetAcquireFence() const {
    255     return mCurrentFence;
    256 }
    257 
    258 // ----------------------------------------------------------------------------
    259 }; // namespace android
    260 // ----------------------------------------------------------------------------
    261