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