blob: dbc0b052e8191c5972b509f3a768049f0e360ea8 [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
Marissa Wall61c58622018-07-18 10:12:20 -070042 void onLayerDisplayed(const sp<Fence>& releaseFence) override;
Marissa Wall61c58622018-07-18 10:12:20 -070043 void releasePendingBuffer(nsecs_t dequeueReadyTime) override;
44
Valerie Hau871d6352020-01-29 08:44:02 -080045 void finalizeFrameEventHistory(const std::shared_ptr<FenceTime>& glDoneFence,
46 const CompositorTiming& compositorTiming) override;
47
Ady Abraham63a3e592021-01-06 10:47:15 -080048 bool isBufferDue(nsecs_t /*expectedPresentTime*/) const override { return true; }
Marissa Wall61c58622018-07-18 10:12:20 -070049
Marissa Wall61c58622018-07-18 10:12:20 -070050 Region getActiveTransparentRegion(const Layer::State& s) const override {
51 return s.transparentRegionHint;
52 }
53 Rect getCrop(const Layer::State& s) const;
Marissa Wall61c58622018-07-18 10:12:20 -070054
Lloyd Pique0449b0f2018-12-20 16:23:45 -080055 bool setTransform(uint32_t transform) override;
56 bool setTransformToDisplayInverse(bool transformToDisplayInverse) override;
57 bool setCrop(const Rect& crop) override;
Alec Mouria90a5702021-04-16 16:36:21 +000058 bool setBuffer(const std::shared_ptr<renderengine::ExternalTexture>& buffer,
59 const sp<Fence>& acquireFence, nsecs_t postTime, nsecs_t desiredPresentTime,
60 bool isAutoTimestamp, const client_cache_t& clientCacheId, uint64_t frameNumber,
Vishnu Nair1506b182021-02-22 14:35:15 -080061 std::optional<nsecs_t> dequeueTime, const FrameTimelineInfo& info,
chaviw0b06a8d2021-08-06 11:49:08 -050062 const sp<ITransactionCompletedListener>& transactionListener,
63 const sp<IBinder>& releaseBufferEndpoint) override;
Lloyd Pique0449b0f2018-12-20 16:23:45 -080064 bool setAcquireFence(const sp<Fence>& fence) override;
65 bool setDataspace(ui::Dataspace dataspace) override;
66 bool setHdrMetadata(const HdrMetadata& hdrMetadata) override;
67 bool setSurfaceDamageRegion(const Region& surfaceDamage) override;
68 bool setApi(int32_t api) override;
69 bool setSidebandStream(const sp<NativeHandle>& sidebandStream) override;
70 bool setTransactionCompletedListeners(const std::vector<sp<CallbackHandle>>& handles) override;
Valerie Hau871d6352020-01-29 08:44:02 -080071 bool addFrameEvent(const sp<Fence>& acquireFence, nsecs_t postedTime,
72 nsecs_t requestedPresentTime) override;
Chavi Weingartena5aedbd2021-04-09 13:37:33 +000073 bool setPosition(float /*x*/, float /*y*/) override;
74 bool setMatrix(const layer_state_t::matrix22_t& /*matrix*/,
75 bool /*allowNonRectPreservingTransforms*/);
Marissa Wall61c58622018-07-18 10:12:20 -070076
Marissa Wall61c58622018-07-18 10:12:20 -070077 // Override to ignore legacy layer state properties that are not used by BufferStateLayer
Marissa Wall861616d2018-10-22 12:52:23 -070078 bool setSize(uint32_t /*w*/, uint32_t /*h*/) override { return false; }
Marissa Wall861616d2018-10-22 12:52:23 -070079 bool setTransparentRegionHint(const Region& transparent) override;
Marissa Wall861616d2018-10-22 12:52:23 -070080
Lloyd Pique0449b0f2018-12-20 16:23:45 -080081 Rect getBufferSize(const State& s) const override;
Vishnu Nair4351ad52019-02-11 14:13:02 -080082 FloatRect computeSourceBounds(const FloatRect& parentBounds) const override;
Vishnu Naircf26a0a2020-11-13 12:56:20 -080083 void setAutoRefresh(bool autoRefresh) override;
Valerie Haubc6ddb12019-03-08 11:10:15 -080084
chaviwf3f40fe2021-04-27 15:54:02 -050085 bool setBufferCrop(const Rect& bufferCrop) override;
Vishnu Nair6bdec7d2021-05-10 15:01:13 -070086 bool setDestinationFrame(const Rect& destinationFrame) override;
Robert Carr6a160312021-05-17 12:08:20 -070087 bool updateGeometry() override;
chaviwf3f40fe2021-04-27 15:54:02 -050088
Marissa Wall61c58622018-07-18 10:12:20 -070089 // -----------------------------------------------------------------------
90
91 // -----------------------------------------------------------------------
92 // Interface implementation for BufferLayer
93 // -----------------------------------------------------------------------
Lloyd Pique0449b0f2018-12-20 16:23:45 -080094 bool fenceHasSignaled() const override;
Dominik Laskowskia8955dd2019-07-10 10:19:09 -070095 bool framePresentTimeIsCurrent(nsecs_t expectedPresentTime) const override;
Valerie Hau871d6352020-01-29 08:44:02 -080096 bool onPreComposition(nsecs_t refreshStartTime) override;
Robert Carr916b0362020-10-06 13:53:03 -070097 uint32_t getEffectiveScalingMode() const override;
Marissa Wall61c58622018-07-18 10:12:20 -070098
Robert Carr7121caf2020-12-15 13:07:32 -080099 // See mPendingBufferTransactions
Robert Carr7121caf2020-12-15 13:07:32 -0800100 void decrementPendingBufferCount();
Alec Mouria90a5702021-04-16 16:36:21 +0000101 void bufferMayChange(const sp<GraphicBuffer>& newBuffer) override;
Vishnu Nair8eda69e2021-02-26 10:42:10 -0800102 std::atomic<int32_t>* getPendingBufferCounter() override { return &mPendingBufferTransactions; }
103 std::string getPendingBufferCounterName() override { return mBlastTransactionName; }
Robert Carr7121caf2020-12-15 13:07:32 -0800104
Robert Carr6a5dc1f2021-03-11 12:36:40 -0800105 bool shouldPresentNow(nsecs_t /*expectedPresentTime*/) const override { return true; }
106
chaviw4244e032019-09-04 11:27:49 -0700107protected:
108 void gatherBufferInfo() override;
Robert Carrfe1209c2020-02-11 12:25:35 -0800109 uint64_t getHeadFrameNumber(nsecs_t expectedPresentTime) const;
Jorim Jaggi9c03b502020-11-24 23:51:31 +0100110 void onSurfaceFrameCreated(const std::shared_ptr<frametimeline::SurfaceFrame>& surfaceFrame);
chaviw39d01472021-04-08 14:26:24 -0500111 ui::Transform getInputTransform() const override;
112 Rect getInputBounds() const override;
Marissa Wall61c58622018-07-18 10:12:20 -0700113
chaviw4244e032019-09-04 11:27:49 -0700114private:
Peiyong Linf243e5e2020-08-22 17:40:59 -0700115 friend class SlotGenerationTest;
Adithya Srinivasanb238cd52021-02-04 17:54:05 +0000116 friend class TransactionFrameTracerTest;
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +0000117 friend class TransactionSurfaceFrameTest;
118
Vishnu Nair8eda69e2021-02-26 10:42:10 -0800119 inline void tracePendingBufferCount(int32_t pendingBuffers);
Peiyong Linf243e5e2020-08-22 17:40:59 -0700120
Valerie Haubf784642020-01-29 07:25:23 -0800121 bool updateFrameEventHistory(const sp<Fence>& acquireFence, nsecs_t postedTime,
122 nsecs_t requestedPresentTime);
123
Robert Carr8d958532020-11-10 14:09:16 -0800124 status_t addReleaseFence(const sp<CallbackHandle>& ch, const sp<Fence>& releaseFence);
125
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700126 uint64_t getFrameNumber(nsecs_t expectedPresentTime) const override;
Marissa Wall61c58622018-07-18 10:12:20 -0700127
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800128 bool latchSidebandStream(bool& recomputeVisibleRegions) override;
Marissa Wall61c58622018-07-18 10:12:20 -0700129
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800130 bool hasFrameUpdate() const override;
Marissa Wall61c58622018-07-18 10:12:20 -0700131
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700132 status_t updateTexImage(bool& recomputeVisibleRegions, nsecs_t latchTime,
133 nsecs_t expectedPresentTime) override;
Marissa Wall61c58622018-07-18 10:12:20 -0700134
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800135 status_t updateActiveBuffer() override;
Marissa Wall61c58622018-07-18 10:12:20 -0700136 status_t updateFrameNumber(nsecs_t latchTime) override;
137
chaviwb4c6e582019-08-16 14:35:07 -0700138 sp<Layer> createClone() override;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700139
chaviw4244e032019-09-04 11:27:49 -0700140 // Crop that applies to the buffer
Vishnu Nair5cc9ac02021-04-19 13:23:38 -0700141 Rect computeBufferCrop(const State& s);
chaviw4244e032019-09-04 11:27:49 -0700142
Marissa Walle2ffb422018-10-12 11:33:52 -0700143 bool willPresentCurrentTransaction() const;
Marissa Wall61c58622018-07-18 10:12:20 -0700144
Vishnu Naire7f79c52020-10-29 14:45:03 -0700145 bool bufferNeedsFiltering() const override;
146
Marissa Wallfda30bb2018-10-12 11:34:28 -0700147 sp<Fence> mPreviousReleaseFence;
Vishnu Nair4ba0c2e2021-06-24 11:27:17 -0700148 ReleaseCallbackId mPreviousReleaseCallbackId = ReleaseCallbackId::INVALID_ID;
Mikael Pessa2e1608f2019-07-19 11:25:35 -0700149 uint64_t mPreviousReleasedFrameNumber = 0;
Marissa Wallfda30bb2018-10-12 11:34:28 -0700150
Marissa Wallfda30bb2018-10-12 11:34:28 -0700151 bool mReleasePreviousBuffer = false;
Vishnu Nair1506b182021-02-22 14:35:15 -0800152
153 // Stores the last set acquire fence signal time used to populate the callback handle's acquire
154 // time.
Marissa Wallfda30bb2018-10-12 11:34:28 -0700155 nsecs_t mCallbackHandleAcquireTime = -1;
Marissa Wall024a1912018-08-13 13:55:35 -0700156
Jorim Jaggi9c03b502020-11-24 23:51:31 +0100157 std::deque<std::shared_ptr<android::frametimeline::SurfaceFrame>> mPendingJankClassifications;
Adithya Srinivasand17c7da2021-03-05 20:43:32 +0000158 // An upper bound on the number of SurfaceFrames in the pending classifications deque.
159 static constexpr int kPendingClassificationMaxSurfaceFrames = 25;
Jorim Jaggi9c03b502020-11-24 23:51:31 +0100160
Robert Carr7121caf2020-12-15 13:07:32 -0800161 const std::string mBlastTransactionName{"BufferTX - " + mName};
162 // This integer is incremented everytime a buffer arrives at the server for this layer,
163 // and decremented when a buffer is dropped or latched. When changed the integer is exported
164 // to systrace with ATRACE_INT and mBlastTransactionName. This way when debugging perf it is
165 // possible to see when a buffer arrived at the server, and in which frame it latched.
166 //
167 // You can understand the trace this way:
168 // - If the integer increases, a buffer arrived at the server.
169 // - If the integer decreases in latchBuffer, that buffer was latched
170 // - If the integer decreases in setBuffer or doTransaction, a buffer was dropped
Vishnu Nair8eda69e2021-02-26 10:42:10 -0800171 std::atomic<int32_t> mPendingBufferTransactions{0};
Robert Carr7121caf2020-12-15 13:07:32 -0800172
Vishnu Nair6bdec7d2021-05-10 15:01:13 -0700173 // Contains requested position and matrix updates. This will be applied if the client does
174 // not specify a destination frame.
175 ui::Transform mRequestedTransform;
176
Marissa Wall61c58622018-07-18 10:12:20 -0700177 // TODO(marissaw): support sticky transform for LEGACY camera mode
Marissa Wall947d34e2019-03-29 14:03:53 -0700178
179 class HwcSlotGenerator : public ClientCache::ErasedRecipient {
180 public:
181 HwcSlotGenerator() {
182 for (uint32_t i = 0; i < BufferQueue::NUM_BUFFER_SLOTS; i++) {
183 mFreeHwcCacheSlots.push(i);
184 }
185 }
186
187 void bufferErased(const client_cache_t& clientCacheId);
188
189 uint32_t getHwcCacheSlot(const client_cache_t& clientCacheId);
190
191 private:
Valerie Hau64499682019-04-10 11:04:29 -0700192 friend class SlotGenerationTest;
Marissa Wall947d34e2019-03-29 14:03:53 -0700193 uint32_t addCachedBuffer(const client_cache_t& clientCacheId) REQUIRES(mMutex);
194 uint32_t getFreeHwcCacheSlot() REQUIRES(mMutex);
195 void evictLeastRecentlyUsed() REQUIRES(mMutex);
196 void eraseBufferLocked(const client_cache_t& clientCacheId) REQUIRES(mMutex);
197
198 struct CachedBufferHash {
199 std::size_t operator()(const client_cache_t& clientCacheId) const {
200 return std::hash<uint64_t>{}(clientCacheId.id);
201 }
202 };
203
204 std::mutex mMutex;
205
206 std::unordered_map<client_cache_t,
207 std::pair<uint32_t /*HwcCacheSlot*/, uint32_t /*counter*/>,
208 CachedBufferHash>
209 mCachedBuffers GUARDED_BY(mMutex);
210 std::stack<uint32_t /*HwcCacheSlot*/> mFreeHwcCacheSlots GUARDED_BY(mMutex);
211
212 // The cache increments this counter value when a slot is updated or used.
213 // Used to track the least recently-used buffer
214 uint64_t mCounter = 0;
215 };
216
217 sp<HwcSlotGenerator> mHwcSlotGenerator;
Marissa Wall61c58622018-07-18 10:12:20 -0700218};
219
220} // namespace android