blob: d358baeae0fe57bca18c3b7c527cec88ed52f5bc [file] [log] [blame]
David Sodman0c69cad2017-08-21 12:12:51 -07001/*
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 Piquefeb73d72018-12-04 17:23:44 -080019#include <sys/types.h>
20#include <cstdint>
21#include <list>
David Sodman0c69cad2017-08-21 12:12:51 -070022
23#include <gui/ISurfaceComposerClient.h>
24#include <gui/LayerState.h>
Alec Mourie7d1d4a2019-02-05 01:13:46 +000025#include <renderengine/Image.h>
Peiyong Lincbc184f2018-08-22 13:24:10 -070026#include <renderengine/Mesh.h>
27#include <renderengine/Texture.h>
Lloyd Piquefeb73d72018-12-04 17:23:44 -080028#include <system/window.h> // For NATIVE_WINDOW_SCALING_MODE_FREEZE
David Sodman0c69cad2017-08-21 12:12:51 -070029#include <ui/FrameStats.h>
30#include <ui/GraphicBuffer.h>
31#include <ui/PixelFormat.h>
32#include <ui/Region.h>
David Sodman0c69cad2017-08-21 12:12:51 -070033#include <utils/RefBase.h>
34#include <utils/String8.h>
35#include <utils/Timers.h>
36
Lloyd Piquefeb73d72018-12-04 17:23:44 -080037#include "BufferLayerConsumer.h"
38#include "Client.h"
39#include "DisplayHardware/HWComposer.h"
Lloyd Piquefeb73d72018-12-04 17:23:44 -080040#include "FrameTracker.h"
41#include "Layer.h"
42#include "LayerVector.h"
43#include "MonitoredProducer.h"
44#include "SurfaceFlinger.h"
David Sodman0c69cad2017-08-21 12:12:51 -070045
46namespace android {
47
Marissa Wallfd668622018-05-10 10:21:13 -070048class BufferLayer : public Layer {
David Sodman0c69cad2017-08-21 12:12:51 -070049public:
Lloyd Pique42ab75e2018-09-12 20:46:03 -070050 explicit BufferLayer(const LayerCreationArgs& args);
David Sodman0c69cad2017-08-21 12:12:51 -070051 ~BufferLayer() override;
52
53 // -----------------------------------------------------------------------
54 // Overriden from Layer
55 // -----------------------------------------------------------------------
Marissa Wallfd668622018-05-10 10:21:13 -070056public:
Lloyd Piquefeb73d72018-12-04 17:23:44 -080057 std::shared_ptr<compositionengine::Layer> getCompositionLayer() const override;
58
Marissa Wallfd668622018-05-10 10:21:13 -070059 // 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 Sodman0c69cad2017-08-21 12:12:51 -070064
Marissa Wallfd668622018-05-10 10:21:13 -070065 // getTypeId - Provide unique string for each class type in the Layer
66 // hierarchy
David Sodman0c69cad2017-08-21 12:12:51 -070067 const char* getTypeId() const override { return "BufferLayer"; }
68
Marissa Wallfd668622018-05-10 10:21:13 -070069 bool isOpaque(const Layer::State& s) const override;
David Sodman0c69cad2017-08-21 12:12:51 -070070
Marissa Wallfd668622018-05-10 10:21:13 -070071 // isVisible - true if this layer is visible, false otherwise
Lloyd Pique0449b0f2018-12-20 16:23:45 -080072 bool isVisible() const override;
David Sodman0c69cad2017-08-21 12:12:51 -070073
Peiyong Linfb530cf2018-12-15 05:07:38 +000074 // isProtected - true if the layer may contain protected content in the
75 // GRALLOC_USAGE_PROTECTED sense.
76 bool isProtected() const override;
77
Marissa Wallfd668622018-05-10 10:21:13 -070078 // isFixedSize - true if content has a fixed size
David Sodman0c69cad2017-08-21 12:12:51 -070079 bool isFixedSize() const override;
80
Marissa Wallfd668622018-05-10 10:21:13 -070081 bool isHdrY410() const override;
David Sodmaneb085e02017-10-05 18:49:04 -070082
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080083 void setPerFrameData(const sp<const DisplayDevice>& display, const ui::Transform& transform,
84 const Rect& viewport, int32_t supportedPerFrameMetadata) override;
Marissa Wallfd668622018-05-10 10:21:13 -070085
86 bool onPreComposition(nsecs_t refreshStartTime) override;
Dominik Laskowski075d3172018-05-24 15:50:06 -070087 bool onPostComposition(const std::optional<DisplayId>& displayId,
88 const std::shared_ptr<FenceTime>& glDoneFence,
David Sodmaneb085e02017-10-05 18:49:04 -070089 const std::shared_ptr<FenceTime>& presentFence,
Lloyd Pique0449b0f2018-12-20 16:23:45 -080090 const CompositorTiming& compositorTiming) override;
David Sodmaneb085e02017-10-05 18:49:04 -070091
Marissa Wallfd668622018-05-10 10:21:13 -070092 // 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 Mouri86770e52018-09-24 22:40:58 +000096 // If there was a GL composition step rendering the previous frame, then
97 // releaseFence will be populated with a native fence that fires when
98 // composition has completed.
Vishnu Nair6194e2e2019-02-06 12:58:39 -080099 bool latchBuffer(bool& recomputeVisibleRegions, nsecs_t latchTime,
100 const sp<Fence>& releaseFence) override;
Marissa Wallfd668622018-05-10 10:21:13 -0700101
David Sodman0c69cad2017-08-21 12:12:51 -0700102 bool isBufferLatched() const override { return mRefreshPending; }
103
Marissa Wallfd668622018-05-10 10:21:13 -0700104 void notifyAvailableFrames() override;
Chia-I Wu692e0832018-06-05 15:46:58 -0700105
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800106 bool hasReadyFrame() const override;
David Sodman0c69cad2017-08-21 12:12:51 -0700107
David Sodman0c69cad2017-08-21 12:12:51 -0700108 // Returns the current scaling mode, unless mOverrideScalingMode
109 // is set, in which case, it returns mOverrideScalingMode
110 uint32_t getEffectiveScalingMode() const override;
Marissa Wallfd668622018-05-10 10:21:13 -0700111 // -----------------------------------------------------------------------
112
113 // -----------------------------------------------------------------------
114 // Functions that must be implemented by derived classes
115 // -----------------------------------------------------------------------
116private:
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800117 virtual bool fenceHasSignaled() const = 0;
Marissa Wallfd668622018-05-10 10:21:13 -0700118
119 virtual nsecs_t getDesiredPresentTime() = 0;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800120 virtual std::shared_ptr<FenceTime> getCurrentFenceTime() const = 0;
Marissa Wallfd668622018-05-10 10:21:13 -0700121
Marissa Wall61c58622018-07-18 10:12:20 -0700122 virtual void getDrawingTransformMatrix(float *matrix) = 0;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800123 virtual uint32_t getDrawingTransform() const = 0;
124 virtual ui::Dataspace getDrawingDataSpace() const = 0;
125 virtual Rect getDrawingCrop() const = 0;
Marissa Wallfd668622018-05-10 10:21:13 -0700126 virtual uint32_t getDrawingScalingMode() const = 0;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800127 virtual Region getDrawingSurfaceDamage() const = 0;
128 virtual const HdrMetadata& getDrawingHdrMetadata() const = 0;
129 virtual int getDrawingApi() const = 0;
Marissa Wallfd668622018-05-10 10:21:13 -0700130 virtual PixelFormat getPixelFormat() const = 0;
131
132 virtual uint64_t getFrameNumber() const = 0;
133
134 virtual bool getAutoRefresh() const = 0;
135 virtual bool getSidebandStreamChanged() const = 0;
136
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800137 // Latch sideband stream and returns true if the dirty region should be updated.
138 virtual bool latchSidebandStream(bool& recomputeVisibleRegions) = 0;
Marissa Wallfd668622018-05-10 10:21:13 -0700139
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800140 virtual bool hasFrameUpdate() const = 0;
Marissa Wallfd668622018-05-10 10:21:13 -0700141
Marissa Wall61c58622018-07-18 10:12:20 -0700142 virtual void setFilteringEnabled(bool enabled) = 0;
Marissa Wallfd668622018-05-10 10:21:13 -0700143
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800144 virtual status_t bindTextureImage() = 0;
Alec Mouri86770e52018-09-24 22:40:58 +0000145 virtual status_t updateTexImage(bool& recomputeVisibleRegions, nsecs_t latchTime,
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800146 const sp<Fence>& flushFence) = 0;
Marissa Wallfd668622018-05-10 10:21:13 -0700147
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800148 virtual status_t updateActiveBuffer() = 0;
Marissa Wallfd668622018-05-10 10:21:13 -0700149 virtual status_t updateFrameNumber(nsecs_t latchTime) = 0;
150
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800151 virtual void setHwcLayerBuffer(const sp<const DisplayDevice>& displayDevice) = 0;
Marissa Wallfd668622018-05-10 10:21:13 -0700152
Marissa Wallfd668622018-05-10 10:21:13 -0700153protected:
154 // Loads the corresponding system property once per process
155 static bool latchUnsignaledBuffers();
David Sodmaneb085e02017-10-05 18:49:04 -0700156
David Sodman0c69cad2017-08-21 12:12:51 -0700157 // Check all of the local sync points to ensure that all transactions
158 // which need to have been applied prior to the frame which is about to
159 // be latched have signaled
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800160 bool allTransactionsSignaled();
David Sodman0c69cad2017-08-21 12:12:51 -0700161
Marissa Wallfd668622018-05-10 10:21:13 -0700162 static bool getOpacityForFormat(uint32_t format);
David Sodman0c69cad2017-08-21 12:12:51 -0700163
Marissa Wallfd668622018-05-10 10:21:13 -0700164 // from GLES
165 const uint32_t mTextureName;
166
chaviwf206b662019-01-11 13:07:19 -0800167 bool mRefreshPending{false};
168
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000169 // Returns true if, when drawing the active buffer during gpu compositon, we
170 // should use a cached buffer or not.
171 virtual bool useCachedBufferForClientComposition() const = 0;
172
173 // prepareClientLayer - constructs a RenderEngine layer for GPU composition.
174 bool prepareClientLayer(const RenderArea& renderArea, const Region& clip,
175 bool useIdentityTransform, Region& clearRegion,
176 renderengine::LayerSettings& layer);
177
Marissa Wallfd668622018-05-10 10:21:13 -0700178private:
Peiyong Linc2020ca2019-01-10 11:36:12 -0800179 // Returns true if this layer requires filtering
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800180 bool needsFiltering(const sp<const DisplayDevice>& displayDevice) const;
Marissa Wallfd668622018-05-10 10:21:13 -0700181
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800182 uint64_t getHeadFrameNumber() const;
Marissa Wallfd668622018-05-10 10:21:13 -0700183
Lloyd Pique42ab75e2018-09-12 20:46:03 -0700184 uint32_t mCurrentScalingMode{NATIVE_WINDOW_SCALING_MODE_FREEZE};
Marissa Wallfd668622018-05-10 10:21:13 -0700185
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800186 bool mTransformToDisplayInverse{false};
187
Marissa Wallfd668622018-05-10 10:21:13 -0700188 // main thread.
Lloyd Pique42ab75e2018-09-12 20:46:03 -0700189 bool mBufferLatched{false}; // TODO: Use mActiveBuffer?
Marissa Wallfd668622018-05-10 10:21:13 -0700190
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800191 Rect getBufferSize(const State& s) const override;
Lloyd Piquefeb73d72018-12-04 17:23:44 -0800192
193 std::shared_ptr<compositionengine::Layer> mCompositionLayer;
Vishnu Nair4351ad52019-02-11 14:13:02 -0800194
195 FloatRect computeSourceBounds(const FloatRect& parentBounds) const override;
David Sodman0c69cad2017-08-21 12:12:51 -0700196};
197
198} // namespace android