blob: d5ba09d174720d76026a91c53b26ebf8c40c8515 [file] [log] [blame]
Andy McFaddenbf974ab2012-12-04 16:51:15 -08001/*
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 Gennis1df8c342012-12-20 14:05:45 -080018//#define LOG_NDEBUG 0
Andy McFaddenbf974ab2012-12-04 16:51:15 -080019
20#include "SurfaceFlingerConsumer.h"
Pablo Ceballosce796e72016-02-04 19:10:51 -080021#include "Layer.h"
Andy McFaddenbf974ab2012-12-04 16:51:15 -080022
Mathias Agopianca088332013-03-28 17:44:13 -070023#include <private/gui/SyncFeatures.h>
24
Dan Stoza11611f92015-03-12 15:12:44 -070025#include <gui/BufferItem.h>
Mathias Agopiana9347642017-02-13 16:42:28 -080026#include <gui/BufferQueue.h>
Dan Stoza11611f92015-03-12 15:12:44 -070027
Andy McFaddenbf974ab2012-12-04 16:51:15 -080028#include <utils/Errors.h>
Jesse Hall399184a2014-03-03 15:42:54 -080029#include <utils/NativeHandle.h>
30#include <utils/Trace.h>
Andy McFaddenbf974ab2012-12-04 16:51:15 -080031
Andy McFaddenbf974ab2012-12-04 16:51:15 -080032namespace android {
33
34// ---------------------------------------------------------------------------
35
Andy McFadden41d67d72014-04-25 16:58:34 -070036status_t SurfaceFlingerConsumer::updateTexImage(BufferRejecter* rejecter,
Pablo Ceballosff95aab2016-01-13 17:09:58 -080037 const DispSync& dispSync, bool* autoRefresh, bool* queuedBuffer,
Pablo Ceballos06312182015-10-07 16:32:12 -070038 uint64_t maxFrameNumber)
Andy McFaddenbf974ab2012-12-04 16:51:15 -080039{
40 ATRACE_CALL();
41 ALOGV("updateTexImage");
42 Mutex::Autolock lock(mMutex);
43
44 if (mAbandoned) {
Andy McFadden2adaf042012-12-18 09:49:45 -080045 ALOGE("updateTexImage: GLConsumer is abandoned!");
Andy McFaddenbf974ab2012-12-04 16:51:15 -080046 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 Stoza11611f92015-03-12 15:12:44 -070055 BufferItem item;
Andy McFaddenbf974ab2012-12-04 16:51:15 -080056
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 Stozaa4650a52015-05-12 12:56:16 -070060 err = acquireBufferLocked(&item, computeExpectedPresent(dispSync),
61 maxFrameNumber);
Andy McFaddenbf974ab2012-12-04 16:51:15 -080062 if (err != NO_ERROR) {
63 if (err == BufferQueue::NO_BUFFER_AVAILABLE) {
Andy McFaddenbf974ab2012-12-04 16:51:15 -080064 err = NO_ERROR;
Andy McFadden1585c4d2013-06-28 13:52:40 -070065 } else if (err == BufferQueue::PRESENT_LATER) {
66 // return the error, without logging
Andy McFaddenbf974ab2012-12-04 16:51:15 -080067 } else {
68 ALOGE("updateTexImage: acquire failed: %s (%d)",
69 strerror(-err), err);
70 }
71 return err;
72 }
73
Fabien Sanglard0a4b26e2016-10-14 18:13:33 -070074 if (autoRefresh) {
75 *autoRefresh = item.mAutoRefresh;
76 }
77
78 if (queuedBuffer) {
79 *queuedBuffer = item.mQueuedBuffer;
80 }
81
Andy McFaddenbf974ab2012-12-04 16:51:15 -080082 // 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 Ceballos47650f42015-08-04 16:38:17 -070085 int slot = item.mSlot;
86 if (rejecter && rejecter->reject(mSlots[slot].mGraphicBuffer, item)) {
87 releaseBufferLocked(slot, mSlots[slot].mGraphicBuffer, EGL_NO_SYNC_KHR);
Dan Stozaecc50402015-04-28 14:42:06 -070088 return BUFFER_REJECTED;
Andy McFaddenbf974ab2012-12-04 16:51:15 -080089 }
90
91 // Release the previous buffer.
Dan Stoza9e56aa02015-11-02 13:00:03 -080092 err = updateAndReleaseLocked(item, &mPendingRelease);
Andy McFaddenbf974ab2012-12-04 16:51:15 -080093 if (err != NO_ERROR) {
94 return err;
95 }
96
Mathias Agopianca088332013-03-28 17:44:13 -070097 if (!SyncFeatures::getInstance().useNativeFenceSync()) {
Andy McFadden97eba892012-12-11 15:21:45 -080098 // Bind the new buffer to the GL texture.
99 //
100 // Older devices require the "implicit" synchronization provided
101 // by glEGLImageTargetTexture2DOES, which this method calls. Newer
102 // devices will either call this in Layer::onDraw, or (if it's not
103 // a GL-composited layer) not at all.
104 err = bindTextureImageLocked();
105 }
106
107 return err;
108}
109
110status_t SurfaceFlingerConsumer::bindTextureImage()
111{
112 Mutex::Autolock lock(mMutex);
113
114 return bindTextureImageLocked();
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800115}
116
Dan Stoza11611f92015-03-12 15:12:44 -0700117status_t SurfaceFlingerConsumer::acquireBufferLocked(BufferItem* item,
Dan Stozaa4650a52015-05-12 12:56:16 -0700118 nsecs_t presentWhen, uint64_t maxFrameNumber) {
119 status_t result = GLConsumer::acquireBufferLocked(item, presentWhen,
120 maxFrameNumber);
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700121 if (result == NO_ERROR) {
122 mTransformToDisplayInverse = item->mTransformToDisplayInverse;
Dan Stozaee44edd2015-03-23 15:50:23 -0700123 mSurfaceDamage = item->mSurfaceDamage;
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700124 }
125 return result;
126}
127
128bool SurfaceFlingerConsumer::getTransformToDisplayInverse() const {
Robert Carr367c5682016-06-20 11:55:28 -0700129 Mutex::Autolock lock(mMutex);
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700130 return mTransformToDisplayInverse;
131}
132
Dan Stozaee44edd2015-03-23 15:50:23 -0700133const Region& SurfaceFlingerConsumer::getSurfaceDamage() const {
134 return mSurfaceDamage;
135}
136
Andy McFadden1585c4d2013-06-28 13:52:40 -0700137// We need to determine the time when a buffer acquired now will be
138// displayed. This can be calculated:
139// time when previous buffer's actual-present fence was signaled
140// + current display refresh rate * HWC latency
141// + a little extra padding
142//
143// Buffer producers are expected to set their desired presentation time
144// based on choreographer time stamps, which (coming from vsync events)
145// will be slightly later then the actual-present timing. If we get a
146// desired-present time that is unintentionally a hair after the next
147// vsync, we'll hold the frame when we really want to display it. We
Andy McFadden41d67d72014-04-25 16:58:34 -0700148// need to take the offset between actual-present and reported-vsync
149// into account.
150//
151// If the system is configured without a DispSync phase offset for the app,
152// we also want to throw in a bit of padding to avoid edge cases where we
153// just barely miss. We want to do it here, not in every app. A major
154// source of trouble is the app's use of the display's ideal refresh time
155// (via Display.getRefreshRate()), which could be off of the actual refresh
156// by a few percent, with the error multiplied by the number of frames
157// between now and when the buffer should be displayed.
158//
159// If the refresh reported to the app has a phase offset, we shouldn't need
160// to tweak anything here.
161nsecs_t SurfaceFlingerConsumer::computeExpectedPresent(const DispSync& dispSync)
Andy McFadden1585c4d2013-06-28 13:52:40 -0700162{
Andy McFadden1585c4d2013-06-28 13:52:40 -0700163 // The HWC doesn't currently have a way to report additional latency.
Andy McFadden41d67d72014-04-25 16:58:34 -0700164 // Assume that whatever we submit now will appear right after the flip.
165 // For a smart panel this might be 1. This is expressed in frames,
166 // rather than time, because we expect to have a constant frame delay
167 // regardless of the refresh rate.
168 const uint32_t hwcLatency = 0;
Andy McFadden1585c4d2013-06-28 13:52:40 -0700169
Andy McFadden41d67d72014-04-25 16:58:34 -0700170 // Ask DispSync when the next refresh will be (CLOCK_MONOTONIC).
171 const nsecs_t nextRefresh = dispSync.computeNextRefresh(hwcLatency);
Andy McFadden1585c4d2013-06-28 13:52:40 -0700172
Andy McFadden41d67d72014-04-25 16:58:34 -0700173 // The DispSync time is already adjusted for the difference between
Fabien Sanglardc45a7d92017-03-14 13:24:22 -0700174 // vsync and reported-vsync (SurfaceFlinger::dispSyncPresentTimeOffset), so
Andy McFadden41d67d72014-04-25 16:58:34 -0700175 // we don't need to factor that in here. Pad a little to avoid
176 // weird effects if apps might be requesting times right on the edge.
177 nsecs_t extraPadding = 0;
Fabien Sanglard0cc19382017-03-06 11:54:40 -0800178 if (SurfaceFlinger::vsyncPhaseOffsetNs == 0) {
Andy McFadden41d67d72014-04-25 16:58:34 -0700179 extraPadding = 1000000; // 1ms (6% of 60Hz)
180 }
181
182 return nextRefresh + extraPadding;
Andy McFadden1585c4d2013-06-28 13:52:40 -0700183}
184
Brian Anderson3546a3f2016-07-14 11:51:14 -0700185sp<Fence> SurfaceFlingerConsumer::getPrevFinalReleaseFence() const {
186 Mutex::Autolock lock(mMutex);
187 return ConsumerBase::mPrevFinalReleaseFence;
188}
189
Dan Stoza9e56aa02015-11-02 13:00:03 -0800190void SurfaceFlingerConsumer::setReleaseFence(const sp<Fence>& fence)
191{
192 if (!mPendingRelease.isPending) {
193 GLConsumer::setReleaseFence(fence);
194 return;
195 }
196 auto currentTexture = mPendingRelease.currentTexture;
197 if (fence->isValid() &&
198 currentTexture != BufferQueue::INVALID_BUFFER_SLOT) {
199 status_t result = addReleaseFence(currentTexture,
200 mPendingRelease.graphicBuffer, fence);
201 ALOGE_IF(result != NO_ERROR, "setReleaseFence: failed to add the"
202 " fence: %s (%d)", strerror(-result), result);
203 }
204}
205
Brian Anderson5ea5e592016-12-01 16:54:33 -0800206bool SurfaceFlingerConsumer::releasePendingBuffer()
Dan Stoza9e56aa02015-11-02 13:00:03 -0800207{
208 if (!mPendingRelease.isPending) {
209 ALOGV("Pending buffer already released");
Brian Anderson5ea5e592016-12-01 16:54:33 -0800210 return false;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800211 }
212 ALOGV("Releasing pending buffer");
213 Mutex::Autolock lock(mMutex);
214 status_t result = releaseBufferLocked(mPendingRelease.currentTexture,
215 mPendingRelease.graphicBuffer, mPendingRelease.display,
216 mPendingRelease.fence);
Fabien Sanglard3a156e12017-02-23 15:02:34 -0800217 ALOGE_IF(result < NO_ERROR, "releasePendingBuffer failed: %s (%d)",
Dan Stoza9e56aa02015-11-02 13:00:03 -0800218 strerror(-result), result);
219 mPendingRelease = PendingRelease();
Brian Anderson5ea5e592016-12-01 16:54:33 -0800220 return true;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800221}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800222
Jesse Hall399184a2014-03-03 15:42:54 -0800223void SurfaceFlingerConsumer::setContentsChangedListener(
224 const wp<ContentsChangedListener>& listener) {
225 setFrameAvailableListener(listener);
226 Mutex::Autolock lock(mMutex);
227 mContentsChangedListener = listener;
228}
229
230void SurfaceFlingerConsumer::onSidebandStreamChanged() {
Dan Stoza95971c82017-06-26 14:27:18 -0700231 FrameAvailableListener* unsafeFrameAvailableListener = nullptr;
232 {
233 Mutex::Autolock lock(mFrameAvailableMutex);
234 unsafeFrameAvailableListener = mFrameAvailableListener.unsafe_get();
235 }
Jesse Hall399184a2014-03-03 15:42:54 -0800236 sp<ContentsChangedListener> listener;
237 { // scope for the lock
238 Mutex::Autolock lock(mMutex);
Dan Stoza95971c82017-06-26 14:27:18 -0700239 ALOG_ASSERT(unsafeFrameAvailableListener == mContentsChangedListener.unsafe_get());
Jesse Hall399184a2014-03-03 15:42:54 -0800240 listener = mContentsChangedListener.promote();
241 }
242
243 if (listener != NULL) {
244 listener->onSidebandStreamChanged();
245 }
246}
247
Brian Anderson5ea5e592016-12-01 16:54:33 -0800248void SurfaceFlingerConsumer::onDisconnect() {
249 sp<Layer> l = mLayer.promote();
250 if (l.get()) {
251 l->onDisconnect();
252 }
253}
254
Brian Anderson3890c392016-07-25 12:48:08 -0700255void SurfaceFlingerConsumer::addAndGetFrameTimestamps(
Brian Andersond6927fb2016-07-23 23:37:30 -0700256 const NewFrameEventsEntry* newTimestamps,
Brian Anderson3890c392016-07-25 12:48:08 -0700257 FrameEventHistoryDelta *outDelta) {
Brian Andersond6927fb2016-07-23 23:37:30 -0700258 sp<Layer> l = mLayer.promote();
Brian Anderson3890c392016-07-25 12:48:08 -0700259 if (l.get()) {
260 l->addAndGetFrameTimestamps(newTimestamps, outDelta);
Brian Andersond6927fb2016-07-23 23:37:30 -0700261 }
Pablo Ceballosce796e72016-02-04 19:10:51 -0800262}
263
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800264// ---------------------------------------------------------------------------
265}; // namespace android
266