blob: df7ae611b877ceeb6a4ed4981bdc7f1135c355c9 [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"
43#include "Layer.h"
44#include "LayerVector.h"
45#include "SurfaceFlinger.h"
46
Marissa Wall61c58622018-07-18 10:12:20 -070047namespace android {
48
Valerie Hau64499682019-04-10 11:04:29 -070049class SlotGenerationTest;
50
Vishnu Naira72f6c02022-07-01 05:33:08 +000051class BufferStateLayer : public Layer {
Marissa Wall61c58622018-07-18 10:12:20 -070052public:
Lloyd Pique42ab75e2018-09-12 20:46:03 -070053 explicit BufferStateLayer(const LayerCreationArgs&);
Marissa Wall61c58622018-07-18 10:12:20 -070054
Alec Mouri4545a8a2019-08-08 20:05:32 -070055 ~BufferStateLayer() override;
56
Peiyong Linf243e5e2020-08-22 17:40:59 -070057 // Implements Layer.
Vishnu Naira72f6c02022-07-01 05:33:08 +000058 sp<compositionengine::LayerFE> getCompositionEngineLayerFE() const override;
59 compositionengine::LayerFECompositionState* editCompositionState() override;
60
61 // If we have received a new buffer this frame, we will pass its surface
62 // damage down to hardware composer. Otherwise, we must send a region with
63 // one empty rect.
64 void useSurfaceDamage() override;
65 void useEmptyDamage() override;
66
67 bool isOpaque(const Layer::State& s) const override;
68 bool canReceiveInput() const override;
69
70 // isVisible - true if this layer is visible, false otherwise
71 bool isVisible() const override;
72
73 // isProtected - true if the layer may contain protected content in the
74 // GRALLOC_USAGE_PROTECTED sense.
75 bool isProtected() const override;
76
77 // isFixedSize - true if content has a fixed size
78 bool isFixedSize() const override;
79
80 bool usesSourceCrop() const override;
81
82 bool isHdrY410() const override;
83
84 void onPostComposition(const DisplayDevice*, const std::shared_ptr<FenceTime>& glDoneFence,
85 const std::shared_ptr<FenceTime>& presentFence,
86 const CompositorTiming&) override;
87
88 // latchBuffer - called each time the screen is redrawn and returns whether
89 // the visible regions need to be recomputed (this is a fairly heavy
90 // operation, so this should be set only if needed). Typically this is used
91 // to figure out if the content or size of a surface has changed.
92 bool latchBuffer(bool& recomputeVisibleRegions, nsecs_t latchTime,
93 nsecs_t expectedPresentTime) override;
94 bool hasReadyFrame() const override;
95
96 // Returns the current scaling mode
97 uint32_t getEffectiveScalingMode() const override;
98
99 // Calls latchBuffer if the buffer has a frame queued and then releases the buffer.
100 // This is used if the buffer is just latched and releases to free up the buffer
101 // and will not be shown on screen.
102 // Should only be called on the main thread.
103 void latchAndReleaseBuffer() override;
104
105 bool getTransformToDisplayInverse() const override;
106
107 Rect getBufferCrop() const override;
108
109 uint32_t getBufferTransform() const override;
110
111 ui::Dataspace getDataSpace() const override;
112
113 sp<GraphicBuffer> getBuffer() const override;
114 const std::shared_ptr<renderengine::ExternalTexture>& getExternalTexture() const override;
115
116 ui::Transform::RotationFlags getTransformHint() const override { return mTransformHint; }
117
118 // Implements Layer.
chaviw8a01fa42019-08-19 12:39:31 -0700119 const char* getType() const override { return "BufferStateLayer"; }
120
Dominik Laskowskib17c6212022-05-09 09:36:19 -0700121 void onLayerDisplayed(ftl::SharedFuture<FenceResult>) override;
Sally Qi59a9f502021-10-12 18:53:23 +0000122
Marissa Wall61c58622018-07-18 10:12:20 -0700123 void releasePendingBuffer(nsecs_t dequeueReadyTime) override;
124
Valerie Hau871d6352020-01-29 08:44:02 -0800125 void finalizeFrameEventHistory(const std::shared_ptr<FenceTime>& glDoneFence,
126 const CompositorTiming& compositorTiming) override;
127
Vishnu Naira72f6c02022-07-01 05:33:08 +0000128 // Returns true if the next buffer should be presented at the expected present time,
129 // overridden by BufferStateLayer and BufferQueueLayer for implementation
130 // specific logic
131 bool isBufferDue(nsecs_t /*expectedPresentTime*/) const { return true; }
Marissa Wall61c58622018-07-18 10:12:20 -0700132
Marissa Wall61c58622018-07-18 10:12:20 -0700133 Region getActiveTransparentRegion(const Layer::State& s) const override {
134 return s.transparentRegionHint;
135 }
136 Rect getCrop(const Layer::State& s) const;
Marissa Wall61c58622018-07-18 10:12:20 -0700137
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800138 bool setTransform(uint32_t transform) override;
139 bool setTransformToDisplayInverse(bool transformToDisplayInverse) override;
140 bool setCrop(const Rect& crop) override;
Vishnu Nairdbbe3852022-01-12 20:22:11 -0800141 bool setBuffer(std::shared_ptr<renderengine::ExternalTexture>& /* buffer */,
142 const BufferData& bufferData, nsecs_t postTime, nsecs_t desiredPresentTime,
chaviwba4320c2021-09-15 15:20:53 -0500143 bool isAutoTimestamp, std::optional<nsecs_t> dequeueTime,
144 const FrameTimelineInfo& info) override;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800145 bool setDataspace(ui::Dataspace dataspace) override;
146 bool setHdrMetadata(const HdrMetadata& hdrMetadata) override;
147 bool setSurfaceDamageRegion(const Region& surfaceDamage) override;
148 bool setApi(int32_t api) override;
149 bool setSidebandStream(const sp<NativeHandle>& sidebandStream) override;
Jiakai Zhanga5505cb2021-11-09 11:46:30 +0000150 bool setTransactionCompletedListeners(const std::vector<sp<CallbackHandle>>& handles) override;
Chavi Weingartena5aedbd2021-04-09 13:37:33 +0000151 bool setPosition(float /*x*/, float /*y*/) override;
Robert Carrde6d7b42022-01-07 18:23:06 -0800152 bool setMatrix(const layer_state_t::matrix22_t& /*matrix*/);
Marissa Wall61c58622018-07-18 10:12:20 -0700153
Marissa Wall61c58622018-07-18 10:12:20 -0700154 // Override to ignore legacy layer state properties that are not used by BufferStateLayer
Marissa Wall861616d2018-10-22 12:52:23 -0700155 bool setSize(uint32_t /*w*/, uint32_t /*h*/) override { return false; }
Marissa Wall861616d2018-10-22 12:52:23 -0700156 bool setTransparentRegionHint(const Region& transparent) override;
Marissa Wall861616d2018-10-22 12:52:23 -0700157
Vishnu Naira72f6c02022-07-01 05:33:08 +0000158 // BufferStateLayers can return Rect::INVALID_RECT if the layer does not have a display frame
159 // and its parent layer is not bounded
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800160 Rect getBufferSize(const State& s) const override;
Vishnu Nair4351ad52019-02-11 14:13:02 -0800161 FloatRect computeSourceBounds(const FloatRect& parentBounds) const override;
Vishnu Naircf26a0a2020-11-13 12:56:20 -0800162 void setAutoRefresh(bool autoRefresh) override;
Valerie Haubc6ddb12019-03-08 11:10:15 -0800163
chaviwf3f40fe2021-04-27 15:54:02 -0500164 bool setBufferCrop(const Rect& bufferCrop) override;
Vishnu Nair6bdec7d2021-05-10 15:01:13 -0700165 bool setDestinationFrame(const Rect& destinationFrame) override;
Robert Carr6a160312021-05-17 12:08:20 -0700166 bool updateGeometry() override;
chaviwf3f40fe2021-04-27 15:54:02 -0500167
Vishnu Naira72f6c02022-07-01 05:33:08 +0000168 bool fenceHasSignaled() const;
169 bool framePresentTimeIsCurrent(nsecs_t expectedPresentTime) const;
170 bool onPreComposition(nsecs_t) override;
Marissa Wall61c58622018-07-18 10:12:20 -0700171
Robert Carr7121caf2020-12-15 13:07:32 -0800172 // See mPendingBufferTransactions
Robert Carr7121caf2020-12-15 13:07:32 -0800173 void decrementPendingBufferCount();
Vishnu Nair8eda69e2021-02-26 10:42:10 -0800174 std::atomic<int32_t>* getPendingBufferCounter() override { return &mPendingBufferTransactions; }
175 std::string getPendingBufferCounterName() override { return mBlastTransactionName; }
Robert Carr7121caf2020-12-15 13:07:32 -0800176
Vishnu Naira72f6c02022-07-01 05:33:08 +0000177 // Returns true if the next buffer should be presented at the expected present time
Robert Carr6a5dc1f2021-03-11 12:36:40 -0800178 bool shouldPresentNow(nsecs_t /*expectedPresentTime*/) const override { return true; }
179
chaviw4244e032019-09-04 11:27:49 -0700180protected:
Vishnu Naira72f6c02022-07-01 05:33:08 +0000181 void gatherBufferInfo();
Jorim Jaggi9c03b502020-11-24 23:51:31 +0100182 void onSurfaceFrameCreated(const std::shared_ptr<frametimeline::SurfaceFrame>& surfaceFrame);
chaviw39d01472021-04-08 14:26:24 -0500183 ui::Transform getInputTransform() const override;
184 Rect getInputBounds() const override;
Marissa Wall61c58622018-07-18 10:12:20 -0700185
Vishnu Naira72f6c02022-07-01 05:33:08 +0000186 struct BufferInfo {
187 nsecs_t mDesiredPresentTime;
188 std::shared_ptr<FenceTime> mFenceTime;
189 sp<Fence> mFence;
190 uint32_t mTransform{0};
191 ui::Dataspace mDataspace{ui::Dataspace::UNKNOWN};
192 Rect mCrop;
193 uint32_t mScaleMode{NATIVE_WINDOW_SCALING_MODE_FREEZE};
194 Region mSurfaceDamage;
195 HdrMetadata mHdrMetadata;
196 int mApi;
197 PixelFormat mPixelFormat{PIXEL_FORMAT_NONE};
198 bool mTransformToDisplayInverse{false};
199
200 std::shared_ptr<renderengine::ExternalTexture> mBuffer;
201 uint64_t mFrameNumber;
202 int mBufferSlot{BufferQueue::INVALID_BUFFER_SLOT};
203
204 bool mFrameLatencyNeeded{false};
205 };
206
207 BufferInfo mBufferInfo;
208
209 std::optional<compositionengine::LayerFE::LayerSettings> prepareClientComposition(
210 compositionengine::LayerFE::ClientCompositionTargetSettings&) override;
211
212 /*
213 * compositionengine::LayerFE overrides
214 */
215 const compositionengine::LayerFECompositionState* getCompositionState() const override;
216 void preparePerFrameCompositionState() override;
217
218 static bool getOpacityForFormat(PixelFormat format);
219
220 // from graphics API
221 const uint32_t mTextureName;
222 ui::Dataspace translateDataspace(ui::Dataspace dataspace);
223 void setInitialValuesForClone(const sp<Layer>& clonedFrom);
224 void updateCloneBufferInfo() override;
225 uint64_t mPreviousFrameNumber = 0;
226
227 void setTransformHint(ui::Transform::RotationFlags displayTransformHint) override;
228
229 // Transform hint provided to the producer. This must be accessed holding
230 // the mStateLock.
231 ui::Transform::RotationFlags mTransformHint = ui::Transform::ROT_0;
232
233 bool getAutoRefresh() const { return mDrawingState.autoRefresh; }
234 bool getSidebandStreamChanged() const { return mSidebandStreamChanged; }
235
236 std::atomic<bool> mSidebandStreamChanged{false};
237
chaviw4244e032019-09-04 11:27:49 -0700238private:
Peiyong Linf243e5e2020-08-22 17:40:59 -0700239 friend class SlotGenerationTest;
Adithya Srinivasanb238cd52021-02-04 17:54:05 +0000240 friend class TransactionFrameTracerTest;
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +0000241 friend class TransactionSurfaceFrameTest;
242
Vishnu Naira72f6c02022-07-01 05:33:08 +0000243 // We generate InputWindowHandles for all non-cursor buffered layers regardless of whether they
244 // have an InputChannel. This is to enable the InputDispatcher to do PID based occlusion
245 // detection.
246 bool needsInputInfo() const override { return !mPotentialCursor; }
247
248 // Returns true if this layer requires filtering
249 bool needsFiltering(const DisplayDevice*) const override;
250 bool needsFilteringForScreenshots(const DisplayDevice*,
251 const ui::Transform& inverseParentTransform) const override;
252
253 PixelFormat getPixelFormat() const;
254
255 // Computes the transform matrix using the setFilteringEnabled to determine whether the
256 // transform matrix should be computed for use with bilinear filtering.
257 void getDrawingTransformMatrix(bool filteringEnabled, float outMatrix[16]);
258
259 std::unique_ptr<compositionengine::LayerFECompositionState> mCompositionState;
260
Vishnu Nair8eda69e2021-02-26 10:42:10 -0800261 inline void tracePendingBufferCount(int32_t pendingBuffers);
Peiyong Linf243e5e2020-08-22 17:40:59 -0700262
Valerie Haubf784642020-01-29 07:25:23 -0800263 bool updateFrameEventHistory(const sp<Fence>& acquireFence, nsecs_t postedTime,
264 nsecs_t requestedPresentTime);
265
Vishnu Naira72f6c02022-07-01 05:33:08 +0000266 // Latch sideband stream and returns true if the dirty region should be updated.
267 bool latchSidebandStream(bool& recomputeVisibleRegions);
Marissa Wall61c58622018-07-18 10:12:20 -0700268
Vishnu Naira72f6c02022-07-01 05:33:08 +0000269 bool hasFrameUpdate() const;
Marissa Wall61c58622018-07-18 10:12:20 -0700270
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700271 status_t updateTexImage(bool& recomputeVisibleRegions, nsecs_t latchTime,
Vishnu Naira72f6c02022-07-01 05:33:08 +0000272 nsecs_t expectedPresentTime);
Marissa Wall61c58622018-07-18 10:12:20 -0700273
Vishnu Naira72f6c02022-07-01 05:33:08 +0000274 status_t updateActiveBuffer();
275 status_t updateFrameNumber();
Marissa Wall61c58622018-07-18 10:12:20 -0700276
chaviwb4c6e582019-08-16 14:35:07 -0700277 sp<Layer> createClone() override;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700278
chaviw4244e032019-09-04 11:27:49 -0700279 // Crop that applies to the buffer
Vishnu Nair5cc9ac02021-04-19 13:23:38 -0700280 Rect computeBufferCrop(const State& s);
chaviw4244e032019-09-04 11:27:49 -0700281
Marissa Walle2ffb422018-10-12 11:33:52 -0700282 bool willPresentCurrentTransaction() const;
Marissa Wall61c58622018-07-18 10:12:20 -0700283
Vishnu Naira72f6c02022-07-01 05:33:08 +0000284 // Returns true if the transformed buffer size does not match the layer size and we need
285 // to apply filtering.
286 bool bufferNeedsFiltering() const;
Vishnu Naire7f79c52020-10-29 14:45:03 -0700287
Ady Abraham9dada822022-02-03 10:26:59 -0800288 bool simpleBufferUpdate(const layer_state_t& s) const override;
289
Chavi Weingartend00e0f72022-07-14 15:59:20 +0000290 void callReleaseBufferCallback(const sp<ITransactionCompletedListener>& listener,
291 const sp<GraphicBuffer>& buffer, uint64_t framenumber,
292 const sp<Fence>& releaseFence,
293 uint32_t currentMaxAcquiredBufferCount);
294
Vishnu Nair4ba0c2e2021-06-24 11:27:17 -0700295 ReleaseCallbackId mPreviousReleaseCallbackId = ReleaseCallbackId::INVALID_ID;
Mikael Pessa2e1608f2019-07-19 11:25:35 -0700296 uint64_t mPreviousReleasedFrameNumber = 0;
Marissa Wallfda30bb2018-10-12 11:34:28 -0700297
Robert Carr79dc06a2022-02-22 15:28:59 -0800298 uint64_t mPreviousBarrierFrameNumber = 0;
299
Marissa Wallfda30bb2018-10-12 11:34:28 -0700300 bool mReleasePreviousBuffer = false;
Vishnu Nair1506b182021-02-22 14:35:15 -0800301
302 // Stores the last set acquire fence signal time used to populate the callback handle's acquire
303 // time.
Ady Abraham461296a2022-01-21 11:11:31 -0800304 std::variant<nsecs_t, sp<Fence>> mCallbackHandleAcquireTimeOrFence = -1;
Marissa Wall024a1912018-08-13 13:55:35 -0700305
Jorim Jaggi9c03b502020-11-24 23:51:31 +0100306 std::deque<std::shared_ptr<android::frametimeline::SurfaceFrame>> mPendingJankClassifications;
Adithya Srinivasand17c7da2021-03-05 20:43:32 +0000307 // An upper bound on the number of SurfaceFrames in the pending classifications deque.
308 static constexpr int kPendingClassificationMaxSurfaceFrames = 25;
Jorim Jaggi9c03b502020-11-24 23:51:31 +0100309
Robert Carr7121caf2020-12-15 13:07:32 -0800310 const std::string mBlastTransactionName{"BufferTX - " + mName};
311 // This integer is incremented everytime a buffer arrives at the server for this layer,
312 // and decremented when a buffer is dropped or latched. When changed the integer is exported
313 // to systrace with ATRACE_INT and mBlastTransactionName. This way when debugging perf it is
314 // possible to see when a buffer arrived at the server, and in which frame it latched.
315 //
316 // You can understand the trace this way:
317 // - If the integer increases, a buffer arrived at the server.
318 // - If the integer decreases in latchBuffer, that buffer was latched
319 // - If the integer decreases in setBuffer or doTransaction, a buffer was dropped
Vishnu Nair8eda69e2021-02-26 10:42:10 -0800320 std::atomic<int32_t> mPendingBufferTransactions{0};
Robert Carr7121caf2020-12-15 13:07:32 -0800321
Vishnu Nair6bdec7d2021-05-10 15:01:13 -0700322 // Contains requested position and matrix updates. This will be applied if the client does
323 // not specify a destination frame.
324 ui::Transform mRequestedTransform;
325
Marissa Wall61c58622018-07-18 10:12:20 -0700326 // TODO(marissaw): support sticky transform for LEGACY camera mode
Marissa Wall947d34e2019-03-29 14:03:53 -0700327
328 class HwcSlotGenerator : public ClientCache::ErasedRecipient {
329 public:
330 HwcSlotGenerator() {
rnleeed20fa42021-08-10 18:00:03 -0700331 for (int i = 0; i < BufferQueue::NUM_BUFFER_SLOTS; i++) {
Marissa Wall947d34e2019-03-29 14:03:53 -0700332 mFreeHwcCacheSlots.push(i);
333 }
334 }
335
336 void bufferErased(const client_cache_t& clientCacheId);
337
rnleeed20fa42021-08-10 18:00:03 -0700338 int getHwcCacheSlot(const client_cache_t& clientCacheId);
Marissa Wall947d34e2019-03-29 14:03:53 -0700339
340 private:
Valerie Hau64499682019-04-10 11:04:29 -0700341 friend class SlotGenerationTest;
rnleeed20fa42021-08-10 18:00:03 -0700342 int addCachedBuffer(const client_cache_t& clientCacheId) REQUIRES(mMutex);
343 int getFreeHwcCacheSlot() REQUIRES(mMutex);
Marissa Wall947d34e2019-03-29 14:03:53 -0700344 void evictLeastRecentlyUsed() REQUIRES(mMutex);
345 void eraseBufferLocked(const client_cache_t& clientCacheId) REQUIRES(mMutex);
346
347 struct CachedBufferHash {
348 std::size_t operator()(const client_cache_t& clientCacheId) const {
349 return std::hash<uint64_t>{}(clientCacheId.id);
350 }
351 };
352
353 std::mutex mMutex;
354
rnleeed20fa42021-08-10 18:00:03 -0700355 std::unordered_map<client_cache_t, std::pair<int /*HwcCacheSlot*/, uint64_t /*counter*/>,
Marissa Wall947d34e2019-03-29 14:03:53 -0700356 CachedBufferHash>
357 mCachedBuffers GUARDED_BY(mMutex);
rnleeed20fa42021-08-10 18:00:03 -0700358 std::stack<int /*HwcCacheSlot*/> mFreeHwcCacheSlots GUARDED_BY(mMutex);
Marissa Wall947d34e2019-03-29 14:03:53 -0700359
360 // The cache increments this counter value when a slot is updated or used.
361 // Used to track the least recently-used buffer
362 uint64_t mCounter = 0;
363 };
364
365 sp<HwcSlotGenerator> mHwcSlotGenerator;
Marissa Wall61c58622018-07-18 10:12:20 -0700366};
367
368} // namespace android