blob: bf98a279a35b0b72f786688fd24f3649e4884866 [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
Marissa Wall61c58622018-07-18 10:12:20 -070019#include "BufferLayer.h"
20#include "Layer.h"
21
Peiyong Lincbc184f2018-08-22 13:24:10 -070022#include <renderengine/Image.h>
23#include <renderengine/RenderEngine.h>
Marissa Wall61c58622018-07-18 10:12:20 -070024#include <system/window.h>
25#include <utils/String8.h>
26
Marissa Wall947d34e2019-03-29 14:03:53 -070027#include <stack>
28
Marissa Wall61c58622018-07-18 10:12:20 -070029namespace android {
30
Valerie Hau64499682019-04-10 11:04:29 -070031class SlotGenerationTest;
32
Marissa Wall61c58622018-07-18 10:12:20 -070033class BufferStateLayer : public BufferLayer {
34public:
Lloyd Pique42ab75e2018-09-12 20:46:03 -070035 explicit BufferStateLayer(const LayerCreationArgs&);
Marissa Wall61c58622018-07-18 10:12:20 -070036
Alec Mouri4545a8a2019-08-08 20:05:32 -070037 ~BufferStateLayer() override;
38
Peiyong Linf243e5e2020-08-22 17:40:59 -070039 // Implements Layer.
chaviw8a01fa42019-08-19 12:39:31 -070040 const char* getType() const override { return "BufferStateLayer"; }
41
Dominik Laskowskib17c6212022-05-09 09:36:19 -070042 void onLayerDisplayed(ftl::SharedFuture<FenceResult>) override;
Sally Qi59a9f502021-10-12 18:53:23 +000043
Marissa Wall61c58622018-07-18 10:12:20 -070044 void releasePendingBuffer(nsecs_t dequeueReadyTime) override;
45
Valerie Hau871d6352020-01-29 08:44:02 -080046 void finalizeFrameEventHistory(const std::shared_ptr<FenceTime>& glDoneFence,
47 const CompositorTiming& compositorTiming) override;
48
Ady Abraham63a3e592021-01-06 10:47:15 -080049 bool isBufferDue(nsecs_t /*expectedPresentTime*/) const override { return true; }
Marissa Wall61c58622018-07-18 10:12:20 -070050
Marissa Wall61c58622018-07-18 10:12:20 -070051 Region getActiveTransparentRegion(const Layer::State& s) const override {
52 return s.transparentRegionHint;
53 }
54 Rect getCrop(const Layer::State& s) const;
Marissa Wall61c58622018-07-18 10:12:20 -070055
Lloyd Pique0449b0f2018-12-20 16:23:45 -080056 bool setTransform(uint32_t transform) override;
57 bool setTransformToDisplayInverse(bool transformToDisplayInverse) override;
58 bool setCrop(const Rect& crop) override;
Vishnu Nairdbbe3852022-01-12 20:22:11 -080059 bool setBuffer(std::shared_ptr<renderengine::ExternalTexture>& /* buffer */,
60 const BufferData& bufferData, nsecs_t postTime, nsecs_t desiredPresentTime,
chaviwba4320c2021-09-15 15:20:53 -050061 bool isAutoTimestamp, std::optional<nsecs_t> dequeueTime,
62 const FrameTimelineInfo& info) override;
Lloyd Pique0449b0f2018-12-20 16:23:45 -080063 bool setDataspace(ui::Dataspace dataspace) override;
64 bool setHdrMetadata(const HdrMetadata& hdrMetadata) override;
65 bool setSurfaceDamageRegion(const Region& surfaceDamage) override;
66 bool setApi(int32_t api) override;
67 bool setSidebandStream(const sp<NativeHandle>& sidebandStream) override;
Jiakai Zhanga5505cb2021-11-09 11:46:30 +000068 bool setTransactionCompletedListeners(const std::vector<sp<CallbackHandle>>& handles) override;
Chavi Weingartena5aedbd2021-04-09 13:37:33 +000069 bool setPosition(float /*x*/, float /*y*/) override;
Robert Carrde6d7b42022-01-07 18:23:06 -080070 bool setMatrix(const layer_state_t::matrix22_t& /*matrix*/);
Marissa Wall61c58622018-07-18 10:12:20 -070071
Marissa Wall61c58622018-07-18 10:12:20 -070072 // Override to ignore legacy layer state properties that are not used by BufferStateLayer
Marissa Wall861616d2018-10-22 12:52:23 -070073 bool setSize(uint32_t /*w*/, uint32_t /*h*/) override { return false; }
Marissa Wall861616d2018-10-22 12:52:23 -070074 bool setTransparentRegionHint(const Region& transparent) override;
Marissa Wall861616d2018-10-22 12:52:23 -070075
Lloyd Pique0449b0f2018-12-20 16:23:45 -080076 Rect getBufferSize(const State& s) const override;
Vishnu Nair4351ad52019-02-11 14:13:02 -080077 FloatRect computeSourceBounds(const FloatRect& parentBounds) const override;
Vishnu Naircf26a0a2020-11-13 12:56:20 -080078 void setAutoRefresh(bool autoRefresh) override;
Valerie Haubc6ddb12019-03-08 11:10:15 -080079
chaviwf3f40fe2021-04-27 15:54:02 -050080 bool setBufferCrop(const Rect& bufferCrop) override;
Vishnu Nair6bdec7d2021-05-10 15:01:13 -070081 bool setDestinationFrame(const Rect& destinationFrame) override;
Robert Carr6a160312021-05-17 12:08:20 -070082 bool updateGeometry() override;
chaviwf3f40fe2021-04-27 15:54:02 -050083
Marissa Wall61c58622018-07-18 10:12:20 -070084 // -----------------------------------------------------------------------
85
86 // -----------------------------------------------------------------------
87 // Interface implementation for BufferLayer
88 // -----------------------------------------------------------------------
Lloyd Pique0449b0f2018-12-20 16:23:45 -080089 bool fenceHasSignaled() const override;
Dominik Laskowskia8955dd2019-07-10 10:19:09 -070090 bool framePresentTimeIsCurrent(nsecs_t expectedPresentTime) const override;
Valerie Hau871d6352020-01-29 08:44:02 -080091 bool onPreComposition(nsecs_t refreshStartTime) override;
Robert Carr916b0362020-10-06 13:53:03 -070092 uint32_t getEffectiveScalingMode() const override;
Marissa Wall61c58622018-07-18 10:12:20 -070093
Robert Carr7121caf2020-12-15 13:07:32 -080094 // See mPendingBufferTransactions
Robert Carr7121caf2020-12-15 13:07:32 -080095 void decrementPendingBufferCount();
Vishnu Nair8eda69e2021-02-26 10:42:10 -080096 std::atomic<int32_t>* getPendingBufferCounter() override { return &mPendingBufferTransactions; }
97 std::string getPendingBufferCounterName() override { return mBlastTransactionName; }
Robert Carr7121caf2020-12-15 13:07:32 -080098
Robert Carr6a5dc1f2021-03-11 12:36:40 -080099 bool shouldPresentNow(nsecs_t /*expectedPresentTime*/) const override { return true; }
100
chaviw4244e032019-09-04 11:27:49 -0700101protected:
102 void gatherBufferInfo() override;
Jorim Jaggi9c03b502020-11-24 23:51:31 +0100103 void onSurfaceFrameCreated(const std::shared_ptr<frametimeline::SurfaceFrame>& surfaceFrame);
chaviw39d01472021-04-08 14:26:24 -0500104 ui::Transform getInputTransform() const override;
105 Rect getInputBounds() const override;
Marissa Wall61c58622018-07-18 10:12:20 -0700106
chaviw4244e032019-09-04 11:27:49 -0700107private:
Peiyong Linf243e5e2020-08-22 17:40:59 -0700108 friend class SlotGenerationTest;
Adithya Srinivasanb238cd52021-02-04 17:54:05 +0000109 friend class TransactionFrameTracerTest;
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +0000110 friend class TransactionSurfaceFrameTest;
111
Vishnu Nair8eda69e2021-02-26 10:42:10 -0800112 inline void tracePendingBufferCount(int32_t pendingBuffers);
Peiyong Linf243e5e2020-08-22 17:40:59 -0700113
Valerie Haubf784642020-01-29 07:25:23 -0800114 bool updateFrameEventHistory(const sp<Fence>& acquireFence, nsecs_t postedTime,
115 nsecs_t requestedPresentTime);
116
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800117 bool latchSidebandStream(bool& recomputeVisibleRegions) override;
Marissa Wall61c58622018-07-18 10:12:20 -0700118
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800119 bool hasFrameUpdate() const override;
Marissa Wall61c58622018-07-18 10:12:20 -0700120
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700121 status_t updateTexImage(bool& recomputeVisibleRegions, nsecs_t latchTime,
122 nsecs_t expectedPresentTime) override;
Marissa Wall61c58622018-07-18 10:12:20 -0700123
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800124 status_t updateActiveBuffer() override;
Alec Mouri8977ce92022-05-07 00:34:50 +0000125 status_t updateFrameNumber() override;
Marissa Wall61c58622018-07-18 10:12:20 -0700126
chaviwb4c6e582019-08-16 14:35:07 -0700127 sp<Layer> createClone() override;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700128
chaviw4244e032019-09-04 11:27:49 -0700129 // Crop that applies to the buffer
Vishnu Nair5cc9ac02021-04-19 13:23:38 -0700130 Rect computeBufferCrop(const State& s);
chaviw4244e032019-09-04 11:27:49 -0700131
Marissa Walle2ffb422018-10-12 11:33:52 -0700132 bool willPresentCurrentTransaction() const;
Marissa Wall61c58622018-07-18 10:12:20 -0700133
Vishnu Naire7f79c52020-10-29 14:45:03 -0700134 bool bufferNeedsFiltering() const override;
135
Ady Abraham9dada822022-02-03 10:26:59 -0800136 bool simpleBufferUpdate(const layer_state_t& s) const override;
137
Chavi Weingartend00e0f72022-07-14 15:59:20 +0000138 void callReleaseBufferCallback(const sp<ITransactionCompletedListener>& listener,
139 const sp<GraphicBuffer>& buffer, uint64_t framenumber,
140 const sp<Fence>& releaseFence,
141 uint32_t currentMaxAcquiredBufferCount);
142
Vishnu Nair4ba0c2e2021-06-24 11:27:17 -0700143 ReleaseCallbackId mPreviousReleaseCallbackId = ReleaseCallbackId::INVALID_ID;
Mikael Pessa2e1608f2019-07-19 11:25:35 -0700144 uint64_t mPreviousReleasedFrameNumber = 0;
Marissa Wallfda30bb2018-10-12 11:34:28 -0700145
Robert Carr79dc06a2022-02-22 15:28:59 -0800146 uint64_t mPreviousBarrierFrameNumber = 0;
147
Marissa Wallfda30bb2018-10-12 11:34:28 -0700148 bool mReleasePreviousBuffer = false;
Vishnu Nair1506b182021-02-22 14:35:15 -0800149
150 // Stores the last set acquire fence signal time used to populate the callback handle's acquire
151 // time.
Ady Abraham461296a2022-01-21 11:11:31 -0800152 std::variant<nsecs_t, sp<Fence>> mCallbackHandleAcquireTimeOrFence = -1;
Marissa Wall024a1912018-08-13 13:55:35 -0700153
Jorim Jaggi9c03b502020-11-24 23:51:31 +0100154 std::deque<std::shared_ptr<android::frametimeline::SurfaceFrame>> mPendingJankClassifications;
Adithya Srinivasand17c7da2021-03-05 20:43:32 +0000155 // An upper bound on the number of SurfaceFrames in the pending classifications deque.
156 static constexpr int kPendingClassificationMaxSurfaceFrames = 25;
Jorim Jaggi9c03b502020-11-24 23:51:31 +0100157
Robert Carr7121caf2020-12-15 13:07:32 -0800158 const std::string mBlastTransactionName{"BufferTX - " + mName};
159 // This integer is incremented everytime a buffer arrives at the server for this layer,
160 // and decremented when a buffer is dropped or latched. When changed the integer is exported
161 // to systrace with ATRACE_INT and mBlastTransactionName. This way when debugging perf it is
162 // possible to see when a buffer arrived at the server, and in which frame it latched.
163 //
164 // You can understand the trace this way:
165 // - If the integer increases, a buffer arrived at the server.
166 // - If the integer decreases in latchBuffer, that buffer was latched
167 // - If the integer decreases in setBuffer or doTransaction, a buffer was dropped
Vishnu Nair8eda69e2021-02-26 10:42:10 -0800168 std::atomic<int32_t> mPendingBufferTransactions{0};
Robert Carr7121caf2020-12-15 13:07:32 -0800169
Vishnu Nair6bdec7d2021-05-10 15:01:13 -0700170 // Contains requested position and matrix updates. This will be applied if the client does
171 // not specify a destination frame.
172 ui::Transform mRequestedTransform;
173
Marissa Wall61c58622018-07-18 10:12:20 -0700174 // TODO(marissaw): support sticky transform for LEGACY camera mode
Marissa Wall947d34e2019-03-29 14:03:53 -0700175
176 class HwcSlotGenerator : public ClientCache::ErasedRecipient {
177 public:
178 HwcSlotGenerator() {
rnleeed20fa42021-08-10 18:00:03 -0700179 for (int i = 0; i < BufferQueue::NUM_BUFFER_SLOTS; i++) {
Marissa Wall947d34e2019-03-29 14:03:53 -0700180 mFreeHwcCacheSlots.push(i);
181 }
182 }
183
184 void bufferErased(const client_cache_t& clientCacheId);
185
rnleeed20fa42021-08-10 18:00:03 -0700186 int getHwcCacheSlot(const client_cache_t& clientCacheId);
Marissa Wall947d34e2019-03-29 14:03:53 -0700187
188 private:
Valerie Hau64499682019-04-10 11:04:29 -0700189 friend class SlotGenerationTest;
rnleeed20fa42021-08-10 18:00:03 -0700190 int addCachedBuffer(const client_cache_t& clientCacheId) REQUIRES(mMutex);
191 int getFreeHwcCacheSlot() REQUIRES(mMutex);
Marissa Wall947d34e2019-03-29 14:03:53 -0700192 void evictLeastRecentlyUsed() REQUIRES(mMutex);
193 void eraseBufferLocked(const client_cache_t& clientCacheId) REQUIRES(mMutex);
194
195 struct CachedBufferHash {
196 std::size_t operator()(const client_cache_t& clientCacheId) const {
197 return std::hash<uint64_t>{}(clientCacheId.id);
198 }
199 };
200
201 std::mutex mMutex;
202
rnleeed20fa42021-08-10 18:00:03 -0700203 std::unordered_map<client_cache_t, std::pair<int /*HwcCacheSlot*/, uint64_t /*counter*/>,
Marissa Wall947d34e2019-03-29 14:03:53 -0700204 CachedBufferHash>
205 mCachedBuffers GUARDED_BY(mMutex);
rnleeed20fa42021-08-10 18:00:03 -0700206 std::stack<int /*HwcCacheSlot*/> mFreeHwcCacheSlots GUARDED_BY(mMutex);
Marissa Wall947d34e2019-03-29 14:03:53 -0700207
208 // The cache increments this counter value when a slot is updated or used.
209 // Used to track the least recently-used buffer
210 uint64_t mCounter = 0;
211 };
212
213 sp<HwcSlotGenerator> mHwcSlotGenerator;
Marissa Wall61c58622018-07-18 10:12:20 -0700214};
215
216} // namespace android