blob: 19c497a5d7ac8ec80bc814c59a945a23990b7825 [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,
35 const DispSync& dispSync)
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.
Andy McFadden41d67d72014-04-25 16:58:34 -070057 err = acquireBufferLocked(&item, computeExpectedPresent(dispSync));
Andy McFaddenbf974ab2012-12-04 16:51:15 -080058 if (err != NO_ERROR) {
59 if (err == BufferQueue::NO_BUFFER_AVAILABLE) {
Andy McFaddenbf974ab2012-12-04 16:51:15 -080060 err = NO_ERROR;
Andy McFadden1585c4d2013-06-28 13:52:40 -070061 } else if (err == BufferQueue::PRESENT_LATER) {
62 // return the error, without logging
Andy McFaddenbf974ab2012-12-04 16:51:15 -080063 } else {
64 ALOGE("updateTexImage: acquire failed: %s (%d)",
65 strerror(-err), err);
66 }
67 return err;
68 }
69
70
71 // We call the rejecter here, in case the caller has a reason to
72 // not accept this buffer. This is used by SurfaceFlinger to
73 // reject buffers which have the wrong size
74 int buf = item.mBuf;
75 if (rejecter && rejecter->reject(mSlots[buf].mGraphicBuffer, item)) {
Lajos Molnar98d3d6e2013-06-27 11:51:25 -070076 releaseBufferLocked(buf, mSlots[buf].mGraphicBuffer, EGL_NO_SYNC_KHR);
Andy McFaddenbf974ab2012-12-04 16:51:15 -080077 return NO_ERROR;
78 }
79
80 // Release the previous buffer.
Mathias Agopianad678e12013-07-23 17:28:53 -070081 err = updateAndReleaseLocked(item);
Andy McFaddenbf974ab2012-12-04 16:51:15 -080082 if (err != NO_ERROR) {
83 return err;
84 }
85
Mathias Agopianca088332013-03-28 17:44:13 -070086 if (!SyncFeatures::getInstance().useNativeFenceSync()) {
Andy McFadden97eba892012-12-11 15:21:45 -080087 // Bind the new buffer to the GL texture.
88 //
89 // Older devices require the "implicit" synchronization provided
90 // by glEGLImageTargetTexture2DOES, which this method calls. Newer
91 // devices will either call this in Layer::onDraw, or (if it's not
92 // a GL-composited layer) not at all.
93 err = bindTextureImageLocked();
94 }
95
96 return err;
97}
98
99status_t SurfaceFlingerConsumer::bindTextureImage()
100{
101 Mutex::Autolock lock(mMutex);
102
103 return bindTextureImageLocked();
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800104}
105
Dan Stoza11611f92015-03-12 15:12:44 -0700106status_t SurfaceFlingerConsumer::acquireBufferLocked(BufferItem* item,
107 nsecs_t presentWhen) {
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700108 status_t result = GLConsumer::acquireBufferLocked(item, presentWhen);
109 if (result == NO_ERROR) {
110 mTransformToDisplayInverse = item->mTransformToDisplayInverse;
Dan Stozaee44edd2015-03-23 15:50:23 -0700111 mSurfaceDamage = item->mSurfaceDamage;
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700112 }
113 return result;
114}
115
116bool SurfaceFlingerConsumer::getTransformToDisplayInverse() const {
117 return mTransformToDisplayInverse;
118}
119
Dan Stozaee44edd2015-03-23 15:50:23 -0700120const Region& SurfaceFlingerConsumer::getSurfaceDamage() const {
121 return mSurfaceDamage;
122}
123
Jesse Hall399184a2014-03-03 15:42:54 -0800124sp<NativeHandle> SurfaceFlingerConsumer::getSidebandStream() const {
125 return mConsumer->getSidebandStream();
126}
127
Andy McFadden1585c4d2013-06-28 13:52:40 -0700128// We need to determine the time when a buffer acquired now will be
129// displayed. This can be calculated:
130// time when previous buffer's actual-present fence was signaled
131// + current display refresh rate * HWC latency
132// + a little extra padding
133//
134// Buffer producers are expected to set their desired presentation time
135// based on choreographer time stamps, which (coming from vsync events)
136// will be slightly later then the actual-present timing. If we get a
137// desired-present time that is unintentionally a hair after the next
138// vsync, we'll hold the frame when we really want to display it. We
Andy McFadden41d67d72014-04-25 16:58:34 -0700139// need to take the offset between actual-present and reported-vsync
140// into account.
141//
142// If the system is configured without a DispSync phase offset for the app,
143// we also want to throw in a bit of padding to avoid edge cases where we
144// just barely miss. We want to do it here, not in every app. A major
145// source of trouble is the app's use of the display's ideal refresh time
146// (via Display.getRefreshRate()), which could be off of the actual refresh
147// by a few percent, with the error multiplied by the number of frames
148// between now and when the buffer should be displayed.
149//
150// If the refresh reported to the app has a phase offset, we shouldn't need
151// to tweak anything here.
152nsecs_t SurfaceFlingerConsumer::computeExpectedPresent(const DispSync& dispSync)
Andy McFadden1585c4d2013-06-28 13:52:40 -0700153{
Andy McFadden1585c4d2013-06-28 13:52:40 -0700154 // The HWC doesn't currently have a way to report additional latency.
Andy McFadden41d67d72014-04-25 16:58:34 -0700155 // Assume that whatever we submit now will appear right after the flip.
156 // For a smart panel this might be 1. This is expressed in frames,
157 // rather than time, because we expect to have a constant frame delay
158 // regardless of the refresh rate.
159 const uint32_t hwcLatency = 0;
Andy McFadden1585c4d2013-06-28 13:52:40 -0700160
Andy McFadden41d67d72014-04-25 16:58:34 -0700161 // Ask DispSync when the next refresh will be (CLOCK_MONOTONIC).
162 const nsecs_t nextRefresh = dispSync.computeNextRefresh(hwcLatency);
Andy McFadden1585c4d2013-06-28 13:52:40 -0700163
Andy McFadden41d67d72014-04-25 16:58:34 -0700164 // The DispSync time is already adjusted for the difference between
165 // vsync and reported-vsync (PRESENT_TIME_OFFSET_FROM_VSYNC_NS), so
166 // we don't need to factor that in here. Pad a little to avoid
167 // weird effects if apps might be requesting times right on the edge.
168 nsecs_t extraPadding = 0;
169 if (VSYNC_EVENT_PHASE_OFFSET_NS == 0) {
170 extraPadding = 1000000; // 1ms (6% of 60Hz)
171 }
172
173 return nextRefresh + extraPadding;
Andy McFadden1585c4d2013-06-28 13:52:40 -0700174}
175
Jesse Hall399184a2014-03-03 15:42:54 -0800176void SurfaceFlingerConsumer::setContentsChangedListener(
177 const wp<ContentsChangedListener>& listener) {
178 setFrameAvailableListener(listener);
179 Mutex::Autolock lock(mMutex);
180 mContentsChangedListener = listener;
181}
182
183void SurfaceFlingerConsumer::onSidebandStreamChanged() {
184 sp<ContentsChangedListener> listener;
185 { // scope for the lock
186 Mutex::Autolock lock(mMutex);
187 ALOG_ASSERT(mFrameAvailableListener.unsafe_get() == mContentsChangedListener.unsafe_get());
188 listener = mContentsChangedListener.promote();
189 }
190
191 if (listener != NULL) {
192 listener->onSidebandStreamChanged();
193 }
194}
195
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800196// ---------------------------------------------------------------------------
197}; // namespace android
198