blob: 9babeef87f7c02c8d4eac00cf171234657e2fff7 [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#ifdef USE_HWC2
93 err = updateAndReleaseLocked(item, &mPendingRelease);
94#else
Mathias Agopianad678e12013-07-23 17:28:53 -070095 err = updateAndReleaseLocked(item);
Dan Stoza9e56aa02015-11-02 13:00:03 -080096#endif
Andy McFaddenbf974ab2012-12-04 16:51:15 -080097 if (err != NO_ERROR) {
98 return err;
99 }
100
Mathias Agopianca088332013-03-28 17:44:13 -0700101 if (!SyncFeatures::getInstance().useNativeFenceSync()) {
Andy McFadden97eba892012-12-11 15:21:45 -0800102 // 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
114status_t SurfaceFlingerConsumer::bindTextureImage()
115{
116 Mutex::Autolock lock(mMutex);
117
118 return bindTextureImageLocked();
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800119}
120
Dan Stoza11611f92015-03-12 15:12:44 -0700121status_t SurfaceFlingerConsumer::acquireBufferLocked(BufferItem* item,
Dan Stozaa4650a52015-05-12 12:56:16 -0700122 nsecs_t presentWhen, uint64_t maxFrameNumber) {
123 status_t result = GLConsumer::acquireBufferLocked(item, presentWhen,
124 maxFrameNumber);
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700125 if (result == NO_ERROR) {
126 mTransformToDisplayInverse = item->mTransformToDisplayInverse;
Dan Stozaee44edd2015-03-23 15:50:23 -0700127 mSurfaceDamage = item->mSurfaceDamage;
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700128 }
129 return result;
130}
131
132bool SurfaceFlingerConsumer::getTransformToDisplayInverse() const {
Robert Carr367c5682016-06-20 11:55:28 -0700133 Mutex::Autolock lock(mMutex);
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700134 return mTransformToDisplayInverse;
135}
136
Dan Stozaee44edd2015-03-23 15:50:23 -0700137const Region& SurfaceFlingerConsumer::getSurfaceDamage() const {
138 return mSurfaceDamage;
139}
140
Jesse Hall399184a2014-03-03 15:42:54 -0800141sp<NativeHandle> SurfaceFlingerConsumer::getSidebandStream() const {
Dan Stoza0c9a1ed2017-04-06 15:10:21 -0700142 sp<NativeHandle> stream;
143 mConsumer->getSidebandStream(&stream);
144 return stream;
Jesse Hall399184a2014-03-03 15:42:54 -0800145}
146
Andy McFadden1585c4d2013-06-28 13:52:40 -0700147// We need to determine the time when a buffer acquired now will be
148// displayed. This can be calculated:
149// time when previous buffer's actual-present fence was signaled
150// + current display refresh rate * HWC latency
151// + a little extra padding
152//
153// Buffer producers are expected to set their desired presentation time
154// based on choreographer time stamps, which (coming from vsync events)
155// will be slightly later then the actual-present timing. If we get a
156// desired-present time that is unintentionally a hair after the next
157// vsync, we'll hold the frame when we really want to display it. We
Andy McFadden41d67d72014-04-25 16:58:34 -0700158// need to take the offset between actual-present and reported-vsync
159// into account.
160//
161// If the system is configured without a DispSync phase offset for the app,
162// we also want to throw in a bit of padding to avoid edge cases where we
163// just barely miss. We want to do it here, not in every app. A major
164// source of trouble is the app's use of the display's ideal refresh time
165// (via Display.getRefreshRate()), which could be off of the actual refresh
166// by a few percent, with the error multiplied by the number of frames
167// between now and when the buffer should be displayed.
168//
169// If the refresh reported to the app has a phase offset, we shouldn't need
170// to tweak anything here.
171nsecs_t SurfaceFlingerConsumer::computeExpectedPresent(const DispSync& dispSync)
Andy McFadden1585c4d2013-06-28 13:52:40 -0700172{
Andy McFadden1585c4d2013-06-28 13:52:40 -0700173 // The HWC doesn't currently have a way to report additional latency.
Andy McFadden41d67d72014-04-25 16:58:34 -0700174 // Assume that whatever we submit now will appear right after the flip.
175 // For a smart panel this might be 1. This is expressed in frames,
176 // rather than time, because we expect to have a constant frame delay
177 // regardless of the refresh rate.
178 const uint32_t hwcLatency = 0;
Andy McFadden1585c4d2013-06-28 13:52:40 -0700179
Andy McFadden41d67d72014-04-25 16:58:34 -0700180 // Ask DispSync when the next refresh will be (CLOCK_MONOTONIC).
181 const nsecs_t nextRefresh = dispSync.computeNextRefresh(hwcLatency);
Andy McFadden1585c4d2013-06-28 13:52:40 -0700182
Andy McFadden41d67d72014-04-25 16:58:34 -0700183 // The DispSync time is already adjusted for the difference between
Fabien Sanglardc45a7d92017-03-14 13:24:22 -0700184 // vsync and reported-vsync (SurfaceFlinger::dispSyncPresentTimeOffset), so
Andy McFadden41d67d72014-04-25 16:58:34 -0700185 // we don't need to factor that in here. Pad a little to avoid
186 // weird effects if apps might be requesting times right on the edge.
187 nsecs_t extraPadding = 0;
Fabien Sanglard0cc19382017-03-06 11:54:40 -0800188 if (SurfaceFlinger::vsyncPhaseOffsetNs == 0) {
Andy McFadden41d67d72014-04-25 16:58:34 -0700189 extraPadding = 1000000; // 1ms (6% of 60Hz)
190 }
191
192 return nextRefresh + extraPadding;
Andy McFadden1585c4d2013-06-28 13:52:40 -0700193}
194
Brian Anderson3546a3f2016-07-14 11:51:14 -0700195sp<Fence> SurfaceFlingerConsumer::getPrevFinalReleaseFence() const {
196 Mutex::Autolock lock(mMutex);
197 return ConsumerBase::mPrevFinalReleaseFence;
198}
199
Dan Stoza9e56aa02015-11-02 13:00:03 -0800200#ifdef USE_HWC2
201void SurfaceFlingerConsumer::setReleaseFence(const sp<Fence>& fence)
202{
203 if (!mPendingRelease.isPending) {
204 GLConsumer::setReleaseFence(fence);
205 return;
206 }
207 auto currentTexture = mPendingRelease.currentTexture;
208 if (fence->isValid() &&
209 currentTexture != BufferQueue::INVALID_BUFFER_SLOT) {
210 status_t result = addReleaseFence(currentTexture,
211 mPendingRelease.graphicBuffer, fence);
212 ALOGE_IF(result != NO_ERROR, "setReleaseFence: failed to add the"
213 " fence: %s (%d)", strerror(-result), result);
214 }
215}
216
Brian Anderson5ea5e592016-12-01 16:54:33 -0800217bool SurfaceFlingerConsumer::releasePendingBuffer()
Dan Stoza9e56aa02015-11-02 13:00:03 -0800218{
219 if (!mPendingRelease.isPending) {
220 ALOGV("Pending buffer already released");
Brian Anderson5ea5e592016-12-01 16:54:33 -0800221 return false;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800222 }
223 ALOGV("Releasing pending buffer");
224 Mutex::Autolock lock(mMutex);
225 status_t result = releaseBufferLocked(mPendingRelease.currentTexture,
226 mPendingRelease.graphicBuffer, mPendingRelease.display,
227 mPendingRelease.fence);
Fabien Sanglard3a156e12017-02-23 15:02:34 -0800228 ALOGE_IF(result < NO_ERROR, "releasePendingBuffer failed: %s (%d)",
Dan Stoza9e56aa02015-11-02 13:00:03 -0800229 strerror(-result), result);
230 mPendingRelease = PendingRelease();
Brian Anderson5ea5e592016-12-01 16:54:33 -0800231 return true;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800232}
233#endif
234
Jesse Hall399184a2014-03-03 15:42:54 -0800235void SurfaceFlingerConsumer::setContentsChangedListener(
236 const wp<ContentsChangedListener>& listener) {
237 setFrameAvailableListener(listener);
238 Mutex::Autolock lock(mMutex);
239 mContentsChangedListener = listener;
240}
241
242void SurfaceFlingerConsumer::onSidebandStreamChanged() {
243 sp<ContentsChangedListener> listener;
244 { // scope for the lock
245 Mutex::Autolock lock(mMutex);
246 ALOG_ASSERT(mFrameAvailableListener.unsafe_get() == mContentsChangedListener.unsafe_get());
247 listener = mContentsChangedListener.promote();
248 }
249
250 if (listener != NULL) {
251 listener->onSidebandStreamChanged();
252 }
253}
254
Brian Anderson5ea5e592016-12-01 16:54:33 -0800255void SurfaceFlingerConsumer::onDisconnect() {
256 sp<Layer> l = mLayer.promote();
257 if (l.get()) {
258 l->onDisconnect();
259 }
260}
261
Brian Anderson3890c392016-07-25 12:48:08 -0700262void SurfaceFlingerConsumer::addAndGetFrameTimestamps(
Brian Andersond6927fb2016-07-23 23:37:30 -0700263 const NewFrameEventsEntry* newTimestamps,
Brian Anderson3890c392016-07-25 12:48:08 -0700264 FrameEventHistoryDelta *outDelta) {
Brian Andersond6927fb2016-07-23 23:37:30 -0700265 sp<Layer> l = mLayer.promote();
Brian Anderson3890c392016-07-25 12:48:08 -0700266 if (l.get()) {
267 l->addAndGetFrameTimestamps(newTimestamps, outDelta);
Brian Andersond6927fb2016-07-23 23:37:30 -0700268 }
Pablo Ceballosce796e72016-02-04 19:10:51 -0800269}
270
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800271// ---------------------------------------------------------------------------
272}; // namespace android
273