blob: a412543ef80845e527b70654a54d8c2b4151ed9c [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
Andy McFaddenbf974ab2012-12-04 16:51:15 -080024#include <utils/Errors.h>
Jesse Hall399184a2014-03-03 15:42:54 -080025#include <utils/NativeHandle.h>
26#include <utils/Trace.h>
Andy McFaddenbf974ab2012-12-04 16:51:15 -080027
Andy McFaddenbf974ab2012-12-04 16:51:15 -080028namespace android {
29
30// ---------------------------------------------------------------------------
31
32status_t SurfaceFlingerConsumer::updateTexImage(BufferRejecter* rejecter)
33{
34 ATRACE_CALL();
35 ALOGV("updateTexImage");
36 Mutex::Autolock lock(mMutex);
37
38 if (mAbandoned) {
Andy McFadden2adaf042012-12-18 09:49:45 -080039 ALOGE("updateTexImage: GLConsumer is abandoned!");
Andy McFaddenbf974ab2012-12-04 16:51:15 -080040 return NO_INIT;
41 }
42
43 // Make sure the EGL state is the same as in previous calls.
44 status_t err = checkAndUpdateEglStateLocked();
45 if (err != NO_ERROR) {
46 return err;
47 }
48
49 BufferQueue::BufferItem item;
50
51 // Acquire the next buffer.
52 // In asynchronous mode the list is guaranteed to be one buffer
53 // deep, while in synchronous mode we use the oldest buffer.
Andy McFadden1585c4d2013-06-28 13:52:40 -070054 err = acquireBufferLocked(&item, computeExpectedPresent());
Andy McFaddenbf974ab2012-12-04 16:51:15 -080055 if (err != NO_ERROR) {
56 if (err == BufferQueue::NO_BUFFER_AVAILABLE) {
Andy McFaddenbf974ab2012-12-04 16:51:15 -080057 err = NO_ERROR;
Andy McFadden1585c4d2013-06-28 13:52:40 -070058 } else if (err == BufferQueue::PRESENT_LATER) {
59 // return the error, without logging
Andy McFaddenbf974ab2012-12-04 16:51:15 -080060 } else {
61 ALOGE("updateTexImage: acquire failed: %s (%d)",
62 strerror(-err), err);
63 }
64 return err;
65 }
66
67
68 // We call the rejecter here, in case the caller has a reason to
69 // not accept this buffer. This is used by SurfaceFlinger to
70 // reject buffers which have the wrong size
71 int buf = item.mBuf;
72 if (rejecter && rejecter->reject(mSlots[buf].mGraphicBuffer, item)) {
Lajos Molnar98d3d6e2013-06-27 11:51:25 -070073 releaseBufferLocked(buf, mSlots[buf].mGraphicBuffer, EGL_NO_SYNC_KHR);
Andy McFaddenbf974ab2012-12-04 16:51:15 -080074 return NO_ERROR;
75 }
76
77 // Release the previous buffer.
Mathias Agopianad678e12013-07-23 17:28:53 -070078 err = updateAndReleaseLocked(item);
Andy McFaddenbf974ab2012-12-04 16:51:15 -080079 if (err != NO_ERROR) {
80 return err;
81 }
82
Mathias Agopianca088332013-03-28 17:44:13 -070083 if (!SyncFeatures::getInstance().useNativeFenceSync()) {
Andy McFadden97eba892012-12-11 15:21:45 -080084 // Bind the new buffer to the GL texture.
85 //
86 // Older devices require the "implicit" synchronization provided
87 // by glEGLImageTargetTexture2DOES, which this method calls. Newer
88 // devices will either call this in Layer::onDraw, or (if it's not
89 // a GL-composited layer) not at all.
90 err = bindTextureImageLocked();
91 }
92
93 return err;
94}
95
96status_t SurfaceFlingerConsumer::bindTextureImage()
97{
98 Mutex::Autolock lock(mMutex);
99
100 return bindTextureImageLocked();
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800101}
102
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700103status_t SurfaceFlingerConsumer::acquireBufferLocked(
104 BufferQueue::BufferItem *item, nsecs_t presentWhen) {
105 status_t result = GLConsumer::acquireBufferLocked(item, presentWhen);
106 if (result == NO_ERROR) {
107 mTransformToDisplayInverse = item->mTransformToDisplayInverse;
108 }
109 return result;
110}
111
112bool SurfaceFlingerConsumer::getTransformToDisplayInverse() const {
113 return mTransformToDisplayInverse;
114}
115
Jesse Hall399184a2014-03-03 15:42:54 -0800116sp<NativeHandle> SurfaceFlingerConsumer::getSidebandStream() const {
117 return mConsumer->getSidebandStream();
118}
119
Andy McFadden1585c4d2013-06-28 13:52:40 -0700120// We need to determine the time when a buffer acquired now will be
121// displayed. This can be calculated:
122// time when previous buffer's actual-present fence was signaled
123// + current display refresh rate * HWC latency
124// + a little extra padding
125//
126// Buffer producers are expected to set their desired presentation time
127// based on choreographer time stamps, which (coming from vsync events)
128// will be slightly later then the actual-present timing. If we get a
129// desired-present time that is unintentionally a hair after the next
130// vsync, we'll hold the frame when we really want to display it. We
131// want to use an expected-presentation time that is slightly late to
132// avoid this sort of edge case.
133nsecs_t SurfaceFlingerConsumer::computeExpectedPresent()
134{
135 // Don't yet have an easy way to get actual buffer flip time for
136 // the specific display, so use the current time. This is typically
137 // 1.3ms past the vsync event time.
138 const nsecs_t prevVsync = systemTime(CLOCK_MONOTONIC);
139
140 // Given a SurfaceFlinger reference, and information about what display
141 // we're destined for, we could query the HWC for the refresh rate. This
142 // could change over time, e.g. we could switch to 24fps for a movie.
143 // For now, assume 60fps.
144 //const nsecs_t vsyncPeriod =
145 // getHwComposer().getRefreshPeriod(HWC_DISPLAY_PRIMARY);
146 const nsecs_t vsyncPeriod = 16700000;
147
148 // The HWC doesn't currently have a way to report additional latency.
149 // Assume that whatever we submit now will appear on the next flip,
150 // i.e. 1 frame of latency w.r.t. the previous flip.
151 const uint32_t hwcLatency = 1;
152
153 // A little extra padding to compensate for slack between actual vsync
154 // time and vsync event receipt. Currently not needed since we're
155 // using "now" instead of a vsync time.
156 const nsecs_t extraPadding = 0;
157
158 // Total it up.
159 return prevVsync + hwcLatency * vsyncPeriod + extraPadding;
160}
161
Jesse Hall399184a2014-03-03 15:42:54 -0800162void SurfaceFlingerConsumer::setContentsChangedListener(
163 const wp<ContentsChangedListener>& listener) {
164 setFrameAvailableListener(listener);
165 Mutex::Autolock lock(mMutex);
166 mContentsChangedListener = listener;
167}
168
169void SurfaceFlingerConsumer::onSidebandStreamChanged() {
170 sp<ContentsChangedListener> listener;
171 { // scope for the lock
172 Mutex::Autolock lock(mMutex);
173 ALOG_ASSERT(mFrameAvailableListener.unsafe_get() == mContentsChangedListener.unsafe_get());
174 listener = mContentsChangedListener.promote();
175 }
176
177 if (listener != NULL) {
178 listener->onSidebandStreamChanged();
179 }
180}
181
Andy McFaddenbf974ab2012-12-04 16:51:15 -0800182// ---------------------------------------------------------------------------
183}; // namespace android
184