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 
19 #include <sys/types.h>
20 #include <cstdint>
21 #include <list>
22 
23 #include <gui/ISurfaceComposerClient.h>
24 #include <gui/LayerState.h>
25 #include <renderengine/Image.h>
26 #include <renderengine/Mesh.h>
27 #include <renderengine/Texture.h>
28 #include <system/window.h> // For NATIVE_WINDOW_SCALING_MODE_FREEZE
29 #include <ui/FrameStats.h>
30 #include <ui/GraphicBuffer.h>
31 #include <ui/PixelFormat.h>
32 #include <ui/Region.h>
33 #include <utils/RefBase.h>
34 #include <utils/String8.h>
35 #include <utils/Timers.h>
36 
37 #include "BufferLayerConsumer.h"
38 #include "Client.h"
39 #include "DisplayHardware/HWComposer.h"
40 #include "FrameTracker.h"
41 #include "Layer.h"
42 #include "LayerVector.h"
43 #include "MonitoredProducer.h"
44 #include "SurfaceFlinger.h"
45 
46 namespace android {
47 
48 class BufferLayer : public Layer {
49 public:
50     explicit BufferLayer(const LayerCreationArgs& args);
51     virtual ~BufferLayer() override;
52 
53     // -----------------------------------------------------------------------
54     // Overriden from Layer
55     // -----------------------------------------------------------------------
56 public:
57     std::shared_ptr<compositionengine::Layer> getCompositionLayer() const override;
58 
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;
64 
65     // getTypeId - Provide unique string for each class type in the Layer
66     // hierarchy
getTypeId()67     const char* getTypeId() const override { return "BufferLayer"; }
68 
69     bool isOpaque(const Layer::State& s) const override;
70 
71     // isVisible - true if this layer is visible, false otherwise
72     bool isVisible() const override;
73 
74     // isProtected - true if the layer may contain protected content in the
75     // GRALLOC_USAGE_PROTECTED sense.
76     bool isProtected() const override;
77 
78     // isFixedSize - true if content has a fixed size
79     bool isFixedSize() const override;
80 
81     bool usesSourceCrop() const override;
82 
83     bool isHdrY410() const override;
84 
85     void setPerFrameData(const sp<const DisplayDevice>& display, const ui::Transform& transform,
86                          const Rect& viewport, int32_t supportedPerFrameMetadata,
87                          const ui::Dataspace targetDataspace) override;
88 
89     bool onPreComposition(nsecs_t refreshStartTime) override;
90     bool onPostComposition(const std::optional<DisplayId>& displayId,
91                            const std::shared_ptr<FenceTime>& glDoneFence,
92                            const std::shared_ptr<FenceTime>& presentFence,
93                            const CompositorTiming& compositorTiming) override;
94 
95     // latchBuffer - called each time the screen is redrawn and returns whether
96     // the visible regions need to be recomputed (this is a fairly heavy
97     // operation, so this should be set only if needed). Typically this is used
98     // to figure out if the content or size of a surface has changed.
99     bool latchBuffer(bool& recomputeVisibleRegions, nsecs_t latchTime) override;
100 
isBufferLatched()101     bool isBufferLatched() const override { return mRefreshPending; }
102 
103     void notifyAvailableFrames() override;
104 
105     bool hasReadyFrame() const override;
106 
107     // Returns the current scaling mode, unless mOverrideScalingMode
108     // is set, in which case, it returns mOverrideScalingMode
109     uint32_t getEffectiveScalingMode() const override;
110     // -----------------------------------------------------------------------
111 
112     // -----------------------------------------------------------------------
113     // Functions that must be implemented by derived classes
114     // -----------------------------------------------------------------------
115 private:
116     virtual bool fenceHasSignaled() const = 0;
117     virtual bool framePresentTimeIsCurrent() const = 0;
118 
119     virtual nsecs_t getDesiredPresentTime() = 0;
120     virtual std::shared_ptr<FenceTime> getCurrentFenceTime() const = 0;
121 
122     virtual void getDrawingTransformMatrix(float *matrix) = 0;
123     virtual uint32_t getDrawingTransform() const = 0;
124     virtual ui::Dataspace getDrawingDataSpace() const = 0;
125     virtual Rect getDrawingCrop() const = 0;
126     virtual uint32_t getDrawingScalingMode() const = 0;
127     virtual Region getDrawingSurfaceDamage() const = 0;
128     virtual const HdrMetadata& getDrawingHdrMetadata() const = 0;
129     virtual int getDrawingApi() const = 0;
130 
131     virtual uint64_t getFrameNumber() const = 0;
132 
133     virtual bool getAutoRefresh() const = 0;
134     virtual bool getSidebandStreamChanged() const = 0;
135 
136     // Latch sideband stream and returns true if the dirty region should be updated.
137     virtual bool latchSidebandStream(bool& recomputeVisibleRegions) = 0;
138 
139     virtual bool hasFrameUpdate() const = 0;
140 
141     virtual void setFilteringEnabled(bool enabled) = 0;
142 
143     virtual status_t bindTextureImage() = 0;
144     virtual status_t updateTexImage(bool& recomputeVisibleRegions, nsecs_t latchTime) = 0;
145 
146     virtual status_t updateActiveBuffer() = 0;
147     virtual status_t updateFrameNumber(nsecs_t latchTime) = 0;
148 
149     virtual void setHwcLayerBuffer(const sp<const DisplayDevice>& displayDevice) = 0;
150 
151 protected:
152     // Loads the corresponding system property once per process
153     static bool latchUnsignaledBuffers();
154 
155     // Check all of the local sync points to ensure that all transactions
156     // which need to have been applied prior to the frame which is about to
157     // be latched have signaled
158     bool allTransactionsSignaled();
159 
160     static bool getOpacityForFormat(uint32_t format);
161 
162     // from GLES
163     const uint32_t mTextureName;
164 
165     bool mRefreshPending{false};
166 
167     // prepareClientLayer - constructs a RenderEngine layer for GPU composition.
168     bool prepareClientLayer(const RenderArea& renderArea, const Region& clip,
169                             bool useIdentityTransform, Region& clearRegion,
170                             const bool supportProtectedContent,
171                             renderengine::LayerSettings& layer) override;
172 
173 private:
174     // Returns true if this layer requires filtering
175     bool needsFiltering(const sp<const DisplayDevice>& displayDevice) const;
176 
177     uint64_t getHeadFrameNumber() const;
178 
179     uint32_t mCurrentScalingMode{NATIVE_WINDOW_SCALING_MODE_FREEZE};
180 
181     bool mTransformToDisplayInverse{false};
182 
183     // main thread.
184     bool mBufferLatched{false}; // TODO: Use mActiveBuffer?
185 
186     // BufferStateLayers can return Rect::INVALID_RECT if the layer does not have a display frame
187     // and its parent layer is not bounded
188     Rect getBufferSize(const State& s) const override;
189 
190     std::shared_ptr<compositionengine::Layer> mCompositionLayer;
191 
192     FloatRect computeSourceBounds(const FloatRect& parentBounds) const override;
193 
194     PixelFormat getPixelFormat() const;
195 };
196 
197 } // namespace android
198