blob: 4c80fa063ae51b15b332e6ff52c309a50154485f [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"
21
Mathias Agopianca088332013-03-28 17:44:13 -070022#include <private/gui/SyncFeatures.h>
23
Dan Stoza11611f92015-03-12 15:12:44 -070024#include <gui/BufferItem.h>
25
Andy McFaddenbf974ab2012-12-04 16:51:15 -080026#include <utils/Errors.h>
Jesse Hall399184a2014-03-03 15:42:54 -080027#include <utils/NativeHandle.h>
28#include <utils/Trace.h>
Andy McFaddenbf974ab2012-12-04 16:51:15 -080029
Andy McFaddenbf974ab2012-12-04 16:51:15 -080030namespace android {
31
32// ---------------------------------------------------------------------------
33
Andy McFadden41d67d72014-04-25 16:58:34 -070034status_t SurfaceFlingerConsumer::updateTexImage(BufferRejecter* rejecter,
Pablo Ceballosff95aab2016-01-13 17:09:58 -080035 const DispSync& dispSync, bool* autoRefresh, bool* queuedBuffer,
Pablo Ceballos06312182015-10-07 16:32:12 -070036 uint64_t maxFrameNumber)
Andy McFaddenbf974ab2012-12-04 16:51:15 -080037{
38 ATRACE_CALL();
39 ALOGV("updateTexImage");
40 Mutex::Autolock lock(mMutex);
41
42 if (mAbandoned) {
Andy McFadden2adaf042012-12-18 09:49:45 -080043 ALOGE("updateTexImage: GLConsumer is abandoned!");
Andy McFaddenbf974ab2012-12-04 16:51:15 -080044 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 Stoza11611f92015-03-12 15:12:44 -070053 BufferItem item;
Andy McFaddenbf974ab2012-12-04 16:51:15 -080054
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 Stozaa4650a52015-05-12 12:56:16 -070058 err = acquireBufferLocked(&item, computeExpectedPresent(dispSync),
59 maxFrameNumber);
Andy McFaddenbf974ab2012-12-04 16:51:15 -080060 if (err != NO_ERROR) {
61 if (err == BufferQueue::NO_BUFFER_AVAILABLE) {
Andy McFaddenbf974ab2012-12-04 16:51:15 -080062 err = NO_ERROR;
Andy McFadden1585c4d2013-06-28 13:52:40 -070063 } else if (err == BufferQueue::PRESENT_LATER) {
64 // return the error, without logging
Andy McFaddenbf974ab2012-12-04 16:51:15 -080065 } else {
66 ALOGE("updateTexImage: acquire failed: %s (%d)",
67 strerror(-err), err);
68 }
69 return err;
70 }
71
Andy McFaddenbf974ab2012-12-04 16:51:15 -080072 // 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 Ceballos47650f42015-08-04 16:38:17 -070075 int slot = item.mSlot;
76 if (rejecter && rejecter->reject(mSlots[slot].mGraphicBuffer, item)) {
77 releaseBufferLocked(slot, mSlots[slot].mGraphicBuffer, EGL_NO_SYNC_KHR);
Dan Stozaecc50402015-04-28 14:42:06 -070078 return BUFFER_REJECTED;
Andy McFaddenbf974ab2012-12-04 16:51:15 -080079 }
80
Pablo Ceballosff95aab2016-01-13 17:09:58 -080081 if (autoRefresh) {
82 *autoRefresh = item.mAutoRefresh;
Pablo Ceballos06312182015-10-07 16:32:12 -070083 }
84
85 if (queuedBuffer) {
86 *queuedBuffer = item.mQueuedBuffer;
87 }
88
Andy McFaddenbf974ab2012-12-04 16:51:15 -080089 // Release the previous buffer.
Dan Stoza9e56aa02015-11-02 13:00:03 -080090#ifdef USE_HWC2
91 err = updateAndReleaseLocked(item, &mPendingRelease);
92#else
Mathias Agopianad678e12013-07-23 17:28:53 -070093 err = updateAndReleaseLocked(item);
Dan Stoza9e56aa02015-11-02 13:00:03 -080094#endif
Andy McFaddenbf974ab2012-12-04 16:51:15 -080095 if (err != NO_ERROR) {
96 return err;
97 }
98
Mathias Agopianca088332013-03-28 17:44:13 -070099 if (!SyncFeatures::getInstance().useNativeFenceSync()) {
Andy McFadden97eba892012-12-11 15:21:45 -0800100 // Bind the new buffer to the GL texture.
101 //
102 // Older devices require the "implicit" synchronization provided
103 // by glEGLImageTargetTexture2DOES, which this method calls. Newer
104 // devices will either call this in Layer::onDraw, or (if it's not
105 // a GL-composited layer) not at all.
106 err = bindTextureImageLocked();
107 }
108
109 return err;
110}
111
112status_t SurfaceFlingerConsumer::bindTextureImage()
113{
114 Mutex::Autolock lock(mMutex);
115
116 return bindTextureImageLocked();
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800117}
118
Dan Stoza11611f92015-03-12 15:12:44 -0700119status_t SurfaceFlingerConsumer::acquireBufferLocked(BufferItem* item,
Dan Stozaa4650a52015-05-12 12:56:16 -0700120 nsecs_t presentWhen, uint64_t maxFrameNumber) {
121 status_t result = GLConsumer::acquireBufferLocked(item, presentWhen,
122 maxFrameNumber);
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700123 if (result == NO_ERROR) {
124 mTransformToDisplayInverse = item->mTransformToDisplayInverse;
Dan Stozaee44edd2015-03-23 15:50:23 -0700125 mSurfaceDamage = item->mSurfaceDamage;
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700126 }
127 return result;
128}
129
130bool SurfaceFlingerConsumer::getTransformToDisplayInverse() const {
131 return mTransformToDisplayInverse;
132}
133
Dan Stozaee44edd2015-03-23 15:50:23 -0700134const Region& SurfaceFlingerConsumer::getSurfaceDamage() const {
135 return mSurfaceDamage;
136}
137
Jesse Hall399184a2014-03-03 15:42:54 -0800138sp<NativeHandle> SurfaceFlingerConsumer::getSidebandStream() const {
139 return mConsumer->getSidebandStream();
140}
141
Andy McFadden1585c4d2013-06-28 13:52:40 -0700142// We need to determine the time when a buffer acquired now will be
143// displayed. This can be calculated:
144// time when previous buffer's actual-present fence was signaled
145// + current display refresh rate * HWC latency
146// + a little extra padding
147//
148// Buffer producers are expected to set their desired presentation time
149// based on choreographer time stamps, which (coming from vsync events)
150// will be slightly later then the actual-present timing. If we get a
151// desired-present time that is unintentionally a hair after the next
152// vsync, we'll hold the frame when we really want to display it. We
Andy McFadden41d67d72014-04-25 16:58:34 -0700153// need to take the offset between actual-present and reported-vsync
154// into account.
155//
156// If the system is configured without a DispSync phase offset for the app,
157// we also want to throw in a bit of padding to avoid edge cases where we
158// just barely miss. We want to do it here, not in every app. A major
159// source of trouble is the app's use of the display's ideal refresh time
160// (via Display.getRefreshRate()), which could be off of the actual refresh
161// by a few percent, with the error multiplied by the number of frames
162// between now and when the buffer should be displayed.
163//
164// If the refresh reported to the app has a phase offset, we shouldn't need
165// to tweak anything here.
166nsecs_t SurfaceFlingerConsumer::computeExpectedPresent(const DispSync& dispSync)
Andy McFadden1585c4d2013-06-28 13:52:40 -0700167{
Andy McFadden1585c4d2013-06-28 13:52:40 -0700168 // The HWC doesn't currently have a way to report additional latency.
Andy McFadden41d67d72014-04-25 16:58:34 -0700169 // Assume that whatever we submit now will appear right after the flip.
170 // For a smart panel this might be 1. This is expressed in frames,
171 // rather than time, because we expect to have a constant frame delay
172 // regardless of the refresh rate.
173 const uint32_t hwcLatency = 0;
Andy McFadden1585c4d2013-06-28 13:52:40 -0700174
Andy McFadden41d67d72014-04-25 16:58:34 -0700175 // Ask DispSync when the next refresh will be (CLOCK_MONOTONIC).
176 const nsecs_t nextRefresh = dispSync.computeNextRefresh(hwcLatency);
Andy McFadden1585c4d2013-06-28 13:52:40 -0700177
Andy McFadden41d67d72014-04-25 16:58:34 -0700178 // The DispSync time is already adjusted for the difference between
179 // vsync and reported-vsync (PRESENT_TIME_OFFSET_FROM_VSYNC_NS), so
180 // we don't need to factor that in here. Pad a little to avoid
181 // weird effects if apps might be requesting times right on the edge.
182 nsecs_t extraPadding = 0;
183 if (VSYNC_EVENT_PHASE_OFFSET_NS == 0) {
184 extraPadding = 1000000; // 1ms (6% of 60Hz)
185 }
186
187 return nextRefresh + extraPadding;
Andy McFadden1585c4d2013-06-28 13:52:40 -0700188}
189
Dan Stoza9e56aa02015-11-02 13:00:03 -0800190#ifdef USE_HWC2
191void SurfaceFlingerConsumer::setReleaseFence(const sp<Fence>& fence)
192{
193 if (!mPendingRelease.isPending) {
194 GLConsumer::setReleaseFence(fence);
195 return;
196 }
197 auto currentTexture = mPendingRelease.currentTexture;
198 if (fence->isValid() &&
199 currentTexture != BufferQueue::INVALID_BUFFER_SLOT) {
200 status_t result = addReleaseFence(currentTexture,
201 mPendingRelease.graphicBuffer, fence);
202 ALOGE_IF(result != NO_ERROR, "setReleaseFence: failed to add the"
203 " fence: %s (%d)", strerror(-result), result);
204 }
205}
206
207void SurfaceFlingerConsumer::releasePendingBuffer()
208{
209 if (!mPendingRelease.isPending) {
210 ALOGV("Pending buffer already released");
211 return;
212 }
213 ALOGV("Releasing pending buffer");
214 Mutex::Autolock lock(mMutex);
215 status_t result = releaseBufferLocked(mPendingRelease.currentTexture,
216 mPendingRelease.graphicBuffer, mPendingRelease.display,
217 mPendingRelease.fence);
218 ALOGE_IF(result != NO_ERROR, "releasePendingBuffer failed: %s (%d)",
219 strerror(-result), result);
220 mPendingRelease = PendingRelease();
221}
222#endif
223
Jesse Hall399184a2014-03-03 15:42:54 -0800224void SurfaceFlingerConsumer::setContentsChangedListener(
225 const wp<ContentsChangedListener>& listener) {
226 setFrameAvailableListener(listener);
227 Mutex::Autolock lock(mMutex);
228 mContentsChangedListener = listener;
229}
230
231void SurfaceFlingerConsumer::onSidebandStreamChanged() {
232 sp<ContentsChangedListener> listener;
233 { // scope for the lock
234 Mutex::Autolock lock(mMutex);
235 ALOG_ASSERT(mFrameAvailableListener.unsafe_get() == mContentsChangedListener.unsafe_get());
236 listener = mContentsChangedListener.promote();
237 }
238
239 if (listener != NULL) {
240 listener->onSidebandStreamChanged();
241 }
242}
243
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800244// ---------------------------------------------------------------------------
245}; // namespace android
246