The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007 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 | |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 17 | //#define LOG_NDEBUG 0 |
| 18 | #undef LOG_TAG |
| 19 | #define LOG_TAG "Layer" |
Jamie Gennis | 1c8e95c | 2012-02-23 19:27:23 -0800 | [diff] [blame] | 20 | #define ATRACE_TAG ATRACE_TAG_GRAPHICS |
| 21 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 22 | #include <stdlib.h> |
| 23 | #include <stdint.h> |
| 24 | #include <sys/types.h> |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 25 | #include <math.h> |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 26 | |
Mathias Agopian | a67932f | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 27 | #include <cutils/compiler.h> |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 28 | #include <cutils/native_handle.h> |
Mathias Agopian | a67932f | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 29 | #include <cutils/properties.h> |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 30 | |
| 31 | #include <utils/Errors.h> |
| 32 | #include <utils/Log.h> |
Jesse Hall | 399184a | 2014-03-03 15:42:54 -0800 | [diff] [blame] | 33 | #include <utils/NativeHandle.h> |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 34 | #include <utils/StopWatch.h> |
Jamie Gennis | 1c8e95c | 2012-02-23 19:27:23 -0800 | [diff] [blame] | 35 | #include <utils/Trace.h> |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 36 | |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 37 | #include <ui/GraphicBuffer.h> |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 38 | #include <ui/PixelFormat.h> |
Mathias Agopian | 9cce325 | 2010-02-09 17:46:37 -0800 | [diff] [blame] | 39 | |
Dan Stoza | 6b9454d | 2014-11-07 16:00:59 -0800 | [diff] [blame] | 40 | #include <gui/BufferItem.h> |
Mathias Agopian | 90ac799 | 2012-02-25 18:48:35 -0800 | [diff] [blame] | 41 | #include <gui/Surface.h> |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 42 | |
| 43 | #include "clz.h" |
Mathias Agopian | 3e25fd8 | 2013-04-22 17:52:16 +0200 | [diff] [blame] | 44 | #include "Colorizer.h" |
Mathias Agopian | 0f2f5ff | 2012-07-31 23:09:07 -0700 | [diff] [blame] | 45 | #include "DisplayDevice.h" |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 46 | #include "Layer.h" |
Dan Stoza | b9b0883 | 2014-03-13 11:55:57 -0700 | [diff] [blame] | 47 | #include "MonitoredProducer.h" |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 48 | #include "SurfaceFlinger.h" |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 49 | |
Mathias Agopian | 1b03149 | 2012-06-20 17:51:20 -0700 | [diff] [blame] | 50 | #include "DisplayHardware/HWComposer.h" |
| 51 | |
Mathias Agopian | 875d8e1 | 2013-06-07 15:35:48 -0700 | [diff] [blame] | 52 | #include "RenderEngine/RenderEngine.h" |
| 53 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 54 | #define DEBUG_RESIZE 0 |
| 55 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 56 | namespace android { |
| 57 | |
| 58 | // --------------------------------------------------------------------------- |
| 59 | |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 60 | int32_t Layer::sSequence = 1; |
| 61 | |
Mathias Agopian | 4d9b822 | 2013-03-12 17:11:48 -0700 | [diff] [blame] | 62 | Layer::Layer(SurfaceFlinger* flinger, const sp<Client>& client, |
| 63 | const String8& name, uint32_t w, uint32_t h, uint32_t flags) |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 64 | : contentDirty(false), |
| 65 | sequence(uint32_t(android_atomic_inc(&sSequence))), |
| 66 | mFlinger(flinger), |
Mathias Agopian | a67932f | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 67 | mTextureName(-1U), |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 68 | mPremultipliedAlpha(true), |
| 69 | mName("unnamed"), |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 70 | mFormat(PIXEL_FORMAT_NONE), |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 71 | mTransactionFlags(0), |
Dan Stoza | 7dde599 | 2015-05-22 09:51:44 -0700 | [diff] [blame] | 72 | mPendingStateMutex(), |
| 73 | mPendingStates(), |
Mathias Agopian | a67932f | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 74 | mQueuedFrames(0), |
Jesse Hall | 399184a | 2014-03-03 15:42:54 -0800 | [diff] [blame] | 75 | mSidebandStreamChanged(false), |
Mathias Agopian | a67932f | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 76 | mCurrentTransform(0), |
Mathias Agopian | 933389f | 2011-07-18 16:15:08 -0700 | [diff] [blame] | 77 | mCurrentScalingMode(NATIVE_WINDOW_SCALING_MODE_FREEZE), |
Robert Carr | c3574f7 | 2016-03-24 12:19:32 -0700 | [diff] [blame] | 78 | mOverrideScalingMode(-1), |
Mathias Agopian | a67932f | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 79 | mCurrentOpacity(true), |
Dan Stoza | cac3538 | 2016-01-27 12:21:06 -0800 | [diff] [blame] | 80 | mCurrentFrameNumber(0), |
Mathias Agopian | 4d143ee | 2012-02-23 20:05:39 -0800 | [diff] [blame] | 81 | mRefreshPending(false), |
Mathias Agopian | 82d7ab6 | 2012-01-19 18:34:40 -0800 | [diff] [blame] | 82 | mFrameLatencyNeeded(false), |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 83 | mFiltering(false), |
| 84 | mNeedsFiltering(false), |
Mathias Agopian | 5cdc899 | 2013-08-13 20:51:23 -0700 | [diff] [blame] | 85 | mMesh(Mesh::TRIANGLE_FAN, 4, 2, 2), |
Pablo Ceballos | 40845df | 2016-01-25 17:41:15 -0800 | [diff] [blame] | 86 | #ifndef USE_HWC2 |
| 87 | mIsGlesComposition(false), |
| 88 | #endif |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 89 | mProtectedByApp(false), |
| 90 | mHasSurface(false), |
Riley Andrews | 03414a1 | 2014-07-01 14:22:59 -0700 | [diff] [blame] | 91 | mClientRef(client), |
Dan Stoza | a4650a5 | 2015-05-12 12:56:16 -0700 | [diff] [blame] | 92 | mPotentialCursor(false), |
| 93 | mQueueItemLock(), |
| 94 | mQueueItemCondition(), |
| 95 | mQueueItems(), |
Dan Stoza | 65476f3 | 2015-05-14 09:27:25 -0700 | [diff] [blame] | 96 | mLastFrameNumberReceived(0), |
Pablo Ceballos | 04839ab | 2015-11-13 13:39:23 -0800 | [diff] [blame] | 97 | mUpdateTexImageFailed(false), |
Pablo Ceballos | ff95aab | 2016-01-13 17:09:58 -0800 | [diff] [blame] | 98 | mAutoRefresh(false) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 99 | { |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 100 | #ifdef USE_HWC2 |
| 101 | ALOGV("Creating Layer %s", name.string()); |
| 102 | #endif |
| 103 | |
Mathias Agopian | a67932f | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 104 | mCurrentCrop.makeInvalid(); |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 105 | mFlinger->getRenderEngine().genTextures(1, &mTextureName); |
Mathias Agopian | 49457ac | 2013-08-14 18:20:17 -0700 | [diff] [blame] | 106 | mTexture.init(Texture::TEXTURE_EXTERNAL, mTextureName); |
Mathias Agopian | 4d9b822 | 2013-03-12 17:11:48 -0700 | [diff] [blame] | 107 | |
| 108 | uint32_t layerFlags = 0; |
| 109 | if (flags & ISurfaceComposerClient::eHidden) |
Andy McFadden | 4125a4f | 2014-01-29 17:17:11 -0800 | [diff] [blame] | 110 | layerFlags |= layer_state_t::eLayerHidden; |
| 111 | if (flags & ISurfaceComposerClient::eOpaque) |
| 112 | layerFlags |= layer_state_t::eLayerOpaque; |
Dan Stoza | 2311608 | 2015-06-18 14:58:39 -0700 | [diff] [blame] | 113 | if (flags & ISurfaceComposerClient::eSecure) |
| 114 | layerFlags |= layer_state_t::eLayerSecure; |
Mathias Agopian | 4d9b822 | 2013-03-12 17:11:48 -0700 | [diff] [blame] | 115 | |
| 116 | if (flags & ISurfaceComposerClient::eNonPremultiplied) |
| 117 | mPremultipliedAlpha = false; |
| 118 | |
| 119 | mName = name; |
| 120 | |
| 121 | mCurrentState.active.w = w; |
| 122 | mCurrentState.active.h = h; |
Robert Carr | 3dcabfa | 2016-03-01 18:36:58 -0800 | [diff] [blame] | 123 | mCurrentState.active.transform.set(0, 0); |
Robert Carr | b5d3d26 | 2016-03-25 15:08:13 -0700 | [diff] [blame] | 124 | mCurrentState.crop.makeInvalid(); |
| 125 | mCurrentState.finalCrop.makeInvalid(); |
Mathias Agopian | 4d9b822 | 2013-03-12 17:11:48 -0700 | [diff] [blame] | 126 | mCurrentState.z = 0; |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 127 | #ifdef USE_HWC2 |
| 128 | mCurrentState.alpha = 1.0f; |
| 129 | #else |
Mathias Agopian | 4d9b822 | 2013-03-12 17:11:48 -0700 | [diff] [blame] | 130 | mCurrentState.alpha = 0xFF; |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 131 | #endif |
Mathias Agopian | 4d9b822 | 2013-03-12 17:11:48 -0700 | [diff] [blame] | 132 | mCurrentState.layerStack = 0; |
| 133 | mCurrentState.flags = layerFlags; |
| 134 | mCurrentState.sequence = 0; |
Mathias Agopian | 4d9b822 | 2013-03-12 17:11:48 -0700 | [diff] [blame] | 135 | mCurrentState.requested = mCurrentState.active; |
| 136 | |
| 137 | // drawing state & current state are identical |
| 138 | mDrawingState = mCurrentState; |
Jamie Gennis | 6547ff4 | 2013-07-16 20:12:42 -0700 | [diff] [blame] | 139 | |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 140 | #ifdef USE_HWC2 |
| 141 | const auto& hwc = flinger->getHwComposer(); |
| 142 | const auto& activeConfig = hwc.getActiveConfig(HWC_DISPLAY_PRIMARY); |
| 143 | nsecs_t displayPeriod = activeConfig->getVsyncPeriod(); |
| 144 | #else |
Jamie Gennis | 6547ff4 | 2013-07-16 20:12:42 -0700 | [diff] [blame] | 145 | nsecs_t displayPeriod = |
| 146 | flinger->getHwComposer().getRefreshPeriod(HWC_DISPLAY_PRIMARY); |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 147 | #endif |
Jamie Gennis | 6547ff4 | 2013-07-16 20:12:42 -0700 | [diff] [blame] | 148 | mFrameTracker.setDisplayRefreshPeriod(displayPeriod); |
Jamie Gennis | e8696a4 | 2012-01-15 18:54:57 -0800 | [diff] [blame] | 149 | } |
| 150 | |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 151 | void Layer::onFirstRef() { |
Andy McFadden | bf974ab | 2012-12-04 16:51:15 -0800 | [diff] [blame] | 152 | // Creates a custom BufferQueue for SurfaceFlingerConsumer to use |
Dan Stoza | b3d0bdf | 2014-04-07 16:33:59 -0700 | [diff] [blame] | 153 | sp<IGraphicBufferProducer> producer; |
| 154 | sp<IGraphicBufferConsumer> consumer; |
Dan Stoza | b9b0883 | 2014-03-13 11:55:57 -0700 | [diff] [blame] | 155 | BufferQueue::createBufferQueue(&producer, &consumer); |
| 156 | mProducer = new MonitoredProducer(producer, mFlinger); |
| 157 | mSurfaceFlingerConsumer = new SurfaceFlingerConsumer(consumer, mTextureName); |
Andy McFadden | bf974ab | 2012-12-04 16:51:15 -0800 | [diff] [blame] | 158 | mSurfaceFlingerConsumer->setConsumerUsageBits(getEffectiveUsage(0)); |
Jesse Hall | 399184a | 2014-03-03 15:42:54 -0800 | [diff] [blame] | 159 | mSurfaceFlingerConsumer->setContentsChangedListener(this); |
Mathias Agopian | 4d9b822 | 2013-03-12 17:11:48 -0700 | [diff] [blame] | 160 | mSurfaceFlingerConsumer->setName(mName); |
Daniel Lam | b267579 | 2012-02-23 14:35:13 -0800 | [diff] [blame] | 161 | |
Mathias Agopian | 7f42a9c | 2012-04-23 20:00:16 -0700 | [diff] [blame] | 162 | #ifdef TARGET_DISABLE_TRIPLE_BUFFERING |
| 163 | #warning "disabling triple buffering" |
Mathias Agopian | 7f42a9c | 2012-04-23 20:00:16 -0700 | [diff] [blame] | 164 | #else |
Pablo Ceballos | 19e3e06 | 2015-08-19 16:16:06 -0700 | [diff] [blame] | 165 | mProducer->setMaxDequeuedBufferCount(2); |
Mathias Agopian | 303d538 | 2012-02-05 01:49:16 -0800 | [diff] [blame] | 166 | #endif |
Andy McFadden | 6905205 | 2012-09-14 16:10:11 -0700 | [diff] [blame] | 167 | |
Mathias Agopian | 8430095 | 2012-11-21 16:02:13 -0800 | [diff] [blame] | 168 | const sp<const DisplayDevice> hw(mFlinger->getDefaultDisplayDevice()); |
| 169 | updateTransformHint(hw); |
Mathias Agopian | b7e930d | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 170 | } |
| 171 | |
Mathias Agopian | 4d9b822 | 2013-03-12 17:11:48 -0700 | [diff] [blame] | 172 | Layer::~Layer() { |
Pablo Ceballos | 8ea4e7b | 2016-03-03 15:20:02 -0800 | [diff] [blame] | 173 | sp<Client> c(mClientRef.promote()); |
| 174 | if (c != 0) { |
| 175 | c->detachLayer(this); |
| 176 | } |
| 177 | |
Dan Stoza | cac3538 | 2016-01-27 12:21:06 -0800 | [diff] [blame] | 178 | for (auto& point : mRemoteSyncPoints) { |
| 179 | point->setTransactionApplied(); |
| 180 | } |
Dan Stoza | c814517 | 2016-04-28 16:29:06 -0700 | [diff] [blame] | 181 | for (auto& point : mLocalSyncPoints) { |
| 182 | point->setFrameAvailable(); |
| 183 | } |
Mathias Agopian | 921e6ac | 2012-07-23 23:11:29 -0700 | [diff] [blame] | 184 | mFlinger->deleteTextureAsync(mTextureName); |
Jamie Gennis | 6547ff4 | 2013-07-16 20:12:42 -0700 | [diff] [blame] | 185 | mFrameTracker.logAndResetStats(mName); |
Mathias Agopian | 96f0819 | 2010-06-02 23:28:45 -0700 | [diff] [blame] | 186 | } |
| 187 | |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 188 | // --------------------------------------------------------------------------- |
| 189 | // callbacks |
| 190 | // --------------------------------------------------------------------------- |
| 191 | |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 192 | #ifdef USE_HWC2 |
| 193 | void Layer::onLayerDisplayed(const sp<Fence>& releaseFence) { |
| 194 | if (mHwcLayers.empty()) { |
| 195 | return; |
| 196 | } |
| 197 | mSurfaceFlingerConsumer->setReleaseFence(releaseFence); |
| 198 | } |
| 199 | #else |
Dan Stoza | c701401 | 2014-02-14 15:03:43 -0800 | [diff] [blame] | 200 | void Layer::onLayerDisplayed(const sp<const DisplayDevice>& /* hw */, |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 201 | HWComposer::HWCLayerInterface* layer) { |
| 202 | if (layer) { |
| 203 | layer->onDisplayed(); |
Jesse Hall | 13f01cb | 2013-03-20 11:37:21 -0700 | [diff] [blame] | 204 | mSurfaceFlingerConsumer->setReleaseFence(layer->getAndResetReleaseFence()); |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 205 | } |
| 206 | } |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 207 | #endif |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 208 | |
Dan Stoza | 6b9454d | 2014-11-07 16:00:59 -0800 | [diff] [blame] | 209 | void Layer::onFrameAvailable(const BufferItem& item) { |
| 210 | // Add this buffer from our internal queue tracker |
| 211 | { // Autolock scope |
| 212 | Mutex::Autolock lock(mQueueItemLock); |
Dan Stoza | a4650a5 | 2015-05-12 12:56:16 -0700 | [diff] [blame] | 213 | |
| 214 | // Reset the frame number tracker when we receive the first buffer after |
| 215 | // a frame number reset |
| 216 | if (item.mFrameNumber == 1) { |
| 217 | mLastFrameNumberReceived = 0; |
| 218 | } |
| 219 | |
| 220 | // Ensure that callbacks are handled in order |
| 221 | while (item.mFrameNumber != mLastFrameNumberReceived + 1) { |
| 222 | status_t result = mQueueItemCondition.waitRelative(mQueueItemLock, |
| 223 | ms2ns(500)); |
| 224 | if (result != NO_ERROR) { |
| 225 | ALOGE("[%s] Timed out waiting on callback", mName.string()); |
| 226 | } |
| 227 | } |
| 228 | |
Dan Stoza | 6b9454d | 2014-11-07 16:00:59 -0800 | [diff] [blame] | 229 | mQueueItems.push_back(item); |
Dan Stoza | ecc5040 | 2015-04-28 14:42:06 -0700 | [diff] [blame] | 230 | android_atomic_inc(&mQueuedFrames); |
Dan Stoza | a4650a5 | 2015-05-12 12:56:16 -0700 | [diff] [blame] | 231 | |
| 232 | // Wake up any pending callbacks |
| 233 | mLastFrameNumberReceived = item.mFrameNumber; |
| 234 | mQueueItemCondition.broadcast(); |
Dan Stoza | 6b9454d | 2014-11-07 16:00:59 -0800 | [diff] [blame] | 235 | } |
| 236 | |
Mathias Agopian | 99ce5cd | 2012-01-31 18:24:27 -0800 | [diff] [blame] | 237 | mFlinger->signalLayerUpdate(); |
Mathias Agopian | 579b3f8 | 2010-06-08 19:54:15 -0700 | [diff] [blame] | 238 | } |
| 239 | |
Dan Stoza | 6b9454d | 2014-11-07 16:00:59 -0800 | [diff] [blame] | 240 | void Layer::onFrameReplaced(const BufferItem& item) { |
Dan Stoza | 7dde599 | 2015-05-22 09:51:44 -0700 | [diff] [blame] | 241 | { // Autolock scope |
| 242 | Mutex::Autolock lock(mQueueItemLock); |
Dan Stoza | a4650a5 | 2015-05-12 12:56:16 -0700 | [diff] [blame] | 243 | |
Dan Stoza | 7dde599 | 2015-05-22 09:51:44 -0700 | [diff] [blame] | 244 | // Ensure that callbacks are handled in order |
| 245 | while (item.mFrameNumber != mLastFrameNumberReceived + 1) { |
| 246 | status_t result = mQueueItemCondition.waitRelative(mQueueItemLock, |
| 247 | ms2ns(500)); |
| 248 | if (result != NO_ERROR) { |
| 249 | ALOGE("[%s] Timed out waiting on callback", mName.string()); |
| 250 | } |
Dan Stoza | a4650a5 | 2015-05-12 12:56:16 -0700 | [diff] [blame] | 251 | } |
Dan Stoza | 7dde599 | 2015-05-22 09:51:44 -0700 | [diff] [blame] | 252 | |
| 253 | if (mQueueItems.empty()) { |
| 254 | ALOGE("Can't replace a frame on an empty queue"); |
| 255 | return; |
| 256 | } |
Pablo Ceballos | 4d85da4 | 2016-04-19 20:11:56 -0700 | [diff] [blame] | 257 | mQueueItems.editItemAt(mQueueItems.size() - 1) = item; |
Dan Stoza | 7dde599 | 2015-05-22 09:51:44 -0700 | [diff] [blame] | 258 | |
| 259 | // Wake up any pending callbacks |
| 260 | mLastFrameNumberReceived = item.mFrameNumber; |
| 261 | mQueueItemCondition.broadcast(); |
Dan Stoza | a4650a5 | 2015-05-12 12:56:16 -0700 | [diff] [blame] | 262 | } |
Dan Stoza | 6b9454d | 2014-11-07 16:00:59 -0800 | [diff] [blame] | 263 | } |
| 264 | |
Jesse Hall | 399184a | 2014-03-03 15:42:54 -0800 | [diff] [blame] | 265 | void Layer::onSidebandStreamChanged() { |
| 266 | if (android_atomic_release_cas(false, true, &mSidebandStreamChanged) == 0) { |
| 267 | // mSidebandStreamChanged was false |
| 268 | mFlinger->signalLayerUpdate(); |
| 269 | } |
| 270 | } |
| 271 | |
Mathias Agopian | 6710604 | 2013-03-14 19:18:13 -0700 | [diff] [blame] | 272 | // called with SurfaceFlinger::mStateLock from the drawing thread after |
| 273 | // the layer has been remove from the current state list (and just before |
| 274 | // it's removed from the drawing state list) |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 275 | void Layer::onRemoved() { |
Andy McFadden | bf974ab | 2012-12-04 16:51:15 -0800 | [diff] [blame] | 276 | mSurfaceFlingerConsumer->abandon(); |
Mathias Agopian | 48d819a | 2009-09-10 19:41:18 -0700 | [diff] [blame] | 277 | } |
Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 278 | |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 279 | // --------------------------------------------------------------------------- |
| 280 | // set-up |
| 281 | // --------------------------------------------------------------------------- |
| 282 | |
Mathias Agopian | 1eae0ee | 2013-06-05 16:59:15 -0700 | [diff] [blame] | 283 | const String8& Layer::getName() const { |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 284 | return mName; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 285 | } |
| 286 | |
Mathias Agopian | f9d9327 | 2009-06-19 17:00:27 -0700 | [diff] [blame] | 287 | status_t Layer::setBuffers( uint32_t w, uint32_t h, |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 288 | PixelFormat format, uint32_t flags) |
| 289 | { |
Mathias Agopian | ca99fb8 | 2010-04-14 16:43:44 -0700 | [diff] [blame] | 290 | uint32_t const maxSurfaceDims = min( |
Mathias Agopian | a491260 | 2012-07-12 14:25:33 -0700 | [diff] [blame] | 291 | mFlinger->getMaxTextureSize(), mFlinger->getMaxViewportDims()); |
Mathias Agopian | ca99fb8 | 2010-04-14 16:43:44 -0700 | [diff] [blame] | 292 | |
| 293 | // never allow a surface larger than what our underlying GL implementation |
| 294 | // can handle. |
| 295 | if ((uint32_t(w)>maxSurfaceDims) || (uint32_t(h)>maxSurfaceDims)) { |
Mathias Agopian | ff615cc | 2012-02-24 14:58:36 -0800 | [diff] [blame] | 296 | ALOGE("dimensions too large %u x %u", uint32_t(w), uint32_t(h)); |
Mathias Agopian | ca99fb8 | 2010-04-14 16:43:44 -0700 | [diff] [blame] | 297 | return BAD_VALUE; |
| 298 | } |
| 299 | |
Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 300 | mFormat = format; |
Mathias Agopian | eff062c | 2010-08-25 14:59:15 -0700 | [diff] [blame] | 301 | |
Riley Andrews | 03414a1 | 2014-07-01 14:22:59 -0700 | [diff] [blame] | 302 | mPotentialCursor = (flags & ISurfaceComposerClient::eCursorWindow) ? true : false; |
Mathias Agopian | 3165cc2 | 2012-08-08 19:42:09 -0700 | [diff] [blame] | 303 | mProtectedByApp = (flags & ISurfaceComposerClient::eProtectedByApp) ? true : false; |
Mathias Agopian | a67932f | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 304 | mCurrentOpacity = getOpacityForFormat(format); |
| 305 | |
Andy McFadden | bf974ab | 2012-12-04 16:51:15 -0800 | [diff] [blame] | 306 | mSurfaceFlingerConsumer->setDefaultBufferSize(w, h); |
| 307 | mSurfaceFlingerConsumer->setDefaultBufferFormat(format); |
| 308 | mSurfaceFlingerConsumer->setConsumerUsageBits(getEffectiveUsage(0)); |
Mathias Agopian | ca99fb8 | 2010-04-14 16:43:44 -0700 | [diff] [blame] | 309 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 310 | return NO_ERROR; |
| 311 | } |
| 312 | |
Dan Stoza | 7dde599 | 2015-05-22 09:51:44 -0700 | [diff] [blame] | 313 | /* |
| 314 | * The layer handle is just a BBinder object passed to the client |
| 315 | * (remote process) -- we don't keep any reference on our side such that |
| 316 | * the dtor is called when the remote side let go of its reference. |
| 317 | * |
| 318 | * LayerCleaner ensures that mFlinger->onLayerDestroyed() is called for |
| 319 | * this layer when the handle is destroyed. |
| 320 | */ |
| 321 | class Layer::Handle : public BBinder, public LayerCleaner { |
| 322 | public: |
| 323 | Handle(const sp<SurfaceFlinger>& flinger, const sp<Layer>& layer) |
| 324 | : LayerCleaner(flinger, layer), owner(layer) {} |
| 325 | |
| 326 | wp<Layer> owner; |
| 327 | }; |
| 328 | |
Mathias Agopian | 4d9b822 | 2013-03-12 17:11:48 -0700 | [diff] [blame] | 329 | sp<IBinder> Layer::getHandle() { |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 330 | Mutex::Autolock _l(mLock); |
| 331 | |
| 332 | LOG_ALWAYS_FATAL_IF(mHasSurface, |
Mathias Agopian | 4d9b822 | 2013-03-12 17:11:48 -0700 | [diff] [blame] | 333 | "Layer::getHandle() has already been called"); |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 334 | |
| 335 | mHasSurface = true; |
Mathias Agopian | 4d9b822 | 2013-03-12 17:11:48 -0700 | [diff] [blame] | 336 | |
Mathias Agopian | 4d9b822 | 2013-03-12 17:11:48 -0700 | [diff] [blame] | 337 | return new Handle(mFlinger, this); |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 338 | } |
| 339 | |
Dan Stoza | b9b0883 | 2014-03-13 11:55:57 -0700 | [diff] [blame] | 340 | sp<IGraphicBufferProducer> Layer::getProducer() const { |
| 341 | return mProducer; |
Mathias Agopian | 4d9b822 | 2013-03-12 17:11:48 -0700 | [diff] [blame] | 342 | } |
| 343 | |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 344 | // --------------------------------------------------------------------------- |
| 345 | // h/w composer set-up |
| 346 | // --------------------------------------------------------------------------- |
| 347 | |
Mathias Agopian | a8bca8d | 2013-02-27 22:03:19 -0800 | [diff] [blame] | 348 | Rect Layer::getContentCrop() const { |
| 349 | // this is the crop rectangle that applies to the buffer |
| 350 | // itself (as opposed to the window) |
Jamie Gennis | f15a83f | 2012-05-10 20:43:55 -0700 | [diff] [blame] | 351 | Rect crop; |
| 352 | if (!mCurrentCrop.isEmpty()) { |
Mathias Agopian | a8bca8d | 2013-02-27 22:03:19 -0800 | [diff] [blame] | 353 | // if the buffer crop is defined, we use that |
Jamie Gennis | f15a83f | 2012-05-10 20:43:55 -0700 | [diff] [blame] | 354 | crop = mCurrentCrop; |
Mathias Agopian | a8bca8d | 2013-02-27 22:03:19 -0800 | [diff] [blame] | 355 | } else if (mActiveBuffer != NULL) { |
| 356 | // otherwise we use the whole buffer |
| 357 | crop = mActiveBuffer->getBounds(); |
Jamie Gennis | f15a83f | 2012-05-10 20:43:55 -0700 | [diff] [blame] | 358 | } else { |
Mathias Agopian | a8bca8d | 2013-02-27 22:03:19 -0800 | [diff] [blame] | 359 | // if we don't have a buffer yet, we use an empty/invalid crop |
Mathias Agopian | 4fec873 | 2012-06-29 14:12:52 -0700 | [diff] [blame] | 360 | crop.makeInvalid(); |
Jamie Gennis | f15a83f | 2012-05-10 20:43:55 -0700 | [diff] [blame] | 361 | } |
Jamie Gennis | f15a83f | 2012-05-10 20:43:55 -0700 | [diff] [blame] | 362 | return crop; |
| 363 | } |
| 364 | |
Mathias Agopian | f3e85d4 | 2013-05-10 18:01:12 -0700 | [diff] [blame] | 365 | static Rect reduce(const Rect& win, const Region& exclude) { |
| 366 | if (CC_LIKELY(exclude.isEmpty())) { |
| 367 | return win; |
| 368 | } |
| 369 | if (exclude.isRect()) { |
| 370 | return win.reduce(exclude.getBounds()); |
| 371 | } |
| 372 | return Region(win).subtract(exclude).getBounds(); |
| 373 | } |
| 374 | |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 375 | Rect Layer::computeBounds() const { |
Mathias Agopian | 1eae0ee | 2013-06-05 16:59:15 -0700 | [diff] [blame] | 376 | const Layer::State& s(getDrawingState()); |
Michael Lentine | 6c925ed | 2014-09-26 17:55:01 -0700 | [diff] [blame] | 377 | return computeBounds(s.activeTransparentRegion); |
| 378 | } |
| 379 | |
| 380 | Rect Layer::computeBounds(const Region& activeTransparentRegion) const { |
| 381 | const Layer::State& s(getDrawingState()); |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 382 | Rect win(s.active.w, s.active.h); |
Robert Carr | b5d3d26 | 2016-03-25 15:08:13 -0700 | [diff] [blame] | 383 | |
| 384 | if (!s.crop.isEmpty()) { |
| 385 | win.intersect(s.crop, &win); |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 386 | } |
Mathias Agopian | 6c7f25a | 2013-05-09 20:37:10 -0700 | [diff] [blame] | 387 | // subtract the transparent region and snap to the bounds |
Michael Lentine | 6c925ed | 2014-09-26 17:55:01 -0700 | [diff] [blame] | 388 | return reduce(win, activeTransparentRegion); |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 389 | } |
| 390 | |
Mathias Agopian | 6b44267 | 2013-07-09 21:24:52 -0700 | [diff] [blame] | 391 | FloatRect Layer::computeCrop(const sp<const DisplayDevice>& hw) const { |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 392 | // the content crop is the area of the content that gets scaled to the |
| 393 | // layer's size. |
Mathias Agopian | 6b44267 | 2013-07-09 21:24:52 -0700 | [diff] [blame] | 394 | FloatRect crop(getContentCrop()); |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 395 | |
Robert Carr | b5d3d26 | 2016-03-25 15:08:13 -0700 | [diff] [blame] | 396 | // the crop is the area of the window that gets cropped, but not |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 397 | // scaled in any ways. |
Mathias Agopian | 1eae0ee | 2013-06-05 16:59:15 -0700 | [diff] [blame] | 398 | const State& s(getDrawingState()); |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 399 | |
| 400 | // apply the projection's clipping to the window crop in |
| 401 | // layerstack space, and convert-back to layer space. |
Mathias Agopian | 6b44267 | 2013-07-09 21:24:52 -0700 | [diff] [blame] | 402 | // if there are no window scaling involved, this operation will map to full |
| 403 | // pixels in the buffer. |
| 404 | // FIXME: the 3 lines below can produce slightly incorrect clipping when we have |
| 405 | // a viewport clipping and a window transform. we should use floating point to fix this. |
Mathias Agopian | 0e8f144 | 2013-08-20 21:41:07 -0700 | [diff] [blame] | 406 | |
| 407 | Rect activeCrop(s.active.w, s.active.h); |
Robert Carr | b5d3d26 | 2016-03-25 15:08:13 -0700 | [diff] [blame] | 408 | if (!s.crop.isEmpty()) { |
| 409 | activeCrop = s.crop; |
Mathias Agopian | 0e8f144 | 2013-08-20 21:41:07 -0700 | [diff] [blame] | 410 | } |
| 411 | |
Robert Carr | 3dcabfa | 2016-03-01 18:36:58 -0800 | [diff] [blame] | 412 | activeCrop = s.active.transform.transform(activeCrop); |
Pablo Ceballos | acbe678 | 2016-03-04 17:54:21 +0000 | [diff] [blame] | 413 | if (!activeCrop.intersect(hw->getViewport(), &activeCrop)) { |
| 414 | activeCrop.clear(); |
| 415 | } |
Robert Carr | b5d3d26 | 2016-03-25 15:08:13 -0700 | [diff] [blame] | 416 | if (!s.finalCrop.isEmpty()) { |
| 417 | if(!activeCrop.intersect(s.finalCrop, &activeCrop)) { |
Pablo Ceballos | acbe678 | 2016-03-04 17:54:21 +0000 | [diff] [blame] | 418 | activeCrop.clear(); |
| 419 | } |
| 420 | } |
Robert Carr | 3dcabfa | 2016-03-01 18:36:58 -0800 | [diff] [blame] | 421 | activeCrop = s.active.transform.inverse().transform(activeCrop); |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 422 | |
Michael Lentine | 28ea217 | 2014-11-19 18:32:37 -0800 | [diff] [blame] | 423 | // This needs to be here as transform.transform(Rect) computes the |
| 424 | // transformed rect and then takes the bounding box of the result before |
| 425 | // returning. This means |
| 426 | // transform.inverse().transform(transform.transform(Rect)) != Rect |
| 427 | // in which case we need to make sure the final rect is clipped to the |
| 428 | // display bounds. |
Pablo Ceballos | acbe678 | 2016-03-04 17:54:21 +0000 | [diff] [blame] | 429 | if (!activeCrop.intersect(Rect(s.active.w, s.active.h), &activeCrop)) { |
| 430 | activeCrop.clear(); |
| 431 | } |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 432 | |
Mathias Agopian | f3e85d4 | 2013-05-10 18:01:12 -0700 | [diff] [blame] | 433 | // subtract the transparent region and snap to the bounds |
| 434 | activeCrop = reduce(activeCrop, s.activeTransparentRegion); |
| 435 | |
Pablo Ceballos | acbe678 | 2016-03-04 17:54:21 +0000 | [diff] [blame] | 436 | // Transform the window crop to match the buffer coordinate system, |
| 437 | // which means using the inverse of the current transform set on the |
| 438 | // SurfaceFlingerConsumer. |
| 439 | uint32_t invTransform = mCurrentTransform; |
| 440 | if (mSurfaceFlingerConsumer->getTransformToDisplayInverse()) { |
| 441 | /* |
Pablo Ceballos | 021623b | 2016-04-15 17:31:51 -0700 | [diff] [blame] | 442 | * the code below applies the primary display's inverse transform to the |
| 443 | * buffer |
Pablo Ceballos | acbe678 | 2016-03-04 17:54:21 +0000 | [diff] [blame] | 444 | */ |
Pablo Ceballos | 021623b | 2016-04-15 17:31:51 -0700 | [diff] [blame] | 445 | uint32_t invTransformOrient = |
| 446 | DisplayDevice::getPrimaryDisplayOrientationTransform(); |
Pablo Ceballos | acbe678 | 2016-03-04 17:54:21 +0000 | [diff] [blame] | 447 | // calculate the inverse transform |
| 448 | if (invTransformOrient & NATIVE_WINDOW_TRANSFORM_ROT_90) { |
| 449 | invTransformOrient ^= NATIVE_WINDOW_TRANSFORM_FLIP_V | |
| 450 | NATIVE_WINDOW_TRANSFORM_FLIP_H; |
| 451 | // If the transform has been rotated the axis of flip has been swapped |
| 452 | // so we need to swap which flip operations we are performing |
Michael Lentine | 7b90258 | 2014-08-19 18:14:06 -0700 | [diff] [blame] | 453 | bool is_h_flipped = (invTransform & NATIVE_WINDOW_TRANSFORM_FLIP_H) != 0; |
| 454 | bool is_v_flipped = (invTransform & NATIVE_WINDOW_TRANSFORM_FLIP_V) != 0; |
Pablo Ceballos | acbe678 | 2016-03-04 17:54:21 +0000 | [diff] [blame] | 455 | if (is_h_flipped != is_v_flipped) { |
Michael Lentine | 7b90258 | 2014-08-19 18:14:06 -0700 | [diff] [blame] | 456 | invTransform ^= NATIVE_WINDOW_TRANSFORM_FLIP_V | |
| 457 | NATIVE_WINDOW_TRANSFORM_FLIP_H; |
| 458 | } |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 459 | } |
Pablo Ceballos | acbe678 | 2016-03-04 17:54:21 +0000 | [diff] [blame] | 460 | // and apply to the current transform |
| 461 | invTransform = (Transform(invTransform) * Transform(invTransformOrient)).getOrientation(); |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 462 | } |
Pablo Ceballos | acbe678 | 2016-03-04 17:54:21 +0000 | [diff] [blame] | 463 | |
| 464 | int winWidth = s.active.w; |
| 465 | int winHeight = s.active.h; |
| 466 | if (invTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) { |
| 467 | // If the activeCrop has been rotate the ends are rotated but not |
| 468 | // the space itself so when transforming ends back we can't rely on |
| 469 | // a modification of the axes of rotation. To account for this we |
| 470 | // need to reorient the inverse rotation in terms of the current |
| 471 | // axes of rotation. |
| 472 | bool is_h_flipped = (invTransform & NATIVE_WINDOW_TRANSFORM_FLIP_H) != 0; |
| 473 | bool is_v_flipped = (invTransform & NATIVE_WINDOW_TRANSFORM_FLIP_V) != 0; |
| 474 | if (is_h_flipped == is_v_flipped) { |
| 475 | invTransform ^= NATIVE_WINDOW_TRANSFORM_FLIP_V | |
| 476 | NATIVE_WINDOW_TRANSFORM_FLIP_H; |
| 477 | } |
| 478 | winWidth = s.active.h; |
| 479 | winHeight = s.active.w; |
| 480 | } |
| 481 | const Rect winCrop = activeCrop.transform( |
| 482 | invTransform, s.active.w, s.active.h); |
| 483 | |
| 484 | // below, crop is intersected with winCrop expressed in crop's coordinate space |
| 485 | float xScale = crop.getWidth() / float(winWidth); |
| 486 | float yScale = crop.getHeight() / float(winHeight); |
| 487 | |
| 488 | float insetL = winCrop.left * xScale; |
| 489 | float insetT = winCrop.top * yScale; |
| 490 | float insetR = (winWidth - winCrop.right ) * xScale; |
| 491 | float insetB = (winHeight - winCrop.bottom) * yScale; |
| 492 | |
| 493 | crop.left += insetL; |
| 494 | crop.top += insetT; |
| 495 | crop.right -= insetR; |
| 496 | crop.bottom -= insetB; |
| 497 | |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 498 | return crop; |
| 499 | } |
| 500 | |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 501 | #ifdef USE_HWC2 |
| 502 | void Layer::setGeometry(const sp<const DisplayDevice>& displayDevice) |
| 503 | #else |
Mathias Agopian | 4fec873 | 2012-06-29 14:12:52 -0700 | [diff] [blame] | 504 | void Layer::setGeometry( |
Mathias Agopian | 4297734 | 2012-08-05 00:40:46 -0700 | [diff] [blame] | 505 | const sp<const DisplayDevice>& hw, |
Mathias Agopian | 4fec873 | 2012-06-29 14:12:52 -0700 | [diff] [blame] | 506 | HWComposer::HWCLayerInterface& layer) |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 507 | #endif |
Mathias Agopian | a350ff9 | 2010-08-10 17:14:02 -0700 | [diff] [blame] | 508 | { |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 509 | #ifdef USE_HWC2 |
| 510 | const auto hwcId = displayDevice->getHwcDisplayId(); |
| 511 | auto& hwcInfo = mHwcLayers[hwcId]; |
| 512 | #else |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 513 | layer.setDefaultState(); |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 514 | #endif |
Mathias Agopian | a537c0f | 2011-08-02 15:51:37 -0700 | [diff] [blame] | 515 | |
Mathias Agopian | 3e8b853 | 2012-05-13 20:42:01 -0700 | [diff] [blame] | 516 | // enable this layer |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 517 | #ifdef USE_HWC2 |
| 518 | hwcInfo.forceClientComposition = false; |
| 519 | |
| 520 | if (isSecure() && !displayDevice->isSecure()) { |
| 521 | hwcInfo.forceClientComposition = true; |
| 522 | } |
| 523 | |
| 524 | auto& hwcLayer = hwcInfo.layer; |
| 525 | #else |
Mathias Agopian | 3e8b853 | 2012-05-13 20:42:01 -0700 | [diff] [blame] | 526 | layer.setSkip(false); |
Mathias Agopian | a350ff9 | 2010-08-10 17:14:02 -0700 | [diff] [blame] | 527 | |
Jamie Gennis | dd3cb84 | 2012-10-19 18:19:11 -0700 | [diff] [blame] | 528 | if (isSecure() && !hw->isSecure()) { |
| 529 | layer.setSkip(true); |
| 530 | } |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 531 | #endif |
Jamie Gennis | dd3cb84 | 2012-10-19 18:19:11 -0700 | [diff] [blame] | 532 | |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 533 | // this gives us only the "orientation" component of the transform |
Mathias Agopian | 1eae0ee | 2013-06-05 16:59:15 -0700 | [diff] [blame] | 534 | const State& s(getDrawingState()); |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 535 | #ifdef USE_HWC2 |
| 536 | if (!isOpaque(s) || s.alpha != 1.0f) { |
| 537 | auto blendMode = mPremultipliedAlpha ? |
| 538 | HWC2::BlendMode::Premultiplied : HWC2::BlendMode::Coverage; |
| 539 | auto error = hwcLayer->setBlendMode(blendMode); |
| 540 | ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set blend mode %s:" |
| 541 | " %s (%d)", mName.string(), to_string(blendMode).c_str(), |
| 542 | to_string(error).c_str(), static_cast<int32_t>(error)); |
| 543 | } |
| 544 | #else |
Andy McFadden | 4125a4f | 2014-01-29 17:17:11 -0800 | [diff] [blame] | 545 | if (!isOpaque(s) || s.alpha != 0xFF) { |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 546 | layer.setBlending(mPremultipliedAlpha ? |
| 547 | HWC_BLENDING_PREMULT : |
| 548 | HWC_BLENDING_COVERAGE); |
| 549 | } |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 550 | #endif |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 551 | |
| 552 | // apply the layer's transform, followed by the display's global transform |
| 553 | // here we're guaranteed that the layer's transform preserves rects |
Michael Lentine | 6c925ed | 2014-09-26 17:55:01 -0700 | [diff] [blame] | 554 | Region activeTransparentRegion(s.activeTransparentRegion); |
Robert Carr | b5d3d26 | 2016-03-25 15:08:13 -0700 | [diff] [blame] | 555 | if (!s.crop.isEmpty()) { |
| 556 | Rect activeCrop(s.crop); |
Robert Carr | 3dcabfa | 2016-03-01 18:36:58 -0800 | [diff] [blame] | 557 | activeCrop = s.active.transform.transform(activeCrop); |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 558 | #ifdef USE_HWC2 |
Pablo Ceballos | acbe678 | 2016-03-04 17:54:21 +0000 | [diff] [blame] | 559 | if(!activeCrop.intersect(displayDevice->getViewport(), &activeCrop)) { |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 560 | #else |
Pablo Ceballos | acbe678 | 2016-03-04 17:54:21 +0000 | [diff] [blame] | 561 | if(!activeCrop.intersect(hw->getViewport(), &activeCrop)) { |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 562 | #endif |
Pablo Ceballos | acbe678 | 2016-03-04 17:54:21 +0000 | [diff] [blame] | 563 | activeCrop.clear(); |
| 564 | } |
Robert Carr | 3dcabfa | 2016-03-01 18:36:58 -0800 | [diff] [blame] | 565 | activeCrop = s.active.transform.inverse().transform(activeCrop); |
Michael Lentine | 28ea217 | 2014-11-19 18:32:37 -0800 | [diff] [blame] | 566 | // This needs to be here as transform.transform(Rect) computes the |
| 567 | // transformed rect and then takes the bounding box of the result before |
| 568 | // returning. This means |
| 569 | // transform.inverse().transform(transform.transform(Rect)) != Rect |
| 570 | // in which case we need to make sure the final rect is clipped to the |
| 571 | // display bounds. |
Pablo Ceballos | acbe678 | 2016-03-04 17:54:21 +0000 | [diff] [blame] | 572 | if(!activeCrop.intersect(Rect(s.active.w, s.active.h), &activeCrop)) { |
| 573 | activeCrop.clear(); |
| 574 | } |
Michael Lentine | 6c925ed | 2014-09-26 17:55:01 -0700 | [diff] [blame] | 575 | // mark regions outside the crop as transparent |
| 576 | activeTransparentRegion.orSelf(Rect(0, 0, s.active.w, activeCrop.top)); |
| 577 | activeTransparentRegion.orSelf(Rect(0, activeCrop.bottom, |
| 578 | s.active.w, s.active.h)); |
| 579 | activeTransparentRegion.orSelf(Rect(0, activeCrop.top, |
| 580 | activeCrop.left, activeCrop.bottom)); |
| 581 | activeTransparentRegion.orSelf(Rect(activeCrop.right, activeCrop.top, |
| 582 | s.active.w, activeCrop.bottom)); |
| 583 | } |
Robert Carr | 3dcabfa | 2016-03-01 18:36:58 -0800 | [diff] [blame] | 584 | Rect frame(s.active.transform.transform(computeBounds(activeTransparentRegion))); |
Robert Carr | b5d3d26 | 2016-03-25 15:08:13 -0700 | [diff] [blame] | 585 | if (!s.finalCrop.isEmpty()) { |
| 586 | if(!frame.intersect(s.finalCrop, &frame)) { |
Pablo Ceballos | acbe678 | 2016-03-04 17:54:21 +0000 | [diff] [blame] | 587 | frame.clear(); |
| 588 | } |
| 589 | } |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 590 | #ifdef USE_HWC2 |
Pablo Ceballos | acbe678 | 2016-03-04 17:54:21 +0000 | [diff] [blame] | 591 | if (!frame.intersect(displayDevice->getViewport(), &frame)) { |
| 592 | frame.clear(); |
| 593 | } |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 594 | const Transform& tr(displayDevice->getTransform()); |
| 595 | Rect transformedFrame = tr.transform(frame); |
| 596 | auto error = hwcLayer->setDisplayFrame(transformedFrame); |
| 597 | ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set display frame " |
| 598 | "[%d, %d, %d, %d]: %s (%d)", mName.string(), transformedFrame.left, |
| 599 | transformedFrame.top, transformedFrame.right, |
| 600 | transformedFrame.bottom, to_string(error).c_str(), |
| 601 | static_cast<int32_t>(error)); |
| 602 | |
| 603 | FloatRect sourceCrop = computeCrop(displayDevice); |
| 604 | error = hwcLayer->setSourceCrop(sourceCrop); |
| 605 | ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set source crop " |
| 606 | "[%.3f, %.3f, %.3f, %.3f]: %s (%d)", mName.string(), |
| 607 | sourceCrop.left, sourceCrop.top, sourceCrop.right, |
| 608 | sourceCrop.bottom, to_string(error).c_str(), |
| 609 | static_cast<int32_t>(error)); |
| 610 | |
| 611 | error = hwcLayer->setPlaneAlpha(s.alpha); |
| 612 | ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set plane alpha %.3f: " |
| 613 | "%s (%d)", mName.string(), s.alpha, to_string(error).c_str(), |
| 614 | static_cast<int32_t>(error)); |
| 615 | |
| 616 | error = hwcLayer->setZOrder(s.z); |
| 617 | ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set Z %u: %s (%d)", |
| 618 | mName.string(), s.z, to_string(error).c_str(), |
| 619 | static_cast<int32_t>(error)); |
| 620 | #else |
Pablo Ceballos | acbe678 | 2016-03-04 17:54:21 +0000 | [diff] [blame] | 621 | if (!frame.intersect(hw->getViewport(), &frame)) { |
| 622 | frame.clear(); |
| 623 | } |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 624 | const Transform& tr(hw->getTransform()); |
| 625 | layer.setFrame(tr.transform(frame)); |
| 626 | layer.setCrop(computeCrop(hw)); |
Mathias Agopian | 9f8386e | 2013-01-29 18:56:42 -0800 | [diff] [blame] | 627 | layer.setPlaneAlpha(s.alpha); |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 628 | #endif |
Mathias Agopian | 9f8386e | 2013-01-29 18:56:42 -0800 | [diff] [blame] | 629 | |
Mathias Agopian | 29a367b | 2011-07-12 14:51:45 -0700 | [diff] [blame] | 630 | /* |
| 631 | * Transformations are applied in this order: |
| 632 | * 1) buffer orientation/flip/mirror |
| 633 | * 2) state transformation (window manager) |
| 634 | * 3) layer orientation (screen orientation) |
| 635 | * (NOTE: the matrices are multiplied in reverse order) |
| 636 | */ |
| 637 | |
| 638 | const Transform bufferOrientation(mCurrentTransform); |
Robert Carr | 3dcabfa | 2016-03-01 18:36:58 -0800 | [diff] [blame] | 639 | Transform transform(tr * s.active.transform * bufferOrientation); |
Mathias Agopian | c1c05de | 2013-09-17 23:45:22 -0700 | [diff] [blame] | 640 | |
| 641 | if (mSurfaceFlingerConsumer->getTransformToDisplayInverse()) { |
| 642 | /* |
Pablo Ceballos | 021623b | 2016-04-15 17:31:51 -0700 | [diff] [blame] | 643 | * the code below applies the primary display's inverse transform to the |
| 644 | * buffer |
Mathias Agopian | c1c05de | 2013-09-17 23:45:22 -0700 | [diff] [blame] | 645 | */ |
Pablo Ceballos | 021623b | 2016-04-15 17:31:51 -0700 | [diff] [blame] | 646 | uint32_t invTransform = |
| 647 | DisplayDevice::getPrimaryDisplayOrientationTransform(); |
| 648 | |
Michael Lentine | 1440963 | 2014-08-19 11:27:30 -0700 | [diff] [blame] | 649 | uint32_t t_orientation = transform.getOrientation(); |
Mathias Agopian | c1c05de | 2013-09-17 23:45:22 -0700 | [diff] [blame] | 650 | // calculate the inverse transform |
| 651 | if (invTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) { |
| 652 | invTransform ^= NATIVE_WINDOW_TRANSFORM_FLIP_V | |
| 653 | NATIVE_WINDOW_TRANSFORM_FLIP_H; |
Michael Lentine | 1440963 | 2014-08-19 11:27:30 -0700 | [diff] [blame] | 654 | // If the transform has been rotated the axis of flip has been swapped |
| 655 | // so we need to swap which flip operations we are performing |
| 656 | bool is_h_flipped = (t_orientation & NATIVE_WINDOW_TRANSFORM_FLIP_H) != 0; |
| 657 | bool is_v_flipped = (t_orientation & NATIVE_WINDOW_TRANSFORM_FLIP_V) != 0; |
| 658 | if (is_h_flipped != is_v_flipped) { |
| 659 | t_orientation ^= NATIVE_WINDOW_TRANSFORM_FLIP_V | |
| 660 | NATIVE_WINDOW_TRANSFORM_FLIP_H; |
| 661 | } |
Mathias Agopian | c1c05de | 2013-09-17 23:45:22 -0700 | [diff] [blame] | 662 | } |
| 663 | // and apply to the current transform |
Michael Lentine | 1440963 | 2014-08-19 11:27:30 -0700 | [diff] [blame] | 664 | transform = Transform(t_orientation) * Transform(invTransform); |
Mathias Agopian | c1c05de | 2013-09-17 23:45:22 -0700 | [diff] [blame] | 665 | } |
Mathias Agopian | 29a367b | 2011-07-12 14:51:45 -0700 | [diff] [blame] | 666 | |
| 667 | // this gives us only the "orientation" component of the transform |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 668 | const uint32_t orientation = transform.getOrientation(); |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 669 | #ifdef USE_HWC2 |
| 670 | if (orientation & Transform::ROT_INVALID) { |
| 671 | // we can only handle simple transformation |
| 672 | hwcInfo.forceClientComposition = true; |
| 673 | } else { |
| 674 | auto transform = static_cast<HWC2::Transform>(orientation); |
| 675 | auto error = hwcLayer->setTransform(transform); |
| 676 | ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set transform %s: " |
| 677 | "%s (%d)", mName.string(), to_string(transform).c_str(), |
| 678 | to_string(error).c_str(), static_cast<int32_t>(error)); |
| 679 | } |
| 680 | #else |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 681 | if (orientation & Transform::ROT_INVALID) { |
| 682 | // we can only handle simple transformation |
Mathias Agopian | 3e8b853 | 2012-05-13 20:42:01 -0700 | [diff] [blame] | 683 | layer.setSkip(true); |
Mathias Agopian | a537c0f | 2011-08-02 15:51:37 -0700 | [diff] [blame] | 684 | } else { |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 685 | layer.setTransform(orientation); |
Mathias Agopian | a350ff9 | 2010-08-10 17:14:02 -0700 | [diff] [blame] | 686 | } |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 687 | #endif |
Mathias Agopian | a350ff9 | 2010-08-10 17:14:02 -0700 | [diff] [blame] | 688 | } |
| 689 | |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 690 | #ifdef USE_HWC2 |
| 691 | void Layer::forceClientComposition(int32_t hwcId) { |
| 692 | if (mHwcLayers.count(hwcId) == 0) { |
| 693 | ALOGE("forceClientComposition: no HWC layer found (%d)", hwcId); |
| 694 | return; |
| 695 | } |
| 696 | |
| 697 | mHwcLayers[hwcId].forceClientComposition = true; |
| 698 | } |
| 699 | #endif |
| 700 | |
| 701 | #ifdef USE_HWC2 |
| 702 | void Layer::setPerFrameData(const sp<const DisplayDevice>& displayDevice) { |
| 703 | // Apply this display's projection's viewport to the visible region |
| 704 | // before giving it to the HWC HAL. |
| 705 | const Transform& tr = displayDevice->getTransform(); |
| 706 | const auto& viewport = displayDevice->getViewport(); |
| 707 | Region visible = tr.transform(visibleRegion.intersect(viewport)); |
| 708 | auto hwcId = displayDevice->getHwcDisplayId(); |
| 709 | auto& hwcLayer = mHwcLayers[hwcId].layer; |
| 710 | auto error = hwcLayer->setVisibleRegion(visible); |
| 711 | if (error != HWC2::Error::None) { |
| 712 | ALOGE("[%s] Failed to set visible region: %s (%d)", mName.string(), |
| 713 | to_string(error).c_str(), static_cast<int32_t>(error)); |
| 714 | visible.dump(LOG_TAG); |
| 715 | } |
| 716 | |
| 717 | error = hwcLayer->setSurfaceDamage(surfaceDamageRegion); |
| 718 | if (error != HWC2::Error::None) { |
| 719 | ALOGE("[%s] Failed to set surface damage: %s (%d)", mName.string(), |
| 720 | to_string(error).c_str(), static_cast<int32_t>(error)); |
| 721 | surfaceDamageRegion.dump(LOG_TAG); |
| 722 | } |
| 723 | |
| 724 | auto compositionType = HWC2::Composition::Invalid; |
| 725 | if (mSidebandStream.get()) { |
| 726 | compositionType = HWC2::Composition::Sideband; |
| 727 | auto error = hwcLayer->setSidebandStream(mSidebandStream->handle()); |
| 728 | if (error != HWC2::Error::None) { |
| 729 | ALOGE("[%s] Failed to set sideband stream %p: %s (%d)", |
| 730 | mName.string(), mSidebandStream->handle(), |
| 731 | to_string(error).c_str(), static_cast<int32_t>(error)); |
| 732 | return; |
| 733 | } |
| 734 | } else { |
| 735 | if (mActiveBuffer == nullptr || mActiveBuffer->handle == nullptr) { |
| 736 | compositionType = HWC2::Composition::Client; |
| 737 | auto error = hwcLayer->setBuffer(nullptr, Fence::NO_FENCE); |
| 738 | if (error != HWC2::Error::None) { |
| 739 | ALOGE("[%s] Failed to set null buffer: %s (%d)", mName.string(), |
| 740 | to_string(error).c_str(), static_cast<int32_t>(error)); |
| 741 | return; |
| 742 | } |
| 743 | } else { |
| 744 | if (mPotentialCursor) { |
| 745 | compositionType = HWC2::Composition::Cursor; |
| 746 | } |
| 747 | auto acquireFence = mSurfaceFlingerConsumer->getCurrentFence(); |
| 748 | auto error = hwcLayer->setBuffer(mActiveBuffer->handle, |
| 749 | acquireFence); |
| 750 | if (error != HWC2::Error::None) { |
| 751 | ALOGE("[%s] Failed to set buffer %p: %s (%d)", mName.string(), |
| 752 | mActiveBuffer->handle, to_string(error).c_str(), |
| 753 | static_cast<int32_t>(error)); |
| 754 | return; |
| 755 | } |
| 756 | // If it's not a cursor, default to device composition |
| 757 | } |
| 758 | } |
| 759 | |
| 760 | if (mHwcLayers[hwcId].forceClientComposition) { |
| 761 | ALOGV("[%s] Forcing Client composition", mName.string()); |
| 762 | setCompositionType(hwcId, HWC2::Composition::Client); |
| 763 | } else if (compositionType != HWC2::Composition::Invalid) { |
| 764 | ALOGV("[%s] Requesting %s composition", mName.string(), |
| 765 | to_string(compositionType).c_str()); |
| 766 | setCompositionType(hwcId, compositionType); |
| 767 | } else { |
| 768 | ALOGV("[%s] Requesting Device composition", mName.string()); |
| 769 | setCompositionType(hwcId, HWC2::Composition::Device); |
| 770 | } |
| 771 | } |
| 772 | #else |
Mathias Agopian | 4297734 | 2012-08-05 00:40:46 -0700 | [diff] [blame] | 773 | void Layer::setPerFrameData(const sp<const DisplayDevice>& hw, |
Mathias Agopian | d3ee231 | 2012-08-02 14:01:42 -0700 | [diff] [blame] | 774 | HWComposer::HWCLayerInterface& layer) { |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 775 | // we have to set the visible region on every frame because |
| 776 | // we currently free it during onLayerDisplayed(), which is called |
| 777 | // after HWComposer::commit() -- every frame. |
| 778 | // Apply this display's projection's viewport to the visible region |
| 779 | // before giving it to the HWC HAL. |
| 780 | const Transform& tr = hw->getTransform(); |
| 781 | Region visible = tr.transform(visibleRegion.intersect(hw->getViewport())); |
| 782 | layer.setVisibleRegionScreen(visible); |
Dan Stoza | db4850c | 2015-06-25 16:10:18 -0700 | [diff] [blame] | 783 | layer.setSurfaceDamage(surfaceDamageRegion); |
Pablo Ceballos | 40845df | 2016-01-25 17:41:15 -0800 | [diff] [blame] | 784 | mIsGlesComposition = (layer.getCompositionType() == HWC_FRAMEBUFFER); |
Dan Stoza | ee44edd | 2015-03-23 15:50:23 -0700 | [diff] [blame] | 785 | |
Jesse Hall | 399184a | 2014-03-03 15:42:54 -0800 | [diff] [blame] | 786 | if (mSidebandStream.get()) { |
| 787 | layer.setSidebandStream(mSidebandStream); |
| 788 | } else { |
| 789 | // NOTE: buffer can be NULL if the client never drew into this |
| 790 | // layer yet, or if we ran out of memory |
| 791 | layer.setBuffer(mActiveBuffer); |
| 792 | } |
Jesse Hall | c5c5a14 | 2012-07-02 16:49:28 -0700 | [diff] [blame] | 793 | } |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 794 | #endif |
Jesse Hall | dc5b485 | 2012-06-29 15:21:18 -0700 | [diff] [blame] | 795 | |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 796 | #ifdef USE_HWC2 |
| 797 | void Layer::updateCursorPosition(const sp<const DisplayDevice>& displayDevice) { |
| 798 | auto hwcId = displayDevice->getHwcDisplayId(); |
| 799 | if (mHwcLayers.count(hwcId) == 0 || |
| 800 | getCompositionType(hwcId) != HWC2::Composition::Cursor) { |
| 801 | return; |
| 802 | } |
| 803 | |
| 804 | // This gives us only the "orientation" component of the transform |
| 805 | const State& s(getCurrentState()); |
| 806 | |
| 807 | // Apply the layer's transform, followed by the display's global transform |
| 808 | // Here we're guaranteed that the layer's transform preserves rects |
| 809 | Rect win(s.active.w, s.active.h); |
Robert Carr | b5d3d26 | 2016-03-25 15:08:13 -0700 | [diff] [blame] | 810 | if (!s.crop.isEmpty()) { |
| 811 | win.intersect(s.crop, &win); |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 812 | } |
| 813 | // Subtract the transparent region and snap to the bounds |
| 814 | Rect bounds = reduce(win, s.activeTransparentRegion); |
Dan Stoza | baf416d | 2016-03-10 11:57:08 -0800 | [diff] [blame] | 815 | Rect frame(s.active.transform.transform(bounds)); |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 816 | frame.intersect(displayDevice->getViewport(), &frame); |
Robert Carr | b5d3d26 | 2016-03-25 15:08:13 -0700 | [diff] [blame] | 817 | if (!s.finalCrop.isEmpty()) { |
| 818 | frame.intersect(s.finalCrop, &frame); |
Pablo Ceballos | acbe678 | 2016-03-04 17:54:21 +0000 | [diff] [blame] | 819 | } |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 820 | auto& displayTransform(displayDevice->getTransform()); |
| 821 | auto position = displayTransform.transform(frame); |
| 822 | |
| 823 | auto error = mHwcLayers[hwcId].layer->setCursorPosition(position.left, |
| 824 | position.top); |
| 825 | ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set cursor position " |
| 826 | "to (%d, %d): %s (%d)", mName.string(), position.left, |
| 827 | position.top, to_string(error).c_str(), |
| 828 | static_cast<int32_t>(error)); |
| 829 | } |
| 830 | #else |
Dan Stoza | c701401 | 2014-02-14 15:03:43 -0800 | [diff] [blame] | 831 | void Layer::setAcquireFence(const sp<const DisplayDevice>& /* hw */, |
Mathias Agopian | d3ee231 | 2012-08-02 14:01:42 -0700 | [diff] [blame] | 832 | HWComposer::HWCLayerInterface& layer) { |
Jesse Hall | c5c5a14 | 2012-07-02 16:49:28 -0700 | [diff] [blame] | 833 | int fenceFd = -1; |
Mathias Agopian | d3ee231 | 2012-08-02 14:01:42 -0700 | [diff] [blame] | 834 | |
| 835 | // TODO: there is a possible optimization here: we only need to set the |
| 836 | // acquire fence the first time a new buffer is acquired on EACH display. |
| 837 | |
Riley Andrews | 03414a1 | 2014-07-01 14:22:59 -0700 | [diff] [blame] | 838 | if (layer.getCompositionType() == HWC_OVERLAY || layer.getCompositionType() == HWC_CURSOR_OVERLAY) { |
Andy McFadden | bf974ab | 2012-12-04 16:51:15 -0800 | [diff] [blame] | 839 | sp<Fence> fence = mSurfaceFlingerConsumer->getCurrentFence(); |
Jamie Gennis | 1df8c34 | 2012-12-20 14:05:45 -0800 | [diff] [blame] | 840 | if (fence->isValid()) { |
Jesse Hall | c5c5a14 | 2012-07-02 16:49:28 -0700 | [diff] [blame] | 841 | fenceFd = fence->dup(); |
Jesse Hall | dc5b485 | 2012-06-29 15:21:18 -0700 | [diff] [blame] | 842 | if (fenceFd == -1) { |
| 843 | ALOGW("failed to dup layer fence, skipping sync: %d", errno); |
| 844 | } |
Jesse Hall | dc5b485 | 2012-06-29 15:21:18 -0700 | [diff] [blame] | 845 | } |
Jesse Hall | dc5b485 | 2012-06-29 15:21:18 -0700 | [diff] [blame] | 846 | } |
Jesse Hall | c5c5a14 | 2012-07-02 16:49:28 -0700 | [diff] [blame] | 847 | layer.setAcquireFenceFd(fenceFd); |
Mathias Agopian | a350ff9 | 2010-08-10 17:14:02 -0700 | [diff] [blame] | 848 | } |
| 849 | |
Riley Andrews | 03414a1 | 2014-07-01 14:22:59 -0700 | [diff] [blame] | 850 | Rect Layer::getPosition( |
| 851 | const sp<const DisplayDevice>& hw) |
| 852 | { |
| 853 | // this gives us only the "orientation" component of the transform |
| 854 | const State& s(getCurrentState()); |
| 855 | |
| 856 | // apply the layer's transform, followed by the display's global transform |
| 857 | // here we're guaranteed that the layer's transform preserves rects |
| 858 | Rect win(s.active.w, s.active.h); |
Robert Carr | b5d3d26 | 2016-03-25 15:08:13 -0700 | [diff] [blame] | 859 | if (!s.crop.isEmpty()) { |
| 860 | win.intersect(s.crop, &win); |
Riley Andrews | 03414a1 | 2014-07-01 14:22:59 -0700 | [diff] [blame] | 861 | } |
| 862 | // subtract the transparent region and snap to the bounds |
| 863 | Rect bounds = reduce(win, s.activeTransparentRegion); |
Robert Carr | 3dcabfa | 2016-03-01 18:36:58 -0800 | [diff] [blame] | 864 | Rect frame(s.active.transform.transform(bounds)); |
Riley Andrews | 03414a1 | 2014-07-01 14:22:59 -0700 | [diff] [blame] | 865 | frame.intersect(hw->getViewport(), &frame); |
Robert Carr | b5d3d26 | 2016-03-25 15:08:13 -0700 | [diff] [blame] | 866 | if (!s.finalCrop.isEmpty()) { |
| 867 | frame.intersect(s.finalCrop, &frame); |
Pablo Ceballos | acbe678 | 2016-03-04 17:54:21 +0000 | [diff] [blame] | 868 | } |
Riley Andrews | 03414a1 | 2014-07-01 14:22:59 -0700 | [diff] [blame] | 869 | const Transform& tr(hw->getTransform()); |
| 870 | return Rect(tr.transform(frame)); |
| 871 | } |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 872 | #endif |
Riley Andrews | 03414a1 | 2014-07-01 14:22:59 -0700 | [diff] [blame] | 873 | |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 874 | // --------------------------------------------------------------------------- |
| 875 | // drawing... |
| 876 | // --------------------------------------------------------------------------- |
| 877 | |
| 878 | void Layer::draw(const sp<const DisplayDevice>& hw, const Region& clip) const { |
Dan Stoza | c701401 | 2014-02-14 15:03:43 -0800 | [diff] [blame] | 879 | onDraw(hw, clip, false); |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 880 | } |
| 881 | |
Dan Stoza | c701401 | 2014-02-14 15:03:43 -0800 | [diff] [blame] | 882 | void Layer::draw(const sp<const DisplayDevice>& hw, |
| 883 | bool useIdentityTransform) const { |
| 884 | onDraw(hw, Region(hw->bounds()), useIdentityTransform); |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 885 | } |
| 886 | |
Dan Stoza | c701401 | 2014-02-14 15:03:43 -0800 | [diff] [blame] | 887 | void Layer::draw(const sp<const DisplayDevice>& hw) const { |
| 888 | onDraw(hw, Region(hw->bounds()), false); |
| 889 | } |
| 890 | |
| 891 | void Layer::onDraw(const sp<const DisplayDevice>& hw, const Region& clip, |
| 892 | bool useIdentityTransform) const |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 893 | { |
Jamie Gennis | 1c8e95c | 2012-02-23 19:27:23 -0800 | [diff] [blame] | 894 | ATRACE_CALL(); |
| 895 | |
Mathias Agopian | a67932f | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 896 | if (CC_UNLIKELY(mActiveBuffer == 0)) { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 897 | // the texture has not been created yet, this Layer has |
Mathias Agopian | 179169e | 2010-05-06 20:21:45 -0700 | [diff] [blame] | 898 | // in fact never been drawn into. This happens frequently with |
| 899 | // SurfaceView because the WindowManager can't know when the client |
| 900 | // has drawn the first time. |
| 901 | |
| 902 | // If there is nothing under us, we paint the screen in black, otherwise |
| 903 | // we just skip this update. |
| 904 | |
| 905 | // figure out if there is something below us |
| 906 | Region under; |
Mathias Agopian | f7ae69d | 2011-08-23 12:34:29 -0700 | [diff] [blame] | 907 | const SurfaceFlinger::LayerVector& drawingLayers( |
| 908 | mFlinger->mDrawingState.layersSortedByZ); |
Mathias Agopian | 179169e | 2010-05-06 20:21:45 -0700 | [diff] [blame] | 909 | const size_t count = drawingLayers.size(); |
| 910 | for (size_t i=0 ; i<count ; ++i) { |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 911 | const sp<Layer>& layer(drawingLayers[i]); |
| 912 | if (layer.get() == static_cast<Layer const*>(this)) |
Mathias Agopian | 179169e | 2010-05-06 20:21:45 -0700 | [diff] [blame] | 913 | break; |
Mathias Agopian | 4297734 | 2012-08-05 00:40:46 -0700 | [diff] [blame] | 914 | under.orSelf( hw->getTransform().transform(layer->visibleRegion) ); |
Mathias Agopian | 179169e | 2010-05-06 20:21:45 -0700 | [diff] [blame] | 915 | } |
| 916 | // if not everything below us is covered, we plug the holes! |
| 917 | Region holes(clip.subtract(under)); |
| 918 | if (!holes.isEmpty()) { |
Mathias Agopian | 1b03149 | 2012-06-20 17:51:20 -0700 | [diff] [blame] | 919 | clearWithOpenGL(hw, holes, 0, 0, 0, 1); |
Mathias Agopian | 179169e | 2010-05-06 20:21:45 -0700 | [diff] [blame] | 920 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 921 | return; |
| 922 | } |
Mathias Agopian | a67932f | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 923 | |
Andy McFadden | 97eba89 | 2012-12-11 15:21:45 -0800 | [diff] [blame] | 924 | // Bind the current buffer to the GL texture, and wait for it to be |
| 925 | // ready for us to draw into. |
Andy McFadden | bf974ab | 2012-12-04 16:51:15 -0800 | [diff] [blame] | 926 | status_t err = mSurfaceFlingerConsumer->bindTextureImage(); |
| 927 | if (err != NO_ERROR) { |
Andy McFadden | 97eba89 | 2012-12-11 15:21:45 -0800 | [diff] [blame] | 928 | ALOGW("onDraw: bindTextureImage failed (err=%d)", err); |
Jesse Hall | dc5b485 | 2012-06-29 15:21:18 -0700 | [diff] [blame] | 929 | // Go ahead and draw the buffer anyway; no matter what we do the screen |
| 930 | // is probably going to have something visibly wrong. |
| 931 | } |
| 932 | |
Jamie Gennis | dd3cb84 | 2012-10-19 18:19:11 -0700 | [diff] [blame] | 933 | bool blackOutLayer = isProtected() || (isSecure() && !hw->isSecure()); |
| 934 | |
Mathias Agopian | 875d8e1 | 2013-06-07 15:35:48 -0700 | [diff] [blame] | 935 | RenderEngine& engine(mFlinger->getRenderEngine()); |
| 936 | |
Jamie Gennis | dd3cb84 | 2012-10-19 18:19:11 -0700 | [diff] [blame] | 937 | if (!blackOutLayer) { |
Jamie Gennis | cbb1a95 | 2012-05-08 17:05:52 -0700 | [diff] [blame] | 938 | // TODO: we could be more subtle with isFixedSize() |
Mathias Agopian | eba8c68 | 2012-09-19 23:14:45 -0700 | [diff] [blame] | 939 | const bool useFiltering = getFiltering() || needsFiltering(hw) || isFixedSize(); |
Jamie Gennis | cbb1a95 | 2012-05-08 17:05:52 -0700 | [diff] [blame] | 940 | |
| 941 | // Query the texture matrix given our current filtering mode. |
| 942 | float textureMatrix[16]; |
Andy McFadden | bf974ab | 2012-12-04 16:51:15 -0800 | [diff] [blame] | 943 | mSurfaceFlingerConsumer->setFilteringEnabled(useFiltering); |
| 944 | mSurfaceFlingerConsumer->getTransformMatrix(textureMatrix); |
Jamie Gennis | cbb1a95 | 2012-05-08 17:05:52 -0700 | [diff] [blame] | 945 | |
Mathias Agopian | c1c05de | 2013-09-17 23:45:22 -0700 | [diff] [blame] | 946 | if (mSurfaceFlingerConsumer->getTransformToDisplayInverse()) { |
| 947 | |
| 948 | /* |
Pablo Ceballos | 021623b | 2016-04-15 17:31:51 -0700 | [diff] [blame] | 949 | * the code below applies the primary display's inverse transform to |
| 950 | * the texture transform |
Mathias Agopian | c1c05de | 2013-09-17 23:45:22 -0700 | [diff] [blame] | 951 | */ |
| 952 | |
| 953 | // create a 4x4 transform matrix from the display transform flags |
| 954 | const mat4 flipH(-1,0,0,0, 0,1,0,0, 0,0,1,0, 1,0,0,1); |
| 955 | const mat4 flipV( 1,0,0,0, 0,-1,0,0, 0,0,1,0, 0,1,0,1); |
| 956 | const mat4 rot90( 0,1,0,0, -1,0,0,0, 0,0,1,0, 1,0,0,1); |
| 957 | |
| 958 | mat4 tr; |
Pablo Ceballos | 021623b | 2016-04-15 17:31:51 -0700 | [diff] [blame] | 959 | uint32_t transform = |
| 960 | DisplayDevice::getPrimaryDisplayOrientationTransform(); |
Mathias Agopian | c1c05de | 2013-09-17 23:45:22 -0700 | [diff] [blame] | 961 | if (transform & NATIVE_WINDOW_TRANSFORM_ROT_90) |
| 962 | tr = tr * rot90; |
| 963 | if (transform & NATIVE_WINDOW_TRANSFORM_FLIP_H) |
| 964 | tr = tr * flipH; |
| 965 | if (transform & NATIVE_WINDOW_TRANSFORM_FLIP_V) |
| 966 | tr = tr * flipV; |
| 967 | |
| 968 | // calculate the inverse |
| 969 | tr = inverse(tr); |
| 970 | |
| 971 | // and finally apply it to the original texture matrix |
| 972 | const mat4 texTransform(mat4(static_cast<const float*>(textureMatrix)) * tr); |
| 973 | memcpy(textureMatrix, texTransform.asArray(), sizeof(textureMatrix)); |
| 974 | } |
| 975 | |
Jamie Gennis | cbb1a95 | 2012-05-08 17:05:52 -0700 | [diff] [blame] | 976 | // Set things up for texturing. |
Mathias Agopian | 49457ac | 2013-08-14 18:20:17 -0700 | [diff] [blame] | 977 | mTexture.setDimensions(mActiveBuffer->getWidth(), mActiveBuffer->getHeight()); |
| 978 | mTexture.setFiltering(useFiltering); |
| 979 | mTexture.setMatrix(textureMatrix); |
| 980 | |
| 981 | engine.setupLayerTexturing(mTexture); |
Mathias Agopian | a67932f | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 982 | } else { |
Mathias Agopian | 875d8e1 | 2013-06-07 15:35:48 -0700 | [diff] [blame] | 983 | engine.setupLayerBlackedOut(); |
Mathias Agopian | a67932f | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 984 | } |
Dan Stoza | c701401 | 2014-02-14 15:03:43 -0800 | [diff] [blame] | 985 | drawWithOpenGL(hw, clip, useIdentityTransform); |
Mathias Agopian | 875d8e1 | 2013-06-07 15:35:48 -0700 | [diff] [blame] | 986 | engine.disableTexturing(); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 987 | } |
| 988 | |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 989 | |
Dan Stoza | c701401 | 2014-02-14 15:03:43 -0800 | [diff] [blame] | 990 | void Layer::clearWithOpenGL(const sp<const DisplayDevice>& hw, |
| 991 | const Region& /* clip */, float red, float green, float blue, |
| 992 | float alpha) const |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 993 | { |
Mathias Agopian | 19733a3 | 2013-08-28 18:13:56 -0700 | [diff] [blame] | 994 | RenderEngine& engine(mFlinger->getRenderEngine()); |
Dan Stoza | c701401 | 2014-02-14 15:03:43 -0800 | [diff] [blame] | 995 | computeGeometry(hw, mMesh, false); |
Mathias Agopian | 19733a3 | 2013-08-28 18:13:56 -0700 | [diff] [blame] | 996 | engine.setupFillWithColor(red, green, blue, alpha); |
| 997 | engine.drawMesh(mMesh); |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 998 | } |
| 999 | |
| 1000 | void Layer::clearWithOpenGL( |
| 1001 | const sp<const DisplayDevice>& hw, const Region& clip) const { |
| 1002 | clearWithOpenGL(hw, clip, 0,0,0,0); |
| 1003 | } |
| 1004 | |
Dan Stoza | c701401 | 2014-02-14 15:03:43 -0800 | [diff] [blame] | 1005 | void Layer::drawWithOpenGL(const sp<const DisplayDevice>& hw, |
| 1006 | const Region& /* clip */, bool useIdentityTransform) const { |
Mathias Agopian | 1eae0ee | 2013-06-05 16:59:15 -0700 | [diff] [blame] | 1007 | const State& s(getDrawingState()); |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 1008 | |
Dan Stoza | c701401 | 2014-02-14 15:03:43 -0800 | [diff] [blame] | 1009 | computeGeometry(hw, mMesh, useIdentityTransform); |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 1010 | |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 1011 | /* |
| 1012 | * NOTE: the way we compute the texture coordinates here produces |
| 1013 | * different results than when we take the HWC path -- in the later case |
| 1014 | * the "source crop" is rounded to texel boundaries. |
| 1015 | * This can produce significantly different results when the texture |
| 1016 | * is scaled by a large amount. |
| 1017 | * |
| 1018 | * The GL code below is more logical (imho), and the difference with |
| 1019 | * HWC is due to a limitation of the HWC API to integers -- a question |
Mathias Agopian | c1c05de | 2013-09-17 23:45:22 -0700 | [diff] [blame] | 1020 | * is suspend is whether we should ignore this problem or revert to |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 1021 | * GL composition when a buffer scaling is applied (maybe with some |
| 1022 | * minimal value)? Or, we could make GL behave like HWC -- but this feel |
| 1023 | * like more of a hack. |
| 1024 | */ |
Pablo Ceballos | acbe678 | 2016-03-04 17:54:21 +0000 | [diff] [blame] | 1025 | Rect win(computeBounds()); |
| 1026 | |
Robert Carr | b5d3d26 | 2016-03-25 15:08:13 -0700 | [diff] [blame] | 1027 | if (!s.finalCrop.isEmpty()) { |
Pablo Ceballos | acbe678 | 2016-03-04 17:54:21 +0000 | [diff] [blame] | 1028 | win = s.active.transform.transform(win); |
Robert Carr | b5d3d26 | 2016-03-25 15:08:13 -0700 | [diff] [blame] | 1029 | if (!win.intersect(s.finalCrop, &win)) { |
Pablo Ceballos | acbe678 | 2016-03-04 17:54:21 +0000 | [diff] [blame] | 1030 | win.clear(); |
| 1031 | } |
| 1032 | win = s.active.transform.inverse().transform(win); |
| 1033 | if (!win.intersect(computeBounds(), &win)) { |
| 1034 | win.clear(); |
| 1035 | } |
| 1036 | } |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 1037 | |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 1038 | float left = float(win.left) / float(s.active.w); |
| 1039 | float top = float(win.top) / float(s.active.h); |
| 1040 | float right = float(win.right) / float(s.active.w); |
| 1041 | float bottom = float(win.bottom) / float(s.active.h); |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 1042 | |
Mathias Agopian | 875d8e1 | 2013-06-07 15:35:48 -0700 | [diff] [blame] | 1043 | // TODO: we probably want to generate the texture coords with the mesh |
| 1044 | // here we assume that we only have 4 vertices |
Mathias Agopian | ff2ed70 | 2013-09-01 21:36:12 -0700 | [diff] [blame] | 1045 | Mesh::VertexArray<vec2> texCoords(mMesh.getTexCoordArray<vec2>()); |
| 1046 | texCoords[0] = vec2(left, 1.0f - top); |
| 1047 | texCoords[1] = vec2(left, 1.0f - bottom); |
| 1048 | texCoords[2] = vec2(right, 1.0f - bottom); |
| 1049 | texCoords[3] = vec2(right, 1.0f - top); |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 1050 | |
Mathias Agopian | 875d8e1 | 2013-06-07 15:35:48 -0700 | [diff] [blame] | 1051 | RenderEngine& engine(mFlinger->getRenderEngine()); |
Andy McFadden | 4125a4f | 2014-01-29 17:17:11 -0800 | [diff] [blame] | 1052 | engine.setupLayerBlending(mPremultipliedAlpha, isOpaque(s), s.alpha); |
Mathias Agopian | 5cdc899 | 2013-08-13 20:51:23 -0700 | [diff] [blame] | 1053 | engine.drawMesh(mMesh); |
Mathias Agopian | 875d8e1 | 2013-06-07 15:35:48 -0700 | [diff] [blame] | 1054 | engine.disableBlending(); |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 1055 | } |
| 1056 | |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 1057 | #ifdef USE_HWC2 |
| 1058 | void Layer::setCompositionType(int32_t hwcId, HWC2::Composition type, |
| 1059 | bool callIntoHwc) { |
| 1060 | if (mHwcLayers.count(hwcId) == 0) { |
| 1061 | ALOGE("setCompositionType called without a valid HWC layer"); |
| 1062 | return; |
| 1063 | } |
| 1064 | auto& hwcInfo = mHwcLayers[hwcId]; |
| 1065 | auto& hwcLayer = hwcInfo.layer; |
| 1066 | ALOGV("setCompositionType(%" PRIx64 ", %s, %d)", hwcLayer->getId(), |
| 1067 | to_string(type).c_str(), static_cast<int>(callIntoHwc)); |
| 1068 | if (hwcInfo.compositionType != type) { |
| 1069 | ALOGV(" actually setting"); |
| 1070 | hwcInfo.compositionType = type; |
| 1071 | if (callIntoHwc) { |
| 1072 | auto error = hwcLayer->setCompositionType(type); |
| 1073 | ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set " |
| 1074 | "composition type %s: %s (%d)", mName.string(), |
| 1075 | to_string(type).c_str(), to_string(error).c_str(), |
| 1076 | static_cast<int32_t>(error)); |
| 1077 | } |
| 1078 | } |
| 1079 | } |
| 1080 | |
| 1081 | HWC2::Composition Layer::getCompositionType(int32_t hwcId) const { |
| 1082 | if (mHwcLayers.count(hwcId) == 0) { |
| 1083 | ALOGE("getCompositionType called without a valid HWC layer"); |
| 1084 | return HWC2::Composition::Invalid; |
| 1085 | } |
| 1086 | return mHwcLayers.at(hwcId).compositionType; |
| 1087 | } |
| 1088 | |
| 1089 | void Layer::setClearClientTarget(int32_t hwcId, bool clear) { |
| 1090 | if (mHwcLayers.count(hwcId) == 0) { |
| 1091 | ALOGE("setClearClientTarget called without a valid HWC layer"); |
| 1092 | return; |
| 1093 | } |
| 1094 | mHwcLayers[hwcId].clearClientTarget = clear; |
| 1095 | } |
| 1096 | |
| 1097 | bool Layer::getClearClientTarget(int32_t hwcId) const { |
| 1098 | if (mHwcLayers.count(hwcId) == 0) { |
| 1099 | ALOGE("getClearClientTarget called without a valid HWC layer"); |
| 1100 | return false; |
| 1101 | } |
| 1102 | return mHwcLayers.at(hwcId).clearClientTarget; |
| 1103 | } |
| 1104 | #endif |
| 1105 | |
Ruben Brunk | 1681d95 | 2014-06-27 15:51:55 -0700 | [diff] [blame] | 1106 | uint32_t Layer::getProducerStickyTransform() const { |
| 1107 | int producerStickyTransform = 0; |
| 1108 | int ret = mProducer->query(NATIVE_WINDOW_STICKY_TRANSFORM, &producerStickyTransform); |
| 1109 | if (ret != OK) { |
| 1110 | ALOGW("%s: Error %s (%d) while querying window sticky transform.", __FUNCTION__, |
| 1111 | strerror(-ret), ret); |
| 1112 | return 0; |
| 1113 | } |
| 1114 | return static_cast<uint32_t>(producerStickyTransform); |
| 1115 | } |
| 1116 | |
Dan Stoza | cac3538 | 2016-01-27 12:21:06 -0800 | [diff] [blame] | 1117 | uint64_t Layer::getHeadFrameNumber() const { |
| 1118 | Mutex::Autolock lock(mQueueItemLock); |
| 1119 | if (!mQueueItems.empty()) { |
| 1120 | return mQueueItems[0].mFrameNumber; |
| 1121 | } else { |
| 1122 | return mCurrentFrameNumber; |
| 1123 | } |
| 1124 | } |
| 1125 | |
| 1126 | bool Layer::addSyncPoint(const std::shared_ptr<SyncPoint>& point) { |
| 1127 | if (point->getFrameNumber() <= mCurrentFrameNumber) { |
| 1128 | // Don't bother with a SyncPoint, since we've already latched the |
| 1129 | // relevant frame |
| 1130 | return false; |
Dan Stoza | 7dde599 | 2015-05-22 09:51:44 -0700 | [diff] [blame] | 1131 | } |
| 1132 | |
Dan Stoza | cac3538 | 2016-01-27 12:21:06 -0800 | [diff] [blame] | 1133 | Mutex::Autolock lock(mLocalSyncPointMutex); |
| 1134 | mLocalSyncPoints.push_back(point); |
| 1135 | return true; |
Dan Stoza | 7dde599 | 2015-05-22 09:51:44 -0700 | [diff] [blame] | 1136 | } |
| 1137 | |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 1138 | void Layer::setFiltering(bool filtering) { |
| 1139 | mFiltering = filtering; |
| 1140 | } |
| 1141 | |
| 1142 | bool Layer::getFiltering() const { |
| 1143 | return mFiltering; |
| 1144 | } |
| 1145 | |
Eric Hassold | ac45e6b | 2011-02-10 14:41:26 -0800 | [diff] [blame] | 1146 | // As documented in libhardware header, formats in the range |
| 1147 | // 0x100 - 0x1FF are specific to the HAL implementation, and |
| 1148 | // are known to have no alpha channel |
| 1149 | // TODO: move definition for device-specific range into |
| 1150 | // hardware.h, instead of using hard-coded values here. |
| 1151 | #define HARDWARE_IS_DEVICE_FORMAT(f) ((f) >= 0x100 && (f) <= 0x1FF) |
| 1152 | |
Mathias Agopian | 5773d3f | 2013-07-25 19:24:31 -0700 | [diff] [blame] | 1153 | bool Layer::getOpacityForFormat(uint32_t format) { |
Mathias Agopian | a67932f | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 1154 | if (HARDWARE_IS_DEVICE_FORMAT(format)) { |
| 1155 | return true; |
Eric Hassold | ac45e6b | 2011-02-10 14:41:26 -0800 | [diff] [blame] | 1156 | } |
Mathias Agopian | 5773d3f | 2013-07-25 19:24:31 -0700 | [diff] [blame] | 1157 | switch (format) { |
| 1158 | case HAL_PIXEL_FORMAT_RGBA_8888: |
| 1159 | case HAL_PIXEL_FORMAT_BGRA_8888: |
Mathias Agopian | dd53371 | 2013-07-26 15:31:39 -0700 | [diff] [blame] | 1160 | return false; |
Mathias Agopian | 5773d3f | 2013-07-25 19:24:31 -0700 | [diff] [blame] | 1161 | } |
| 1162 | // in all other case, we have no blending (also for unknown formats) |
Mathias Agopian | dd53371 | 2013-07-26 15:31:39 -0700 | [diff] [blame] | 1163 | return true; |
Eric Hassold | ac45e6b | 2011-02-10 14:41:26 -0800 | [diff] [blame] | 1164 | } |
| 1165 | |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 1166 | // ---------------------------------------------------------------------------- |
| 1167 | // local state |
| 1168 | // ---------------------------------------------------------------------------- |
| 1169 | |
Pablo Ceballos | acbe678 | 2016-03-04 17:54:21 +0000 | [diff] [blame] | 1170 | static void boundPoint(vec2* point, const Rect& crop) { |
| 1171 | if (point->x < crop.left) { |
| 1172 | point->x = crop.left; |
| 1173 | } |
| 1174 | if (point->x > crop.right) { |
| 1175 | point->x = crop.right; |
| 1176 | } |
| 1177 | if (point->y < crop.top) { |
| 1178 | point->y = crop.top; |
| 1179 | } |
| 1180 | if (point->y > crop.bottom) { |
| 1181 | point->y = crop.bottom; |
| 1182 | } |
| 1183 | } |
| 1184 | |
Dan Stoza | c701401 | 2014-02-14 15:03:43 -0800 | [diff] [blame] | 1185 | void Layer::computeGeometry(const sp<const DisplayDevice>& hw, Mesh& mesh, |
| 1186 | bool useIdentityTransform) const |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 1187 | { |
Mathias Agopian | 1eae0ee | 2013-06-05 16:59:15 -0700 | [diff] [blame] | 1188 | const Layer::State& s(getDrawingState()); |
Pablo Ceballos | acbe678 | 2016-03-04 17:54:21 +0000 | [diff] [blame] | 1189 | const Transform tr(hw->getTransform()); |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 1190 | const uint32_t hw_h = hw->getHeight(); |
| 1191 | Rect win(s.active.w, s.active.h); |
Robert Carr | b5d3d26 | 2016-03-25 15:08:13 -0700 | [diff] [blame] | 1192 | if (!s.crop.isEmpty()) { |
| 1193 | win.intersect(s.crop, &win); |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 1194 | } |
Mathias Agopian | 6c7f25a | 2013-05-09 20:37:10 -0700 | [diff] [blame] | 1195 | // subtract the transparent region and snap to the bounds |
Mathias Agopian | f3e85d4 | 2013-05-10 18:01:12 -0700 | [diff] [blame] | 1196 | win = reduce(win, s.activeTransparentRegion); |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 1197 | |
Pablo Ceballos | acbe678 | 2016-03-04 17:54:21 +0000 | [diff] [blame] | 1198 | vec2 lt = vec2(win.left, win.top); |
| 1199 | vec2 lb = vec2(win.left, win.bottom); |
| 1200 | vec2 rb = vec2(win.right, win.bottom); |
| 1201 | vec2 rt = vec2(win.right, win.top); |
| 1202 | |
| 1203 | if (!useIdentityTransform) { |
| 1204 | lt = s.active.transform.transform(lt); |
| 1205 | lb = s.active.transform.transform(lb); |
| 1206 | rb = s.active.transform.transform(rb); |
| 1207 | rt = s.active.transform.transform(rt); |
| 1208 | } |
| 1209 | |
Robert Carr | b5d3d26 | 2016-03-25 15:08:13 -0700 | [diff] [blame] | 1210 | if (!s.finalCrop.isEmpty()) { |
| 1211 | boundPoint(<, s.finalCrop); |
| 1212 | boundPoint(&lb, s.finalCrop); |
| 1213 | boundPoint(&rb, s.finalCrop); |
| 1214 | boundPoint(&rt, s.finalCrop); |
Pablo Ceballos | acbe678 | 2016-03-04 17:54:21 +0000 | [diff] [blame] | 1215 | } |
| 1216 | |
Mathias Agopian | ff2ed70 | 2013-09-01 21:36:12 -0700 | [diff] [blame] | 1217 | Mesh::VertexArray<vec2> position(mesh.getPositionArray<vec2>()); |
Pablo Ceballos | acbe678 | 2016-03-04 17:54:21 +0000 | [diff] [blame] | 1218 | position[0] = tr.transform(lt); |
| 1219 | position[1] = tr.transform(lb); |
| 1220 | position[2] = tr.transform(rb); |
| 1221 | position[3] = tr.transform(rt); |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 1222 | for (size_t i=0 ; i<4 ; i++) { |
Mathias Agopian | 5cdc899 | 2013-08-13 20:51:23 -0700 | [diff] [blame] | 1223 | position[i].y = hw_h - position[i].y; |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 1224 | } |
| 1225 | } |
Eric Hassold | ac45e6b | 2011-02-10 14:41:26 -0800 | [diff] [blame] | 1226 | |
Andy McFadden | 4125a4f | 2014-01-29 17:17:11 -0800 | [diff] [blame] | 1227 | bool Layer::isOpaque(const Layer::State& s) const |
Mathias Agopian | a7f6692 | 2010-05-26 22:08:52 -0700 | [diff] [blame] | 1228 | { |
Mathias Agopian | a67932f | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 1229 | // if we don't have a buffer yet, we're translucent regardless of the |
| 1230 | // layer's opaque flag. |
Jamie Gennis | db5230f | 2011-07-28 14:54:07 -0700 | [diff] [blame] | 1231 | if (mActiveBuffer == 0) { |
Mathias Agopian | a67932f | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 1232 | return false; |
Jamie Gennis | db5230f | 2011-07-28 14:54:07 -0700 | [diff] [blame] | 1233 | } |
Mathias Agopian | a67932f | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 1234 | |
| 1235 | // if the layer has the opaque flag, then we're always opaque, |
| 1236 | // otherwise we use the current buffer's format. |
Andy McFadden | 4125a4f | 2014-01-29 17:17:11 -0800 | [diff] [blame] | 1237 | return ((s.flags & layer_state_t::eLayerOpaque) != 0) || mCurrentOpacity; |
Mathias Agopian | a7f6692 | 2010-05-26 22:08:52 -0700 | [diff] [blame] | 1238 | } |
| 1239 | |
Dan Stoza | 2311608 | 2015-06-18 14:58:39 -0700 | [diff] [blame] | 1240 | bool Layer::isSecure() const |
| 1241 | { |
| 1242 | const Layer::State& s(mDrawingState); |
| 1243 | return (s.flags & layer_state_t::eLayerSecure); |
| 1244 | } |
| 1245 | |
Jamie Gennis | 7a4d0df | 2011-03-09 17:05:02 -0800 | [diff] [blame] | 1246 | bool Layer::isProtected() const |
| 1247 | { |
Mathias Agopian | a67932f | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 1248 | const sp<GraphicBuffer>& activeBuffer(mActiveBuffer); |
Jamie Gennis | 7a4d0df | 2011-03-09 17:05:02 -0800 | [diff] [blame] | 1249 | return (activeBuffer != 0) && |
| 1250 | (activeBuffer->getUsage() & GRALLOC_USAGE_PROTECTED); |
| 1251 | } |
Mathias Agopian | b5b7f26 | 2010-05-07 15:58:44 -0700 | [diff] [blame] | 1252 | |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 1253 | bool Layer::isFixedSize() const { |
Robert Carr | c3574f7 | 2016-03-24 12:19:32 -0700 | [diff] [blame] | 1254 | return getEffectiveScalingMode() != NATIVE_WINDOW_SCALING_MODE_FREEZE; |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 1255 | } |
| 1256 | |
| 1257 | bool Layer::isCropped() const { |
| 1258 | return !mCurrentCrop.isEmpty(); |
| 1259 | } |
| 1260 | |
| 1261 | bool Layer::needsFiltering(const sp<const DisplayDevice>& hw) const { |
| 1262 | return mNeedsFiltering || hw->needsFiltering(); |
| 1263 | } |
| 1264 | |
| 1265 | void Layer::setVisibleRegion(const Region& visibleRegion) { |
| 1266 | // always called from main thread |
| 1267 | this->visibleRegion = visibleRegion; |
| 1268 | } |
| 1269 | |
| 1270 | void Layer::setCoveredRegion(const Region& coveredRegion) { |
| 1271 | // always called from main thread |
| 1272 | this->coveredRegion = coveredRegion; |
| 1273 | } |
| 1274 | |
| 1275 | void Layer::setVisibleNonTransparentRegion(const Region& |
| 1276 | setVisibleNonTransparentRegion) { |
| 1277 | // always called from main thread |
| 1278 | this->visibleNonTransparentRegion = setVisibleNonTransparentRegion; |
| 1279 | } |
| 1280 | |
| 1281 | // ---------------------------------------------------------------------------- |
| 1282 | // transaction |
| 1283 | // ---------------------------------------------------------------------------- |
| 1284 | |
Dan Stoza | 7dde599 | 2015-05-22 09:51:44 -0700 | [diff] [blame] | 1285 | void Layer::pushPendingState() { |
| 1286 | if (!mCurrentState.modified) { |
| 1287 | return; |
| 1288 | } |
| 1289 | |
Dan Stoza | 7dde599 | 2015-05-22 09:51:44 -0700 | [diff] [blame] | 1290 | // If this transaction is waiting on the receipt of a frame, generate a sync |
| 1291 | // point and send it to the remote layer. |
| 1292 | if (mCurrentState.handle != nullptr) { |
| 1293 | sp<Handle> handle = static_cast<Handle*>(mCurrentState.handle.get()); |
| 1294 | sp<Layer> handleLayer = handle->owner.promote(); |
| 1295 | if (handleLayer == nullptr) { |
| 1296 | ALOGE("[%s] Unable to promote Layer handle", mName.string()); |
| 1297 | // If we can't promote the layer we are intended to wait on, |
| 1298 | // then it is expired or otherwise invalid. Allow this transaction |
| 1299 | // to be applied as per normal (no synchronization). |
| 1300 | mCurrentState.handle = nullptr; |
Pablo Ceballos | 3bddd5b | 2015-11-19 14:39:14 -0800 | [diff] [blame] | 1301 | } else { |
| 1302 | auto syncPoint = std::make_shared<SyncPoint>( |
| 1303 | mCurrentState.frameNumber); |
Dan Stoza | cac3538 | 2016-01-27 12:21:06 -0800 | [diff] [blame] | 1304 | if (handleLayer->addSyncPoint(syncPoint)) { |
| 1305 | mRemoteSyncPoints.push_back(std::move(syncPoint)); |
| 1306 | } else { |
| 1307 | // We already missed the frame we're supposed to synchronize |
| 1308 | // on, so go ahead and apply the state update |
| 1309 | mCurrentState.handle = nullptr; |
| 1310 | } |
Dan Stoza | 7dde599 | 2015-05-22 09:51:44 -0700 | [diff] [blame] | 1311 | } |
| 1312 | |
Dan Stoza | 7dde599 | 2015-05-22 09:51:44 -0700 | [diff] [blame] | 1313 | // Wake us up to check if the frame has been received |
| 1314 | setTransactionFlags(eTransactionNeeded); |
| 1315 | } |
| 1316 | mPendingStates.push_back(mCurrentState); |
| 1317 | } |
| 1318 | |
Pablo Ceballos | 05289c2 | 2016-04-14 15:49:55 -0700 | [diff] [blame] | 1319 | void Layer::popPendingState(State* stateToCommit) { |
| 1320 | auto oldFlags = stateToCommit->flags; |
| 1321 | *stateToCommit = mPendingStates[0]; |
| 1322 | stateToCommit->flags = (oldFlags & ~stateToCommit->mask) | |
| 1323 | (stateToCommit->flags & stateToCommit->mask); |
Dan Stoza | 7dde599 | 2015-05-22 09:51:44 -0700 | [diff] [blame] | 1324 | |
| 1325 | mPendingStates.removeAt(0); |
| 1326 | } |
| 1327 | |
Pablo Ceballos | 05289c2 | 2016-04-14 15:49:55 -0700 | [diff] [blame] | 1328 | bool Layer::applyPendingStates(State* stateToCommit) { |
Dan Stoza | 7dde599 | 2015-05-22 09:51:44 -0700 | [diff] [blame] | 1329 | bool stateUpdateAvailable = false; |
| 1330 | while (!mPendingStates.empty()) { |
| 1331 | if (mPendingStates[0].handle != nullptr) { |
| 1332 | if (mRemoteSyncPoints.empty()) { |
| 1333 | // If we don't have a sync point for this, apply it anyway. It |
| 1334 | // will be visually wrong, but it should keep us from getting |
| 1335 | // into too much trouble. |
| 1336 | ALOGE("[%s] No local sync point found", mName.string()); |
Pablo Ceballos | 05289c2 | 2016-04-14 15:49:55 -0700 | [diff] [blame] | 1337 | popPendingState(stateToCommit); |
Dan Stoza | 7dde599 | 2015-05-22 09:51:44 -0700 | [diff] [blame] | 1338 | stateUpdateAvailable = true; |
| 1339 | continue; |
| 1340 | } |
| 1341 | |
Dan Stoza | cac3538 | 2016-01-27 12:21:06 -0800 | [diff] [blame] | 1342 | if (mRemoteSyncPoints.front()->getFrameNumber() != |
| 1343 | mPendingStates[0].frameNumber) { |
| 1344 | ALOGE("[%s] Unexpected sync point frame number found", |
| 1345 | mName.string()); |
| 1346 | |
| 1347 | // Signal our end of the sync point and then dispose of it |
| 1348 | mRemoteSyncPoints.front()->setTransactionApplied(); |
| 1349 | mRemoteSyncPoints.pop_front(); |
| 1350 | continue; |
| 1351 | } |
| 1352 | |
Dan Stoza | 7dde599 | 2015-05-22 09:51:44 -0700 | [diff] [blame] | 1353 | if (mRemoteSyncPoints.front()->frameIsAvailable()) { |
| 1354 | // Apply the state update |
Pablo Ceballos | 05289c2 | 2016-04-14 15:49:55 -0700 | [diff] [blame] | 1355 | popPendingState(stateToCommit); |
Dan Stoza | 7dde599 | 2015-05-22 09:51:44 -0700 | [diff] [blame] | 1356 | stateUpdateAvailable = true; |
| 1357 | |
| 1358 | // Signal our end of the sync point and then dispose of it |
| 1359 | mRemoteSyncPoints.front()->setTransactionApplied(); |
| 1360 | mRemoteSyncPoints.pop_front(); |
Dan Stoza | 792e529 | 2016-02-11 11:43:58 -0800 | [diff] [blame] | 1361 | } else { |
| 1362 | break; |
Dan Stoza | 7dde599 | 2015-05-22 09:51:44 -0700 | [diff] [blame] | 1363 | } |
Dan Stoza | 7dde599 | 2015-05-22 09:51:44 -0700 | [diff] [blame] | 1364 | } else { |
Pablo Ceballos | 05289c2 | 2016-04-14 15:49:55 -0700 | [diff] [blame] | 1365 | popPendingState(stateToCommit); |
Dan Stoza | 7dde599 | 2015-05-22 09:51:44 -0700 | [diff] [blame] | 1366 | stateUpdateAvailable = true; |
| 1367 | } |
| 1368 | } |
| 1369 | |
| 1370 | // If we still have pending updates, wake SurfaceFlinger back up and point |
| 1371 | // it at this layer so we can process them |
| 1372 | if (!mPendingStates.empty()) { |
| 1373 | setTransactionFlags(eTransactionNeeded); |
| 1374 | mFlinger->setTransactionFlags(eTraversalNeeded); |
| 1375 | } |
| 1376 | |
| 1377 | mCurrentState.modified = false; |
| 1378 | return stateUpdateAvailable; |
| 1379 | } |
| 1380 | |
| 1381 | void Layer::notifyAvailableFrames() { |
Dan Stoza | cac3538 | 2016-01-27 12:21:06 -0800 | [diff] [blame] | 1382 | auto headFrameNumber = getHeadFrameNumber(); |
| 1383 | Mutex::Autolock lock(mLocalSyncPointMutex); |
| 1384 | for (auto& point : mLocalSyncPoints) { |
| 1385 | if (headFrameNumber >= point->getFrameNumber()) { |
| 1386 | point->setFrameAvailable(); |
| 1387 | } |
Dan Stoza | 7dde599 | 2015-05-22 09:51:44 -0700 | [diff] [blame] | 1388 | } |
| 1389 | } |
| 1390 | |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 1391 | uint32_t Layer::doTransaction(uint32_t flags) { |
Jamie Gennis | 1c8e95c | 2012-02-23 19:27:23 -0800 | [diff] [blame] | 1392 | ATRACE_CALL(); |
| 1393 | |
Dan Stoza | 7dde599 | 2015-05-22 09:51:44 -0700 | [diff] [blame] | 1394 | pushPendingState(); |
Pablo Ceballos | 05289c2 | 2016-04-14 15:49:55 -0700 | [diff] [blame] | 1395 | Layer::State c = getCurrentState(); |
| 1396 | if (!applyPendingStates(&c)) { |
Dan Stoza | 7dde599 | 2015-05-22 09:51:44 -0700 | [diff] [blame] | 1397 | return 0; |
| 1398 | } |
| 1399 | |
Mathias Agopian | 1eae0ee | 2013-06-05 16:59:15 -0700 | [diff] [blame] | 1400 | const Layer::State& s(getDrawingState()); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1401 | |
Mathias Agopian | 1eae0ee | 2013-06-05 16:59:15 -0700 | [diff] [blame] | 1402 | const bool sizeChanged = (c.requested.w != s.requested.w) || |
| 1403 | (c.requested.h != s.requested.h); |
Mathias Agopian | a138f89 | 2010-05-21 17:24:35 -0700 | [diff] [blame] | 1404 | |
| 1405 | if (sizeChanged) { |
Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 1406 | // the size changed, we need to ask our client to request a new buffer |
Steve Block | 9d45368 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 1407 | ALOGD_IF(DEBUG_RESIZE, |
Andy McFadden | 6905205 | 2012-09-14 16:10:11 -0700 | [diff] [blame] | 1408 | "doTransaction: geometry (layer=%p '%s'), tr=%02x, scalingMode=%d\n" |
Mathias Agopian | 419e196 | 2012-05-23 14:34:07 -0700 | [diff] [blame] | 1409 | " current={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n" |
Robert Carr | b5d3d26 | 2016-03-25 15:08:13 -0700 | [diff] [blame] | 1410 | " requested={ wh={%4u,%4u} }}\n" |
Mathias Agopian | 419e196 | 2012-05-23 14:34:07 -0700 | [diff] [blame] | 1411 | " drawing={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n" |
Robert Carr | b5d3d26 | 2016-03-25 15:08:13 -0700 | [diff] [blame] | 1412 | " requested={ wh={%4u,%4u} }}\n", |
Robert Carr | c3574f7 | 2016-03-24 12:19:32 -0700 | [diff] [blame] | 1413 | this, getName().string(), mCurrentTransform, |
| 1414 | getEffectiveScalingMode(), |
Mathias Agopian | 1eae0ee | 2013-06-05 16:59:15 -0700 | [diff] [blame] | 1415 | c.active.w, c.active.h, |
Robert Carr | b5d3d26 | 2016-03-25 15:08:13 -0700 | [diff] [blame] | 1416 | c.crop.left, |
| 1417 | c.crop.top, |
| 1418 | c.crop.right, |
| 1419 | c.crop.bottom, |
| 1420 | c.crop.getWidth(), |
| 1421 | c.crop.getHeight(), |
Mathias Agopian | 1eae0ee | 2013-06-05 16:59:15 -0700 | [diff] [blame] | 1422 | c.requested.w, c.requested.h, |
Mathias Agopian | 1eae0ee | 2013-06-05 16:59:15 -0700 | [diff] [blame] | 1423 | s.active.w, s.active.h, |
Robert Carr | b5d3d26 | 2016-03-25 15:08:13 -0700 | [diff] [blame] | 1424 | s.crop.left, |
| 1425 | s.crop.top, |
| 1426 | s.crop.right, |
| 1427 | s.crop.bottom, |
| 1428 | s.crop.getWidth(), |
| 1429 | s.crop.getHeight(), |
| 1430 | s.requested.w, s.requested.h); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1431 | |
Jamie Gennis | 2a0d5b6 | 2011-09-26 16:54:44 -0700 | [diff] [blame] | 1432 | // record the new size, form this point on, when the client request |
| 1433 | // a buffer, it'll get the new size. |
Andy McFadden | bf974ab | 2012-12-04 16:51:15 -0800 | [diff] [blame] | 1434 | mSurfaceFlingerConsumer->setDefaultBufferSize( |
Mathias Agopian | 1eae0ee | 2013-06-05 16:59:15 -0700 | [diff] [blame] | 1435 | c.requested.w, c.requested.h); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1436 | } |
Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 1437 | |
Mathias Agopian | 0cd545f | 2012-06-07 14:18:55 -0700 | [diff] [blame] | 1438 | if (!isFixedSize()) { |
| 1439 | |
Mathias Agopian | 1eae0ee | 2013-06-05 16:59:15 -0700 | [diff] [blame] | 1440 | const bool resizePending = (c.requested.w != c.active.w) || |
| 1441 | (c.requested.h != c.active.h); |
Mathias Agopian | 0cd545f | 2012-06-07 14:18:55 -0700 | [diff] [blame] | 1442 | |
Dan Stoza | 9e9b044 | 2015-04-22 14:59:08 -0700 | [diff] [blame] | 1443 | if (resizePending && mSidebandStream == NULL) { |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 1444 | // don't let Layer::doTransaction update the drawing state |
Mathias Agopian | 0cd545f | 2012-06-07 14:18:55 -0700 | [diff] [blame] | 1445 | // if we have a pending resize, unless we are in fixed-size mode. |
| 1446 | // the drawing state will be updated only once we receive a buffer |
| 1447 | // with the correct size. |
| 1448 | // |
| 1449 | // in particular, we want to make sure the clip (which is part |
| 1450 | // of the geometry state) is latched together with the size but is |
| 1451 | // latched immediately when no resizing is involved. |
Dan Stoza | 9e9b044 | 2015-04-22 14:59:08 -0700 | [diff] [blame] | 1452 | // |
| 1453 | // If a sideband stream is attached, however, we want to skip this |
| 1454 | // optimization so that transactions aren't missed when a buffer |
| 1455 | // never arrives |
Mathias Agopian | 0cd545f | 2012-06-07 14:18:55 -0700 | [diff] [blame] | 1456 | |
| 1457 | flags |= eDontUpdateGeometryState; |
| 1458 | } |
| 1459 | } |
| 1460 | |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 1461 | // always set active to requested, unless we're asked not to |
| 1462 | // this is used by Layer, which special cases resizes. |
| 1463 | if (flags & eDontUpdateGeometryState) { |
| 1464 | } else { |
Pablo Ceballos | 05289c2 | 2016-04-14 15:49:55 -0700 | [diff] [blame] | 1465 | c.active = c.requested; |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 1466 | } |
| 1467 | |
Mathias Agopian | 1eae0ee | 2013-06-05 16:59:15 -0700 | [diff] [blame] | 1468 | if (s.active != c.active) { |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 1469 | // invalidate and recompute the visible regions if needed |
| 1470 | flags |= Layer::eVisibleRegion; |
| 1471 | } |
| 1472 | |
Mathias Agopian | 1eae0ee | 2013-06-05 16:59:15 -0700 | [diff] [blame] | 1473 | if (c.sequence != s.sequence) { |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 1474 | // invalidate and recompute the visible regions if needed |
| 1475 | flags |= eVisibleRegion; |
| 1476 | this->contentDirty = true; |
| 1477 | |
| 1478 | // we may use linear filtering, if the matrix scales us |
Robert Carr | 3dcabfa | 2016-03-01 18:36:58 -0800 | [diff] [blame] | 1479 | const uint8_t type = c.active.transform.getType(); |
| 1480 | mNeedsFiltering = (!c.active.transform.preserveRects() || |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 1481 | (type >= Transform::SCALE)); |
| 1482 | } |
| 1483 | |
Dan Stoza | c814517 | 2016-04-28 16:29:06 -0700 | [diff] [blame] | 1484 | // If the layer is hidden, signal and clear out all local sync points so |
| 1485 | // that transactions for layers depending on this layer's frames becoming |
| 1486 | // visible are not blocked |
| 1487 | if (c.flags & layer_state_t::eLayerHidden) { |
| 1488 | Mutex::Autolock lock(mLocalSyncPointMutex); |
| 1489 | for (auto& point : mLocalSyncPoints) { |
| 1490 | point->setFrameAvailable(); |
| 1491 | } |
| 1492 | mLocalSyncPoints.clear(); |
| 1493 | } |
| 1494 | |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 1495 | // Commit the transaction |
Pablo Ceballos | 05289c2 | 2016-04-14 15:49:55 -0700 | [diff] [blame] | 1496 | commitTransaction(c); |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 1497 | return flags; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1498 | } |
| 1499 | |
Pablo Ceballos | 05289c2 | 2016-04-14 15:49:55 -0700 | [diff] [blame] | 1500 | void Layer::commitTransaction(const State& stateToCommit) { |
| 1501 | mDrawingState = stateToCommit; |
Mathias Agopian | a67932f | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 1502 | } |
| 1503 | |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 1504 | uint32_t Layer::getTransactionFlags(uint32_t flags) { |
| 1505 | return android_atomic_and(~flags, &mTransactionFlags) & flags; |
| 1506 | } |
| 1507 | |
| 1508 | uint32_t Layer::setTransactionFlags(uint32_t flags) { |
| 1509 | return android_atomic_or(flags, &mTransactionFlags); |
| 1510 | } |
| 1511 | |
| 1512 | bool Layer::setPosition(float x, float y) { |
Robert Carr | 3dcabfa | 2016-03-01 18:36:58 -0800 | [diff] [blame] | 1513 | if (mCurrentState.requested.transform.tx() == x && mCurrentState.requested.transform.ty() == y) |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 1514 | return false; |
| 1515 | mCurrentState.sequence++; |
Robert Carr | 69663fb | 2016-03-27 19:59:19 -0700 | [diff] [blame] | 1516 | |
| 1517 | // We update the requested and active position simultaneously because |
| 1518 | // we want to apply the position portion of the transform matrix immediately, |
| 1519 | // but still delay scaling when resizing a SCALING_MODE_FREEZE layer. |
Robert Carr | 3dcabfa | 2016-03-01 18:36:58 -0800 | [diff] [blame] | 1520 | mCurrentState.requested.transform.set(x, y); |
Robert Carr | 69663fb | 2016-03-27 19:59:19 -0700 | [diff] [blame] | 1521 | mCurrentState.active.transform.set(x, y); |
| 1522 | |
Dan Stoza | 7dde599 | 2015-05-22 09:51:44 -0700 | [diff] [blame] | 1523 | mCurrentState.modified = true; |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 1524 | setTransactionFlags(eTransactionNeeded); |
| 1525 | return true; |
| 1526 | } |
| 1527 | bool Layer::setLayer(uint32_t z) { |
| 1528 | if (mCurrentState.z == z) |
| 1529 | return false; |
| 1530 | mCurrentState.sequence++; |
| 1531 | mCurrentState.z = z; |
Dan Stoza | 7dde599 | 2015-05-22 09:51:44 -0700 | [diff] [blame] | 1532 | mCurrentState.modified = true; |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 1533 | setTransactionFlags(eTransactionNeeded); |
| 1534 | return true; |
| 1535 | } |
| 1536 | bool Layer::setSize(uint32_t w, uint32_t h) { |
| 1537 | if (mCurrentState.requested.w == w && mCurrentState.requested.h == h) |
| 1538 | return false; |
| 1539 | mCurrentState.requested.w = w; |
| 1540 | mCurrentState.requested.h = h; |
Dan Stoza | 7dde599 | 2015-05-22 09:51:44 -0700 | [diff] [blame] | 1541 | mCurrentState.modified = true; |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 1542 | setTransactionFlags(eTransactionNeeded); |
| 1543 | return true; |
| 1544 | } |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 1545 | #ifdef USE_HWC2 |
| 1546 | bool Layer::setAlpha(float alpha) { |
| 1547 | #else |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 1548 | bool Layer::setAlpha(uint8_t alpha) { |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 1549 | #endif |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 1550 | if (mCurrentState.alpha == alpha) |
| 1551 | return false; |
| 1552 | mCurrentState.sequence++; |
| 1553 | mCurrentState.alpha = alpha; |
Dan Stoza | 7dde599 | 2015-05-22 09:51:44 -0700 | [diff] [blame] | 1554 | mCurrentState.modified = true; |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 1555 | setTransactionFlags(eTransactionNeeded); |
| 1556 | return true; |
| 1557 | } |
| 1558 | bool Layer::setMatrix(const layer_state_t::matrix22_t& matrix) { |
| 1559 | mCurrentState.sequence++; |
Robert Carr | 3dcabfa | 2016-03-01 18:36:58 -0800 | [diff] [blame] | 1560 | mCurrentState.requested.transform.set( |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 1561 | matrix.dsdx, matrix.dsdy, matrix.dtdx, matrix.dtdy); |
Dan Stoza | 7dde599 | 2015-05-22 09:51:44 -0700 | [diff] [blame] | 1562 | mCurrentState.modified = true; |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 1563 | setTransactionFlags(eTransactionNeeded); |
| 1564 | return true; |
| 1565 | } |
| 1566 | bool Layer::setTransparentRegionHint(const Region& transparent) { |
Mathias Agopian | 2ca7939 | 2013-04-02 18:30:32 -0700 | [diff] [blame] | 1567 | mCurrentState.requestedTransparentRegion = transparent; |
Dan Stoza | 7dde599 | 2015-05-22 09:51:44 -0700 | [diff] [blame] | 1568 | mCurrentState.modified = true; |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 1569 | setTransactionFlags(eTransactionNeeded); |
| 1570 | return true; |
| 1571 | } |
| 1572 | bool Layer::setFlags(uint8_t flags, uint8_t mask) { |
| 1573 | const uint32_t newFlags = (mCurrentState.flags & ~mask) | (flags & mask); |
| 1574 | if (mCurrentState.flags == newFlags) |
| 1575 | return false; |
| 1576 | mCurrentState.sequence++; |
| 1577 | mCurrentState.flags = newFlags; |
Dan Stoza | 7dde599 | 2015-05-22 09:51:44 -0700 | [diff] [blame] | 1578 | mCurrentState.mask = mask; |
| 1579 | mCurrentState.modified = true; |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 1580 | setTransactionFlags(eTransactionNeeded); |
| 1581 | return true; |
| 1582 | } |
| 1583 | bool Layer::setCrop(const Rect& crop) { |
Robert Carr | b5d3d26 | 2016-03-25 15:08:13 -0700 | [diff] [blame] | 1584 | if (mCurrentState.crop == crop) |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 1585 | return false; |
| 1586 | mCurrentState.sequence++; |
Robert Carr | b5d3d26 | 2016-03-25 15:08:13 -0700 | [diff] [blame] | 1587 | mCurrentState.crop = crop; |
Dan Stoza | 7dde599 | 2015-05-22 09:51:44 -0700 | [diff] [blame] | 1588 | mCurrentState.modified = true; |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 1589 | setTransactionFlags(eTransactionNeeded); |
| 1590 | return true; |
| 1591 | } |
Pablo Ceballos | acbe678 | 2016-03-04 17:54:21 +0000 | [diff] [blame] | 1592 | bool Layer::setFinalCrop(const Rect& crop) { |
Robert Carr | b5d3d26 | 2016-03-25 15:08:13 -0700 | [diff] [blame] | 1593 | if (mCurrentState.finalCrop == crop) |
Pablo Ceballos | acbe678 | 2016-03-04 17:54:21 +0000 | [diff] [blame] | 1594 | return false; |
| 1595 | mCurrentState.sequence++; |
Robert Carr | b5d3d26 | 2016-03-25 15:08:13 -0700 | [diff] [blame] | 1596 | mCurrentState.finalCrop = crop; |
Pablo Ceballos | acbe678 | 2016-03-04 17:54:21 +0000 | [diff] [blame] | 1597 | mCurrentState.modified = true; |
| 1598 | setTransactionFlags(eTransactionNeeded); |
| 1599 | return true; |
| 1600 | } |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 1601 | |
Robert Carr | c3574f7 | 2016-03-24 12:19:32 -0700 | [diff] [blame] | 1602 | bool Layer::setOverrideScalingMode(int32_t scalingMode) { |
| 1603 | if (scalingMode == mOverrideScalingMode) |
| 1604 | return false; |
| 1605 | mOverrideScalingMode = scalingMode; |
| 1606 | return true; |
| 1607 | } |
| 1608 | |
| 1609 | uint32_t Layer::getEffectiveScalingMode() const { |
| 1610 | if (mOverrideScalingMode >= 0) { |
| 1611 | return mOverrideScalingMode; |
| 1612 | } |
| 1613 | return mCurrentScalingMode; |
| 1614 | } |
| 1615 | |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 1616 | bool Layer::setLayerStack(uint32_t layerStack) { |
| 1617 | if (mCurrentState.layerStack == layerStack) |
| 1618 | return false; |
| 1619 | mCurrentState.sequence++; |
| 1620 | mCurrentState.layerStack = layerStack; |
Dan Stoza | 7dde599 | 2015-05-22 09:51:44 -0700 | [diff] [blame] | 1621 | mCurrentState.modified = true; |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 1622 | setTransactionFlags(eTransactionNeeded); |
| 1623 | return true; |
Mathias Agopian | a67932f | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 1624 | } |
| 1625 | |
Dan Stoza | 7dde599 | 2015-05-22 09:51:44 -0700 | [diff] [blame] | 1626 | void Layer::deferTransactionUntil(const sp<IBinder>& handle, |
| 1627 | uint64_t frameNumber) { |
| 1628 | mCurrentState.handle = handle; |
| 1629 | mCurrentState.frameNumber = frameNumber; |
| 1630 | // We don't set eTransactionNeeded, because just receiving a deferral |
| 1631 | // request without any other state updates shouldn't actually induce a delay |
| 1632 | mCurrentState.modified = true; |
| 1633 | pushPendingState(); |
Dan Stoza | 792e529 | 2016-02-11 11:43:58 -0800 | [diff] [blame] | 1634 | mCurrentState.handle = nullptr; |
| 1635 | mCurrentState.frameNumber = 0; |
Dan Stoza | 7dde599 | 2015-05-22 09:51:44 -0700 | [diff] [blame] | 1636 | mCurrentState.modified = false; |
| 1637 | } |
| 1638 | |
Dan Stoza | ee44edd | 2015-03-23 15:50:23 -0700 | [diff] [blame] | 1639 | void Layer::useSurfaceDamage() { |
| 1640 | if (mFlinger->mForceFullDamage) { |
| 1641 | surfaceDamageRegion = Region::INVALID_REGION; |
| 1642 | } else { |
| 1643 | surfaceDamageRegion = mSurfaceFlingerConsumer->getSurfaceDamage(); |
| 1644 | } |
| 1645 | } |
| 1646 | |
| 1647 | void Layer::useEmptyDamage() { |
| 1648 | surfaceDamageRegion.clear(); |
| 1649 | } |
| 1650 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1651 | // ---------------------------------------------------------------------------- |
| 1652 | // pageflip handling... |
| 1653 | // ---------------------------------------------------------------------------- |
| 1654 | |
Dan Stoza | 6b9454d | 2014-11-07 16:00:59 -0800 | [diff] [blame] | 1655 | bool Layer::shouldPresentNow(const DispSync& dispSync) const { |
Pablo Ceballos | ff95aab | 2016-01-13 17:09:58 -0800 | [diff] [blame] | 1656 | if (mSidebandStreamChanged || mAutoRefresh) { |
Dan Stoza | d87defa | 2015-07-29 16:15:50 -0700 | [diff] [blame] | 1657 | return true; |
| 1658 | } |
| 1659 | |
Dan Stoza | 6b9454d | 2014-11-07 16:00:59 -0800 | [diff] [blame] | 1660 | Mutex::Autolock lock(mQueueItemLock); |
Dan Stoza | 0eb2d39 | 2015-07-06 12:56:50 -0700 | [diff] [blame] | 1661 | if (mQueueItems.empty()) { |
| 1662 | return false; |
| 1663 | } |
| 1664 | auto timestamp = mQueueItems[0].mTimestamp; |
Dan Stoza | 6b9454d | 2014-11-07 16:00:59 -0800 | [diff] [blame] | 1665 | nsecs_t expectedPresent = |
| 1666 | mSurfaceFlingerConsumer->computeExpectedPresent(dispSync); |
Dan Stoza | 0eb2d39 | 2015-07-06 12:56:50 -0700 | [diff] [blame] | 1667 | |
| 1668 | // Ignore timestamps more than a second in the future |
| 1669 | bool isPlausible = timestamp < (expectedPresent + s2ns(1)); |
| 1670 | ALOGW_IF(!isPlausible, "[%s] Timestamp %" PRId64 " seems implausible " |
| 1671 | "relative to expectedPresent %" PRId64, mName.string(), timestamp, |
| 1672 | expectedPresent); |
| 1673 | |
| 1674 | bool isDue = timestamp < expectedPresent; |
| 1675 | return isDue || !isPlausible; |
Dan Stoza | 6b9454d | 2014-11-07 16:00:59 -0800 | [diff] [blame] | 1676 | } |
| 1677 | |
Mathias Agopian | 4d143ee | 2012-02-23 20:05:39 -0800 | [diff] [blame] | 1678 | bool Layer::onPreComposition() { |
| 1679 | mRefreshPending = false; |
Pablo Ceballos | ff95aab | 2016-01-13 17:09:58 -0800 | [diff] [blame] | 1680 | return mQueuedFrames > 0 || mSidebandStreamChanged || mAutoRefresh; |
Mathias Agopian | 99ce5cd | 2012-01-31 18:24:27 -0800 | [diff] [blame] | 1681 | } |
| 1682 | |
Dan Stoza | e77c766 | 2016-05-13 11:37:28 -0700 | [diff] [blame^] | 1683 | bool Layer::onPostComposition() { |
| 1684 | bool frameLatencyNeeded = mFrameLatencyNeeded; |
Mathias Agopian | d3ee231 | 2012-08-02 14:01:42 -0700 | [diff] [blame] | 1685 | if (mFrameLatencyNeeded) { |
Andy McFadden | bf974ab | 2012-12-04 16:51:15 -0800 | [diff] [blame] | 1686 | nsecs_t desiredPresentTime = mSurfaceFlingerConsumer->getTimestamp(); |
Jamie Gennis | 82dbc74 | 2012-11-08 19:23:28 -0800 | [diff] [blame] | 1687 | mFrameTracker.setDesiredPresentTime(desiredPresentTime); |
| 1688 | |
Andy McFadden | bf974ab | 2012-12-04 16:51:15 -0800 | [diff] [blame] | 1689 | sp<Fence> frameReadyFence = mSurfaceFlingerConsumer->getCurrentFence(); |
Jamie Gennis | 789a6c3 | 2013-02-25 13:37:54 -0800 | [diff] [blame] | 1690 | if (frameReadyFence->isValid()) { |
Jamie Gennis | 82dbc74 | 2012-11-08 19:23:28 -0800 | [diff] [blame] | 1691 | mFrameTracker.setFrameReadyFence(frameReadyFence); |
| 1692 | } else { |
| 1693 | // There was no fence for this frame, so assume that it was ready |
| 1694 | // to be presented at the desired present time. |
| 1695 | mFrameTracker.setFrameReadyTime(desiredPresentTime); |
| 1696 | } |
| 1697 | |
Mathias Agopian | d3ee231 | 2012-08-02 14:01:42 -0700 | [diff] [blame] | 1698 | const HWComposer& hwc = mFlinger->getHwComposer(); |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 1699 | #ifdef USE_HWC2 |
| 1700 | sp<Fence> presentFence = hwc.getRetireFence(HWC_DISPLAY_PRIMARY); |
| 1701 | #else |
Jamie Gennis | 82dbc74 | 2012-11-08 19:23:28 -0800 | [diff] [blame] | 1702 | sp<Fence> presentFence = hwc.getDisplayFence(HWC_DISPLAY_PRIMARY); |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 1703 | #endif |
Jamie Gennis | 789a6c3 | 2013-02-25 13:37:54 -0800 | [diff] [blame] | 1704 | if (presentFence->isValid()) { |
Jamie Gennis | 82dbc74 | 2012-11-08 19:23:28 -0800 | [diff] [blame] | 1705 | mFrameTracker.setActualPresentFence(presentFence); |
| 1706 | } else { |
| 1707 | // The HWC doesn't support present fences, so use the refresh |
| 1708 | // timestamp instead. |
| 1709 | nsecs_t presentTime = hwc.getRefreshTimestamp(HWC_DISPLAY_PRIMARY); |
| 1710 | mFrameTracker.setActualPresentTime(presentTime); |
| 1711 | } |
| 1712 | |
| 1713 | mFrameTracker.advanceFrame(); |
Mathias Agopian | d3ee231 | 2012-08-02 14:01:42 -0700 | [diff] [blame] | 1714 | mFrameLatencyNeeded = false; |
| 1715 | } |
Dan Stoza | e77c766 | 2016-05-13 11:37:28 -0700 | [diff] [blame^] | 1716 | return frameLatencyNeeded; |
Mathias Agopian | d3ee231 | 2012-08-02 14:01:42 -0700 | [diff] [blame] | 1717 | } |
| 1718 | |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 1719 | #ifdef USE_HWC2 |
| 1720 | void Layer::releasePendingBuffer() { |
| 1721 | mSurfaceFlingerConsumer->releasePendingBuffer(); |
| 1722 | } |
| 1723 | #endif |
| 1724 | |
Mathias Agopian | da27af9 | 2012-09-13 18:17:13 -0700 | [diff] [blame] | 1725 | bool Layer::isVisible() const { |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 1726 | const Layer::State& s(mDrawingState); |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 1727 | #ifdef USE_HWC2 |
| 1728 | return !(s.flags & layer_state_t::eLayerHidden) && s.alpha > 0.0f |
| 1729 | && (mActiveBuffer != NULL || mSidebandStream != NULL); |
| 1730 | #else |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 1731 | return !(s.flags & layer_state_t::eLayerHidden) && s.alpha |
Wonsik Kim | afe3081 | 2014-03-31 23:16:08 +0900 | [diff] [blame] | 1732 | && (mActiveBuffer != NULL || mSidebandStream != NULL); |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 1733 | #endif |
Mathias Agopian | da27af9 | 2012-09-13 18:17:13 -0700 | [diff] [blame] | 1734 | } |
| 1735 | |
Mathias Agopian | 4fec873 | 2012-06-29 14:12:52 -0700 | [diff] [blame] | 1736 | Region Layer::latchBuffer(bool& recomputeVisibleRegions) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1737 | { |
Jamie Gennis | 1c8e95c | 2012-02-23 19:27:23 -0800 | [diff] [blame] | 1738 | ATRACE_CALL(); |
| 1739 | |
Jesse Hall | 399184a | 2014-03-03 15:42:54 -0800 | [diff] [blame] | 1740 | if (android_atomic_acquire_cas(true, false, &mSidebandStreamChanged) == 0) { |
| 1741 | // mSidebandStreamChanged was true |
| 1742 | mSidebandStream = mSurfaceFlingerConsumer->getSidebandStream(); |
Dan Stoza | 12e0a27 | 2015-05-05 14:00:52 -0700 | [diff] [blame] | 1743 | if (mSidebandStream != NULL) { |
| 1744 | setTransactionFlags(eTransactionNeeded); |
| 1745 | mFlinger->setTransactionFlags(eTraversalNeeded); |
| 1746 | } |
Jesse Hall | 5bf786d | 2014-09-30 10:35:11 -0700 | [diff] [blame] | 1747 | recomputeVisibleRegions = true; |
| 1748 | |
| 1749 | const State& s(getDrawingState()); |
Robert Carr | 3dcabfa | 2016-03-01 18:36:58 -0800 | [diff] [blame] | 1750 | return s.active.transform.transform(Region(Rect(s.active.w, s.active.h))); |
Jesse Hall | 399184a | 2014-03-03 15:42:54 -0800 | [diff] [blame] | 1751 | } |
| 1752 | |
Mathias Agopian | 4fec873 | 2012-06-29 14:12:52 -0700 | [diff] [blame] | 1753 | Region outDirtyRegion; |
Pablo Ceballos | ff95aab | 2016-01-13 17:09:58 -0800 | [diff] [blame] | 1754 | if (mQueuedFrames > 0 || mAutoRefresh) { |
Mathias Agopian | 99ce5cd | 2012-01-31 18:24:27 -0800 | [diff] [blame] | 1755 | |
| 1756 | // if we've already called updateTexImage() without going through |
| 1757 | // a composition step, we have to skip this layer at this point |
| 1758 | // because we cannot call updateTeximage() without a corresponding |
| 1759 | // compositionComplete() call. |
| 1760 | // we'll trigger an update in onPreComposition(). |
Mathias Agopian | 4d143ee | 2012-02-23 20:05:39 -0800 | [diff] [blame] | 1761 | if (mRefreshPending) { |
Mathias Agopian | 4fec873 | 2012-06-29 14:12:52 -0700 | [diff] [blame] | 1762 | return outDirtyRegion; |
Mathias Agopian | 99ce5cd | 2012-01-31 18:24:27 -0800 | [diff] [blame] | 1763 | } |
| 1764 | |
Jamie Gennis | 351a513 | 2011-09-14 18:23:37 -0700 | [diff] [blame] | 1765 | // Capture the old state of the layer for comparisons later |
Andy McFadden | 4125a4f | 2014-01-29 17:17:11 -0800 | [diff] [blame] | 1766 | const State& s(getDrawingState()); |
| 1767 | const bool oldOpacity = isOpaque(s); |
Jamie Gennis | 351a513 | 2011-09-14 18:23:37 -0700 | [diff] [blame] | 1768 | sp<GraphicBuffer> oldActiveBuffer = mActiveBuffer; |
Jamie Gennis | db5230f | 2011-07-28 14:54:07 -0700 | [diff] [blame] | 1769 | |
Andy McFadden | bf974ab | 2012-12-04 16:51:15 -0800 | [diff] [blame] | 1770 | struct Reject : public SurfaceFlingerConsumer::BufferRejecter { |
Mathias Agopian | 2c8207e | 2012-05-23 17:56:42 -0700 | [diff] [blame] | 1771 | Layer::State& front; |
| 1772 | Layer::State& current; |
| 1773 | bool& recomputeVisibleRegions; |
Ruben Brunk | 1681d95 | 2014-06-27 15:51:55 -0700 | [diff] [blame] | 1774 | bool stickyTransformSet; |
Robert Carr | b5d3d26 | 2016-03-25 15:08:13 -0700 | [diff] [blame] | 1775 | const char* name; |
Robert Carr | c3574f7 | 2016-03-24 12:19:32 -0700 | [diff] [blame] | 1776 | int32_t overrideScalingMode; |
Robert Carr | b5d3d26 | 2016-03-25 15:08:13 -0700 | [diff] [blame] | 1777 | |
Mathias Agopian | 2c8207e | 2012-05-23 17:56:42 -0700 | [diff] [blame] | 1778 | Reject(Layer::State& front, Layer::State& current, |
Robert Carr | b5d3d26 | 2016-03-25 15:08:13 -0700 | [diff] [blame] | 1779 | bool& recomputeVisibleRegions, bool stickySet, |
Robert Carr | c3574f7 | 2016-03-24 12:19:32 -0700 | [diff] [blame] | 1780 | const char* name, |
| 1781 | int32_t overrideScalingMode) |
Mathias Agopian | 2c8207e | 2012-05-23 17:56:42 -0700 | [diff] [blame] | 1782 | : front(front), current(current), |
Ruben Brunk | 1681d95 | 2014-06-27 15:51:55 -0700 | [diff] [blame] | 1783 | recomputeVisibleRegions(recomputeVisibleRegions), |
Robert Carr | b5d3d26 | 2016-03-25 15:08:13 -0700 | [diff] [blame] | 1784 | stickyTransformSet(stickySet), |
Robert Carr | c3574f7 | 2016-03-24 12:19:32 -0700 | [diff] [blame] | 1785 | name(name), |
| 1786 | overrideScalingMode(overrideScalingMode) { |
Mathias Agopian | 2c8207e | 2012-05-23 17:56:42 -0700 | [diff] [blame] | 1787 | } |
| 1788 | |
| 1789 | virtual bool reject(const sp<GraphicBuffer>& buf, |
Dan Stoza | 11611f9 | 2015-03-12 15:12:44 -0700 | [diff] [blame] | 1790 | const BufferItem& item) { |
Mathias Agopian | 2c8207e | 2012-05-23 17:56:42 -0700 | [diff] [blame] | 1791 | if (buf == NULL) { |
| 1792 | return false; |
| 1793 | } |
| 1794 | |
| 1795 | uint32_t bufWidth = buf->getWidth(); |
| 1796 | uint32_t bufHeight = buf->getHeight(); |
| 1797 | |
| 1798 | // check that we received a buffer of the right size |
| 1799 | // (Take the buffer's orientation into account) |
| 1800 | if (item.mTransform & Transform::ROT_90) { |
| 1801 | swap(bufWidth, bufHeight); |
| 1802 | } |
| 1803 | |
Robert Carr | c3574f7 | 2016-03-24 12:19:32 -0700 | [diff] [blame] | 1804 | int actualScalingMode = overrideScalingMode >= 0 ? |
| 1805 | overrideScalingMode : item.mScalingMode; |
| 1806 | bool isFixedSize = actualScalingMode != NATIVE_WINDOW_SCALING_MODE_FREEZE; |
Mathias Agopian | 2c8207e | 2012-05-23 17:56:42 -0700 | [diff] [blame] | 1807 | if (front.active != front.requested) { |
| 1808 | |
| 1809 | if (isFixedSize || |
| 1810 | (bufWidth == front.requested.w && |
| 1811 | bufHeight == front.requested.h)) |
| 1812 | { |
| 1813 | // Here we pretend the transaction happened by updating the |
| 1814 | // current and drawing states. Drawing state is only accessed |
| 1815 | // in this thread, no need to have it locked |
| 1816 | front.active = front.requested; |
| 1817 | |
| 1818 | // We also need to update the current state so that |
| 1819 | // we don't end-up overwriting the drawing state with |
| 1820 | // this stale current state during the next transaction |
| 1821 | // |
| 1822 | // NOTE: We don't need to hold the transaction lock here |
| 1823 | // because State::active is only accessed from this thread. |
| 1824 | current.active = front.active; |
Pablo Ceballos | 39c88e8 | 2016-05-10 17:15:24 -0700 | [diff] [blame] | 1825 | current.modified = true; |
Mathias Agopian | 2c8207e | 2012-05-23 17:56:42 -0700 | [diff] [blame] | 1826 | |
| 1827 | // recompute visible region |
| 1828 | recomputeVisibleRegions = true; |
| 1829 | } |
| 1830 | |
| 1831 | ALOGD_IF(DEBUG_RESIZE, |
Robert Carr | b5d3d26 | 2016-03-25 15:08:13 -0700 | [diff] [blame] | 1832 | "[%s] latchBuffer/reject: buffer (%ux%u, tr=%02x), scalingMode=%d\n" |
Mathias Agopian | 2c8207e | 2012-05-23 17:56:42 -0700 | [diff] [blame] | 1833 | " drawing={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n" |
Robert Carr | b5d3d26 | 2016-03-25 15:08:13 -0700 | [diff] [blame] | 1834 | " requested={ wh={%4u,%4u} }}\n", |
| 1835 | name, |
Andy McFadden | 6905205 | 2012-09-14 16:10:11 -0700 | [diff] [blame] | 1836 | bufWidth, bufHeight, item.mTransform, item.mScalingMode, |
Mathias Agopian | 2c8207e | 2012-05-23 17:56:42 -0700 | [diff] [blame] | 1837 | front.active.w, front.active.h, |
Robert Carr | b5d3d26 | 2016-03-25 15:08:13 -0700 | [diff] [blame] | 1838 | front.crop.left, |
| 1839 | front.crop.top, |
| 1840 | front.crop.right, |
| 1841 | front.crop.bottom, |
| 1842 | front.crop.getWidth(), |
| 1843 | front.crop.getHeight(), |
| 1844 | front.requested.w, front.requested.h); |
Mathias Agopian | 2c8207e | 2012-05-23 17:56:42 -0700 | [diff] [blame] | 1845 | } |
| 1846 | |
Ruben Brunk | 1681d95 | 2014-06-27 15:51:55 -0700 | [diff] [blame] | 1847 | if (!isFixedSize && !stickyTransformSet) { |
Mathias Agopian | 2c8207e | 2012-05-23 17:56:42 -0700 | [diff] [blame] | 1848 | if (front.active.w != bufWidth || |
| 1849 | front.active.h != bufHeight) { |
Mathias Agopian | 4824d40 | 2012-06-04 18:16:30 -0700 | [diff] [blame] | 1850 | // reject this buffer |
Robert Carr | b5d3d26 | 2016-03-25 15:08:13 -0700 | [diff] [blame] | 1851 | ALOGE("[%s] rejecting buffer: " |
| 1852 | "bufWidth=%d, bufHeight=%d, front.active.{w=%d, h=%d}", |
| 1853 | name, bufWidth, bufHeight, front.active.w, front.active.h); |
Mathias Agopian | 2c8207e | 2012-05-23 17:56:42 -0700 | [diff] [blame] | 1854 | return true; |
| 1855 | } |
| 1856 | } |
Mathias Agopian | 2ca7939 | 2013-04-02 18:30:32 -0700 | [diff] [blame] | 1857 | |
| 1858 | // if the transparent region has changed (this test is |
| 1859 | // conservative, but that's fine, worst case we're doing |
| 1860 | // a bit of extra work), we latch the new one and we |
| 1861 | // trigger a visible-region recompute. |
| 1862 | if (!front.activeTransparentRegion.isTriviallyEqual( |
| 1863 | front.requestedTransparentRegion)) { |
| 1864 | front.activeTransparentRegion = front.requestedTransparentRegion; |
Mathias Agopian | 6c67f0f | 2013-04-12 16:58:11 -0700 | [diff] [blame] | 1865 | |
| 1866 | // We also need to update the current state so that |
| 1867 | // we don't end-up overwriting the drawing state with |
| 1868 | // this stale current state during the next transaction |
| 1869 | // |
| 1870 | // NOTE: We don't need to hold the transaction lock here |
| 1871 | // because State::active is only accessed from this thread. |
| 1872 | current.activeTransparentRegion = front.activeTransparentRegion; |
| 1873 | |
| 1874 | // recompute visible region |
Mathias Agopian | 2ca7939 | 2013-04-02 18:30:32 -0700 | [diff] [blame] | 1875 | recomputeVisibleRegions = true; |
| 1876 | } |
| 1877 | |
Mathias Agopian | 2c8207e | 2012-05-23 17:56:42 -0700 | [diff] [blame] | 1878 | return false; |
| 1879 | } |
| 1880 | }; |
| 1881 | |
Ruben Brunk | 1681d95 | 2014-06-27 15:51:55 -0700 | [diff] [blame] | 1882 | Reject r(mDrawingState, getCurrentState(), recomputeVisibleRegions, |
Robert Carr | c3574f7 | 2016-03-24 12:19:32 -0700 | [diff] [blame] | 1883 | getProducerStickyTransform() != 0, mName.string(), |
| 1884 | mOverrideScalingMode); |
Mathias Agopian | 2c8207e | 2012-05-23 17:56:42 -0700 | [diff] [blame] | 1885 | |
Dan Stoza | cac3538 | 2016-01-27 12:21:06 -0800 | [diff] [blame] | 1886 | |
| 1887 | // Check all of our local sync points to ensure that all transactions |
| 1888 | // which need to have been applied prior to the frame which is about to |
| 1889 | // be latched have signaled |
| 1890 | |
| 1891 | auto headFrameNumber = getHeadFrameNumber(); |
| 1892 | bool matchingFramesFound = false; |
| 1893 | bool allTransactionsApplied = true; |
Dan Stoza | a4650a5 | 2015-05-12 12:56:16 -0700 | [diff] [blame] | 1894 | { |
Dan Stoza | cac3538 | 2016-01-27 12:21:06 -0800 | [diff] [blame] | 1895 | Mutex::Autolock lock(mLocalSyncPointMutex); |
| 1896 | for (auto& point : mLocalSyncPoints) { |
| 1897 | if (point->getFrameNumber() > headFrameNumber) { |
| 1898 | break; |
| 1899 | } |
| 1900 | |
| 1901 | matchingFramesFound = true; |
| 1902 | |
| 1903 | if (!point->frameIsAvailable()) { |
| 1904 | // We haven't notified the remote layer that the frame for |
| 1905 | // this point is available yet. Notify it now, and then |
| 1906 | // abort this attempt to latch. |
| 1907 | point->setFrameAvailable(); |
| 1908 | allTransactionsApplied = false; |
| 1909 | break; |
| 1910 | } |
| 1911 | |
| 1912 | allTransactionsApplied &= point->transactionIsApplied(); |
Dan Stoza | 7dde599 | 2015-05-22 09:51:44 -0700 | [diff] [blame] | 1913 | } |
| 1914 | } |
| 1915 | |
Dan Stoza | cac3538 | 2016-01-27 12:21:06 -0800 | [diff] [blame] | 1916 | if (matchingFramesFound && !allTransactionsApplied) { |
| 1917 | mFlinger->signalLayerUpdate(); |
| 1918 | return outDirtyRegion; |
Dan Stoza | a4650a5 | 2015-05-12 12:56:16 -0700 | [diff] [blame] | 1919 | } |
| 1920 | |
Pablo Ceballos | 0631218 | 2015-10-07 16:32:12 -0700 | [diff] [blame] | 1921 | // This boolean is used to make sure that SurfaceFlinger's shadow copy |
| 1922 | // of the buffer queue isn't modified when the buffer queue is returning |
Pablo Ceballos | 3559fbf | 2016-03-17 15:50:23 -0700 | [diff] [blame] | 1923 | // BufferItem's that weren't actually queued. This can happen in shared |
Pablo Ceballos | 0631218 | 2015-10-07 16:32:12 -0700 | [diff] [blame] | 1924 | // buffer mode. |
| 1925 | bool queuedBuffer = false; |
Andy McFadden | 41d67d7 | 2014-04-25 16:58:34 -0700 | [diff] [blame] | 1926 | status_t updateResult = mSurfaceFlingerConsumer->updateTexImage(&r, |
Pablo Ceballos | ff95aab | 2016-01-13 17:09:58 -0800 | [diff] [blame] | 1927 | mFlinger->mPrimaryDispSync, &mAutoRefresh, &queuedBuffer, |
Dan Stoza | cac3538 | 2016-01-27 12:21:06 -0800 | [diff] [blame] | 1928 | mLastFrameNumberReceived); |
Andy McFadden | 1585c4d | 2013-06-28 13:52:40 -0700 | [diff] [blame] | 1929 | if (updateResult == BufferQueue::PRESENT_LATER) { |
| 1930 | // Producer doesn't want buffer to be displayed yet. Signal a |
| 1931 | // layer update so we check again at the next opportunity. |
| 1932 | mFlinger->signalLayerUpdate(); |
| 1933 | return outDirtyRegion; |
Dan Stoza | ecc5040 | 2015-04-28 14:42:06 -0700 | [diff] [blame] | 1934 | } else if (updateResult == SurfaceFlingerConsumer::BUFFER_REJECTED) { |
| 1935 | // If the buffer has been rejected, remove it from the shadow queue |
| 1936 | // and return early |
Pablo Ceballos | 0631218 | 2015-10-07 16:32:12 -0700 | [diff] [blame] | 1937 | if (queuedBuffer) { |
| 1938 | Mutex::Autolock lock(mQueueItemLock); |
| 1939 | mQueueItems.removeAt(0); |
| 1940 | android_atomic_dec(&mQueuedFrames); |
| 1941 | } |
Dan Stoza | ecc5040 | 2015-04-28 14:42:06 -0700 | [diff] [blame] | 1942 | return outDirtyRegion; |
Dan Stoza | 65476f3 | 2015-05-14 09:27:25 -0700 | [diff] [blame] | 1943 | } else if (updateResult != NO_ERROR || mUpdateTexImageFailed) { |
| 1944 | // This can occur if something goes wrong when trying to create the |
| 1945 | // EGLImage for this buffer. If this happens, the buffer has already |
| 1946 | // been released, so we need to clean up the queue and bug out |
| 1947 | // early. |
Pablo Ceballos | 0631218 | 2015-10-07 16:32:12 -0700 | [diff] [blame] | 1948 | if (queuedBuffer) { |
Dan Stoza | 65476f3 | 2015-05-14 09:27:25 -0700 | [diff] [blame] | 1949 | Mutex::Autolock lock(mQueueItemLock); |
| 1950 | mQueueItems.clear(); |
| 1951 | android_atomic_and(0, &mQueuedFrames); |
| 1952 | } |
| 1953 | |
| 1954 | // Once we have hit this state, the shadow queue may no longer |
| 1955 | // correctly reflect the incoming BufferQueue's contents, so even if |
| 1956 | // updateTexImage starts working, the only safe course of action is |
| 1957 | // to continue to ignore updates. |
| 1958 | mUpdateTexImageFailed = true; |
| 1959 | |
| 1960 | return outDirtyRegion; |
Andy McFadden | 1585c4d | 2013-06-28 13:52:40 -0700 | [diff] [blame] | 1961 | } |
| 1962 | |
Pablo Ceballos | 0631218 | 2015-10-07 16:32:12 -0700 | [diff] [blame] | 1963 | if (queuedBuffer) { |
| 1964 | // Autolock scope |
Dan Stoza | ecc5040 | 2015-04-28 14:42:06 -0700 | [diff] [blame] | 1965 | auto currentFrameNumber = mSurfaceFlingerConsumer->getFrameNumber(); |
| 1966 | |
Dan Stoza | 6b9454d | 2014-11-07 16:00:59 -0800 | [diff] [blame] | 1967 | Mutex::Autolock lock(mQueueItemLock); |
Dan Stoza | ecc5040 | 2015-04-28 14:42:06 -0700 | [diff] [blame] | 1968 | |
| 1969 | // Remove any stale buffers that have been dropped during |
| 1970 | // updateTexImage |
| 1971 | while (mQueueItems[0].mFrameNumber != currentFrameNumber) { |
| 1972 | mQueueItems.removeAt(0); |
| 1973 | android_atomic_dec(&mQueuedFrames); |
| 1974 | } |
| 1975 | |
Dan Stoza | 6b9454d | 2014-11-07 16:00:59 -0800 | [diff] [blame] | 1976 | mQueueItems.removeAt(0); |
| 1977 | } |
| 1978 | |
Dan Stoza | ecc5040 | 2015-04-28 14:42:06 -0700 | [diff] [blame] | 1979 | |
Andy McFadden | 1585c4d | 2013-06-28 13:52:40 -0700 | [diff] [blame] | 1980 | // Decrement the queued-frames count. Signal another event if we |
| 1981 | // have more frames pending. |
Pablo Ceballos | 0631218 | 2015-10-07 16:32:12 -0700 | [diff] [blame] | 1982 | if ((queuedBuffer && android_atomic_dec(&mQueuedFrames) > 1) |
Pablo Ceballos | ff95aab | 2016-01-13 17:09:58 -0800 | [diff] [blame] | 1983 | || mAutoRefresh) { |
Andy McFadden | 1585c4d | 2013-06-28 13:52:40 -0700 | [diff] [blame] | 1984 | mFlinger->signalLayerUpdate(); |
| 1985 | } |
| 1986 | |
| 1987 | if (updateResult != NO_ERROR) { |
Mathias Agopian | a67932f | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 1988 | // something happened! |
| 1989 | recomputeVisibleRegions = true; |
Mathias Agopian | 4fec873 | 2012-06-29 14:12:52 -0700 | [diff] [blame] | 1990 | return outDirtyRegion; |
Mathias Agopian | a67932f | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 1991 | } |
Mathias Agopian | 96f0819 | 2010-06-02 23:28:45 -0700 | [diff] [blame] | 1992 | |
Jamie Gennis | 351a513 | 2011-09-14 18:23:37 -0700 | [diff] [blame] | 1993 | // update the active buffer |
Andy McFadden | bf974ab | 2012-12-04 16:51:15 -0800 | [diff] [blame] | 1994 | mActiveBuffer = mSurfaceFlingerConsumer->getCurrentBuffer(); |
Mathias Agopian | e31564d | 2012-05-29 20:41:03 -0700 | [diff] [blame] | 1995 | if (mActiveBuffer == NULL) { |
| 1996 | // this can only happen if the very first buffer was rejected. |
Mathias Agopian | 4fec873 | 2012-06-29 14:12:52 -0700 | [diff] [blame] | 1997 | return outDirtyRegion; |
Mathias Agopian | e31564d | 2012-05-29 20:41:03 -0700 | [diff] [blame] | 1998 | } |
Mathias Agopian | da9584d | 2010-12-13 18:51:59 -0800 | [diff] [blame] | 1999 | |
Mathias Agopian | 4824d40 | 2012-06-04 18:16:30 -0700 | [diff] [blame] | 2000 | mRefreshPending = true; |
Mathias Agopian | 2c8207e | 2012-05-23 17:56:42 -0700 | [diff] [blame] | 2001 | mFrameLatencyNeeded = true; |
Mathias Agopian | e31564d | 2012-05-29 20:41:03 -0700 | [diff] [blame] | 2002 | if (oldActiveBuffer == NULL) { |
Mathias Agopian | 2c8207e | 2012-05-23 17:56:42 -0700 | [diff] [blame] | 2003 | // the first time we receive a buffer, we need to trigger a |
| 2004 | // geometry invalidation. |
Andy McFadden | ab10c58 | 2012-09-26 16:19:12 -0700 | [diff] [blame] | 2005 | recomputeVisibleRegions = true; |
Mathias Agopian | 2c8207e | 2012-05-23 17:56:42 -0700 | [diff] [blame] | 2006 | } |
| 2007 | |
Andy McFadden | bf974ab | 2012-12-04 16:51:15 -0800 | [diff] [blame] | 2008 | Rect crop(mSurfaceFlingerConsumer->getCurrentCrop()); |
| 2009 | const uint32_t transform(mSurfaceFlingerConsumer->getCurrentTransform()); |
| 2010 | const uint32_t scalingMode(mSurfaceFlingerConsumer->getCurrentScalingMode()); |
Mathias Agopian | 702634a | 2012-05-23 17:50:31 -0700 | [diff] [blame] | 2011 | if ((crop != mCurrentCrop) || |
| 2012 | (transform != mCurrentTransform) || |
| 2013 | (scalingMode != mCurrentScalingMode)) |
| 2014 | { |
| 2015 | mCurrentCrop = crop; |
| 2016 | mCurrentTransform = transform; |
| 2017 | mCurrentScalingMode = scalingMode; |
Andy McFadden | ab10c58 | 2012-09-26 16:19:12 -0700 | [diff] [blame] | 2018 | recomputeVisibleRegions = true; |
Mathias Agopian | 702634a | 2012-05-23 17:50:31 -0700 | [diff] [blame] | 2019 | } |
| 2020 | |
| 2021 | if (oldActiveBuffer != NULL) { |
Mathias Agopian | e31564d | 2012-05-29 20:41:03 -0700 | [diff] [blame] | 2022 | uint32_t bufWidth = mActiveBuffer->getWidth(); |
| 2023 | uint32_t bufHeight = mActiveBuffer->getHeight(); |
Mathias Agopian | 702634a | 2012-05-23 17:50:31 -0700 | [diff] [blame] | 2024 | if (bufWidth != uint32_t(oldActiveBuffer->width) || |
| 2025 | bufHeight != uint32_t(oldActiveBuffer->height)) { |
Andy McFadden | ab10c58 | 2012-09-26 16:19:12 -0700 | [diff] [blame] | 2026 | recomputeVisibleRegions = true; |
Mathias Agopian | 702634a | 2012-05-23 17:50:31 -0700 | [diff] [blame] | 2027 | } |
| 2028 | } |
| 2029 | |
| 2030 | mCurrentOpacity = getOpacityForFormat(mActiveBuffer->format); |
Andy McFadden | 4125a4f | 2014-01-29 17:17:11 -0800 | [diff] [blame] | 2031 | if (oldOpacity != isOpaque(s)) { |
Mathias Agopian | 702634a | 2012-05-23 17:50:31 -0700 | [diff] [blame] | 2032 | recomputeVisibleRegions = true; |
| 2033 | } |
| 2034 | |
Dan Stoza | cac3538 | 2016-01-27 12:21:06 -0800 | [diff] [blame] | 2035 | mCurrentFrameNumber = mSurfaceFlingerConsumer->getFrameNumber(); |
| 2036 | |
| 2037 | // Remove any sync points corresponding to the buffer which was just |
| 2038 | // latched |
| 2039 | { |
| 2040 | Mutex::Autolock lock(mLocalSyncPointMutex); |
| 2041 | auto point = mLocalSyncPoints.begin(); |
| 2042 | while (point != mLocalSyncPoints.end()) { |
| 2043 | if (!(*point)->frameIsAvailable() || |
| 2044 | !(*point)->transactionIsApplied()) { |
| 2045 | // This sync point must have been added since we started |
| 2046 | // latching. Don't drop it yet. |
| 2047 | ++point; |
| 2048 | continue; |
| 2049 | } |
| 2050 | |
| 2051 | if ((*point)->getFrameNumber() <= mCurrentFrameNumber) { |
| 2052 | point = mLocalSyncPoints.erase(point); |
| 2053 | } else { |
| 2054 | ++point; |
| 2055 | } |
| 2056 | } |
| 2057 | } |
| 2058 | |
Mathias Agopian | 4fec873 | 2012-06-29 14:12:52 -0700 | [diff] [blame] | 2059 | // FIXME: postedRegion should be dirty & bounds |
Mathias Agopian | 1eae0ee | 2013-06-05 16:59:15 -0700 | [diff] [blame] | 2060 | Region dirtyRegion(Rect(s.active.w, s.active.h)); |
Mathias Agopian | 4fec873 | 2012-06-29 14:12:52 -0700 | [diff] [blame] | 2061 | |
| 2062 | // transform the dirty region to window-manager space |
Robert Carr | 3dcabfa | 2016-03-01 18:36:58 -0800 | [diff] [blame] | 2063 | outDirtyRegion = (s.active.transform.transform(dirtyRegion)); |
Mathias Agopian | caa600c | 2009-09-16 18:27:24 -0700 | [diff] [blame] | 2064 | } |
Mathias Agopian | 4fec873 | 2012-06-29 14:12:52 -0700 | [diff] [blame] | 2065 | return outDirtyRegion; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2066 | } |
| 2067 | |
Mathias Agopian | a67932f | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 2068 | uint32_t Layer::getEffectiveUsage(uint32_t usage) const |
Mathias Agopian | b7e930d | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 2069 | { |
Mathias Agopian | a67932f | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 2070 | // TODO: should we do something special if mSecure is set? |
| 2071 | if (mProtectedByApp) { |
| 2072 | // need a hardware-protected path to external video sink |
| 2073 | usage |= GraphicBuffer::USAGE_PROTECTED; |
Jamie Gennis | 54cc83e | 2010-11-02 11:51:32 -0700 | [diff] [blame] | 2074 | } |
Riley Andrews | 03414a1 | 2014-07-01 14:22:59 -0700 | [diff] [blame] | 2075 | if (mPotentialCursor) { |
| 2076 | usage |= GraphicBuffer::USAGE_CURSOR; |
| 2077 | } |
Jamie Gennis | 3599bf2 | 2011-08-10 11:48:07 -0700 | [diff] [blame] | 2078 | usage |= GraphicBuffer::USAGE_HW_COMPOSER; |
Mathias Agopian | a67932f | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 2079 | return usage; |
Mathias Agopian | b5b7f26 | 2010-05-07 15:58:44 -0700 | [diff] [blame] | 2080 | } |
| 2081 | |
Mathias Agopian | 8430095 | 2012-11-21 16:02:13 -0800 | [diff] [blame] | 2082 | void Layer::updateTransformHint(const sp<const DisplayDevice>& hw) const { |
Mathias Agopian | a458364 | 2011-08-23 18:03:18 -0700 | [diff] [blame] | 2083 | uint32_t orientation = 0; |
| 2084 | if (!mFlinger->mDebugDisableTransformHint) { |
Mathias Agopian | 8430095 | 2012-11-21 16:02:13 -0800 | [diff] [blame] | 2085 | // The transform hint is used to improve performance, but we can |
| 2086 | // only have a single transform hint, it cannot |
Mathias Agopian | 4fec873 | 2012-06-29 14:12:52 -0700 | [diff] [blame] | 2087 | // apply to all displays. |
Mathias Agopian | 4297734 | 2012-08-05 00:40:46 -0700 | [diff] [blame] | 2088 | const Transform& planeTransform(hw->getTransform()); |
Mathias Agopian | 4fec873 | 2012-06-29 14:12:52 -0700 | [diff] [blame] | 2089 | orientation = planeTransform.getOrientation(); |
Mathias Agopian | a458364 | 2011-08-23 18:03:18 -0700 | [diff] [blame] | 2090 | if (orientation & Transform::ROT_INVALID) { |
| 2091 | orientation = 0; |
| 2092 | } |
| 2093 | } |
Andy McFadden | bf974ab | 2012-12-04 16:51:15 -0800 | [diff] [blame] | 2094 | mSurfaceFlingerConsumer->setTransformHint(orientation); |
Mathias Agopian | a458364 | 2011-08-23 18:03:18 -0700 | [diff] [blame] | 2095 | } |
| 2096 | |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 2097 | // ---------------------------------------------------------------------------- |
| 2098 | // debugging |
| 2099 | // ---------------------------------------------------------------------------- |
| 2100 | |
Mathias Agopian | 3e25fd8 | 2013-04-22 17:52:16 +0200 | [diff] [blame] | 2101 | void Layer::dump(String8& result, Colorizer& colorizer) const |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 2102 | { |
Mathias Agopian | 1eae0ee | 2013-06-05 16:59:15 -0700 | [diff] [blame] | 2103 | const Layer::State& s(getDrawingState()); |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 2104 | |
Mathias Agopian | 3e25fd8 | 2013-04-22 17:52:16 +0200 | [diff] [blame] | 2105 | colorizer.colorize(result, Colorizer::GREEN); |
Mathias Agopian | 74d211a | 2013-04-22 16:55:35 +0200 | [diff] [blame] | 2106 | result.appendFormat( |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 2107 | "+ %s %p (%s)\n", |
| 2108 | getTypeId(), this, getName().string()); |
Mathias Agopian | 3e25fd8 | 2013-04-22 17:52:16 +0200 | [diff] [blame] | 2109 | colorizer.reset(result); |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 2110 | |
Mathias Agopian | 2ca7939 | 2013-04-02 18:30:32 -0700 | [diff] [blame] | 2111 | s.activeTransparentRegion.dump(result, "transparentRegion"); |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 2112 | visibleRegion.dump(result, "visibleRegion"); |
Dan Stoza | ee44edd | 2015-03-23 15:50:23 -0700 | [diff] [blame] | 2113 | surfaceDamageRegion.dump(result, "surfaceDamageRegion"); |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 2114 | sp<Client> client(mClientRef.promote()); |
| 2115 | |
Mathias Agopian | 74d211a | 2013-04-22 16:55:35 +0200 | [diff] [blame] | 2116 | result.appendFormat( " " |
Pablo Ceballos | acbe678 | 2016-03-04 17:54:21 +0000 | [diff] [blame] | 2117 | "layerStack=%4d, z=%9d, pos=(%g,%g), size=(%4d,%4d), " |
| 2118 | "crop=(%4d,%4d,%4d,%4d), finalCrop=(%4d,%4d,%4d,%4d), " |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 2119 | "isOpaque=%1d, invalidate=%1d, " |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 2120 | #ifdef USE_HWC2 |
| 2121 | "alpha=%.3f, flags=0x%08x, tr=[%.2f, %.2f][%.2f, %.2f]\n" |
| 2122 | #else |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 2123 | "alpha=0x%02x, flags=0x%08x, tr=[%.2f, %.2f][%.2f, %.2f]\n" |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 2124 | #endif |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 2125 | " client=%p\n", |
Robert Carr | 3dcabfa | 2016-03-01 18:36:58 -0800 | [diff] [blame] | 2126 | s.layerStack, s.z, s.active.transform.tx(), s.active.transform.ty(), s.active.w, s.active.h, |
Robert Carr | b5d3d26 | 2016-03-25 15:08:13 -0700 | [diff] [blame] | 2127 | s.crop.left, s.crop.top, |
| 2128 | s.crop.right, s.crop.bottom, |
| 2129 | s.finalCrop.left, s.finalCrop.top, |
| 2130 | s.finalCrop.right, s.finalCrop.bottom, |
Andy McFadden | 4125a4f | 2014-01-29 17:17:11 -0800 | [diff] [blame] | 2131 | isOpaque(s), contentDirty, |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 2132 | s.alpha, s.flags, |
Robert Carr | 3dcabfa | 2016-03-01 18:36:58 -0800 | [diff] [blame] | 2133 | s.active.transform[0][0], s.active.transform[0][1], |
| 2134 | s.active.transform[1][0], s.active.transform[1][1], |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 2135 | client.get()); |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 2136 | |
| 2137 | sp<const GraphicBuffer> buf0(mActiveBuffer); |
| 2138 | uint32_t w0=0, h0=0, s0=0, f0=0; |
| 2139 | if (buf0 != 0) { |
| 2140 | w0 = buf0->getWidth(); |
| 2141 | h0 = buf0->getHeight(); |
| 2142 | s0 = buf0->getStride(); |
| 2143 | f0 = buf0->format; |
| 2144 | } |
Mathias Agopian | 74d211a | 2013-04-22 16:55:35 +0200 | [diff] [blame] | 2145 | result.appendFormat( |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 2146 | " " |
| 2147 | "format=%2d, activeBuffer=[%4ux%4u:%4u,%3X]," |
| 2148 | " queued-frames=%d, mRefreshPending=%d\n", |
| 2149 | mFormat, w0, h0, s0,f0, |
| 2150 | mQueuedFrames, mRefreshPending); |
| 2151 | |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 2152 | if (mSurfaceFlingerConsumer != 0) { |
Mathias Agopian | 74d211a | 2013-04-22 16:55:35 +0200 | [diff] [blame] | 2153 | mSurfaceFlingerConsumer->dump(result, " "); |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 2154 | } |
| 2155 | } |
| 2156 | |
Svetoslav | d85084b | 2014-03-20 10:28:31 -0700 | [diff] [blame] | 2157 | void Layer::dumpFrameStats(String8& result) const { |
| 2158 | mFrameTracker.dumpStats(result); |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 2159 | } |
| 2160 | |
Svetoslav | d85084b | 2014-03-20 10:28:31 -0700 | [diff] [blame] | 2161 | void Layer::clearFrameStats() { |
| 2162 | mFrameTracker.clearStats(); |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 2163 | } |
| 2164 | |
Jamie Gennis | 6547ff4 | 2013-07-16 20:12:42 -0700 | [diff] [blame] | 2165 | void Layer::logFrameStats() { |
| 2166 | mFrameTracker.logAndResetStats(mName); |
| 2167 | } |
| 2168 | |
Svetoslav | d85084b | 2014-03-20 10:28:31 -0700 | [diff] [blame] | 2169 | void Layer::getFrameStats(FrameStats* outStats) const { |
| 2170 | mFrameTracker.getStats(outStats); |
| 2171 | } |
| 2172 | |
Pablo Ceballos | 40845df | 2016-01-25 17:41:15 -0800 | [diff] [blame] | 2173 | void Layer::getFenceData(String8* outName, uint64_t* outFrameNumber, |
| 2174 | bool* outIsGlesComposition, nsecs_t* outPostedTime, |
| 2175 | sp<Fence>* outAcquireFence, sp<Fence>* outPrevReleaseFence) const { |
| 2176 | *outName = mName; |
| 2177 | *outFrameNumber = mSurfaceFlingerConsumer->getFrameNumber(); |
| 2178 | |
| 2179 | #ifdef USE_HWC2 |
| 2180 | *outIsGlesComposition = mHwcLayers.count(HWC_DISPLAY_PRIMARY) ? |
| 2181 | mHwcLayers.at(HWC_DISPLAY_PRIMARY).compositionType == |
| 2182 | HWC2::Composition::Client : true; |
| 2183 | #else |
| 2184 | *outIsGlesComposition = mIsGlesComposition; |
| 2185 | #endif |
| 2186 | *outPostedTime = mSurfaceFlingerConsumer->getTimestamp(); |
| 2187 | *outAcquireFence = mSurfaceFlingerConsumer->getCurrentFence(); |
| 2188 | *outPrevReleaseFence = mSurfaceFlingerConsumer->getPrevReleaseFence(); |
| 2189 | } |
Dan Stoza | e77c766 | 2016-05-13 11:37:28 -0700 | [diff] [blame^] | 2190 | |
| 2191 | std::vector<OccupancyTracker::Segment> Layer::getOccupancyHistory( |
| 2192 | bool forceFlush) { |
| 2193 | std::vector<OccupancyTracker::Segment> history; |
| 2194 | status_t result = mSurfaceFlingerConsumer->getOccupancyHistory(forceFlush, |
| 2195 | &history); |
| 2196 | if (result != NO_ERROR) { |
| 2197 | ALOGW("[%s] Failed to obtain occupancy history (%d)", mName.string(), |
| 2198 | result); |
| 2199 | return {}; |
| 2200 | } |
| 2201 | return history; |
| 2202 | } |
| 2203 | |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 2204 | // --------------------------------------------------------------------------- |
| 2205 | |
| 2206 | Layer::LayerCleaner::LayerCleaner(const sp<SurfaceFlinger>& flinger, |
| 2207 | const sp<Layer>& layer) |
| 2208 | : mFlinger(flinger), mLayer(layer) { |
| 2209 | } |
| 2210 | |
| 2211 | Layer::LayerCleaner::~LayerCleaner() { |
| 2212 | // destroy client resources |
| 2213 | mFlinger->onLayerDestroyed(mLayer); |
| 2214 | } |
| 2215 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2216 | // --------------------------------------------------------------------------- |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2217 | }; // namespace android |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 2218 | |
| 2219 | #if defined(__gl_h_) |
| 2220 | #error "don't include gl/gl.h in this file" |
| 2221 | #endif |
| 2222 | |
| 2223 | #if defined(__gl2_h_) |
| 2224 | #error "don't include gl2/gl2.h in this file" |
| 2225 | #endif |