FramebufferSurface.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. // #define LOG_NDEBUG 0
  18. #undef LOG_TAG
  19. #define LOG_TAG "FramebufferSurface"
  20. #include <errno.h>
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #include <utils/String8.h>
  25. #include <log/log.h>
  26. #include <hardware/hardware.h>
  27. #include <gui/BufferItem.h>
  28. #include <gui/BufferQueue.h>
  29. #include <gui/Surface.h>
  30. #include <ui/DebugUtils.h>
  31. #include <ui/GraphicBuffer.h>
  32. #include <ui/Rect.h>
  33. #include "FramebufferSurface.h"
  34. #include "HWComposer.h"
  35. #include "../SurfaceFlinger.h"
  36. // ----------------------------------------------------------------------------
  37. namespace android {
  38. // ----------------------------------------------------------------------------
  39. using ui::Dataspace;
  40. /*
  41. * This implements the (main) framebuffer management. This class is used
  42. * mostly by SurfaceFlinger, but also by command line GL application.
  43. *
  44. */
  45. FramebufferSurface::FramebufferSurface(HWComposer& hwc, DisplayId displayId,
  46. const sp<IGraphicBufferConsumer>& consumer)
  47. : ConsumerBase(consumer),
  48. mDisplayId(displayId),
  49. mCurrentBufferSlot(-1),
  50. mCurrentBuffer(),
  51. mCurrentFence(Fence::NO_FENCE),
  52. mHwc(hwc),
  53. mHasPendingRelease(false),
  54. mPreviousBufferSlot(BufferQueue::INVALID_BUFFER_SLOT),
  55. mPreviousBuffer() {
  56. ALOGV("Creating for display %s", to_string(displayId).c_str());
  57. mName = "FramebufferSurface";
  58. mConsumer->setConsumerName(mName);
  59. mConsumer->setConsumerUsageBits(GRALLOC_USAGE_HW_FB |
  60. GRALLOC_USAGE_HW_RENDER |
  61. GRALLOC_USAGE_HW_COMPOSER);
  62. const auto& activeConfig = mHwc.getActiveConfig(displayId);
  63. mConsumer->setDefaultBufferSize(activeConfig->getWidth(),
  64. activeConfig->getHeight());
  65. mConsumer->setMaxAcquiredBufferCount(
  66. SurfaceFlinger::maxFrameBufferAcquiredBuffers - 1);
  67. }
  68. void FramebufferSurface::resizeBuffers(const uint32_t width, const uint32_t height) {
  69. mConsumer->setDefaultBufferSize(width, height);
  70. }
  71. status_t FramebufferSurface::beginFrame(bool /*mustRecompose*/) {
  72. return NO_ERROR;
  73. }
  74. status_t FramebufferSurface::prepareFrame(CompositionType /*compositionType*/) {
  75. return NO_ERROR;
  76. }
  77. status_t FramebufferSurface::advanceFrame() {
  78. uint32_t slot = 0;
  79. sp<GraphicBuffer> buf;
  80. sp<Fence> acquireFence(Fence::NO_FENCE);
  81. Dataspace dataspace = Dataspace::UNKNOWN;
  82. status_t result = nextBuffer(slot, buf, acquireFence, dataspace);
  83. mDataSpace = dataspace;
  84. if (result != NO_ERROR) {
  85. ALOGE("error latching next FramebufferSurface buffer: %s (%d)",
  86. strerror(-result), result);
  87. }
  88. return result;
  89. }
  90. status_t FramebufferSurface::nextBuffer(uint32_t& outSlot,
  91. sp<GraphicBuffer>& outBuffer, sp<Fence>& outFence,
  92. Dataspace& outDataspace) {
  93. Mutex::Autolock lock(mMutex);
  94. BufferItem item;
  95. status_t err = acquireBufferLocked(&item, 0);
  96. if (err == BufferQueue::NO_BUFFER_AVAILABLE) {
  97. mHwcBufferCache.getHwcBuffer(mCurrentBufferSlot, mCurrentBuffer, &outSlot, &outBuffer);
  98. return NO_ERROR;
  99. } else if (err != NO_ERROR) {
  100. ALOGE("error acquiring buffer: %s (%d)", strerror(-err), err);
  101. return err;
  102. }
  103. // If the BufferQueue has freed and reallocated a buffer in mCurrentSlot
  104. // then we may have acquired the slot we already own. If we had released
  105. // our current buffer before we call acquireBuffer then that release call
  106. // would have returned STALE_BUFFER_SLOT, and we would have called
  107. // freeBufferLocked on that slot. Because the buffer slot has already
  108. // been overwritten with the new buffer all we have to do is skip the
  109. // releaseBuffer call and we should be in the same state we'd be in if we
  110. // had released the old buffer first.
  111. if (mCurrentBufferSlot != BufferQueue::INVALID_BUFFER_SLOT &&
  112. item.mSlot != mCurrentBufferSlot) {
  113. mHasPendingRelease = true;
  114. mPreviousBufferSlot = mCurrentBufferSlot;
  115. mPreviousBuffer = mCurrentBuffer;
  116. }
  117. mCurrentBufferSlot = item.mSlot;
  118. mCurrentBuffer = mSlots[mCurrentBufferSlot].mGraphicBuffer;
  119. mCurrentFence = item.mFence;
  120. outFence = item.mFence;
  121. mHwcBufferCache.getHwcBuffer(mCurrentBufferSlot, mCurrentBuffer, &outSlot, &outBuffer);
  122. outDataspace = static_cast<Dataspace>(item.mDataSpace);
  123. status_t result = mHwc.setClientTarget(mDisplayId, outSlot, outFence, outBuffer, outDataspace);
  124. if (result != NO_ERROR) {
  125. ALOGE("error posting framebuffer: %d", result);
  126. return result;
  127. }
  128. return NO_ERROR;
  129. }
  130. void FramebufferSurface::freeBufferLocked(int slotIndex) {
  131. ConsumerBase::freeBufferLocked(slotIndex);
  132. if (slotIndex == mCurrentBufferSlot) {
  133. mCurrentBufferSlot = BufferQueue::INVALID_BUFFER_SLOT;
  134. }
  135. }
  136. void FramebufferSurface::onFrameCommitted() {
  137. if (mHasPendingRelease) {
  138. sp<Fence> fence = mHwc.getPresentFence(mDisplayId);
  139. if (fence->isValid()) {
  140. status_t result = addReleaseFence(mPreviousBufferSlot,
  141. mPreviousBuffer, fence);
  142. ALOGE_IF(result != NO_ERROR, "onFrameCommitted: failed to add the"
  143. " fence: %s (%d)", strerror(-result), result);
  144. }
  145. status_t result = releaseBufferLocked(mPreviousBufferSlot, mPreviousBuffer);
  146. ALOGE_IF(result != NO_ERROR, "onFrameCommitted: error releasing buffer:"
  147. " %s (%d)", strerror(-result), result);
  148. mPreviousBuffer.clear();
  149. mHasPendingRelease = false;
  150. }
  151. }
  152. void FramebufferSurface::dumpAsString(String8& result) const {
  153. Mutex::Autolock lock(mMutex);
  154. result.appendFormat(" FramebufferSurface: dataspace: %s(%d)\n",
  155. dataspaceDetails(static_cast<android_dataspace>(mDataSpace)).c_str(),
  156. mDataSpace);
  157. ConsumerBase::dumpLocked(result, " ");
  158. }
  159. void FramebufferSurface::dumpLocked(String8& result, const char* prefix) const
  160. {
  161. ConsumerBase::dumpLocked(result, prefix);
  162. }
  163. const sp<Fence>& FramebufferSurface::getClientTargetAcquireFence() const {
  164. return mCurrentFence;
  165. }
  166. // ----------------------------------------------------------------------------
  167. }; // namespace android
  168. // ----------------------------------------------------------------------------