blob: cc4e6c2bc2f2d050b4bf4c0bd35aee1241caf544 [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 Mouri0f714832018-11-12 15:31:06 -080025#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"
40#include "DisplayHardware/HWComposerBufferCache.h"
41#include "FrameTracker.h"
42#include "Layer.h"
43#include "LayerVector.h"
44#include "MonitoredProducer.h"
45#include "SurfaceFlinger.h"
David Sodman0c69cad2017-08-21 12:12:51 -070046
47namespace android {
48
Marissa Wallfd668622018-05-10 10:21:13 -070049class BufferLayer : public Layer {
David Sodman0c69cad2017-08-21 12:12:51 -070050public:
Lloyd Pique42ab75e2018-09-12 20:46:03 -070051 explicit BufferLayer(const LayerCreationArgs& args);
David Sodman0c69cad2017-08-21 12:12:51 -070052 ~BufferLayer() override;
53
54 // -----------------------------------------------------------------------
55 // Overriden from Layer
56 // -----------------------------------------------------------------------
Marissa Wallfd668622018-05-10 10:21:13 -070057public:
Lloyd Piquefeb73d72018-12-04 17:23:44 -080058 std::shared_ptr<compositionengine::Layer> getCompositionLayer() const override;
59
Marissa Wallfd668622018-05-10 10:21:13 -070060 // If we have received a new buffer this frame, we will pass its surface
61 // damage down to hardware composer. Otherwise, we must send a region with
62 // one empty rect.
63 void useSurfaceDamage() override;
64 void useEmptyDamage() override;
David Sodman0c69cad2017-08-21 12:12:51 -070065
Marissa Wallfd668622018-05-10 10:21:13 -070066 // getTypeId - Provide unique string for each class type in the Layer
67 // hierarchy
David Sodman0c69cad2017-08-21 12:12:51 -070068 const char* getTypeId() const override { return "BufferLayer"; }
69
Marissa Wallfd668622018-05-10 10:21:13 -070070 bool isOpaque(const Layer::State& s) const override;
David Sodman0c69cad2017-08-21 12:12:51 -070071
Marissa Wallfd668622018-05-10 10:21:13 -070072 // isVisible - true if this layer is visible, false otherwise
Lloyd Pique0449b0f2018-12-20 16:23:45 -080073 bool isVisible() const override;
David Sodman0c69cad2017-08-21 12:12:51 -070074
Peiyong Linfb530cf2018-12-15 05:07:38 +000075 // isProtected - true if the layer may contain protected content in the
76 // GRALLOC_USAGE_PROTECTED sense.
77 bool isProtected() const override;
78
Marissa Wallfd668622018-05-10 10:21:13 -070079 // isFixedSize - true if content has a fixed size
David Sodman0c69cad2017-08-21 12:12:51 -070080 bool isFixedSize() const override;
81
Marissa Wallfd668622018-05-10 10:21:13 -070082 bool isHdrY410() const override;
David Sodmaneb085e02017-10-05 18:49:04 -070083
Dominik Laskowski075d3172018-05-24 15:50:06 -070084 void setPerFrameData(DisplayId displayId, const ui::Transform& transform, const Rect& viewport,
85 int32_t supportedPerFrameMetadata) override;
Marissa Wallfd668622018-05-10 10:21:13 -070086
87 bool onPreComposition(nsecs_t refreshStartTime) override;
Dominik Laskowski075d3172018-05-24 15:50:06 -070088 bool onPostComposition(const std::optional<DisplayId>& displayId,
89 const std::shared_ptr<FenceTime>& glDoneFence,
David Sodmaneb085e02017-10-05 18:49:04 -070090 const std::shared_ptr<FenceTime>& presentFence,
Lloyd Pique0449b0f2018-12-20 16:23:45 -080091 const CompositorTiming& compositorTiming) override;
David Sodmaneb085e02017-10-05 18:49:04 -070092
Marissa Wallfd668622018-05-10 10:21:13 -070093 // latchBuffer - called each time the screen is redrawn and returns whether
94 // the visible regions need to be recomputed (this is a fairly heavy
95 // operation, so this should be set only if needed). Typically this is used
96 // to figure out if the content or size of a surface has changed.
Alec Mouri86770e52018-09-24 22:40:58 +000097 // If there was a GL composition step rendering the previous frame, then
98 // releaseFence will be populated with a native fence that fires when
99 // composition has completed.
100 Region latchBuffer(bool& recomputeVisibleRegions, nsecs_t latchTime,
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800101 const sp<Fence>& releaseFence) override;
Marissa Wallfd668622018-05-10 10:21:13 -0700102
David Sodman0c69cad2017-08-21 12:12:51 -0700103 bool isBufferLatched() const override { return mRefreshPending; }
104
Marissa Wallfd668622018-05-10 10:21:13 -0700105 void notifyAvailableFrames() override;
Chia-I Wu692e0832018-06-05 15:46:58 -0700106
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800107 bool hasReadyFrame() const override;
David Sodman0c69cad2017-08-21 12:12:51 -0700108
David Sodman0c69cad2017-08-21 12:12:51 -0700109 // Returns the current scaling mode, unless mOverrideScalingMode
110 // is set, in which case, it returns mOverrideScalingMode
111 uint32_t getEffectiveScalingMode() const override;
Marissa Wallfd668622018-05-10 10:21:13 -0700112 // -----------------------------------------------------------------------
113
114 // -----------------------------------------------------------------------
115 // Functions that must be implemented by derived classes
116 // -----------------------------------------------------------------------
117private:
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800118 virtual bool fenceHasSignaled() const = 0;
Marissa Wallfd668622018-05-10 10:21:13 -0700119
120 virtual nsecs_t getDesiredPresentTime() = 0;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800121 virtual std::shared_ptr<FenceTime> getCurrentFenceTime() const = 0;
Marissa Wallfd668622018-05-10 10:21:13 -0700122
Marissa Wall61c58622018-07-18 10:12:20 -0700123 virtual void getDrawingTransformMatrix(float *matrix) = 0;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800124 virtual uint32_t getDrawingTransform() const = 0;
125 virtual ui::Dataspace getDrawingDataSpace() const = 0;
126 virtual Rect getDrawingCrop() const = 0;
Marissa Wallfd668622018-05-10 10:21:13 -0700127 virtual uint32_t getDrawingScalingMode() const = 0;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800128 virtual Region getDrawingSurfaceDamage() const = 0;
129 virtual const HdrMetadata& getDrawingHdrMetadata() const = 0;
130 virtual int getDrawingApi() const = 0;
Marissa Wallfd668622018-05-10 10:21:13 -0700131 virtual PixelFormat getPixelFormat() const = 0;
132
133 virtual uint64_t getFrameNumber() const = 0;
134
135 virtual bool getAutoRefresh() const = 0;
136 virtual bool getSidebandStreamChanged() const = 0;
137
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800138 virtual std::optional<Region> 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 Pique0449b0f2018-12-20 16:23:45 -0800151 virtual void setHwcLayerBuffer(DisplayId displayId) = 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 Mouri0f714832018-11-12 15:31:06 -0800169 // 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
180 bool needsFiltering() 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
186 // main thread.
Lloyd Pique42ab75e2018-09-12 20:46:03 -0700187 bool mBufferLatched{false}; // TODO: Use mActiveBuffer?
Marissa Wallfd668622018-05-10 10:21:13 -0700188
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800189 Rect getBufferSize(const State& s) const override;
Lloyd Piquefeb73d72018-12-04 17:23:44 -0800190
191 std::shared_ptr<compositionengine::Layer> mCompositionLayer;
David Sodman0c69cad2017-08-21 12:12:51 -0700192};
193
194} // namespace android