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