blob: e17bb58b31585c50607072a97a17a0d31122f624 [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
Jesse Hall399184a2014-03-03 15:42:54 -0800137sp<NativeHandle> SurfaceFlingerConsumer::getSidebandStream() const {
Dan Stoza0c9a1ed2017-04-06 15:10:21 -0700138 sp<NativeHandle> stream;
139 mConsumer->getSidebandStream(&stream);
140 return stream;
Jesse Hall399184a2014-03-03 15:42:54 -0800141}
142
Andy McFadden1585c4d2013-06-28 13:52:40 -0700143// We need to determine the time when a buffer acquired now will be
144// displayed. This can be calculated:
145// time when previous buffer's actual-present fence was signaled
146// + current display refresh rate * HWC latency
147// + a little extra padding
148//
149// Buffer producers are expected to set their desired presentation time
150// based on choreographer time stamps, which (coming from vsync events)
151// will be slightly later then the actual-present timing. If we get a
152// desired-present time that is unintentionally a hair after the next
153// vsync, we'll hold the frame when we really want to display it. We
Andy McFadden41d67d72014-04-25 16:58:34 -0700154// need to take the offset between actual-present and reported-vsync
155// into account.
156//
157// If the system is configured without a DispSync phase offset for the app,
158// we also want to throw in a bit of padding to avoid edge cases where we
159// just barely miss. We want to do it here, not in every app. A major
160// source of trouble is the app's use of the display's ideal refresh time
161// (via Display.getRefreshRate()), which could be off of the actual refresh
162// by a few percent, with the error multiplied by the number of frames
163// between now and when the buffer should be displayed.
164//
165// If the refresh reported to the app has a phase offset, we shouldn't need
166// to tweak anything here.
167nsecs_t SurfaceFlingerConsumer::computeExpectedPresent(const DispSync& dispSync)
Andy McFadden1585c4d2013-06-28 13:52:40 -0700168{
Andy McFadden1585c4d2013-06-28 13:52:40 -0700169 // The HWC doesn't currently have a way to report additional latency.
Andy McFadden41d67d72014-04-25 16:58:34 -0700170 // Assume that whatever we submit now will appear right after the flip.
171 // For a smart panel this might be 1. This is expressed in frames,
172 // rather than time, because we expect to have a constant frame delay
173 // regardless of the refresh rate.
174 const uint32_t hwcLatency = 0;
Andy McFadden1585c4d2013-06-28 13:52:40 -0700175
Andy McFadden41d67d72014-04-25 16:58:34 -0700176 // Ask DispSync when the next refresh will be (CLOCK_MONOTONIC).
177 const nsecs_t nextRefresh = dispSync.computeNextRefresh(hwcLatency);
Andy McFadden1585c4d2013-06-28 13:52:40 -0700178
Andy McFadden41d67d72014-04-25 16:58:34 -0700179 // The DispSync time is already adjusted for the difference between
Fabien Sanglardc45a7d92017-03-14 13:24:22 -0700180 // vsync and reported-vsync (SurfaceFlinger::dispSyncPresentTimeOffset), so
Andy McFadden41d67d72014-04-25 16:58:34 -0700181 // we don't need to factor that in here. Pad a little to avoid
182 // weird effects if apps might be requesting times right on the edge.
183 nsecs_t extraPadding = 0;
Fabien Sanglard0cc19382017-03-06 11:54:40 -0800184 if (SurfaceFlinger::vsyncPhaseOffsetNs == 0) {
Andy McFadden41d67d72014-04-25 16:58:34 -0700185 extraPadding = 1000000; // 1ms (6% of 60Hz)
186 }
187
188 return nextRefresh + extraPadding;
Andy McFadden1585c4d2013-06-28 13:52:40 -0700189}
190
Brian Anderson3546a3f2016-07-14 11:51:14 -0700191sp<Fence> SurfaceFlingerConsumer::getPrevFinalReleaseFence() const {
192 Mutex::Autolock lock(mMutex);
193 return ConsumerBase::mPrevFinalReleaseFence;
194}
195
Dan Stoza9e56aa02015-11-02 13:00:03 -0800196void SurfaceFlingerConsumer::setReleaseFence(const sp<Fence>& fence)
197{
198 if (!mPendingRelease.isPending) {
199 GLConsumer::setReleaseFence(fence);
200 return;
201 }
202 auto currentTexture = mPendingRelease.currentTexture;
203 if (fence->isValid() &&
204 currentTexture != BufferQueue::INVALID_BUFFER_SLOT) {
205 status_t result = addReleaseFence(currentTexture,
206 mPendingRelease.graphicBuffer, fence);
207 ALOGE_IF(result != NO_ERROR, "setReleaseFence: failed to add the"
208 " fence: %s (%d)", strerror(-result), result);
209 }
210}
211
Brian Anderson5ea5e592016-12-01 16:54:33 -0800212bool SurfaceFlingerConsumer::releasePendingBuffer()
Dan Stoza9e56aa02015-11-02 13:00:03 -0800213{
214 if (!mPendingRelease.isPending) {
215 ALOGV("Pending buffer already released");
Brian Anderson5ea5e592016-12-01 16:54:33 -0800216 return false;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800217 }
218 ALOGV("Releasing pending buffer");
219 Mutex::Autolock lock(mMutex);
220 status_t result = releaseBufferLocked(mPendingRelease.currentTexture,
221 mPendingRelease.graphicBuffer, mPendingRelease.display,
222 mPendingRelease.fence);
Fabien Sanglard3a156e12017-02-23 15:02:34 -0800223 ALOGE_IF(result < NO_ERROR, "releasePendingBuffer failed: %s (%d)",
Dan Stoza9e56aa02015-11-02 13:00:03 -0800224 strerror(-result), result);
225 mPendingRelease = PendingRelease();
Brian Anderson5ea5e592016-12-01 16:54:33 -0800226 return true;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800227}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800228
Jesse Hall399184a2014-03-03 15:42:54 -0800229void SurfaceFlingerConsumer::setContentsChangedListener(
230 const wp<ContentsChangedListener>& listener) {
231 setFrameAvailableListener(listener);
232 Mutex::Autolock lock(mMutex);
233 mContentsChangedListener = listener;
234}
235
236void SurfaceFlingerConsumer::onSidebandStreamChanged() {
Dan Stoza95971c82017-06-26 14:27:18 -0700237 FrameAvailableListener* unsafeFrameAvailableListener = nullptr;
238 {
239 Mutex::Autolock lock(mFrameAvailableMutex);
240 unsafeFrameAvailableListener = mFrameAvailableListener.unsafe_get();
241 }
Jesse Hall399184a2014-03-03 15:42:54 -0800242 sp<ContentsChangedListener> listener;
243 { // scope for the lock
244 Mutex::Autolock lock(mMutex);
Dan Stoza95971c82017-06-26 14:27:18 -0700245 ALOG_ASSERT(unsafeFrameAvailableListener == mContentsChangedListener.unsafe_get());
Jesse Hall399184a2014-03-03 15:42:54 -0800246 listener = mContentsChangedListener.promote();
247 }
248
249 if (listener != NULL) {
250 listener->onSidebandStreamChanged();
251 }
252}
253
Brian Anderson5ea5e592016-12-01 16:54:33 -0800254void SurfaceFlingerConsumer::onDisconnect() {
255 sp<Layer> l = mLayer.promote();
256 if (l.get()) {
257 l->onDisconnect();
258 }
259}
260
Brian Anderson3890c392016-07-25 12:48:08 -0700261void SurfaceFlingerConsumer::addAndGetFrameTimestamps(
Brian Andersond6927fb2016-07-23 23:37:30 -0700262 const NewFrameEventsEntry* newTimestamps,
Brian Anderson3890c392016-07-25 12:48:08 -0700263 FrameEventHistoryDelta *outDelta) {
Brian Andersond6927fb2016-07-23 23:37:30 -0700264 sp<Layer> l = mLayer.promote();
Brian Anderson3890c392016-07-25 12:48:08 -0700265 if (l.get()) {
266 l->addAndGetFrameTimestamps(newTimestamps, outDelta);
Brian Andersond6927fb2016-07-23 23:37:30 -0700267 }
Pablo Ceballosce796e72016-02-04 19:10:51 -0800268}
269
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800270// ---------------------------------------------------------------------------
271}; // namespace android
272