FramebufferSurface.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. #include <stdlib.h>
  18. #include <stdio.h>
  19. #include <string.h>
  20. #include <errno.h>
  21. #include <cutils/log.h>
  22. #include <utils/String8.h>
  23. #include <ui/Rect.h>
  24. #include <EGL/egl.h>
  25. #include <hardware/hardware.h>
  26. #include <gui/BufferItem.h>
  27. #include <gui/GraphicBufferAlloc.h>
  28. #include <gui/Surface.h>
  29. #include <ui/GraphicBuffer.h>
  30. #include "FramebufferSurface.h"
  31. #include "HWComposer.h"
  32. #ifndef NUM_FRAMEBUFFER_SURFACE_BUFFERS
  33. #define NUM_FRAMEBUFFER_SURFACE_BUFFERS (2)
  34. #endif
  35. // ----------------------------------------------------------------------------
  36. namespace android {
  37. // ----------------------------------------------------------------------------
  38. /*
  39. * This implements the (main) framebuffer management. This class is used
  40. * mostly by SurfaceFlinger, but also by command line GL application.
  41. *
  42. */
  43. FramebufferSurface::FramebufferSurface(HWComposer& hwc, int disp,
  44. const sp<IGraphicBufferConsumer>& consumer) :
  45. ConsumerBase(consumer),
  46. mDisplayType(disp),
  47. mCurrentBufferSlot(-1),
  48. mCurrentBuffer(0),
  49. mHwc(hwc)
  50. {
  51. mName = "FramebufferSurface";
  52. mConsumer->setConsumerName(mName);
  53. mConsumer->setConsumerUsageBits(GRALLOC_USAGE_HW_FB |
  54. GRALLOC_USAGE_HW_RENDER |
  55. GRALLOC_USAGE_HW_COMPOSER);
  56. mConsumer->setDefaultBufferFormat(mHwc.getFormat(disp));
  57. mConsumer->setDefaultBufferSize(mHwc.getWidth(disp), mHwc.getHeight(disp));
  58. mConsumer->setDefaultMaxBufferCount(NUM_FRAMEBUFFER_SURFACE_BUFFERS);
  59. mConsumer->setMaxAcquiredBufferCount(NUM_FRAMEBUFFER_SURFACE_BUFFERS - 1);
  60. }
  61. status_t FramebufferSurface::beginFrame(bool /*mustRecompose*/) {
  62. return NO_ERROR;
  63. }
  64. status_t FramebufferSurface::prepareFrame(CompositionType /*compositionType*/) {
  65. return NO_ERROR;
  66. }
  67. status_t FramebufferSurface::advanceFrame() {
  68. // Once we remove FB HAL support, we can call nextBuffer() from here
  69. // instead of using onFrameAvailable(). No real benefit, except it'll be
  70. // more like VirtualDisplaySurface.
  71. return NO_ERROR;
  72. }
  73. status_t FramebufferSurface::nextBuffer(sp<GraphicBuffer>& outBuffer, sp<Fence>& outFence) {
  74. Mutex::Autolock lock(mMutex);
  75. BufferItem item;
  76. status_t err = acquireBufferLocked(&item, 0);
  77. if (err == BufferQueue::NO_BUFFER_AVAILABLE) {
  78. outBuffer = mCurrentBuffer;
  79. return NO_ERROR;
  80. } else if (err != NO_ERROR) {
  81. ALOGE("error acquiring buffer: %s (%d)", strerror(-err), err);
  82. return err;
  83. }
  84. // If the BufferQueue has freed and reallocated a buffer in mCurrentSlot
  85. // then we may have acquired the slot we already own. If we had released
  86. // our current buffer before we call acquireBuffer then that release call
  87. // would have returned STALE_BUFFER_SLOT, and we would have called
  88. // freeBufferLocked on that slot. Because the buffer slot has already
  89. // been overwritten with the new buffer all we have to do is skip the
  90. // releaseBuffer call and we should be in the same state we'd be in if we
  91. // had released the old buffer first.
  92. if (mCurrentBufferSlot != BufferQueue::INVALID_BUFFER_SLOT &&
  93. item.mBuf != mCurrentBufferSlot) {
  94. // Release the previous buffer.
  95. err = releaseBufferLocked(mCurrentBufferSlot, mCurrentBuffer,
  96. EGL_NO_DISPLAY, EGL_NO_SYNC_KHR);
  97. if (err < NO_ERROR) {
  98. ALOGE("error releasing buffer: %s (%d)", strerror(-err), err);
  99. return err;
  100. }
  101. }
  102. mCurrentBufferSlot = item.mBuf;
  103. mCurrentBuffer = mSlots[mCurrentBufferSlot].mGraphicBuffer;
  104. outFence = item.mFence;
  105. outBuffer = mCurrentBuffer;
  106. return NO_ERROR;
  107. }
  108. // Overrides ConsumerBase::onFrameAvailable(), does not call base class impl.
  109. void FramebufferSurface::onFrameAvailable(const BufferItem& /* item */) {
  110. sp<GraphicBuffer> buf;
  111. sp<Fence> acquireFence;
  112. status_t err = nextBuffer(buf, acquireFence);
  113. if (err != NO_ERROR) {
  114. ALOGE("error latching nnext FramebufferSurface buffer: %s (%d)",
  115. strerror(-err), err);
  116. return;
  117. }
  118. err = mHwc.fbPost(mDisplayType, acquireFence, buf);
  119. if (err != NO_ERROR) {
  120. ALOGE("error posting framebuffer: %d", err);
  121. }
  122. }
  123. void FramebufferSurface::freeBufferLocked(int slotIndex) {
  124. ConsumerBase::freeBufferLocked(slotIndex);
  125. if (slotIndex == mCurrentBufferSlot) {
  126. mCurrentBufferSlot = BufferQueue::INVALID_BUFFER_SLOT;
  127. }
  128. }
  129. void FramebufferSurface::onFrameCommitted() {
  130. sp<Fence> fence = mHwc.getAndResetReleaseFence(mDisplayType);
  131. if (fence->isValid() &&
  132. mCurrentBufferSlot != BufferQueue::INVALID_BUFFER_SLOT) {
  133. status_t err = addReleaseFence(mCurrentBufferSlot,
  134. mCurrentBuffer, fence);
  135. ALOGE_IF(err, "setReleaseFenceFd: failed to add the fence: %s (%d)",
  136. strerror(-err), err);
  137. }
  138. }
  139. status_t FramebufferSurface::compositionComplete()
  140. {
  141. return mHwc.fbCompositionComplete();
  142. }
  143. void FramebufferSurface::dumpAsString(String8& result) const {
  144. ConsumerBase::dump(result);
  145. }
  146. void FramebufferSurface::dumpLocked(String8& result, const char* prefix) const
  147. {
  148. mHwc.fbDump(result);
  149. ConsumerBase::dumpLocked(result, prefix);
  150. }
  151. // ----------------------------------------------------------------------------
  152. }; // namespace android
  153. // ----------------------------------------------------------------------------