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