SurfaceFlingerConsumer.cpp revision 84493cd420d3d53a16ae7c745ed38afffb4e67f5
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 NO_ERROR; 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 } 112 return result; 113} 114 115bool SurfaceFlingerConsumer::getTransformToDisplayInverse() const { 116 return mTransformToDisplayInverse; 117} 118 119sp<NativeHandle> SurfaceFlingerConsumer::getSidebandStream() const { 120 return mConsumer->getSidebandStream(); 121} 122 123// We need to determine the time when a buffer acquired now will be 124// displayed. This can be calculated: 125// time when previous buffer's actual-present fence was signaled 126// + current display refresh rate * HWC latency 127// + a little extra padding 128// 129// Buffer producers are expected to set their desired presentation time 130// based on choreographer time stamps, which (coming from vsync events) 131// will be slightly later then the actual-present timing. If we get a 132// desired-present time that is unintentionally a hair after the next 133// vsync, we'll hold the frame when we really want to display it. We 134// need to take the offset between actual-present and reported-vsync 135// into account. 136// 137// If the system is configured without a DispSync phase offset for the app, 138// we also want to throw in a bit of padding to avoid edge cases where we 139// just barely miss. We want to do it here, not in every app. A major 140// source of trouble is the app's use of the display's ideal refresh time 141// (via Display.getRefreshRate()), which could be off of the actual refresh 142// by a few percent, with the error multiplied by the number of frames 143// between now and when the buffer should be displayed. 144// 145// If the refresh reported to the app has a phase offset, we shouldn't need 146// to tweak anything here. 147nsecs_t SurfaceFlingerConsumer::computeExpectedPresent(const DispSync& dispSync) 148{ 149 // The HWC doesn't currently have a way to report additional latency. 150 // Assume that whatever we submit now will appear right after the flip. 151 // For a smart panel this might be 1. This is expressed in frames, 152 // rather than time, because we expect to have a constant frame delay 153 // regardless of the refresh rate. 154 const uint32_t hwcLatency = 0; 155 156 // Ask DispSync when the next refresh will be (CLOCK_MONOTONIC). 157 const nsecs_t nextRefresh = dispSync.computeNextRefresh(hwcLatency); 158 159 // The DispSync time is already adjusted for the difference between 160 // vsync and reported-vsync (PRESENT_TIME_OFFSET_FROM_VSYNC_NS), so 161 // we don't need to factor that in here. Pad a little to avoid 162 // weird effects if apps might be requesting times right on the edge. 163 nsecs_t extraPadding = 0; 164 if (VSYNC_EVENT_PHASE_OFFSET_NS == 0) { 165 extraPadding = 1000000; // 1ms (6% of 60Hz) 166 } 167 168 return nextRefresh + extraPadding; 169} 170 171void SurfaceFlingerConsumer::setContentsChangedListener( 172 const wp<ContentsChangedListener>& listener) { 173 setFrameAvailableListener(listener); 174 Mutex::Autolock lock(mMutex); 175 mContentsChangedListener = listener; 176} 177 178void SurfaceFlingerConsumer::onSidebandStreamChanged() { 179 sp<ContentsChangedListener> listener; 180 { // scope for the lock 181 Mutex::Autolock lock(mMutex); 182 ALOG_ASSERT(mFrameAvailableListener.unsafe_get() == mContentsChangedListener.unsafe_get()); 183 listener = mContentsChangedListener.promote(); 184 } 185 186 if (listener != NULL) { 187 listener->onSidebandStreamChanged(); 188 } 189} 190 191// --------------------------------------------------------------------------- 192}; // namespace android 193 194