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