| 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" | 
| Adithya Srinivasan | 5f683cf | 2020-09-15 14:21:04 -0700 | [diff] [blame] | 40 | #include "FrameTimeline.h" | 
| Lloyd Pique | feb73d7 | 2018-12-04 17:23:44 -0800 | [diff] [blame] | 41 | #include "FrameTracker.h" | 
|  | 42 | #include "Layer.h" | 
|  | 43 | #include "LayerVector.h" | 
|  | 44 | #include "MonitoredProducer.h" | 
|  | 45 | #include "SurfaceFlinger.h" | 
| David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 46 |  | 
|  | 47 | namespace android { | 
|  | 48 |  | 
| Marissa Wall | fd66862 | 2018-05-10 10:21:13 -0700 | [diff] [blame] | 49 | class BufferLayer : public Layer { | 
| David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 50 | public: | 
| Lloyd Pique | 42ab75e | 2018-09-12 20:46:03 -0700 | [diff] [blame] | 51 | explicit BufferLayer(const LayerCreationArgs& args); | 
| Alec Mouri | 4545a8a | 2019-08-08 20:05:32 -0700 | [diff] [blame] | 52 | virtual ~BufferLayer() override; | 
| David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 53 |  | 
| Peiyong Lin | f243e5e | 2020-08-22 17:40:59 -0700 | [diff] [blame] | 54 | // Implements Layer. | 
| Lloyd Pique | de19665 | 2020-01-22 17:29:58 -0800 | [diff] [blame] | 55 | sp<compositionengine::LayerFE> getCompositionEngineLayerFE() const override; | 
|  | 56 | compositionengine::LayerFECompositionState* editCompositionState() override; | 
| Lloyd Pique | feb73d7 | 2018-12-04 17:23:44 -0800 | [diff] [blame] | 57 |  | 
| Marissa Wall | fd66862 | 2018-05-10 10:21:13 -0700 | [diff] [blame] | 58 | // If we have received a new buffer this frame, we will pass its surface | 
|  | 59 | // damage down to hardware composer. Otherwise, we must send a region with | 
|  | 60 | // one empty rect. | 
|  | 61 | void useSurfaceDamage() override; | 
|  | 62 | void useEmptyDamage() override; | 
| David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 63 |  | 
| Marissa Wall | fd66862 | 2018-05-10 10:21:13 -0700 | [diff] [blame] | 64 | bool isOpaque(const Layer::State& s) const override; | 
| David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 65 |  | 
| Marissa Wall | fd66862 | 2018-05-10 10:21:13 -0700 | [diff] [blame] | 66 | // isVisible - true if this layer is visible, false otherwise | 
| Lloyd Pique | 0449b0f | 2018-12-20 16:23:45 -0800 | [diff] [blame] | 67 | bool isVisible() const override; | 
| David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 68 |  | 
| Peiyong Lin | fb530cf | 2018-12-15 05:07:38 +0000 | [diff] [blame] | 69 | // isProtected - true if the layer may contain protected content in the | 
|  | 70 | // GRALLOC_USAGE_PROTECTED sense. | 
|  | 71 | bool isProtected() const override; | 
|  | 72 |  | 
| Marissa Wall | fd66862 | 2018-05-10 10:21:13 -0700 | [diff] [blame] | 73 | // isFixedSize - true if content has a fixed size | 
| David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 74 | bool isFixedSize() const override; | 
|  | 75 |  | 
| Lloyd Pique | a83776c | 2019-01-29 18:42:32 -0800 | [diff] [blame] | 76 | bool usesSourceCrop() const override; | 
|  | 77 |  | 
| Marissa Wall | fd66862 | 2018-05-10 10:21:13 -0700 | [diff] [blame] | 78 | bool isHdrY410() const override; | 
| David Sodman | eb085e0 | 2017-10-05 18:49:04 -0700 | [diff] [blame] | 79 |  | 
| Dominik Laskowski | b7251f4 | 2020-04-20 17:42:59 -0700 | [diff] [blame] | 80 | bool onPostComposition(const DisplayDevice*, const std::shared_ptr<FenceTime>& glDoneFence, | 
| David Sodman | eb085e0 | 2017-10-05 18:49:04 -0700 | [diff] [blame] | 81 | const std::shared_ptr<FenceTime>& presentFence, | 
| Dominik Laskowski | b7251f4 | 2020-04-20 17:42:59 -0700 | [diff] [blame] | 82 | const CompositorTiming&) override; | 
| David Sodman | eb085e0 | 2017-10-05 18:49:04 -0700 | [diff] [blame] | 83 |  | 
| Marissa Wall | fd66862 | 2018-05-10 10:21:13 -0700 | [diff] [blame] | 84 | // latchBuffer - called each time the screen is redrawn and returns whether | 
|  | 85 | // the visible regions need to be recomputed (this is a fairly heavy | 
|  | 86 | // operation, so this should be set only if needed). Typically this is used | 
|  | 87 | // to figure out if the content or size of a surface has changed. | 
| Dominik Laskowski | a8955dd | 2019-07-10 10:19:09 -0700 | [diff] [blame] | 88 | bool latchBuffer(bool& recomputeVisibleRegions, nsecs_t latchTime, | 
|  | 89 | nsecs_t expectedPresentTime) override; | 
| Marissa Wall | fd66862 | 2018-05-10 10:21:13 -0700 | [diff] [blame] | 90 |  | 
| David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 91 | bool isBufferLatched() const override { return mRefreshPending; } | 
|  | 92 |  | 
| Dominik Laskowski | a8955dd | 2019-07-10 10:19:09 -0700 | [diff] [blame] | 93 | void notifyAvailableFrames(nsecs_t expectedPresentTime) override; | 
| Chia-I Wu | 692e083 | 2018-06-05 15:46:58 -0700 | [diff] [blame] | 94 |  | 
| Lloyd Pique | 0449b0f | 2018-12-20 16:23:45 -0800 | [diff] [blame] | 95 | bool hasReadyFrame() const override; | 
| David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 96 |  | 
| David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 97 | // Returns the current scaling mode, unless mOverrideScalingMode | 
|  | 98 | // is set, in which case, it returns mOverrideScalingMode | 
|  | 99 | uint32_t getEffectiveScalingMode() const override; | 
| chaviw | 49a108c | 2019-08-12 11:23:06 -0700 | [diff] [blame] | 100 |  | 
|  | 101 | // Calls latchBuffer if the buffer has a frame queued and then releases the buffer. | 
|  | 102 | // This is used if the buffer is just latched and releases to free up the buffer | 
|  | 103 | // and will not be shown on screen. | 
|  | 104 | // Should only be called on the main thread. | 
|  | 105 | void latchAndReleaseBuffer() override; | 
|  | 106 |  | 
| chaviw | 4244e03 | 2019-09-04 11:27:49 -0700 | [diff] [blame] | 107 | bool getTransformToDisplayInverse() const override; | 
|  | 108 |  | 
|  | 109 | Rect getBufferCrop() const override; | 
|  | 110 |  | 
|  | 111 | uint32_t getBufferTransform() const override; | 
|  | 112 |  | 
|  | 113 | ui::Dataspace getDataSpace() const override; | 
|  | 114 |  | 
| chaviw | d62d306 | 2019-09-04 14:48:02 -0700 | [diff] [blame] | 115 | sp<GraphicBuffer> getBuffer() const override; | 
|  | 116 |  | 
| Vishnu Nair | 6213bd9 | 2020-05-08 17:42:25 -0700 | [diff] [blame] | 117 | ui::Transform::RotationFlags getTransformHint() const override { return mTransformHint; } | 
|  | 118 |  | 
| Marissa Wall | fd66862 | 2018-05-10 10:21:13 -0700 | [diff] [blame] | 119 | protected: | 
| chaviw | 4244e03 | 2019-09-04 11:27:49 -0700 | [diff] [blame] | 120 | struct BufferInfo { | 
|  | 121 | nsecs_t mDesiredPresentTime; | 
|  | 122 | std::shared_ptr<FenceTime> mFenceTime; | 
|  | 123 | sp<Fence> mFence; | 
| chaviw | 4244e03 | 2019-09-04 11:27:49 -0700 | [diff] [blame] | 124 | uint32_t mTransform{0}; | 
| chaviw | f83ce18 | 2019-09-12 14:43:08 -0700 | [diff] [blame] | 125 | ui::Dataspace mDataspace{ui::Dataspace::UNKNOWN}; | 
| chaviw | 4244e03 | 2019-09-04 11:27:49 -0700 | [diff] [blame] | 126 | Rect mCrop; | 
|  | 127 | uint32_t mScaleMode{NATIVE_WINDOW_SCALING_MODE_FREEZE}; | 
|  | 128 | Region mSurfaceDamage; | 
|  | 129 | HdrMetadata mHdrMetadata; | 
|  | 130 | int mApi; | 
| chaviw | debadb8 | 2020-03-26 14:57:24 -0700 | [diff] [blame] | 131 | PixelFormat mPixelFormat{PIXEL_FORMAT_NONE}; | 
| chaviw | 4244e03 | 2019-09-04 11:27:49 -0700 | [diff] [blame] | 132 | bool mTransformToDisplayInverse{false}; | 
| chaviw | d62d306 | 2019-09-04 14:48:02 -0700 | [diff] [blame] | 133 |  | 
|  | 134 | sp<GraphicBuffer> mBuffer; | 
|  | 135 | int mBufferSlot{BufferQueue::INVALID_BUFFER_SLOT}; | 
| chaviw | 74b0317 | 2019-08-19 11:09:03 -0700 | [diff] [blame] | 136 |  | 
|  | 137 | bool mFrameLatencyNeeded{false}; | 
| chaviw | 4244e03 | 2019-09-04 11:27:49 -0700 | [diff] [blame] | 138 | }; | 
|  | 139 |  | 
|  | 140 | BufferInfo mBufferInfo; | 
|  | 141 | virtual void gatherBufferInfo() = 0; | 
|  | 142 |  | 
| Vishnu Nair | b87d94f | 2020-02-13 09:17:36 -0800 | [diff] [blame] | 143 | std::optional<compositionengine::LayerFE::LayerSettings> prepareClientComposition( | 
|  | 144 | compositionengine::LayerFE::ClientCompositionTargetSettings&) override; | 
|  | 145 |  | 
| Lloyd Pique | f16688f | 2019-02-19 17:47:57 -0800 | [diff] [blame] | 146 | /* | 
|  | 147 | * compositionengine::LayerFE overrides | 
|  | 148 | */ | 
| Lloyd Pique | de19665 | 2020-01-22 17:29:58 -0800 | [diff] [blame] | 149 | const compositionengine::LayerFECompositionState* getCompositionState() const override; | 
| Lloyd Pique | f16688f | 2019-02-19 17:47:57 -0800 | [diff] [blame] | 150 | bool onPreComposition(nsecs_t) override; | 
| Lloyd Pique | de19665 | 2020-01-22 17:29:58 -0800 | [diff] [blame] | 151 | void preparePerFrameCompositionState() override; | 
| Lloyd Pique | f527548 | 2019-01-29 18:42:42 -0800 | [diff] [blame] | 152 |  | 
| David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 153 | // Check all of the local sync points to ensure that all transactions | 
|  | 154 | // which need to have been applied prior to the frame which is about to | 
|  | 155 | // be latched have signaled | 
| Dominik Laskowski | a8955dd | 2019-07-10 10:19:09 -0700 | [diff] [blame] | 156 | bool allTransactionsSignaled(nsecs_t expectedPresentTime); | 
| David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 157 |  | 
| Marissa Wall | fd66862 | 2018-05-10 10:21:13 -0700 | [diff] [blame] | 158 | static bool getOpacityForFormat(uint32_t format); | 
| David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 159 |  | 
| Peiyong Lin | f3ffc4e | 2019-12-13 00:46:24 -0800 | [diff] [blame] | 160 | // from graphics API | 
| Marissa Wall | fd66862 | 2018-05-10 10:21:13 -0700 | [diff] [blame] | 161 | const uint32_t mTextureName; | 
|  | 162 |  | 
| chaviw | f206b66 | 2019-01-11 13:07:19 -0800 | [diff] [blame] | 163 | bool mRefreshPending{false}; | 
|  | 164 |  | 
| chaviw | 4244e03 | 2019-09-04 11:27:49 -0700 | [diff] [blame] | 165 | ui::Dataspace translateDataspace(ui::Dataspace dataspace); | 
| chaviw | b4c6e58 | 2019-08-16 14:35:07 -0700 | [diff] [blame] | 166 | void setInitialValuesForClone(const sp<Layer>& clonedFrom); | 
| chaviw | 74b0317 | 2019-08-19 11:09:03 -0700 | [diff] [blame] | 167 | void updateCloneBufferInfo() override; | 
|  | 168 | uint64_t mPreviousFrameNumber = 0; | 
| chaviw | 4244e03 | 2019-09-04 11:27:49 -0700 | [diff] [blame] | 169 |  | 
| Vishnu Nair | 6b7c5c9 | 2020-09-29 17:27:05 -0700 | [diff] [blame] | 170 | uint64_t getHeadFrameNumber(nsecs_t expectedPresentTime) const override; | 
| Robert Carr | fe1209c | 2020-02-11 12:25:35 -0800 | [diff] [blame] | 171 |  | 
| Dominik Laskowski | b7251f4 | 2020-04-20 17:42:59 -0700 | [diff] [blame] | 172 | void setTransformHint(ui::Transform::RotationFlags displayTransformHint) override; | 
|  | 173 |  | 
| Vishnu Nair | 6213bd9 | 2020-05-08 17:42:25 -0700 | [diff] [blame] | 174 | // Transform hint provided to the producer. This must be accessed holding | 
|  | 175 | /// the mStateLock. | 
| Dominik Laskowski | b7251f4 | 2020-04-20 17:42:59 -0700 | [diff] [blame] | 176 | ui::Transform::RotationFlags mTransformHint = ui::Transform::ROT_0; | 
| Vishnu Nair | 6213bd9 | 2020-05-08 17:42:25 -0700 | [diff] [blame] | 177 |  | 
| Marissa Wall | fd66862 | 2018-05-10 10:21:13 -0700 | [diff] [blame] | 178 | private: | 
| Peiyong Lin | f243e5e | 2020-08-22 17:40:59 -0700 | [diff] [blame] | 179 | virtual bool fenceHasSignaled() const = 0; | 
|  | 180 | virtual bool framePresentTimeIsCurrent(nsecs_t expectedPresentTime) const = 0; | 
|  | 181 | virtual uint64_t getFrameNumber(nsecs_t expectedPresentTime) const = 0; | 
|  | 182 |  | 
|  | 183 | virtual bool getAutoRefresh() const = 0; | 
|  | 184 | virtual bool getSidebandStreamChanged() const = 0; | 
|  | 185 |  | 
|  | 186 | // Latch sideband stream and returns true if the dirty region should be updated. | 
|  | 187 | virtual bool latchSidebandStream(bool& recomputeVisibleRegions) = 0; | 
|  | 188 |  | 
|  | 189 | virtual bool hasFrameUpdate() const = 0; | 
|  | 190 |  | 
| Peiyong Lin | f243e5e | 2020-08-22 17:40:59 -0700 | [diff] [blame] | 191 | virtual status_t updateTexImage(bool& recomputeVisibleRegions, nsecs_t latchTime, | 
|  | 192 | nsecs_t expectedPresentTime) = 0; | 
|  | 193 |  | 
|  | 194 | virtual status_t updateActiveBuffer() = 0; | 
|  | 195 | virtual status_t updateFrameNumber(nsecs_t latchTime) = 0; | 
|  | 196 |  | 
|  | 197 | // We generate InputWindowHandles for all non-cursor buffered layers regardless of whether they | 
|  | 198 | // have an InputChannel. This is to enable the InputDispatcher to do PID based occlusion | 
|  | 199 | // detection. | 
|  | 200 | bool needsInputInfo() const override { return !mPotentialCursor; } | 
|  | 201 |  | 
| Peiyong Lin | c2020ca | 2019-01-10 11:36:12 -0800 | [diff] [blame] | 202 | // Returns true if this layer requires filtering | 
| Dominik Laskowski | b7251f4 | 2020-04-20 17:42:59 -0700 | [diff] [blame] | 203 | bool needsFiltering(const DisplayDevice*) const override; | 
|  | 204 | bool needsFilteringForScreenshots(const DisplayDevice*, | 
| Alec Mouri | 5a6d857 | 2020-03-23 23:56:15 -0700 | [diff] [blame] | 205 | const ui::Transform& inverseParentTransform) const override; | 
| Marissa Wall | fd66862 | 2018-05-10 10:21:13 -0700 | [diff] [blame] | 206 |  | 
| Marissa Wall | 290ad08 | 2019-03-06 13:23:47 -0800 | [diff] [blame] | 207 | // BufferStateLayers can return Rect::INVALID_RECT if the layer does not have a display frame | 
|  | 208 | // and its parent layer is not bounded | 
| Lloyd Pique | 0449b0f | 2018-12-20 16:23:45 -0800 | [diff] [blame] | 209 | Rect getBufferSize(const State& s) const override; | 
| Lloyd Pique | feb73d7 | 2018-12-04 17:23:44 -0800 | [diff] [blame] | 210 |  | 
| Peiyong Lin | f243e5e | 2020-08-22 17:40:59 -0700 | [diff] [blame] | 211 | PixelFormat getPixelFormat() const; | 
|  | 212 |  | 
|  | 213 | // Computes the transform matrix using the setFilteringEnabled to determine whether the | 
|  | 214 | // transform matrix should be computed for use with bilinear filtering. | 
|  | 215 | void getDrawingTransformMatrix(bool filteringEnabled, float outMatrix[16]); | 
|  | 216 |  | 
| Lloyd Pique | de19665 | 2020-01-22 17:29:58 -0800 | [diff] [blame] | 217 | std::unique_ptr<compositionengine::LayerFECompositionState> mCompositionState; | 
| Vishnu Nair | 4351ad5 | 2019-02-11 14:13:02 -0800 | [diff] [blame] | 218 |  | 
|  | 219 | FloatRect computeSourceBounds(const FloatRect& parentBounds) const override; | 
| David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 220 | }; | 
|  | 221 |  | 
|  | 222 | } // namespace android |