SurfaceFlingerConsumer.cpp revision 1585c4d9fbbba3ba70ae625923b85cd02cb8a0fd
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 <utils/Trace.h>
25#include <utils/Errors.h>
26
27namespace android {
28
29// ---------------------------------------------------------------------------
30
31status_t SurfaceFlingerConsumer::updateTexImage(BufferRejecter* rejecter)
32{
33    ATRACE_CALL();
34    ALOGV("updateTexImage");
35    Mutex::Autolock lock(mMutex);
36
37    if (mAbandoned) {
38        ALOGE("updateTexImage: GLConsumer is abandoned!");
39        return NO_INIT;
40    }
41
42    // Make sure the EGL state is the same as in previous calls.
43    status_t err = checkAndUpdateEglStateLocked();
44    if (err != NO_ERROR) {
45        return err;
46    }
47
48    BufferQueue::BufferItem item;
49
50    // Acquire the next buffer.
51    // In asynchronous mode the list is guaranteed to be one buffer
52    // deep, while in synchronous mode we use the oldest buffer.
53    err = acquireBufferLocked(&item, computeExpectedPresent());
54    if (err != NO_ERROR) {
55        if (err == BufferQueue::NO_BUFFER_AVAILABLE) {
56            // This variant of updateTexImage does not guarantee that the
57            // texture is bound, so no need to call glBindTexture.
58            err = NO_ERROR;
59        } else if (err == BufferQueue::PRESENT_LATER) {
60            // return the error, without logging
61        } else {
62            ALOGE("updateTexImage: acquire failed: %s (%d)",
63                strerror(-err), err);
64        }
65        return err;
66    }
67
68
69    // We call the rejecter here, in case the caller has a reason to
70    // not accept this buffer.  This is used by SurfaceFlinger to
71    // reject buffers which have the wrong size
72    int buf = item.mBuf;
73    if (rejecter && rejecter->reject(mSlots[buf].mGraphicBuffer, item)) {
74        releaseBufferLocked(buf, mSlots[buf].mGraphicBuffer, EGL_NO_SYNC_KHR);
75        return NO_ERROR;
76    }
77
78    // Release the previous buffer.
79    err = releaseAndUpdateLocked(item);
80    if (err != NO_ERROR) {
81        return err;
82    }
83
84    if (!SyncFeatures::getInstance().useNativeFenceSync()) {
85        // Bind the new buffer to the GL texture.
86        //
87        // Older devices require the "implicit" synchronization provided
88        // by glEGLImageTargetTexture2DOES, which this method calls.  Newer
89        // devices will either call this in Layer::onDraw, or (if it's not
90        // a GL-composited layer) not at all.
91        err = bindTextureImageLocked();
92    }
93
94    return err;
95}
96
97status_t SurfaceFlingerConsumer::bindTextureImage()
98{
99    Mutex::Autolock lock(mMutex);
100
101    return bindTextureImageLocked();
102}
103
104// We need to determine the time when a buffer acquired now will be
105// displayed.  This can be calculated:
106//   time when previous buffer's actual-present fence was signaled
107//    + current display refresh rate * HWC latency
108//    + a little extra padding
109//
110// Buffer producers are expected to set their desired presentation time
111// based on choreographer time stamps, which (coming from vsync events)
112// will be slightly later then the actual-present timing.  If we get a
113// desired-present time that is unintentionally a hair after the next
114// vsync, we'll hold the frame when we really want to display it.  We
115// want to use an expected-presentation time that is slightly late to
116// avoid this sort of edge case.
117nsecs_t SurfaceFlingerConsumer::computeExpectedPresent()
118{
119    // Don't yet have an easy way to get actual buffer flip time for
120    // the specific display, so use the current time.  This is typically
121    // 1.3ms past the vsync event time.
122    const nsecs_t prevVsync = systemTime(CLOCK_MONOTONIC);
123
124    // Given a SurfaceFlinger reference, and information about what display
125    // we're destined for, we could query the HWC for the refresh rate.  This
126    // could change over time, e.g. we could switch to 24fps for a movie.
127    // For now, assume 60fps.
128    //const nsecs_t vsyncPeriod =
129    //        getHwComposer().getRefreshPeriod(HWC_DISPLAY_PRIMARY);
130    const nsecs_t vsyncPeriod = 16700000;
131
132    // The HWC doesn't currently have a way to report additional latency.
133    // Assume that whatever we submit now will appear on the next flip,
134    // i.e. 1 frame of latency w.r.t. the previous flip.
135    const uint32_t hwcLatency = 1;
136
137    // A little extra padding to compensate for slack between actual vsync
138    // time and vsync event receipt.  Currently not needed since we're
139    // using "now" instead of a vsync time.
140    const nsecs_t extraPadding = 0;
141
142    // Total it up.
143    return prevVsync + hwcLatency * vsyncPeriod + extraPadding;
144}
145
146// ---------------------------------------------------------------------------
147}; // namespace android
148
149