blob: ba0a527ebc5ca792f53bfa8d4d9f972283c7a7e6 [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>
26
Andy McFaddenbf974ab2012-12-04 16:51:15 -080027#include <utils/Errors.h>
Jesse Hall399184a2014-03-03 15:42:54 -080028#include <utils/NativeHandle.h>
29#include <utils/Trace.h>
Andy McFaddenbf974ab2012-12-04 16:51:15 -080030
Andy McFaddenbf974ab2012-12-04 16:51:15 -080031namespace android {
32
33// ---------------------------------------------------------------------------
34
Andy McFadden41d67d72014-04-25 16:58:34 -070035status_t SurfaceFlingerConsumer::updateTexImage(BufferRejecter* rejecter,
Pablo Ceballosff95aab2016-01-13 17:09:58 -080036 const DispSync& dispSync, bool* autoRefresh, bool* queuedBuffer,
Pablo Ceballos06312182015-10-07 16:32:12 -070037 uint64_t maxFrameNumber)
Andy McFaddenbf974ab2012-12-04 16:51:15 -080038{
39 ATRACE_CALL();
40 ALOGV("updateTexImage");
41 Mutex::Autolock lock(mMutex);
42
43 if (mAbandoned) {
Andy McFadden2adaf042012-12-18 09:49:45 -080044 ALOGE("updateTexImage: GLConsumer is abandoned!");
Andy McFaddenbf974ab2012-12-04 16:51:15 -080045 return NO_INIT;
46 }
47
48 // Make sure the EGL state is the same as in previous calls.
49 status_t err = checkAndUpdateEglStateLocked();
50 if (err != NO_ERROR) {
51 return err;
52 }
53
Dan Stoza11611f92015-03-12 15:12:44 -070054 BufferItem item;
Andy McFaddenbf974ab2012-12-04 16:51:15 -080055
56 // Acquire the next buffer.
57 // In asynchronous mode the list is guaranteed to be one buffer
58 // deep, while in synchronous mode we use the oldest buffer.
Dan Stozaa4650a52015-05-12 12:56:16 -070059 err = acquireBufferLocked(&item, computeExpectedPresent(dispSync),
60 maxFrameNumber);
Andy McFaddenbf974ab2012-12-04 16:51:15 -080061 if (err != NO_ERROR) {
62 if (err == BufferQueue::NO_BUFFER_AVAILABLE) {
Andy McFaddenbf974ab2012-12-04 16:51:15 -080063 err = NO_ERROR;
Andy McFadden1585c4d2013-06-28 13:52:40 -070064 } else if (err == BufferQueue::PRESENT_LATER) {
65 // return the error, without logging
Andy McFaddenbf974ab2012-12-04 16:51:15 -080066 } else {
67 ALOGE("updateTexImage: acquire failed: %s (%d)",
68 strerror(-err), err);
69 }
70 return err;
71 }
72
Andy McFaddenbf974ab2012-12-04 16:51:15 -080073 // We call the rejecter here, in case the caller has a reason to
74 // not accept this buffer. This is used by SurfaceFlinger to
75 // reject buffers which have the wrong size
Pablo Ceballos47650f42015-08-04 16:38:17 -070076 int slot = item.mSlot;
77 if (rejecter && rejecter->reject(mSlots[slot].mGraphicBuffer, item)) {
78 releaseBufferLocked(slot, mSlots[slot].mGraphicBuffer, EGL_NO_SYNC_KHR);
Dan Stozaecc50402015-04-28 14:42:06 -070079 return BUFFER_REJECTED;
Andy McFaddenbf974ab2012-12-04 16:51:15 -080080 }
81
Pablo Ceballosff95aab2016-01-13 17:09:58 -080082 if (autoRefresh) {
83 *autoRefresh = item.mAutoRefresh;
Pablo Ceballos06312182015-10-07 16:32:12 -070084 }
85
86 if (queuedBuffer) {
87 *queuedBuffer = item.mQueuedBuffer;
88 }
89
Andy McFaddenbf974ab2012-12-04 16:51:15 -080090 // Release the previous buffer.
Dan Stoza9e56aa02015-11-02 13:00:03 -080091#ifdef USE_HWC2
92 err = updateAndReleaseLocked(item, &mPendingRelease);
93#else
Mathias Agopianad678e12013-07-23 17:28:53 -070094 err = updateAndReleaseLocked(item);
Dan Stoza9e56aa02015-11-02 13:00:03 -080095#endif
Andy McFaddenbf974ab2012-12-04 16:51:15 -080096 if (err != NO_ERROR) {
97 return err;
98 }
99
Mathias Agopianca088332013-03-28 17:44:13 -0700100 if (!SyncFeatures::getInstance().useNativeFenceSync()) {
Andy McFadden97eba892012-12-11 15:21:45 -0800101 // Bind the new buffer to the GL texture.
102 //
103 // Older devices require the "implicit" synchronization provided
104 // by glEGLImageTargetTexture2DOES, which this method calls. Newer
105 // devices will either call this in Layer::onDraw, or (if it's not
106 // a GL-composited layer) not at all.
107 err = bindTextureImageLocked();
108 }
109
110 return err;
111}
112
113status_t SurfaceFlingerConsumer::bindTextureImage()
114{
115 Mutex::Autolock lock(mMutex);
116
117 return bindTextureImageLocked();
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800118}
119
Dan Stoza11611f92015-03-12 15:12:44 -0700120status_t SurfaceFlingerConsumer::acquireBufferLocked(BufferItem* item,
Dan Stozaa4650a52015-05-12 12:56:16 -0700121 nsecs_t presentWhen, uint64_t maxFrameNumber) {
122 status_t result = GLConsumer::acquireBufferLocked(item, presentWhen,
123 maxFrameNumber);
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700124 if (result == NO_ERROR) {
125 mTransformToDisplayInverse = item->mTransformToDisplayInverse;
Dan Stozaee44edd2015-03-23 15:50:23 -0700126 mSurfaceDamage = item->mSurfaceDamage;
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700127 }
128 return result;
129}
130
131bool SurfaceFlingerConsumer::getTransformToDisplayInverse() const {
132 return mTransformToDisplayInverse;
133}
134
Dan Stozaee44edd2015-03-23 15:50:23 -0700135const Region& SurfaceFlingerConsumer::getSurfaceDamage() const {
136 return mSurfaceDamage;
137}
138
Jesse Hall399184a2014-03-03 15:42:54 -0800139sp<NativeHandle> SurfaceFlingerConsumer::getSidebandStream() const {
140 return mConsumer->getSidebandStream();
141}
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
180 // vsync and reported-vsync (PRESENT_TIME_OFFSET_FROM_VSYNC_NS), so
181 // 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;
184 if (VSYNC_EVENT_PHASE_OFFSET_NS == 0) {
185 extraPadding = 1000000; // 1ms (6% of 60Hz)
186 }
187
188 return nextRefresh + extraPadding;
Andy McFadden1585c4d2013-06-28 13:52:40 -0700189}
190
Dan Stoza9e56aa02015-11-02 13:00:03 -0800191#ifdef USE_HWC2
192void SurfaceFlingerConsumer::setReleaseFence(const sp<Fence>& fence)
193{
Pablo Ceballos40845df2016-01-25 17:41:15 -0800194 mPrevReleaseFence = fence;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800195 if (!mPendingRelease.isPending) {
196 GLConsumer::setReleaseFence(fence);
197 return;
198 }
199 auto currentTexture = mPendingRelease.currentTexture;
200 if (fence->isValid() &&
201 currentTexture != BufferQueue::INVALID_BUFFER_SLOT) {
202 status_t result = addReleaseFence(currentTexture,
203 mPendingRelease.graphicBuffer, fence);
204 ALOGE_IF(result != NO_ERROR, "setReleaseFence: failed to add the"
205 " fence: %s (%d)", strerror(-result), result);
206 }
207}
208
209void SurfaceFlingerConsumer::releasePendingBuffer()
210{
211 if (!mPendingRelease.isPending) {
212 ALOGV("Pending buffer already released");
213 return;
214 }
215 ALOGV("Releasing pending buffer");
216 Mutex::Autolock lock(mMutex);
217 status_t result = releaseBufferLocked(mPendingRelease.currentTexture,
218 mPendingRelease.graphicBuffer, mPendingRelease.display,
219 mPendingRelease.fence);
220 ALOGE_IF(result != NO_ERROR, "releasePendingBuffer failed: %s (%d)",
221 strerror(-result), result);
222 mPendingRelease = PendingRelease();
223}
Pablo Ceballos40845df2016-01-25 17:41:15 -0800224#else
225void SurfaceFlingerConsumer::setReleaseFence(const sp<Fence>& fence) {
226 mPrevReleaseFence = fence;
227 GLConsumer::setReleaseFence(fence);
228}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800229#endif
230
Pablo Ceballos40845df2016-01-25 17:41:15 -0800231sp<Fence> SurfaceFlingerConsumer::getPrevReleaseFence() const {
232 return mPrevReleaseFence;
233}
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
Pablo Ceballosce796e72016-02-04 19:10:51 -0800255bool SurfaceFlingerConsumer::getFrameTimestamps(uint64_t frameNumber,
256 FrameTimestamps* outTimestamps) const {
257 sp<const Layer> l = mLayer.promote();
258 return l.get() ? l->getFrameTimestamps(frameNumber, outTimestamps) : false;
259}
260
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800261// ---------------------------------------------------------------------------
262}; // namespace android
263