SurfaceFlingerConsumer.cpp revision ecc504043fddb7a75042ce402c67aedfac04d5e2
1/*
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
18//#define LOG_NDEBUG 0
19
20#include "SurfaceFlingerConsumer.h"
21
22#include <private/gui/SyncFeatures.h>
23
24#include <gui/BufferItem.h>
25
26#include <utils/Errors.h>
27#include <utils/NativeHandle.h>
28#include <utils/Trace.h>
29
30namespace android {
31
32// ---------------------------------------------------------------------------
33
34status_t SurfaceFlingerConsumer::updateTexImage(BufferRejecter* rejecter,
35        const DispSync& dispSync)
36{
37    ATRACE_CALL();
38    ALOGV("updateTexImage");
39    Mutex::Autolock lock(mMutex);
40
41    if (mAbandoned) {
42        ALOGE("updateTexImage: GLConsumer is abandoned!");
43        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
52    BufferItem item;
53
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.
57    err = acquireBufferLocked(&item, computeExpectedPresent(dispSync));
58    if (err != NO_ERROR) {
59        if (err == BufferQueue::NO_BUFFER_AVAILABLE) {
60            err = NO_ERROR;
61        } else if (err == BufferQueue::PRESENT_LATER) {
62            // return the error, without logging
63        } 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)) {
76        releaseBufferLocked(buf, mSlots[buf].mGraphicBuffer, EGL_NO_SYNC_KHR);
77        return BUFFER_REJECTED;
78    }
79
80    // Release the previous buffer.
81    err = updateAndReleaseLocked(item);
82    if (err != NO_ERROR) {
83        return err;
84    }
85
86    if (!SyncFeatures::getInstance().useNativeFenceSync()) {
87        // 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();
104}
105
106status_t SurfaceFlingerConsumer::acquireBufferLocked(BufferItem* item,
107        nsecs_t presentWhen) {
108    status_t result = GLConsumer::acquireBufferLocked(item, presentWhen);
109    if (result == NO_ERROR) {
110        mTransformToDisplayInverse = item->mTransformToDisplayInverse;
111        mSurfaceDamage = item->mSurfaceDamage;
112    }
113    return result;
114}
115
116bool SurfaceFlingerConsumer::getTransformToDisplayInverse() const {
117    return mTransformToDisplayInverse;
118}
119
120const Region& SurfaceFlingerConsumer::getSurfaceDamage() const {
121    return mSurfaceDamage;
122}
123
124sp<NativeHandle> SurfaceFlingerConsumer::getSidebandStream() const {
125    return mConsumer->getSidebandStream();
126}
127
128void SurfaceFlingerConsumer::setShadowQueueSize(size_t size) {
129    mConsumer->setShadowQueueSize(size);
130}
131
132// We need to determine the time when a buffer acquired now will be
133// displayed.  This can be calculated:
134//   time when previous buffer's actual-present fence was signaled
135//    + current display refresh rate * HWC latency
136//    + a little extra padding
137//
138// Buffer producers are expected to set their desired presentation time
139// based on choreographer time stamps, which (coming from vsync events)
140// will be slightly later then the actual-present timing.  If we get a
141// desired-present time that is unintentionally a hair after the next
142// vsync, we'll hold the frame when we really want to display it.  We
143// need to take the offset between actual-present and reported-vsync
144// into account.
145//
146// If the system is configured without a DispSync phase offset for the app,
147// we also want to throw in a bit of padding to avoid edge cases where we
148// just barely miss.  We want to do it here, not in every app.  A major
149// source of trouble is the app's use of the display's ideal refresh time
150// (via Display.getRefreshRate()), which could be off of the actual refresh
151// by a few percent, with the error multiplied by the number of frames
152// between now and when the buffer should be displayed.
153//
154// If the refresh reported to the app has a phase offset, we shouldn't need
155// to tweak anything here.
156nsecs_t SurfaceFlingerConsumer::computeExpectedPresent(const DispSync& dispSync)
157{
158    // The HWC doesn't currently have a way to report additional latency.
159    // Assume that whatever we submit now will appear right after the flip.
160    // For a smart panel this might be 1.  This is expressed in frames,
161    // rather than time, because we expect to have a constant frame delay
162    // regardless of the refresh rate.
163    const uint32_t hwcLatency = 0;
164
165    // Ask DispSync when the next refresh will be (CLOCK_MONOTONIC).
166    const nsecs_t nextRefresh = dispSync.computeNextRefresh(hwcLatency);
167
168    // The DispSync time is already adjusted for the difference between
169    // vsync and reported-vsync (PRESENT_TIME_OFFSET_FROM_VSYNC_NS), so
170    // we don't need to factor that in here.  Pad a little to avoid
171    // weird effects if apps might be requesting times right on the edge.
172    nsecs_t extraPadding = 0;
173    if (VSYNC_EVENT_PHASE_OFFSET_NS == 0) {
174        extraPadding = 1000000;        // 1ms (6% of 60Hz)
175    }
176
177    return nextRefresh + extraPadding;
178}
179
180void SurfaceFlingerConsumer::setContentsChangedListener(
181        const wp<ContentsChangedListener>& listener) {
182    setFrameAvailableListener(listener);
183    Mutex::Autolock lock(mMutex);
184    mContentsChangedListener = listener;
185}
186
187void SurfaceFlingerConsumer::onSidebandStreamChanged() {
188    sp<ContentsChangedListener> listener;
189    {   // scope for the lock
190        Mutex::Autolock lock(mMutex);
191        ALOG_ASSERT(mFrameAvailableListener.unsafe_get() == mContentsChangedListener.unsafe_get());
192        listener = mContentsChangedListener.promote();
193    }
194
195    if (listener != NULL) {
196        listener->onSidebandStreamChanged();
197    }
198}
199
200// ---------------------------------------------------------------------------
201}; // namespace android
202
203