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