blob: 6ef2b1932ebc4eedc6e22427000c1e840460c7d7 [file] [log] [blame]
Marissa Wall61c58622018-07-18 10:12:20 -07001/*
2 * Copyright (C) 2018 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
Vishnu Naira72f6c02022-07-01 05:33:08 +000019#include <sys/types.h>
20#include <cstdint>
21#include <list>
Marissa Wall947d34e2019-03-29 14:03:53 -070022#include <stack>
23
Vishnu Naira72f6c02022-07-01 05:33:08 +000024#include <android/gui/ISurfaceComposerClient.h>
25#include <gui/LayerState.h>
26#include <renderengine/Image.h>
27#include <renderengine/Mesh.h>
28#include <renderengine/RenderEngine.h>
29#include <renderengine/Texture.h>
30#include <system/window.h> // For NATIVE_WINDOW_SCALING_MODE_FREEZE
31#include <ui/FrameStats.h>
32#include <ui/GraphicBuffer.h>
33#include <ui/PixelFormat.h>
34#include <ui/Region.h>
35#include <utils/RefBase.h>
36#include <utils/String8.h>
37#include <utils/Timers.h>
38
39#include "Client.h"
40#include "DisplayHardware/HWComposer.h"
41#include "FrameTimeline.h"
42#include "FrameTracker.h"
Vishnu Nair397a0e32022-07-26 00:01:48 +000043#include "HwcSlotGenerator.h"
Vishnu Naira72f6c02022-07-01 05:33:08 +000044#include "Layer.h"
45#include "LayerVector.h"
46#include "SurfaceFlinger.h"
47
Marissa Wall61c58622018-07-18 10:12:20 -070048namespace android {
49
Valerie Hau64499682019-04-10 11:04:29 -070050class SlotGenerationTest;
51
Vishnu Naira72f6c02022-07-01 05:33:08 +000052class BufferStateLayer : public Layer {
Marissa Wall61c58622018-07-18 10:12:20 -070053public:
Lloyd Pique42ab75e2018-09-12 20:46:03 -070054 explicit BufferStateLayer(const LayerCreationArgs&);
Marissa Wall61c58622018-07-18 10:12:20 -070055
Alec Mouri4545a8a2019-08-08 20:05:32 -070056 ~BufferStateLayer() override;
57
Peiyong Linf243e5e2020-08-22 17:40:59 -070058 // Implements Layer.
Vishnu Naira72f6c02022-07-01 05:33:08 +000059 sp<compositionengine::LayerFE> getCompositionEngineLayerFE() const override;
60 compositionengine::LayerFECompositionState* editCompositionState() override;
61
62 // If we have received a new buffer this frame, we will pass its surface
63 // damage down to hardware composer. Otherwise, we must send a region with
64 // one empty rect.
65 void useSurfaceDamage() override;
66 void useEmptyDamage() override;
67
68 bool isOpaque(const Layer::State& s) const override;
69 bool canReceiveInput() 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
Vishnu Nairbc4ee5c2022-08-16 03:19:37 +000078 bool usesSourceCrop() const override { return hasBufferOrSidebandStream(); }
Vishnu Naira72f6c02022-07-01 05:33:08 +000079
80 bool isHdrY410() const override;
81
82 void onPostComposition(const DisplayDevice*, const std::shared_ptr<FenceTime>& glDoneFence,
83 const std::shared_ptr<FenceTime>& presentFence,
84 const CompositorTiming&) override;
85
86 // latchBuffer - called each time the screen is redrawn and returns whether
87 // the visible regions need to be recomputed (this is a fairly heavy
88 // operation, so this should be set only if needed). Typically this is used
89 // to figure out if the content or size of a surface has changed.
Vishnu Nair397a0e32022-07-26 00:01:48 +000090 bool latchBuffer(bool& recomputeVisibleRegions, nsecs_t latchTime) override;
Vishnu Naira72f6c02022-07-01 05:33:08 +000091 bool hasReadyFrame() const override;
92
Vishnu Naira72f6c02022-07-01 05:33:08 +000093 // Calls latchBuffer if the buffer has a frame queued and then releases the buffer.
94 // This is used if the buffer is just latched and releases to free up the buffer
95 // and will not be shown on screen.
96 // Should only be called on the main thread.
97 void latchAndReleaseBuffer() override;
98
99 bool getTransformToDisplayInverse() const override;
100
101 Rect getBufferCrop() const override;
102
103 uint32_t getBufferTransform() const override;
104
105 ui::Dataspace getDataSpace() const override;
Vishnu Nairbc4ee5c2022-08-16 03:19:37 +0000106 ui::Dataspace getRequestedDataSpace() const;
Vishnu Naira72f6c02022-07-01 05:33:08 +0000107 sp<GraphicBuffer> getBuffer() const override;
108 const std::shared_ptr<renderengine::ExternalTexture>& getExternalTexture() const override;
109
110 ui::Transform::RotationFlags getTransformHint() const override { return mTransformHint; }
111
112 // Implements Layer.
chaviw8a01fa42019-08-19 12:39:31 -0700113 const char* getType() const override { return "BufferStateLayer"; }
114
Dominik Laskowskib17c6212022-05-09 09:36:19 -0700115 void onLayerDisplayed(ftl::SharedFuture<FenceResult>) override;
Sally Qi59a9f502021-10-12 18:53:23 +0000116
Marissa Wall61c58622018-07-18 10:12:20 -0700117 void releasePendingBuffer(nsecs_t dequeueReadyTime) override;
118
Vishnu Nairbc4ee5c2022-08-16 03:19:37 +0000119 Region getActiveTransparentRegion(const Layer::State& s) const override {
120 return s.transparentRegionHint;
121 }
Marissa Wall61c58622018-07-18 10:12:20 -0700122
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800123 bool setTransform(uint32_t transform) override;
124 bool setTransformToDisplayInverse(bool transformToDisplayInverse) override;
Vishnu Nairdbbe3852022-01-12 20:22:11 -0800125 bool setBuffer(std::shared_ptr<renderengine::ExternalTexture>& /* buffer */,
126 const BufferData& bufferData, nsecs_t postTime, nsecs_t desiredPresentTime,
chaviwba4320c2021-09-15 15:20:53 -0500127 bool isAutoTimestamp, std::optional<nsecs_t> dequeueTime,
128 const FrameTimelineInfo& info) override;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800129 bool setDataspace(ui::Dataspace dataspace) override;
130 bool setHdrMetadata(const HdrMetadata& hdrMetadata) override;
131 bool setSurfaceDamageRegion(const Region& surfaceDamage) override;
132 bool setApi(int32_t api) override;
133 bool setSidebandStream(const sp<NativeHandle>& sidebandStream) override;
Jiakai Zhanga5505cb2021-11-09 11:46:30 +0000134 bool setTransactionCompletedListeners(const std::vector<sp<CallbackHandle>>& handles) override;
Chavi Weingartena5aedbd2021-04-09 13:37:33 +0000135 bool setPosition(float /*x*/, float /*y*/) override;
Robert Carrde6d7b42022-01-07 18:23:06 -0800136 bool setMatrix(const layer_state_t::matrix22_t& /*matrix*/);
Marissa Wall61c58622018-07-18 10:12:20 -0700137
Vishnu Naira72f6c02022-07-01 05:33:08 +0000138 // BufferStateLayers can return Rect::INVALID_RECT if the layer does not have a display frame
139 // and its parent layer is not bounded
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800140 Rect getBufferSize(const State& s) const override;
Vishnu Nair4351ad52019-02-11 14:13:02 -0800141 FloatRect computeSourceBounds(const FloatRect& parentBounds) const override;
Vishnu Naircf26a0a2020-11-13 12:56:20 -0800142 void setAutoRefresh(bool autoRefresh) override;
Valerie Haubc6ddb12019-03-08 11:10:15 -0800143
chaviwf3f40fe2021-04-27 15:54:02 -0500144 bool setBufferCrop(const Rect& bufferCrop) override;
Vishnu Nair6bdec7d2021-05-10 15:01:13 -0700145 bool setDestinationFrame(const Rect& destinationFrame) override;
Robert Carr6a160312021-05-17 12:08:20 -0700146 bool updateGeometry() override;
chaviwf3f40fe2021-04-27 15:54:02 -0500147
Vishnu Naira72f6c02022-07-01 05:33:08 +0000148 bool fenceHasSignaled() const;
Vishnu Naira72f6c02022-07-01 05:33:08 +0000149 bool onPreComposition(nsecs_t) override;
Marissa Wall61c58622018-07-18 10:12:20 -0700150
Robert Carr7121caf2020-12-15 13:07:32 -0800151 // See mPendingBufferTransactions
Robert Carr7121caf2020-12-15 13:07:32 -0800152 void decrementPendingBufferCount();
Vishnu Nair8eda69e2021-02-26 10:42:10 -0800153 std::atomic<int32_t>* getPendingBufferCounter() override { return &mPendingBufferTransactions; }
154 std::string getPendingBufferCounterName() override { return mBlastTransactionName; }
Robert Carr7121caf2020-12-15 13:07:32 -0800155
Patrick Williams16d8b2c2022-08-08 17:29:05 +0000156 std::optional<compositionengine::LayerFE::LayerSettings> prepareClientComposition(
157 compositionengine::LayerFE::ClientCompositionTargetSettings&) const override;
Vishnu Nairbc4ee5c2022-08-16 03:19:37 +0000158 bool setColor(const half3& color) override;
Patrick Williams16d8b2c2022-08-08 17:29:05 +0000159
chaviw4244e032019-09-04 11:27:49 -0700160protected:
Vishnu Naira72f6c02022-07-01 05:33:08 +0000161 void gatherBufferInfo();
Jorim Jaggi9c03b502020-11-24 23:51:31 +0100162 void onSurfaceFrameCreated(const std::shared_ptr<frametimeline::SurfaceFrame>& surfaceFrame);
chaviw39d01472021-04-08 14:26:24 -0500163 ui::Transform getInputTransform() const override;
164 Rect getInputBounds() const override;
Marissa Wall61c58622018-07-18 10:12:20 -0700165
Vishnu Naira72f6c02022-07-01 05:33:08 +0000166 struct BufferInfo {
167 nsecs_t mDesiredPresentTime;
168 std::shared_ptr<FenceTime> mFenceTime;
169 sp<Fence> mFence;
170 uint32_t mTransform{0};
171 ui::Dataspace mDataspace{ui::Dataspace::UNKNOWN};
172 Rect mCrop;
173 uint32_t mScaleMode{NATIVE_WINDOW_SCALING_MODE_FREEZE};
174 Region mSurfaceDamage;
175 HdrMetadata mHdrMetadata;
176 int mApi;
177 PixelFormat mPixelFormat{PIXEL_FORMAT_NONE};
178 bool mTransformToDisplayInverse{false};
179
180 std::shared_ptr<renderengine::ExternalTexture> mBuffer;
181 uint64_t mFrameNumber;
182 int mBufferSlot{BufferQueue::INVALID_BUFFER_SLOT};
183
184 bool mFrameLatencyNeeded{false};
185 };
186
187 BufferInfo mBufferInfo;
188
Vishnu Naira72f6c02022-07-01 05:33:08 +0000189 /*
190 * compositionengine::LayerFE overrides
191 */
192 const compositionengine::LayerFECompositionState* getCompositionState() const override;
193 void preparePerFrameCompositionState() override;
Vishnu Nairbc4ee5c2022-08-16 03:19:37 +0000194 void preparePerFrameBufferCompositionState();
195 void preparePerFrameEffectsCompositionState();
Vishnu Naira72f6c02022-07-01 05:33:08 +0000196
Vishnu Nairbc4ee5c2022-08-16 03:19:37 +0000197 static bool isOpaqueFormat(PixelFormat format);
Vishnu Naira72f6c02022-07-01 05:33:08 +0000198
199 // from graphics API
200 const uint32_t mTextureName;
201 ui::Dataspace translateDataspace(ui::Dataspace dataspace);
202 void setInitialValuesForClone(const sp<Layer>& clonedFrom);
203 void updateCloneBufferInfo() override;
204 uint64_t mPreviousFrameNumber = 0;
205
206 void setTransformHint(ui::Transform::RotationFlags displayTransformHint) override;
207
208 // Transform hint provided to the producer. This must be accessed holding
209 // the mStateLock.
210 ui::Transform::RotationFlags mTransformHint = ui::Transform::ROT_0;
211
212 bool getAutoRefresh() const { return mDrawingState.autoRefresh; }
213 bool getSidebandStreamChanged() const { return mSidebandStreamChanged; }
214
215 std::atomic<bool> mSidebandStreamChanged{false};
216
chaviw4244e032019-09-04 11:27:49 -0700217private:
Peiyong Linf243e5e2020-08-22 17:40:59 -0700218 friend class SlotGenerationTest;
Adithya Srinivasanb238cd52021-02-04 17:54:05 +0000219 friend class TransactionFrameTracerTest;
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +0000220 friend class TransactionSurfaceFrameTest;
221
Vishnu Naira72f6c02022-07-01 05:33:08 +0000222 // We generate InputWindowHandles for all non-cursor buffered layers regardless of whether they
223 // have an InputChannel. This is to enable the InputDispatcher to do PID based occlusion
224 // detection.
Vishnu Nairbc4ee5c2022-08-16 03:19:37 +0000225 bool needsInputInfo() const override {
226 return (hasInputInfo() || hasBufferOrSidebandStream()) && !mPotentialCursor;
227 }
Vishnu Naira72f6c02022-07-01 05:33:08 +0000228
229 // Returns true if this layer requires filtering
230 bool needsFiltering(const DisplayDevice*) const override;
231 bool needsFilteringForScreenshots(const DisplayDevice*,
232 const ui::Transform& inverseParentTransform) const override;
233
234 PixelFormat getPixelFormat() const;
235
236 // Computes the transform matrix using the setFilteringEnabled to determine whether the
237 // transform matrix should be computed for use with bilinear filtering.
Patrick Williams16d8b2c2022-08-08 17:29:05 +0000238 void getDrawingTransformMatrix(bool filteringEnabled, float outMatrix[16]) const;
Vishnu Naira72f6c02022-07-01 05:33:08 +0000239
240 std::unique_ptr<compositionengine::LayerFECompositionState> mCompositionState;
241
Vishnu Nair8eda69e2021-02-26 10:42:10 -0800242 inline void tracePendingBufferCount(int32_t pendingBuffers);
Peiyong Linf243e5e2020-08-22 17:40:59 -0700243
Valerie Haubf784642020-01-29 07:25:23 -0800244 bool updateFrameEventHistory(const sp<Fence>& acquireFence, nsecs_t postedTime,
245 nsecs_t requestedPresentTime);
246
Vishnu Naira72f6c02022-07-01 05:33:08 +0000247 // Latch sideband stream and returns true if the dirty region should be updated.
248 bool latchSidebandStream(bool& recomputeVisibleRegions);
Marissa Wall61c58622018-07-18 10:12:20 -0700249
Vishnu Naira72f6c02022-07-01 05:33:08 +0000250 bool hasFrameUpdate() const;
Marissa Wall61c58622018-07-18 10:12:20 -0700251
Vishnu Nair397a0e32022-07-26 00:01:48 +0000252 void updateTexImage(nsecs_t latchTime);
Marissa Wall61c58622018-07-18 10:12:20 -0700253
chaviwb4c6e582019-08-16 14:35:07 -0700254 sp<Layer> createClone() override;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700255
chaviw4244e032019-09-04 11:27:49 -0700256 // Crop that applies to the buffer
Vishnu Nair5cc9ac02021-04-19 13:23:38 -0700257 Rect computeBufferCrop(const State& s);
chaviw4244e032019-09-04 11:27:49 -0700258
Marissa Walle2ffb422018-10-12 11:33:52 -0700259 bool willPresentCurrentTransaction() const;
Marissa Wall61c58622018-07-18 10:12:20 -0700260
Vishnu Naira72f6c02022-07-01 05:33:08 +0000261 // Returns true if the transformed buffer size does not match the layer size and we need
262 // to apply filtering.
263 bool bufferNeedsFiltering() const;
Vishnu Naire7f79c52020-10-29 14:45:03 -0700264
Ady Abraham9dada822022-02-03 10:26:59 -0800265 bool simpleBufferUpdate(const layer_state_t& s) const override;
266
Chavi Weingartend00e0f72022-07-14 15:59:20 +0000267 void callReleaseBufferCallback(const sp<ITransactionCompletedListener>& listener,
268 const sp<GraphicBuffer>& buffer, uint64_t framenumber,
269 const sp<Fence>& releaseFence,
270 uint32_t currentMaxAcquiredBufferCount);
271
Patrick Williams16d8b2c2022-08-08 17:29:05 +0000272 std::optional<compositionengine::LayerFE::LayerSettings> prepareClientCompositionInternal(
273 compositionengine::LayerFE::ClientCompositionTargetSettings&) const;
Vishnu Nairbc4ee5c2022-08-16 03:19:37 +0000274 // Returns true if there is a valid color to fill.
275 bool fillsColor() const;
276 // Returns true if this layer has a blur value.
277 bool hasBlur() const;
278 bool hasEffect() const { return fillsColor() || drawShadows() || hasBlur(); }
279 bool hasBufferOrSidebandStream() const {
280 return ((mSidebandStream != nullptr) || (mBufferInfo.mBuffer != nullptr));
281 }
282 bool hasSomethingToDraw() const { return hasEffect() || hasBufferOrSidebandStream(); }
283 void prepareEffectsClientComposition(
284 compositionengine::LayerFE::LayerSettings&,
285 compositionengine::LayerFE::ClientCompositionTargetSettings&) const;
Patrick Williams16d8b2c2022-08-08 17:29:05 +0000286
Vishnu Nair4ba0c2e2021-06-24 11:27:17 -0700287 ReleaseCallbackId mPreviousReleaseCallbackId = ReleaseCallbackId::INVALID_ID;
Mikael Pessa2e1608f2019-07-19 11:25:35 -0700288 uint64_t mPreviousReleasedFrameNumber = 0;
Marissa Wallfda30bb2018-10-12 11:34:28 -0700289
Robert Carr79dc06a2022-02-22 15:28:59 -0800290 uint64_t mPreviousBarrierFrameNumber = 0;
291
Marissa Wallfda30bb2018-10-12 11:34:28 -0700292 bool mReleasePreviousBuffer = false;
Vishnu Nair1506b182021-02-22 14:35:15 -0800293
294 // Stores the last set acquire fence signal time used to populate the callback handle's acquire
295 // time.
Ady Abraham461296a2022-01-21 11:11:31 -0800296 std::variant<nsecs_t, sp<Fence>> mCallbackHandleAcquireTimeOrFence = -1;
Marissa Wall024a1912018-08-13 13:55:35 -0700297
Jorim Jaggi9c03b502020-11-24 23:51:31 +0100298 std::deque<std::shared_ptr<android::frametimeline::SurfaceFrame>> mPendingJankClassifications;
Adithya Srinivasand17c7da2021-03-05 20:43:32 +0000299 // An upper bound on the number of SurfaceFrames in the pending classifications deque.
300 static constexpr int kPendingClassificationMaxSurfaceFrames = 25;
Jorim Jaggi9c03b502020-11-24 23:51:31 +0100301
Robert Carr7121caf2020-12-15 13:07:32 -0800302 const std::string mBlastTransactionName{"BufferTX - " + mName};
303 // This integer is incremented everytime a buffer arrives at the server for this layer,
304 // and decremented when a buffer is dropped or latched. When changed the integer is exported
305 // to systrace with ATRACE_INT and mBlastTransactionName. This way when debugging perf it is
306 // possible to see when a buffer arrived at the server, and in which frame it latched.
307 //
308 // You can understand the trace this way:
309 // - If the integer increases, a buffer arrived at the server.
310 // - If the integer decreases in latchBuffer, that buffer was latched
311 // - If the integer decreases in setBuffer or doTransaction, a buffer was dropped
Vishnu Nair8eda69e2021-02-26 10:42:10 -0800312 std::atomic<int32_t> mPendingBufferTransactions{0};
Robert Carr7121caf2020-12-15 13:07:32 -0800313
Vishnu Nair6bdec7d2021-05-10 15:01:13 -0700314 // Contains requested position and matrix updates. This will be applied if the client does
315 // not specify a destination frame.
316 ui::Transform mRequestedTransform;
317
Marissa Wall947d34e2019-03-29 14:03:53 -0700318 sp<HwcSlotGenerator> mHwcSlotGenerator;
Marissa Wall61c58622018-07-18 10:12:20 -0700319};
320
321} // namespace android