• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "FrameTimeline.h"
41 #include "FrameTracker.h"
42 #include "Layer.h"
43 #include "LayerVector.h"
44 #include "MonitoredProducer.h"
45 #include "SurfaceFlinger.h"
46 
47 namespace android {
48 
49 class BufferLayer : public Layer {
50 public:
51     explicit BufferLayer(const LayerCreationArgs& args);
52     virtual ~BufferLayer() override;
53 
54     // Implements Layer.
55     sp<compositionengine::LayerFE> getCompositionEngineLayerFE() const override;
56     compositionengine::LayerFECompositionState* editCompositionState() override;
57 
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;
63 
64     bool isOpaque(const Layer::State& s) const override;
65 
66     // isVisible - true if this layer is visible, false otherwise
67     bool isVisible() const override;
68 
69     // isProtected - true if the layer may contain protected content in the
70     // GRALLOC_USAGE_PROTECTED sense.
71     bool isProtected() const override;
72 
73     // isFixedSize - true if content has a fixed size
74     bool isFixedSize() const override;
75 
76     bool usesSourceCrop() const override;
77 
78     bool isHdrY410() const override;
79 
80     bool onPostComposition(const DisplayDevice*, const std::shared_ptr<FenceTime>& glDoneFence,
81                            const std::shared_ptr<FenceTime>& presentFence,
82                            const CompositorTiming&) override;
83 
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.
88     bool latchBuffer(bool& recomputeVisibleRegions, nsecs_t latchTime,
89                      nsecs_t expectedPresentTime) override;
90 
isBufferLatched()91     bool isBufferLatched() const override { return mRefreshPending; }
92 
93     bool hasReadyFrame() const override;
94 
95     // Returns the current scaling mode
96     uint32_t getEffectiveScalingMode() const override;
97 
98     // Calls latchBuffer if the buffer has a frame queued and then releases the buffer.
99     // This is used if the buffer is just latched and releases to free up the buffer
100     // and will not be shown on screen.
101     // Should only be called on the main thread.
102     void latchAndReleaseBuffer() override;
103 
104     bool getTransformToDisplayInverse() const override;
105 
106     Rect getBufferCrop() const override;
107 
108     uint32_t getBufferTransform() const override;
109 
110     ui::Dataspace getDataSpace() const override;
111 
112     sp<GraphicBuffer> getBuffer() const override;
113 
getTransformHint()114     ui::Transform::RotationFlags getTransformHint() const override { return mTransformHint; }
115 
116     // Returns true if the transformed buffer size does not match the layer size and we need
117     // to apply filtering.
118     virtual bool bufferNeedsFiltering() const;
119 
120 protected:
121     struct BufferInfo {
122         nsecs_t mDesiredPresentTime;
123         std::shared_ptr<FenceTime> mFenceTime;
124         sp<Fence> mFence;
125         uint32_t mTransform{0};
126         ui::Dataspace mDataspace{ui::Dataspace::UNKNOWN};
127         Rect mCrop;
128         uint32_t mScaleMode{NATIVE_WINDOW_SCALING_MODE_FREEZE};
129         Region mSurfaceDamage;
130         HdrMetadata mHdrMetadata;
131         int mApi;
132         PixelFormat mPixelFormat{PIXEL_FORMAT_NONE};
133         bool mTransformToDisplayInverse{false};
134 
135         std::shared_ptr<renderengine::ExternalTexture> mBuffer;
136         uint64_t mFrameNumber;
137         int mBufferSlot{BufferQueue::INVALID_BUFFER_SLOT};
138 
139         bool mFrameLatencyNeeded{false};
140     };
141 
142     BufferInfo mBufferInfo;
143     virtual void gatherBufferInfo() = 0;
144 
145     std::optional<compositionengine::LayerFE::LayerSettings> prepareClientComposition(
146             compositionengine::LayerFE::ClientCompositionTargetSettings&) override;
147 
148     /*
149      * compositionengine::LayerFE overrides
150      */
151     const compositionengine::LayerFECompositionState* getCompositionState() const override;
152     bool onPreComposition(nsecs_t) override;
153     void preparePerFrameCompositionState() override;
154 
155     static bool getOpacityForFormat(uint32_t format);
156 
157     // from graphics API
158     const uint32_t mTextureName;
159 
160     bool mRefreshPending{false};
161 
162     ui::Dataspace translateDataspace(ui::Dataspace dataspace);
163     void setInitialValuesForClone(const sp<Layer>& clonedFrom);
164     void updateCloneBufferInfo() override;
165     uint64_t mPreviousFrameNumber = 0;
166 
167     uint64_t getHeadFrameNumber(nsecs_t expectedPresentTime) const override;
168 
169     void setTransformHint(ui::Transform::RotationFlags displayTransformHint) override;
170 
171     // Transform hint provided to the producer. This must be accessed holding
172     // the mStateLock.
173     ui::Transform::RotationFlags mTransformHint = ui::Transform::ROT_0;
174 
getAutoRefresh()175     bool getAutoRefresh() const { return mAutoRefresh; }
getSidebandStreamChanged()176     bool getSidebandStreamChanged() const { return mSidebandStreamChanged; }
177 
178     // Returns true if the next buffer should be presented at the expected present time
179     bool shouldPresentNow(nsecs_t expectedPresentTime) const;
180 
181     // Returns true if the next buffer should be presented at the expected present time,
182     // overridden by BufferStateLayer and BufferQueueLayer for implementation
183     // specific logic
184     virtual bool isBufferDue(nsecs_t /*expectedPresentTime*/) const = 0;
185 
186     std::atomic<bool> mAutoRefresh{false};
187     std::atomic<bool> mSidebandStreamChanged{false};
188 
189 private:
190     virtual bool fenceHasSignaled() const = 0;
191     virtual bool framePresentTimeIsCurrent(nsecs_t expectedPresentTime) const = 0;
192     virtual uint64_t getFrameNumber(nsecs_t expectedPresentTime) const = 0;
193 
194 
195     // Latch sideband stream and returns true if the dirty region should be updated.
196     virtual bool latchSidebandStream(bool& recomputeVisibleRegions) = 0;
197 
198     virtual bool hasFrameUpdate() const = 0;
199 
200     virtual status_t updateTexImage(bool& recomputeVisibleRegions, nsecs_t latchTime,
201                                     nsecs_t expectedPresentTime) = 0;
202 
203     virtual status_t updateActiveBuffer() = 0;
204     virtual status_t updateFrameNumber(nsecs_t latchTime) = 0;
205 
206     // We generate InputWindowHandles for all non-cursor buffered layers regardless of whether they
207     // have an InputChannel. This is to enable the InputDispatcher to do PID based occlusion
208     // detection.
needsInputInfo()209     bool needsInputInfo() const override { return !mPotentialCursor; }
210 
211     // Returns true if this layer requires filtering
212     bool needsFiltering(const DisplayDevice*) const override;
213     bool needsFilteringForScreenshots(const DisplayDevice*,
214                                       const ui::Transform& inverseParentTransform) const override;
215 
216     // BufferStateLayers can return Rect::INVALID_RECT if the layer does not have a display frame
217     // and its parent layer is not bounded
218     Rect getBufferSize(const State& s) const override;
219 
220     PixelFormat getPixelFormat() const;
221 
222     // Computes the transform matrix using the setFilteringEnabled to determine whether the
223     // transform matrix should be computed for use with bilinear filtering.
224     void getDrawingTransformMatrix(bool filteringEnabled, float outMatrix[16]);
225 
226     std::unique_ptr<compositionengine::LayerFECompositionState> mCompositionState;
227 
228     FloatRect computeSourceBounds(const FloatRect& parentBounds) const override;
229 };
230 
231 } // namespace android
232