blob: aa48c7dceeb496432b5610ea160dfa1147bbff5b [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 {
142 return mConsumer->getSidebandStream();
143}
144
Andy McFadden1585c4d2013-06-28 13:52:40 -0700145// We need to determine the time when a buffer acquired now will be
146// displayed. This can be calculated:
147// time when previous buffer's actual-present fence was signaled
148// + current display refresh rate * HWC latency
149// + a little extra padding
150//
151// Buffer producers are expected to set their desired presentation time
152// based on choreographer time stamps, which (coming from vsync events)
153// will be slightly later then the actual-present timing. If we get a
154// desired-present time that is unintentionally a hair after the next
155// vsync, we'll hold the frame when we really want to display it. We
Andy McFadden41d67d72014-04-25 16:58:34 -0700156// need to take the offset between actual-present and reported-vsync
157// into account.
158//
159// If the system is configured without a DispSync phase offset for the app,
160// we also want to throw in a bit of padding to avoid edge cases where we
161// just barely miss. We want to do it here, not in every app. A major
162// source of trouble is the app's use of the display's ideal refresh time
163// (via Display.getRefreshRate()), which could be off of the actual refresh
164// by a few percent, with the error multiplied by the number of frames
165// between now and when the buffer should be displayed.
166//
167// If the refresh reported to the app has a phase offset, we shouldn't need
168// to tweak anything here.
169nsecs_t SurfaceFlingerConsumer::computeExpectedPresent(const DispSync& dispSync)
Andy McFadden1585c4d2013-06-28 13:52:40 -0700170{
Andy McFadden1585c4d2013-06-28 13:52:40 -0700171 // The HWC doesn't currently have a way to report additional latency.
Andy McFadden41d67d72014-04-25 16:58:34 -0700172 // Assume that whatever we submit now will appear right after the flip.
173 // For a smart panel this might be 1. This is expressed in frames,
174 // rather than time, because we expect to have a constant frame delay
175 // regardless of the refresh rate.
176 const uint32_t hwcLatency = 0;
Andy McFadden1585c4d2013-06-28 13:52:40 -0700177
Andy McFadden41d67d72014-04-25 16:58:34 -0700178 // Ask DispSync when the next refresh will be (CLOCK_MONOTONIC).
179 const nsecs_t nextRefresh = dispSync.computeNextRefresh(hwcLatency);
Andy McFadden1585c4d2013-06-28 13:52:40 -0700180
Andy McFadden41d67d72014-04-25 16:58:34 -0700181 // The DispSync time is already adjusted for the difference between
182 // vsync and reported-vsync (PRESENT_TIME_OFFSET_FROM_VSYNC_NS), so
183 // we don't need to factor that in here. Pad a little to avoid
184 // weird effects if apps might be requesting times right on the edge.
185 nsecs_t extraPadding = 0;
186 if (VSYNC_EVENT_PHASE_OFFSET_NS == 0) {
187 extraPadding = 1000000; // 1ms (6% of 60Hz)
188 }
189
190 return nextRefresh + extraPadding;
Andy McFadden1585c4d2013-06-28 13:52:40 -0700191}
192
Brian Anderson3546a3f2016-07-14 11:51:14 -0700193sp<Fence> SurfaceFlingerConsumer::getPrevFinalReleaseFence() const {
194 Mutex::Autolock lock(mMutex);
195 return ConsumerBase::mPrevFinalReleaseFence;
196}
197
Dan Stoza9e56aa02015-11-02 13:00:03 -0800198#ifdef USE_HWC2
199void SurfaceFlingerConsumer::setReleaseFence(const sp<Fence>& fence)
200{
201 if (!mPendingRelease.isPending) {
202 GLConsumer::setReleaseFence(fence);
203 return;
204 }
205 auto currentTexture = mPendingRelease.currentTexture;
206 if (fence->isValid() &&
207 currentTexture != BufferQueue::INVALID_BUFFER_SLOT) {
208 status_t result = addReleaseFence(currentTexture,
209 mPendingRelease.graphicBuffer, fence);
210 ALOGE_IF(result != NO_ERROR, "setReleaseFence: failed to add the"
211 " fence: %s (%d)", strerror(-result), result);
212 }
213}
214
Brian Anderson5ea5e592016-12-01 16:54:33 -0800215bool SurfaceFlingerConsumer::releasePendingBuffer()
Dan Stoza9e56aa02015-11-02 13:00:03 -0800216{
217 if (!mPendingRelease.isPending) {
218 ALOGV("Pending buffer already released");
Brian Anderson5ea5e592016-12-01 16:54:33 -0800219 return false;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800220 }
221 ALOGV("Releasing pending buffer");
222 Mutex::Autolock lock(mMutex);
223 status_t result = releaseBufferLocked(mPendingRelease.currentTexture,
224 mPendingRelease.graphicBuffer, mPendingRelease.display,
225 mPendingRelease.fence);
Fabien Sanglard3a156e12017-02-23 15:02:34 -0800226 ALOGE_IF(result < NO_ERROR, "releasePendingBuffer failed: %s (%d)",
Dan Stoza9e56aa02015-11-02 13:00:03 -0800227 strerror(-result), result);
228 mPendingRelease = PendingRelease();
Brian Anderson5ea5e592016-12-01 16:54:33 -0800229 return true;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800230}
231#endif
232
Jesse Hall399184a2014-03-03 15:42:54 -0800233void SurfaceFlingerConsumer::setContentsChangedListener(
234 const wp<ContentsChangedListener>& listener) {
235 setFrameAvailableListener(listener);
236 Mutex::Autolock lock(mMutex);
237 mContentsChangedListener = listener;
238}
239
240void SurfaceFlingerConsumer::onSidebandStreamChanged() {
241 sp<ContentsChangedListener> listener;
242 { // scope for the lock
243 Mutex::Autolock lock(mMutex);
244 ALOG_ASSERT(mFrameAvailableListener.unsafe_get() == mContentsChangedListener.unsafe_get());
245 listener = mContentsChangedListener.promote();
246 }
247
248 if (listener != NULL) {
249 listener->onSidebandStreamChanged();
250 }
251}
252
Brian Anderson5ea5e592016-12-01 16:54:33 -0800253void SurfaceFlingerConsumer::onDisconnect() {
254 sp<Layer> l = mLayer.promote();
255 if (l.get()) {
256 l->onDisconnect();
257 }
258}
259
Brian Anderson3890c392016-07-25 12:48:08 -0700260void SurfaceFlingerConsumer::addAndGetFrameTimestamps(
Brian Andersond6927fb2016-07-23 23:37:30 -0700261 const NewFrameEventsEntry* newTimestamps,
Brian Anderson3890c392016-07-25 12:48:08 -0700262 FrameEventHistoryDelta *outDelta) {
Brian Andersond6927fb2016-07-23 23:37:30 -0700263 sp<Layer> l = mLayer.promote();
Brian Anderson3890c392016-07-25 12:48:08 -0700264 if (l.get()) {
265 l->addAndGetFrameTimestamps(newTimestamps, outDelta);
Brian Andersond6927fb2016-07-23 23:37:30 -0700266 }
Pablo Ceballosce796e72016-02-04 19:10:51 -0800267}
268
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800269// ---------------------------------------------------------------------------
270}; // namespace android
271