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