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