blob: ed1f31b55420ef3c46107006b61af4147ca1509b [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,
Dan Stozaa4650a52015-05-12 12:56:16 -070035 const DispSync& dispSync, uint64_t maxFrameNumber)
Andy McFaddenbf974ab2012-12-04 16:51:15 -080036{
37 ATRACE_CALL();
38 ALOGV("updateTexImage");
39 Mutex::Autolock lock(mMutex);
40
41 if (mAbandoned) {
Andy McFadden2adaf042012-12-18 09:49:45 -080042 ALOGE("updateTexImage: GLConsumer is abandoned!");
Andy McFaddenbf974ab2012-12-04 16:51:15 -080043 return NO_INIT;
44 }
45
46 // Make sure the EGL state is the same as in previous calls.
47 status_t err = checkAndUpdateEglStateLocked();
48 if (err != NO_ERROR) {
49 return err;
50 }
51
Dan Stoza11611f92015-03-12 15:12:44 -070052 BufferItem item;
Andy McFaddenbf974ab2012-12-04 16:51:15 -080053
54 // Acquire the next buffer.
55 // In asynchronous mode the list is guaranteed to be one buffer
56 // deep, while in synchronous mode we use the oldest buffer.
Dan Stozaa4650a52015-05-12 12:56:16 -070057 err = acquireBufferLocked(&item, computeExpectedPresent(dispSync),
58 maxFrameNumber);
Andy McFaddenbf974ab2012-12-04 16:51:15 -080059 if (err != NO_ERROR) {
60 if (err == BufferQueue::NO_BUFFER_AVAILABLE) {
Andy McFaddenbf974ab2012-12-04 16:51:15 -080061 err = NO_ERROR;
Andy McFadden1585c4d2013-06-28 13:52:40 -070062 } else if (err == BufferQueue::PRESENT_LATER) {
63 // return the error, without logging
Andy McFaddenbf974ab2012-12-04 16:51:15 -080064 } else {
65 ALOGE("updateTexImage: acquire failed: %s (%d)",
66 strerror(-err), err);
67 }
68 return err;
69 }
70
71
72 // 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
75 int buf = item.mBuf;
76 if (rejecter && rejecter->reject(mSlots[buf].mGraphicBuffer, item)) {
Lajos Molnar98d3d6e2013-06-27 11:51:25 -070077 releaseBufferLocked(buf, mSlots[buf].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
81 // Release the previous buffer.
Mathias Agopianad678e12013-07-23 17:28:53 -070082 err = updateAndReleaseLocked(item);
Andy McFaddenbf974ab2012-12-04 16:51:15 -080083 if (err != NO_ERROR) {
84 return err;
85 }
86
Mathias Agopianca088332013-03-28 17:44:13 -070087 if (!SyncFeatures::getInstance().useNativeFenceSync()) {
Andy McFadden97eba892012-12-11 15:21:45 -080088 // Bind the new buffer to the GL texture.
89 //
90 // Older devices require the "implicit" synchronization provided
91 // by glEGLImageTargetTexture2DOES, which this method calls. Newer
92 // devices will either call this in Layer::onDraw, or (if it's not
93 // a GL-composited layer) not at all.
94 err = bindTextureImageLocked();
95 }
96
97 return err;
98}
99
100status_t SurfaceFlingerConsumer::bindTextureImage()
101{
102 Mutex::Autolock lock(mMutex);
103
104 return bindTextureImageLocked();
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800105}
106
Dan Stoza11611f92015-03-12 15:12:44 -0700107status_t SurfaceFlingerConsumer::acquireBufferLocked(BufferItem* item,
Dan Stozaa4650a52015-05-12 12:56:16 -0700108 nsecs_t presentWhen, uint64_t maxFrameNumber) {
109 status_t result = GLConsumer::acquireBufferLocked(item, presentWhen,
110 maxFrameNumber);
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700111 if (result == NO_ERROR) {
112 mTransformToDisplayInverse = item->mTransformToDisplayInverse;
Dan Stozaee44edd2015-03-23 15:50:23 -0700113 mSurfaceDamage = item->mSurfaceDamage;
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700114 }
115 return result;
116}
117
118bool SurfaceFlingerConsumer::getTransformToDisplayInverse() const {
119 return mTransformToDisplayInverse;
120}
121
Dan Stozaee44edd2015-03-23 15:50:23 -0700122const Region& SurfaceFlingerConsumer::getSurfaceDamage() const {
123 return mSurfaceDamage;
124}
125
Jesse Hall399184a2014-03-03 15:42:54 -0800126sp<NativeHandle> SurfaceFlingerConsumer::getSidebandStream() const {
127 return mConsumer->getSidebandStream();
128}
129
Andy McFadden1585c4d2013-06-28 13:52:40 -0700130// We need to determine the time when a buffer acquired now will be
131// displayed. This can be calculated:
132// time when previous buffer's actual-present fence was signaled
133// + current display refresh rate * HWC latency
134// + a little extra padding
135//
136// Buffer producers are expected to set their desired presentation time
137// based on choreographer time stamps, which (coming from vsync events)
138// will be slightly later then the actual-present timing. If we get a
139// desired-present time that is unintentionally a hair after the next
140// vsync, we'll hold the frame when we really want to display it. We
Andy McFadden41d67d72014-04-25 16:58:34 -0700141// need to take the offset between actual-present and reported-vsync
142// into account.
143//
144// If the system is configured without a DispSync phase offset for the app,
145// we also want to throw in a bit of padding to avoid edge cases where we
146// just barely miss. We want to do it here, not in every app. A major
147// source of trouble is the app's use of the display's ideal refresh time
148// (via Display.getRefreshRate()), which could be off of the actual refresh
149// by a few percent, with the error multiplied by the number of frames
150// between now and when the buffer should be displayed.
151//
152// If the refresh reported to the app has a phase offset, we shouldn't need
153// to tweak anything here.
154nsecs_t SurfaceFlingerConsumer::computeExpectedPresent(const DispSync& dispSync)
Andy McFadden1585c4d2013-06-28 13:52:40 -0700155{
Andy McFadden1585c4d2013-06-28 13:52:40 -0700156 // The HWC doesn't currently have a way to report additional latency.
Andy McFadden41d67d72014-04-25 16:58:34 -0700157 // Assume that whatever we submit now will appear right after the flip.
158 // For a smart panel this might be 1. This is expressed in frames,
159 // rather than time, because we expect to have a constant frame delay
160 // regardless of the refresh rate.
161 const uint32_t hwcLatency = 0;
Andy McFadden1585c4d2013-06-28 13:52:40 -0700162
Andy McFadden41d67d72014-04-25 16:58:34 -0700163 // Ask DispSync when the next refresh will be (CLOCK_MONOTONIC).
164 const nsecs_t nextRefresh = dispSync.computeNextRefresh(hwcLatency);
Andy McFadden1585c4d2013-06-28 13:52:40 -0700165
Andy McFadden41d67d72014-04-25 16:58:34 -0700166 // The DispSync time is already adjusted for the difference between
167 // vsync and reported-vsync (PRESENT_TIME_OFFSET_FROM_VSYNC_NS), so
168 // we don't need to factor that in here. Pad a little to avoid
169 // weird effects if apps might be requesting times right on the edge.
170 nsecs_t extraPadding = 0;
171 if (VSYNC_EVENT_PHASE_OFFSET_NS == 0) {
172 extraPadding = 1000000; // 1ms (6% of 60Hz)
173 }
174
175 return nextRefresh + extraPadding;
Andy McFadden1585c4d2013-06-28 13:52:40 -0700176}
177
Jesse Hall399184a2014-03-03 15:42:54 -0800178void SurfaceFlingerConsumer::setContentsChangedListener(
179 const wp<ContentsChangedListener>& listener) {
180 setFrameAvailableListener(listener);
181 Mutex::Autolock lock(mMutex);
182 mContentsChangedListener = listener;
183}
184
185void SurfaceFlingerConsumer::onSidebandStreamChanged() {
186 sp<ContentsChangedListener> listener;
187 { // scope for the lock
188 Mutex::Autolock lock(mMutex);
189 ALOG_ASSERT(mFrameAvailableListener.unsafe_get() == mContentsChangedListener.unsafe_get());
190 listener = mContentsChangedListener.promote();
191 }
192
193 if (listener != NULL) {
194 listener->onSidebandStreamChanged();
195 }
196}
197
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800198// ---------------------------------------------------------------------------
199}; // namespace android
200