SurfaceFlingerConsumer.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /*
  2. * Copyright (C) 2012 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. #define ATRACE_TAG ATRACE_TAG_GRAPHICS
  17. //#define LOG_NDEBUG 0
  18. #include "SurfaceFlingerConsumer.h"
  19. #include <private/gui/SyncFeatures.h>
  20. #include <gui/BufferItem.h>
  21. #include <utils/Errors.h>
  22. #include <utils/NativeHandle.h>
  23. #include <utils/Trace.h>
  24. namespace android {
  25. // ---------------------------------------------------------------------------
  26. status_t SurfaceFlingerConsumer::updateTexImage(BufferRejecter* rejecter,
  27. const DispSync& dispSync, uint64_t maxFrameNumber)
  28. {
  29. ATRACE_CALL();
  30. ALOGV("updateTexImage");
  31. Mutex::Autolock lock(mMutex);
  32. if (mAbandoned) {
  33. ALOGE("updateTexImage: GLConsumer is abandoned!");
  34. return NO_INIT;
  35. }
  36. // Make sure the EGL state is the same as in previous calls.
  37. status_t err = checkAndUpdateEglStateLocked();
  38. if (err != NO_ERROR) {
  39. return err;
  40. }
  41. BufferItem item;
  42. // Acquire the next buffer.
  43. // In asynchronous mode the list is guaranteed to be one buffer
  44. // deep, while in synchronous mode we use the oldest buffer.
  45. err = acquireBufferLocked(&item, computeExpectedPresent(dispSync),
  46. maxFrameNumber);
  47. if (err != NO_ERROR) {
  48. if (err == BufferQueue::NO_BUFFER_AVAILABLE) {
  49. err = NO_ERROR;
  50. } else if (err == BufferQueue::PRESENT_LATER) {
  51. // return the error, without logging
  52. } else {
  53. ALOGE("updateTexImage: acquire failed: %s (%d)",
  54. strerror(-err), err);
  55. }
  56. return err;
  57. }
  58. // We call the rejecter here, in case the caller has a reason to
  59. // not accept this buffer. This is used by SurfaceFlinger to
  60. // reject buffers which have the wrong size
  61. int buf = item.mBuf;
  62. if (rejecter && rejecter->reject(mSlots[buf].mGraphicBuffer, item)) {
  63. releaseBufferLocked(buf, mSlots[buf].mGraphicBuffer, EGL_NO_SYNC_KHR);
  64. return BUFFER_REJECTED;
  65. }
  66. // Release the previous buffer.
  67. err = updateAndReleaseLocked(item);
  68. if (err != NO_ERROR) {
  69. return err;
  70. }
  71. if (!SyncFeatures::getInstance().useNativeFenceSync()) {
  72. // Bind the new buffer to the GL texture.
  73. //
  74. // Older devices require the "implicit" synchronization provided
  75. // by glEGLImageTargetTexture2DOES, which this method calls. Newer
  76. // devices will either call this in Layer::onDraw, or (if it's not
  77. // a GL-composited layer) not at all.
  78. err = bindTextureImageLocked();
  79. }
  80. return err;
  81. }
  82. status_t SurfaceFlingerConsumer::bindTextureImage()
  83. {
  84. Mutex::Autolock lock(mMutex);
  85. return bindTextureImageLocked();
  86. }
  87. status_t SurfaceFlingerConsumer::acquireBufferLocked(BufferItem* item,
  88. nsecs_t presentWhen, uint64_t maxFrameNumber) {
  89. status_t result = GLConsumer::acquireBufferLocked(item, presentWhen,
  90. maxFrameNumber);
  91. if (result == NO_ERROR) {
  92. mTransformToDisplayInverse = item->mTransformToDisplayInverse;
  93. mSurfaceDamage = item->mSurfaceDamage;
  94. }
  95. return result;
  96. }
  97. bool SurfaceFlingerConsumer::getTransformToDisplayInverse() const {
  98. return mTransformToDisplayInverse;
  99. }
  100. const Region& SurfaceFlingerConsumer::getSurfaceDamage() const {
  101. return mSurfaceDamage;
  102. }
  103. sp<NativeHandle> SurfaceFlingerConsumer::getSidebandStream() const {
  104. return mConsumer->getSidebandStream();
  105. }
  106. // We need to determine the time when a buffer acquired now will be
  107. // displayed. This can be calculated:
  108. // time when previous buffer's actual-present fence was signaled
  109. // + current display refresh rate * HWC latency
  110. // + a little extra padding
  111. //
  112. // Buffer producers are expected to set their desired presentation time
  113. // based on choreographer time stamps, which (coming from vsync events)
  114. // will be slightly later then the actual-present timing. If we get a
  115. // desired-present time that is unintentionally a hair after the next
  116. // vsync, we'll hold the frame when we really want to display it. We
  117. // need to take the offset between actual-present and reported-vsync
  118. // into account.
  119. //
  120. // If the system is configured without a DispSync phase offset for the app,
  121. // we also want to throw in a bit of padding to avoid edge cases where we
  122. // just barely miss. We want to do it here, not in every app. A major
  123. // source of trouble is the app's use of the display's ideal refresh time
  124. // (via Display.getRefreshRate()), which could be off of the actual refresh
  125. // by a few percent, with the error multiplied by the number of frames
  126. // between now and when the buffer should be displayed.
  127. //
  128. // If the refresh reported to the app has a phase offset, we shouldn't need
  129. // to tweak anything here.
  130. nsecs_t SurfaceFlingerConsumer::computeExpectedPresent(const DispSync& dispSync)
  131. {
  132. // The HWC doesn't currently have a way to report additional latency.
  133. // Assume that whatever we submit now will appear right after the flip.
  134. // For a smart panel this might be 1. This is expressed in frames,
  135. // rather than time, because we expect to have a constant frame delay
  136. // regardless of the refresh rate.
  137. const uint32_t hwcLatency = 0;
  138. // Ask DispSync when the next refresh will be (CLOCK_MONOTONIC).
  139. const nsecs_t nextRefresh = dispSync.computeNextRefresh(hwcLatency);
  140. // The DispSync time is already adjusted for the difference between
  141. // vsync and reported-vsync (PRESENT_TIME_OFFSET_FROM_VSYNC_NS), so
  142. // we don't need to factor that in here. Pad a little to avoid
  143. // weird effects if apps might be requesting times right on the edge.
  144. nsecs_t extraPadding = 0;
  145. if (VSYNC_EVENT_PHASE_OFFSET_NS == 0) {
  146. extraPadding = 1000000; // 1ms (6% of 60Hz)
  147. }
  148. return nextRefresh + extraPadding;
  149. }
  150. void SurfaceFlingerConsumer::setContentsChangedListener(
  151. const wp<ContentsChangedListener>& listener) {
  152. setFrameAvailableListener(listener);
  153. Mutex::Autolock lock(mMutex);
  154. mContentsChangedListener = listener;
  155. }
  156. void SurfaceFlingerConsumer::onSidebandStreamChanged() {
  157. sp<ContentsChangedListener> listener;
  158. { // scope for the lock
  159. Mutex::Autolock lock(mMutex);
  160. ALOG_ASSERT(mFrameAvailableListener.unsafe_get() == mContentsChangedListener.unsafe_get());
  161. listener = mContentsChangedListener.promote();
  162. }
  163. if (listener != NULL) {
  164. listener->onSidebandStreamChanged();
  165. }
  166. }
  167. // ---------------------------------------------------------------------------
  168. }; // namespace android