Andy McFadden | bf974ab | 2012-12-04 16:51:15 -0800 | [diff] [blame] | 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 | |
| 17 | #define ATRACE_TAG ATRACE_TAG_GRAPHICS |
Jamie Gennis | 1df8c34 | 2012-12-20 14:05:45 -0800 | [diff] [blame] | 18 | //#define LOG_NDEBUG 0 |
Andy McFadden | bf974ab | 2012-12-04 16:51:15 -0800 | [diff] [blame] | 19 | |
| 20 | #include "SurfaceFlingerConsumer.h" |
Pablo Ceballos | ce796e7 | 2016-02-04 19:10:51 -0800 | [diff] [blame] | 21 | #include "Layer.h" |
Andy McFadden | bf974ab | 2012-12-04 16:51:15 -0800 | [diff] [blame] | 22 | |
Mathias Agopian | ca08833 | 2013-03-28 17:44:13 -0700 | [diff] [blame] | 23 | #include <private/gui/SyncFeatures.h> |
| 24 | |
Dan Stoza | 11611f9 | 2015-03-12 15:12:44 -0700 | [diff] [blame] | 25 | #include <gui/BufferItem.h> |
| 26 | |
Andy McFadden | bf974ab | 2012-12-04 16:51:15 -0800 | [diff] [blame] | 27 | #include <utils/Errors.h> |
Jesse Hall | 399184a | 2014-03-03 15:42:54 -0800 | [diff] [blame] | 28 | #include <utils/NativeHandle.h> |
| 29 | #include <utils/Trace.h> |
Andy McFadden | bf974ab | 2012-12-04 16:51:15 -0800 | [diff] [blame] | 30 | |
Andy McFadden | bf974ab | 2012-12-04 16:51:15 -0800 | [diff] [blame] | 31 | namespace android { |
| 32 | |
| 33 | // --------------------------------------------------------------------------- |
| 34 | |
Andy McFadden | 41d67d7 | 2014-04-25 16:58:34 -0700 | [diff] [blame] | 35 | status_t SurfaceFlingerConsumer::updateTexImage(BufferRejecter* rejecter, |
Pablo Ceballos | ff95aab | 2016-01-13 17:09:58 -0800 | [diff] [blame] | 36 | const DispSync& dispSync, bool* autoRefresh, bool* queuedBuffer, |
Pablo Ceballos | 0631218 | 2015-10-07 16:32:12 -0700 | [diff] [blame] | 37 | uint64_t maxFrameNumber) |
Andy McFadden | bf974ab | 2012-12-04 16:51:15 -0800 | [diff] [blame] | 38 | { |
| 39 | ATRACE_CALL(); |
| 40 | ALOGV("updateTexImage"); |
| 41 | Mutex::Autolock lock(mMutex); |
| 42 | |
| 43 | if (mAbandoned) { |
Andy McFadden | 2adaf04 | 2012-12-18 09:49:45 -0800 | [diff] [blame] | 44 | ALOGE("updateTexImage: GLConsumer is abandoned!"); |
Andy McFadden | bf974ab | 2012-12-04 16:51:15 -0800 | [diff] [blame] | 45 | return NO_INIT; |
| 46 | } |
| 47 | |
| 48 | // Make sure the EGL state is the same as in previous calls. |
| 49 | status_t err = checkAndUpdateEglStateLocked(); |
| 50 | if (err != NO_ERROR) { |
| 51 | return err; |
| 52 | } |
| 53 | |
Dan Stoza | 11611f9 | 2015-03-12 15:12:44 -0700 | [diff] [blame] | 54 | BufferItem item; |
Andy McFadden | bf974ab | 2012-12-04 16:51:15 -0800 | [diff] [blame] | 55 | |
| 56 | // Acquire the next buffer. |
| 57 | // In asynchronous mode the list is guaranteed to be one buffer |
| 58 | // deep, while in synchronous mode we use the oldest buffer. |
Dan Stoza | a4650a5 | 2015-05-12 12:56:16 -0700 | [diff] [blame] | 59 | err = acquireBufferLocked(&item, computeExpectedPresent(dispSync), |
| 60 | maxFrameNumber); |
Andy McFadden | bf974ab | 2012-12-04 16:51:15 -0800 | [diff] [blame] | 61 | if (err != NO_ERROR) { |
| 62 | if (err == BufferQueue::NO_BUFFER_AVAILABLE) { |
Andy McFadden | bf974ab | 2012-12-04 16:51:15 -0800 | [diff] [blame] | 63 | err = NO_ERROR; |
Andy McFadden | 1585c4d | 2013-06-28 13:52:40 -0700 | [diff] [blame] | 64 | } else if (err == BufferQueue::PRESENT_LATER) { |
| 65 | // return the error, without logging |
Andy McFadden | bf974ab | 2012-12-04 16:51:15 -0800 | [diff] [blame] | 66 | } else { |
| 67 | ALOGE("updateTexImage: acquire failed: %s (%d)", |
| 68 | strerror(-err), err); |
| 69 | } |
| 70 | return err; |
| 71 | } |
| 72 | |
Fabien Sanglard | 0a4b26e | 2016-10-14 18:13:33 -0700 | [diff] [blame] | 73 | if (autoRefresh) { |
| 74 | *autoRefresh = item.mAutoRefresh; |
| 75 | } |
| 76 | |
| 77 | if (queuedBuffer) { |
| 78 | *queuedBuffer = item.mQueuedBuffer; |
| 79 | } |
| 80 | |
Andy McFadden | bf974ab | 2012-12-04 16:51:15 -0800 | [diff] [blame] | 81 | // We call the rejecter here, in case the caller has a reason to |
| 82 | // not accept this buffer. This is used by SurfaceFlinger to |
| 83 | // reject buffers which have the wrong size |
Pablo Ceballos | 47650f4 | 2015-08-04 16:38:17 -0700 | [diff] [blame] | 84 | int slot = item.mSlot; |
| 85 | if (rejecter && rejecter->reject(mSlots[slot].mGraphicBuffer, item)) { |
| 86 | releaseBufferLocked(slot, mSlots[slot].mGraphicBuffer, EGL_NO_SYNC_KHR); |
Dan Stoza | ecc5040 | 2015-04-28 14:42:06 -0700 | [diff] [blame] | 87 | return BUFFER_REJECTED; |
Andy McFadden | bf974ab | 2012-12-04 16:51:15 -0800 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | // Release the previous buffer. |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 91 | #ifdef USE_HWC2 |
| 92 | err = updateAndReleaseLocked(item, &mPendingRelease); |
| 93 | #else |
Mathias Agopian | ad678e1 | 2013-07-23 17:28:53 -0700 | [diff] [blame] | 94 | err = updateAndReleaseLocked(item); |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 95 | #endif |
Andy McFadden | bf974ab | 2012-12-04 16:51:15 -0800 | [diff] [blame] | 96 | if (err != NO_ERROR) { |
| 97 | return err; |
| 98 | } |
| 99 | |
Mathias Agopian | ca08833 | 2013-03-28 17:44:13 -0700 | [diff] [blame] | 100 | if (!SyncFeatures::getInstance().useNativeFenceSync()) { |
Andy McFadden | 97eba89 | 2012-12-11 15:21:45 -0800 | [diff] [blame] | 101 | // Bind the new buffer to the GL texture. |
| 102 | // |
| 103 | // Older devices require the "implicit" synchronization provided |
| 104 | // by glEGLImageTargetTexture2DOES, which this method calls. Newer |
| 105 | // devices will either call this in Layer::onDraw, or (if it's not |
| 106 | // a GL-composited layer) not at all. |
| 107 | err = bindTextureImageLocked(); |
| 108 | } |
| 109 | |
| 110 | return err; |
| 111 | } |
| 112 | |
| 113 | status_t SurfaceFlingerConsumer::bindTextureImage() |
| 114 | { |
| 115 | Mutex::Autolock lock(mMutex); |
| 116 | |
| 117 | return bindTextureImageLocked(); |
Andy McFadden | bf974ab | 2012-12-04 16:51:15 -0800 | [diff] [blame] | 118 | } |
| 119 | |
Dan Stoza | 11611f9 | 2015-03-12 15:12:44 -0700 | [diff] [blame] | 120 | status_t SurfaceFlingerConsumer::acquireBufferLocked(BufferItem* item, |
Dan Stoza | a4650a5 | 2015-05-12 12:56:16 -0700 | [diff] [blame] | 121 | nsecs_t presentWhen, uint64_t maxFrameNumber) { |
| 122 | status_t result = GLConsumer::acquireBufferLocked(item, presentWhen, |
| 123 | maxFrameNumber); |
Mathias Agopian | c1c05de | 2013-09-17 23:45:22 -0700 | [diff] [blame] | 124 | if (result == NO_ERROR) { |
| 125 | mTransformToDisplayInverse = item->mTransformToDisplayInverse; |
Dan Stoza | ee44edd | 2015-03-23 15:50:23 -0700 | [diff] [blame] | 126 | mSurfaceDamage = item->mSurfaceDamage; |
Mathias Agopian | c1c05de | 2013-09-17 23:45:22 -0700 | [diff] [blame] | 127 | } |
| 128 | return result; |
| 129 | } |
| 130 | |
| 131 | bool SurfaceFlingerConsumer::getTransformToDisplayInverse() const { |
Robert Carr | 367c568 | 2016-06-20 11:55:28 -0700 | [diff] [blame] | 132 | Mutex::Autolock lock(mMutex); |
Mathias Agopian | c1c05de | 2013-09-17 23:45:22 -0700 | [diff] [blame] | 133 | return mTransformToDisplayInverse; |
| 134 | } |
| 135 | |
Dan Stoza | ee44edd | 2015-03-23 15:50:23 -0700 | [diff] [blame] | 136 | const Region& SurfaceFlingerConsumer::getSurfaceDamage() const { |
| 137 | return mSurfaceDamage; |
| 138 | } |
| 139 | |
Jesse Hall | 399184a | 2014-03-03 15:42:54 -0800 | [diff] [blame] | 140 | sp<NativeHandle> SurfaceFlingerConsumer::getSidebandStream() const { |
| 141 | return mConsumer->getSidebandStream(); |
| 142 | } |
| 143 | |
Andy McFadden | 1585c4d | 2013-06-28 13:52:40 -0700 | [diff] [blame] | 144 | // We need to determine the time when a buffer acquired now will be |
| 145 | // displayed. This can be calculated: |
| 146 | // time when previous buffer's actual-present fence was signaled |
| 147 | // + current display refresh rate * HWC latency |
| 148 | // + a little extra padding |
| 149 | // |
| 150 | // Buffer producers are expected to set their desired presentation time |
| 151 | // based on choreographer time stamps, which (coming from vsync events) |
| 152 | // will be slightly later then the actual-present timing. If we get a |
| 153 | // desired-present time that is unintentionally a hair after the next |
| 154 | // vsync, we'll hold the frame when we really want to display it. We |
Andy McFadden | 41d67d7 | 2014-04-25 16:58:34 -0700 | [diff] [blame] | 155 | // need to take the offset between actual-present and reported-vsync |
| 156 | // into account. |
| 157 | // |
| 158 | // If the system is configured without a DispSync phase offset for the app, |
| 159 | // we also want to throw in a bit of padding to avoid edge cases where we |
| 160 | // just barely miss. We want to do it here, not in every app. A major |
| 161 | // source of trouble is the app's use of the display's ideal refresh time |
| 162 | // (via Display.getRefreshRate()), which could be off of the actual refresh |
| 163 | // by a few percent, with the error multiplied by the number of frames |
| 164 | // between now and when the buffer should be displayed. |
| 165 | // |
| 166 | // If the refresh reported to the app has a phase offset, we shouldn't need |
| 167 | // to tweak anything here. |
| 168 | nsecs_t SurfaceFlingerConsumer::computeExpectedPresent(const DispSync& dispSync) |
Andy McFadden | 1585c4d | 2013-06-28 13:52:40 -0700 | [diff] [blame] | 169 | { |
Andy McFadden | 1585c4d | 2013-06-28 13:52:40 -0700 | [diff] [blame] | 170 | // The HWC doesn't currently have a way to report additional latency. |
Andy McFadden | 41d67d7 | 2014-04-25 16:58:34 -0700 | [diff] [blame] | 171 | // Assume that whatever we submit now will appear right after the flip. |
| 172 | // For a smart panel this might be 1. This is expressed in frames, |
| 173 | // rather than time, because we expect to have a constant frame delay |
| 174 | // regardless of the refresh rate. |
| 175 | const uint32_t hwcLatency = 0; |
Andy McFadden | 1585c4d | 2013-06-28 13:52:40 -0700 | [diff] [blame] | 176 | |
Andy McFadden | 41d67d7 | 2014-04-25 16:58:34 -0700 | [diff] [blame] | 177 | // Ask DispSync when the next refresh will be (CLOCK_MONOTONIC). |
| 178 | const nsecs_t nextRefresh = dispSync.computeNextRefresh(hwcLatency); |
Andy McFadden | 1585c4d | 2013-06-28 13:52:40 -0700 | [diff] [blame] | 179 | |
Andy McFadden | 41d67d7 | 2014-04-25 16:58:34 -0700 | [diff] [blame] | 180 | // The DispSync time is already adjusted for the difference between |
| 181 | // vsync and reported-vsync (PRESENT_TIME_OFFSET_FROM_VSYNC_NS), so |
| 182 | // we don't need to factor that in here. Pad a little to avoid |
| 183 | // weird effects if apps might be requesting times right on the edge. |
| 184 | nsecs_t extraPadding = 0; |
| 185 | if (VSYNC_EVENT_PHASE_OFFSET_NS == 0) { |
| 186 | extraPadding = 1000000; // 1ms (6% of 60Hz) |
| 187 | } |
| 188 | |
| 189 | return nextRefresh + extraPadding; |
Andy McFadden | 1585c4d | 2013-06-28 13:52:40 -0700 | [diff] [blame] | 190 | } |
| 191 | |
Brian Anderson | 3546a3f | 2016-07-14 11:51:14 -0700 | [diff] [blame] | 192 | sp<Fence> SurfaceFlingerConsumer::getPrevFinalReleaseFence() const { |
| 193 | Mutex::Autolock lock(mMutex); |
| 194 | return ConsumerBase::mPrevFinalReleaseFence; |
| 195 | } |
| 196 | |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 197 | #ifdef USE_HWC2 |
| 198 | void SurfaceFlingerConsumer::setReleaseFence(const sp<Fence>& fence) |
| 199 | { |
| 200 | if (!mPendingRelease.isPending) { |
| 201 | GLConsumer::setReleaseFence(fence); |
| 202 | return; |
| 203 | } |
| 204 | auto currentTexture = mPendingRelease.currentTexture; |
| 205 | if (fence->isValid() && |
| 206 | currentTexture != BufferQueue::INVALID_BUFFER_SLOT) { |
| 207 | status_t result = addReleaseFence(currentTexture, |
| 208 | mPendingRelease.graphicBuffer, fence); |
| 209 | ALOGE_IF(result != NO_ERROR, "setReleaseFence: failed to add the" |
| 210 | " fence: %s (%d)", strerror(-result), result); |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | void SurfaceFlingerConsumer::releasePendingBuffer() |
| 215 | { |
| 216 | if (!mPendingRelease.isPending) { |
| 217 | ALOGV("Pending buffer already released"); |
| 218 | return; |
| 219 | } |
| 220 | ALOGV("Releasing pending buffer"); |
| 221 | Mutex::Autolock lock(mMutex); |
| 222 | status_t result = releaseBufferLocked(mPendingRelease.currentTexture, |
| 223 | mPendingRelease.graphicBuffer, mPendingRelease.display, |
| 224 | mPendingRelease.fence); |
| 225 | ALOGE_IF(result != NO_ERROR, "releasePendingBuffer failed: %s (%d)", |
| 226 | strerror(-result), result); |
| 227 | mPendingRelease = PendingRelease(); |
| 228 | } |
| 229 | #endif |
| 230 | |
Jesse Hall | 399184a | 2014-03-03 15:42:54 -0800 | [diff] [blame] | 231 | void SurfaceFlingerConsumer::setContentsChangedListener( |
| 232 | const wp<ContentsChangedListener>& listener) { |
| 233 | setFrameAvailableListener(listener); |
| 234 | Mutex::Autolock lock(mMutex); |
| 235 | mContentsChangedListener = listener; |
| 236 | } |
| 237 | |
Chia-I Wu | 06d63de | 2017-01-04 14:58:51 +0800 | [diff] [blame] | 238 | void SurfaceFlingerConsumer::onBuffersReleased() { |
| 239 | sp<ContentsChangedListener> listener; |
| 240 | { // scope for the lock |
| 241 | Mutex::Autolock lock(mMutex); |
| 242 | ALOG_ASSERT(mFrameAvailableListener.unsafe_get() == mContentsChangedListener.unsafe_get()); |
| 243 | listener = mContentsChangedListener.promote(); |
| 244 | } |
| 245 | |
| 246 | if (listener != NULL) { |
| 247 | listener->onBuffersReleased(); |
| 248 | } |
| 249 | } |
| 250 | |
Jesse Hall | 399184a | 2014-03-03 15:42:54 -0800 | [diff] [blame] | 251 | void SurfaceFlingerConsumer::onSidebandStreamChanged() { |
| 252 | sp<ContentsChangedListener> listener; |
| 253 | { // scope for the lock |
| 254 | Mutex::Autolock lock(mMutex); |
| 255 | ALOG_ASSERT(mFrameAvailableListener.unsafe_get() == mContentsChangedListener.unsafe_get()); |
| 256 | listener = mContentsChangedListener.promote(); |
| 257 | } |
| 258 | |
| 259 | if (listener != NULL) { |
| 260 | listener->onSidebandStreamChanged(); |
| 261 | } |
| 262 | } |
| 263 | |
Brian Anderson | 3890c39 | 2016-07-25 12:48:08 -0700 | [diff] [blame] | 264 | void SurfaceFlingerConsumer::addAndGetFrameTimestamps( |
Brian Anderson | d6927fb | 2016-07-23 23:37:30 -0700 | [diff] [blame] | 265 | const NewFrameEventsEntry* newTimestamps, |
Brian Anderson | 3890c39 | 2016-07-25 12:48:08 -0700 | [diff] [blame] | 266 | FrameEventHistoryDelta *outDelta) { |
Brian Anderson | d6927fb | 2016-07-23 23:37:30 -0700 | [diff] [blame] | 267 | sp<Layer> l = mLayer.promote(); |
Brian Anderson | 3890c39 | 2016-07-25 12:48:08 -0700 | [diff] [blame] | 268 | if (l.get()) { |
| 269 | l->addAndGetFrameTimestamps(newTimestamps, outDelta); |
Brian Anderson | d6927fb | 2016-07-23 23:37:30 -0700 | [diff] [blame] | 270 | } |
Pablo Ceballos | ce796e7 | 2016-02-04 19:10:51 -0800 | [diff] [blame] | 271 | } |
| 272 | |
Andy McFadden | bf974ab | 2012-12-04 16:51:15 -0800 | [diff] [blame] | 273 | // --------------------------------------------------------------------------- |
| 274 | }; // namespace android |
| 275 | |