| David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 1 | /* | 
|  | 2 | * Copyright (C) 2017 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 |  | 
|  | 17 | #pragma once | 
|  | 18 |  | 
| Lloyd Pique | feb73d7 | 2018-12-04 17:23:44 -0800 | [diff] [blame] | 19 | #include <sys/types.h> | 
|  | 20 | #include <cstdint> | 
|  | 21 | #include <list> | 
| David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 22 |  | 
|  | 23 | #include <gui/ISurfaceComposerClient.h> | 
|  | 24 | #include <gui/LayerState.h> | 
| Alec Mouri | e7d1d4a | 2019-02-05 01:13:46 +0000 | [diff] [blame] | 25 | #include <renderengine/Image.h> | 
| Peiyong Lin | cbc184f | 2018-08-22 13:24:10 -0700 | [diff] [blame] | 26 | #include <renderengine/Mesh.h> | 
|  | 27 | #include <renderengine/Texture.h> | 
| Lloyd Pique | feb73d7 | 2018-12-04 17:23:44 -0800 | [diff] [blame] | 28 | #include <system/window.h> // For NATIVE_WINDOW_SCALING_MODE_FREEZE | 
| David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 29 | #include <ui/FrameStats.h> | 
|  | 30 | #include <ui/GraphicBuffer.h> | 
|  | 31 | #include <ui/PixelFormat.h> | 
|  | 32 | #include <ui/Region.h> | 
| David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 33 | #include <utils/RefBase.h> | 
|  | 34 | #include <utils/String8.h> | 
|  | 35 | #include <utils/Timers.h> | 
|  | 36 |  | 
| Lloyd Pique | feb73d7 | 2018-12-04 17:23:44 -0800 | [diff] [blame] | 37 | #include "BufferLayerConsumer.h" | 
|  | 38 | #include "Client.h" | 
|  | 39 | #include "DisplayHardware/HWComposer.h" | 
| Lloyd Pique | feb73d7 | 2018-12-04 17:23:44 -0800 | [diff] [blame] | 40 | #include "FrameTracker.h" | 
|  | 41 | #include "Layer.h" | 
|  | 42 | #include "LayerVector.h" | 
|  | 43 | #include "MonitoredProducer.h" | 
|  | 44 | #include "SurfaceFlinger.h" | 
| David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 45 |  | 
|  | 46 | namespace android { | 
|  | 47 |  | 
| Marissa Wall | fd66862 | 2018-05-10 10:21:13 -0700 | [diff] [blame] | 48 | class BufferLayer : public Layer { | 
| David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 49 | public: | 
| Lloyd Pique | 42ab75e | 2018-09-12 20:46:03 -0700 | [diff] [blame] | 50 | explicit BufferLayer(const LayerCreationArgs& args); | 
| David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 51 | ~BufferLayer() override; | 
|  | 52 |  | 
|  | 53 | // ----------------------------------------------------------------------- | 
|  | 54 | // Overriden from Layer | 
|  | 55 | // ----------------------------------------------------------------------- | 
| Marissa Wall | fd66862 | 2018-05-10 10:21:13 -0700 | [diff] [blame] | 56 | public: | 
| Lloyd Pique | feb73d7 | 2018-12-04 17:23:44 -0800 | [diff] [blame] | 57 | std::shared_ptr<compositionengine::Layer> getCompositionLayer() const override; | 
|  | 58 |  | 
| Marissa Wall | fd66862 | 2018-05-10 10:21:13 -0700 | [diff] [blame] | 59 | // If we have received a new buffer this frame, we will pass its surface | 
|  | 60 | // damage down to hardware composer. Otherwise, we must send a region with | 
|  | 61 | // one empty rect. | 
|  | 62 | void useSurfaceDamage() override; | 
|  | 63 | void useEmptyDamage() override; | 
| David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 64 |  | 
| Marissa Wall | fd66862 | 2018-05-10 10:21:13 -0700 | [diff] [blame] | 65 | // getTypeId - Provide unique string for each class type in the Layer | 
|  | 66 | // hierarchy | 
| David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 67 | const char* getTypeId() const override { return "BufferLayer"; } | 
|  | 68 |  | 
| Marissa Wall | fd66862 | 2018-05-10 10:21:13 -0700 | [diff] [blame] | 69 | bool isOpaque(const Layer::State& s) const override; | 
| David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 70 |  | 
| Marissa Wall | fd66862 | 2018-05-10 10:21:13 -0700 | [diff] [blame] | 71 | // isVisible - true if this layer is visible, false otherwise | 
| Lloyd Pique | 0449b0f | 2018-12-20 16:23:45 -0800 | [diff] [blame] | 72 | bool isVisible() const override; | 
| David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 73 |  | 
| Peiyong Lin | fb530cf | 2018-12-15 05:07:38 +0000 | [diff] [blame] | 74 | // isProtected - true if the layer may contain protected content in the | 
|  | 75 | // GRALLOC_USAGE_PROTECTED sense. | 
|  | 76 | bool isProtected() const override; | 
|  | 77 |  | 
| Marissa Wall | fd66862 | 2018-05-10 10:21:13 -0700 | [diff] [blame] | 78 | // isFixedSize - true if content has a fixed size | 
| David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 79 | bool isFixedSize() const override; | 
|  | 80 |  | 
| Marissa Wall | fd66862 | 2018-05-10 10:21:13 -0700 | [diff] [blame] | 81 | bool isHdrY410() const override; | 
| David Sodman | eb085e0 | 2017-10-05 18:49:04 -0700 | [diff] [blame] | 82 |  | 
| Lloyd Pique | 37c2c9b | 2018-12-04 17:25:10 -0800 | [diff] [blame] | 83 | void setPerFrameData(const sp<const DisplayDevice>& display, const ui::Transform& transform, | 
|  | 84 | const Rect& viewport, int32_t supportedPerFrameMetadata) override; | 
| Marissa Wall | fd66862 | 2018-05-10 10:21:13 -0700 | [diff] [blame] | 85 |  | 
|  | 86 | bool onPreComposition(nsecs_t refreshStartTime) override; | 
| Dominik Laskowski | 075d317 | 2018-05-24 15:50:06 -0700 | [diff] [blame] | 87 | bool onPostComposition(const std::optional<DisplayId>& displayId, | 
|  | 88 | const std::shared_ptr<FenceTime>& glDoneFence, | 
| David Sodman | eb085e0 | 2017-10-05 18:49:04 -0700 | [diff] [blame] | 89 | const std::shared_ptr<FenceTime>& presentFence, | 
| Lloyd Pique | 0449b0f | 2018-12-20 16:23:45 -0800 | [diff] [blame] | 90 | const CompositorTiming& compositorTiming) override; | 
| David Sodman | eb085e0 | 2017-10-05 18:49:04 -0700 | [diff] [blame] | 91 |  | 
| Marissa Wall | fd66862 | 2018-05-10 10:21:13 -0700 | [diff] [blame] | 92 | // latchBuffer - called each time the screen is redrawn and returns whether | 
|  | 93 | // the visible regions need to be recomputed (this is a fairly heavy | 
|  | 94 | // operation, so this should be set only if needed). Typically this is used | 
|  | 95 | // to figure out if the content or size of a surface has changed. | 
| Alec Mouri | 56e538f | 2019-01-14 15:22:01 -0800 | [diff] [blame] | 96 | bool latchBuffer(bool& recomputeVisibleRegions, nsecs_t latchTime) override; | 
| Marissa Wall | fd66862 | 2018-05-10 10:21:13 -0700 | [diff] [blame] | 97 |  | 
| David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 98 | bool isBufferLatched() const override { return mRefreshPending; } | 
|  | 99 |  | 
| Marissa Wall | fd66862 | 2018-05-10 10:21:13 -0700 | [diff] [blame] | 100 | void notifyAvailableFrames() override; | 
| Chia-I Wu | 692e083 | 2018-06-05 15:46:58 -0700 | [diff] [blame] | 101 |  | 
| Lloyd Pique | 0449b0f | 2018-12-20 16:23:45 -0800 | [diff] [blame] | 102 | bool hasReadyFrame() const override; | 
| David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 103 |  | 
| David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 104 | // Returns the current scaling mode, unless mOverrideScalingMode | 
|  | 105 | // is set, in which case, it returns mOverrideScalingMode | 
|  | 106 | uint32_t getEffectiveScalingMode() const override; | 
| Marissa Wall | fd66862 | 2018-05-10 10:21:13 -0700 | [diff] [blame] | 107 | // ----------------------------------------------------------------------- | 
|  | 108 |  | 
|  | 109 | // ----------------------------------------------------------------------- | 
|  | 110 | // Functions that must be implemented by derived classes | 
|  | 111 | // ----------------------------------------------------------------------- | 
|  | 112 | private: | 
| Lloyd Pique | 0449b0f | 2018-12-20 16:23:45 -0800 | [diff] [blame] | 113 | virtual bool fenceHasSignaled() const = 0; | 
| Marissa Wall | fd66862 | 2018-05-10 10:21:13 -0700 | [diff] [blame] | 114 |  | 
|  | 115 | virtual nsecs_t getDesiredPresentTime() = 0; | 
| Lloyd Pique | 0449b0f | 2018-12-20 16:23:45 -0800 | [diff] [blame] | 116 | virtual std::shared_ptr<FenceTime> getCurrentFenceTime() const = 0; | 
| Marissa Wall | fd66862 | 2018-05-10 10:21:13 -0700 | [diff] [blame] | 117 |  | 
| Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 118 | virtual void getDrawingTransformMatrix(float *matrix) = 0; | 
| Lloyd Pique | 0449b0f | 2018-12-20 16:23:45 -0800 | [diff] [blame] | 119 | virtual uint32_t getDrawingTransform() const = 0; | 
|  | 120 | virtual ui::Dataspace getDrawingDataSpace() const = 0; | 
|  | 121 | virtual Rect getDrawingCrop() const = 0; | 
| Marissa Wall | fd66862 | 2018-05-10 10:21:13 -0700 | [diff] [blame] | 122 | virtual uint32_t getDrawingScalingMode() const = 0; | 
| Lloyd Pique | 0449b0f | 2018-12-20 16:23:45 -0800 | [diff] [blame] | 123 | virtual Region getDrawingSurfaceDamage() const = 0; | 
|  | 124 | virtual const HdrMetadata& getDrawingHdrMetadata() const = 0; | 
|  | 125 | virtual int getDrawingApi() const = 0; | 
| Marissa Wall | fd66862 | 2018-05-10 10:21:13 -0700 | [diff] [blame] | 126 | virtual PixelFormat getPixelFormat() const = 0; | 
|  | 127 |  | 
|  | 128 | virtual uint64_t getFrameNumber() const = 0; | 
|  | 129 |  | 
|  | 130 | virtual bool getAutoRefresh() const = 0; | 
|  | 131 | virtual bool getSidebandStreamChanged() const = 0; | 
|  | 132 |  | 
| Vishnu Nair | 6194e2e | 2019-02-06 12:58:39 -0800 | [diff] [blame] | 133 | // Latch sideband stream and returns true if the dirty region should be updated. | 
|  | 134 | virtual bool latchSidebandStream(bool& recomputeVisibleRegions) = 0; | 
| Marissa Wall | fd66862 | 2018-05-10 10:21:13 -0700 | [diff] [blame] | 135 |  | 
| Lloyd Pique | 0449b0f | 2018-12-20 16:23:45 -0800 | [diff] [blame] | 136 | virtual bool hasFrameUpdate() const = 0; | 
| Marissa Wall | fd66862 | 2018-05-10 10:21:13 -0700 | [diff] [blame] | 137 |  | 
| Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 138 | virtual void setFilteringEnabled(bool enabled) = 0; | 
| Marissa Wall | fd66862 | 2018-05-10 10:21:13 -0700 | [diff] [blame] | 139 |  | 
| Lloyd Pique | 0449b0f | 2018-12-20 16:23:45 -0800 | [diff] [blame] | 140 | virtual status_t bindTextureImage() = 0; | 
| Alec Mouri | 56e538f | 2019-01-14 15:22:01 -0800 | [diff] [blame] | 141 | virtual status_t updateTexImage(bool& recomputeVisibleRegions, nsecs_t latchTime) = 0; | 
| Marissa Wall | fd66862 | 2018-05-10 10:21:13 -0700 | [diff] [blame] | 142 |  | 
| Lloyd Pique | 0449b0f | 2018-12-20 16:23:45 -0800 | [diff] [blame] | 143 | virtual status_t updateActiveBuffer() = 0; | 
| Marissa Wall | fd66862 | 2018-05-10 10:21:13 -0700 | [diff] [blame] | 144 | virtual status_t updateFrameNumber(nsecs_t latchTime) = 0; | 
|  | 145 |  | 
| Lloyd Pique | 37c2c9b | 2018-12-04 17:25:10 -0800 | [diff] [blame] | 146 | virtual void setHwcLayerBuffer(const sp<const DisplayDevice>& displayDevice) = 0; | 
| Marissa Wall | fd66862 | 2018-05-10 10:21:13 -0700 | [diff] [blame] | 147 |  | 
| Marissa Wall | fd66862 | 2018-05-10 10:21:13 -0700 | [diff] [blame] | 148 | protected: | 
|  | 149 | // Loads the corresponding system property once per process | 
|  | 150 | static bool latchUnsignaledBuffers(); | 
| David Sodman | eb085e0 | 2017-10-05 18:49:04 -0700 | [diff] [blame] | 151 |  | 
| David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 152 | // Check all of the local sync points to ensure that all transactions | 
|  | 153 | // which need to have been applied prior to the frame which is about to | 
|  | 154 | // be latched have signaled | 
| Lloyd Pique | 0449b0f | 2018-12-20 16:23:45 -0800 | [diff] [blame] | 155 | bool allTransactionsSignaled(); | 
| David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 156 |  | 
| Marissa Wall | fd66862 | 2018-05-10 10:21:13 -0700 | [diff] [blame] | 157 | static bool getOpacityForFormat(uint32_t format); | 
| David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 158 |  | 
| Marissa Wall | fd66862 | 2018-05-10 10:21:13 -0700 | [diff] [blame] | 159 | // from GLES | 
|  | 160 | const uint32_t mTextureName; | 
|  | 161 |  | 
| chaviw | f206b66 | 2019-01-11 13:07:19 -0800 | [diff] [blame] | 162 | bool mRefreshPending{false}; | 
|  | 163 |  | 
| Alec Mouri | e7d1d4a | 2019-02-05 01:13:46 +0000 | [diff] [blame] | 164 | // Returns true if, when drawing the active buffer during gpu compositon, we | 
|  | 165 | // should use a cached buffer or not. | 
|  | 166 | virtual bool useCachedBufferForClientComposition() const = 0; | 
|  | 167 |  | 
|  | 168 | // prepareClientLayer - constructs a RenderEngine layer for GPU composition. | 
|  | 169 | bool prepareClientLayer(const RenderArea& renderArea, const Region& clip, | 
|  | 170 | bool useIdentityTransform, Region& clearRegion, | 
|  | 171 | renderengine::LayerSettings& layer); | 
|  | 172 |  | 
| Marissa Wall | fd66862 | 2018-05-10 10:21:13 -0700 | [diff] [blame] | 173 | private: | 
| Peiyong Lin | c2020ca | 2019-01-10 11:36:12 -0800 | [diff] [blame] | 174 | // Returns true if this layer requires filtering | 
| Lloyd Pique | 37c2c9b | 2018-12-04 17:25:10 -0800 | [diff] [blame] | 175 | bool needsFiltering(const sp<const DisplayDevice>& displayDevice) const; | 
| Marissa Wall | fd66862 | 2018-05-10 10:21:13 -0700 | [diff] [blame] | 176 |  | 
| Lloyd Pique | 0449b0f | 2018-12-20 16:23:45 -0800 | [diff] [blame] | 177 | uint64_t getHeadFrameNumber() const; | 
| Marissa Wall | fd66862 | 2018-05-10 10:21:13 -0700 | [diff] [blame] | 178 |  | 
| Lloyd Pique | 42ab75e | 2018-09-12 20:46:03 -0700 | [diff] [blame] | 179 | uint32_t mCurrentScalingMode{NATIVE_WINDOW_SCALING_MODE_FREEZE}; | 
| Marissa Wall | fd66862 | 2018-05-10 10:21:13 -0700 | [diff] [blame] | 180 |  | 
| Vishnu Nair | 6194e2e | 2019-02-06 12:58:39 -0800 | [diff] [blame] | 181 | bool mTransformToDisplayInverse{false}; | 
|  | 182 |  | 
| Marissa Wall | fd66862 | 2018-05-10 10:21:13 -0700 | [diff] [blame] | 183 | // main thread. | 
| Lloyd Pique | 42ab75e | 2018-09-12 20:46:03 -0700 | [diff] [blame] | 184 | bool mBufferLatched{false}; // TODO: Use mActiveBuffer? | 
| Marissa Wall | fd66862 | 2018-05-10 10:21:13 -0700 | [diff] [blame] | 185 |  | 
| Lloyd Pique | 0449b0f | 2018-12-20 16:23:45 -0800 | [diff] [blame] | 186 | Rect getBufferSize(const State& s) const override; | 
| Lloyd Pique | feb73d7 | 2018-12-04 17:23:44 -0800 | [diff] [blame] | 187 |  | 
|  | 188 | std::shared_ptr<compositionengine::Layer> mCompositionLayer; | 
| Vishnu Nair | 4351ad5 | 2019-02-11 14:13:02 -0800 | [diff] [blame] | 189 |  | 
|  | 190 | FloatRect computeSourceBounds(const FloatRect& parentBounds) const override; | 
| David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 191 | }; | 
|  | 192 |  | 
|  | 193 | } // namespace android |