blob: ea832a2078ddc0215ab9e8a4ed89681c1ad128cb [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 uint32_t doTransactionResize(uint32_t flags, Layer::State* /*stateToCommit*/) override {
51 return flags;
52 }
Valerie Hau3282b3c2020-02-03 15:37:27 -080053 /*TODO:vhau return to using BufferStateLayer override once WM
54 * has removed deferred transactions!
55 void pushPendingState() override;*/
Lloyd Pique0449b0f2018-12-20 16:23:45 -080056 bool applyPendingStates(Layer::State* stateToCommit) override;
Marissa Wall61c58622018-07-18 10:12:20 -070057
58 uint32_t getActiveWidth(const Layer::State& s) const override { return s.active.w; }
59 uint32_t getActiveHeight(const Layer::State& s) const override { return s.active.h; }
Peiyong Linefefaac2018-08-17 12:27:51 -070060 ui::Transform getActiveTransform(const Layer::State& s) const override {
Marissa Wall61c58622018-07-18 10:12:20 -070061 return s.active.transform;
62 }
63 Region getActiveTransparentRegion(const Layer::State& s) const override {
64 return s.transparentRegionHint;
65 }
66 Rect getCrop(const Layer::State& s) const;
Marissa Wall61c58622018-07-18 10:12:20 -070067
Lloyd Pique0449b0f2018-12-20 16:23:45 -080068 bool setTransform(uint32_t transform) override;
69 bool setTransformToDisplayInverse(bool transformToDisplayInverse) override;
70 bool setCrop(const Rect& crop) override;
71 bool setFrame(const Rect& frame) override;
Valerie Haubf784642020-01-29 07:25:23 -080072 bool setBuffer(const sp<GraphicBuffer>& buffer, const sp<Fence>& acquireFence, nsecs_t postTime,
Ady Abrahamf0c56492020-12-17 18:04:15 -080073 nsecs_t desiredPresentTime, bool isAutoTimestamp,
Vishnu Nairadf632b2021-01-07 14:05:08 -080074 const client_cache_t& clientCacheId, uint64_t frameNumber,
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +000075 std::optional<nsecs_t> dequeueTime, const FrameTimelineInfo& info) override;
Lloyd Pique0449b0f2018-12-20 16:23:45 -080076 bool setAcquireFence(const sp<Fence>& fence) override;
77 bool setDataspace(ui::Dataspace dataspace) override;
78 bool setHdrMetadata(const HdrMetadata& hdrMetadata) override;
79 bool setSurfaceDamageRegion(const Region& surfaceDamage) override;
80 bool setApi(int32_t api) override;
81 bool setSidebandStream(const sp<NativeHandle>& sidebandStream) override;
82 bool setTransactionCompletedListeners(const std::vector<sp<CallbackHandle>>& handles) override;
Valerie Hau871d6352020-01-29 08:44:02 -080083 bool addFrameEvent(const sp<Fence>& acquireFence, nsecs_t postedTime,
84 nsecs_t requestedPresentTime) override;
Marissa Wall61c58622018-07-18 10:12:20 -070085
Marissa Wall61c58622018-07-18 10:12:20 -070086 // Override to ignore legacy layer state properties that are not used by BufferStateLayer
Marissa Wall861616d2018-10-22 12:52:23 -070087 bool setSize(uint32_t /*w*/, uint32_t /*h*/) override { return false; }
chaviw214c89d2019-09-04 16:03:53 -070088 bool setPosition(float /*x*/, float /*y*/) override { return false; }
Marissa Wall861616d2018-10-22 12:52:23 -070089 bool setTransparentRegionHint(const Region& transparent) override;
90 bool setMatrix(const layer_state_t::matrix22_t& /*matrix*/,
91 bool /*allowNonRectPreservingTransforms*/) override {
92 return false;
93 }
chaviw214c89d2019-09-04 16:03:53 -070094 bool setCrop_legacy(const Rect& /*crop*/) override { return false; }
Marissa Wall61c58622018-07-18 10:12:20 -070095 void deferTransactionUntil_legacy(const sp<IBinder>& /*barrierHandle*/,
96 uint64_t /*frameNumber*/) override {}
97 void deferTransactionUntil_legacy(const sp<Layer>& /*barrierLayer*/,
98 uint64_t /*frameNumber*/) override {}
Marissa Wall861616d2018-10-22 12:52:23 -070099
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800100 Rect getBufferSize(const State& s) const override;
Vishnu Nair4351ad52019-02-11 14:13:02 -0800101 FloatRect computeSourceBounds(const FloatRect& parentBounds) const override;
Valerie Hau92bf5482020-02-10 09:49:08 -0800102 Layer::RoundedCornerState getRoundedCornerState() const override;
Vishnu Naircf26a0a2020-11-13 12:56:20 -0800103 void setAutoRefresh(bool autoRefresh) override;
Valerie Haubc6ddb12019-03-08 11:10:15 -0800104
Marissa Wall61c58622018-07-18 10:12:20 -0700105 // -----------------------------------------------------------------------
106
107 // -----------------------------------------------------------------------
108 // Interface implementation for BufferLayer
109 // -----------------------------------------------------------------------
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800110 bool fenceHasSignaled() const override;
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700111 bool framePresentTimeIsCurrent(nsecs_t expectedPresentTime) const override;
Valerie Hau871d6352020-01-29 08:44:02 -0800112 bool onPreComposition(nsecs_t refreshStartTime) override;
Robert Carr916b0362020-10-06 13:53:03 -0700113 uint32_t getEffectiveScalingMode() const override;
Marissa Wall61c58622018-07-18 10:12:20 -0700114
Robert Carr7121caf2020-12-15 13:07:32 -0800115 // See mPendingBufferTransactions
116 void incrementPendingBufferCount() override;
117 void decrementPendingBufferCount();
118 uint32_t doTransaction(uint32_t flags) override;
119
chaviw4244e032019-09-04 11:27:49 -0700120protected:
121 void gatherBufferInfo() override;
Robert Carrfe1209c2020-02-11 12:25:35 -0800122 uint64_t getHeadFrameNumber(nsecs_t expectedPresentTime) const;
Jorim Jaggi9c03b502020-11-24 23:51:31 +0100123 void onSurfaceFrameCreated(const std::shared_ptr<frametimeline::SurfaceFrame>& surfaceFrame);
Marissa Wall61c58622018-07-18 10:12:20 -0700124
chaviw4244e032019-09-04 11:27:49 -0700125private:
Peiyong Linf243e5e2020-08-22 17:40:59 -0700126 friend class SlotGenerationTest;
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +0000127 friend class TransactionSurfaceFrameTest;
128
Robert Carr7121caf2020-12-15 13:07:32 -0800129 inline void tracePendingBufferCount();
Peiyong Linf243e5e2020-08-22 17:40:59 -0700130
Valerie Haubf784642020-01-29 07:25:23 -0800131 bool updateFrameEventHistory(const sp<Fence>& acquireFence, nsecs_t postedTime,
132 nsecs_t requestedPresentTime);
133
Robert Carr8d958532020-11-10 14:09:16 -0800134 status_t addReleaseFence(const sp<CallbackHandle>& ch, const sp<Fence>& releaseFence);
135
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700136 uint64_t getFrameNumber(nsecs_t expectedPresentTime) const override;
Marissa Wall61c58622018-07-18 10:12:20 -0700137
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800138 bool latchSidebandStream(bool& recomputeVisibleRegions) override;
Marissa Wall61c58622018-07-18 10:12:20 -0700139
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800140 bool hasFrameUpdate() const override;
Marissa Wall61c58622018-07-18 10:12:20 -0700141
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700142 status_t updateTexImage(bool& recomputeVisibleRegions, nsecs_t latchTime,
143 nsecs_t expectedPresentTime) override;
Marissa Wall61c58622018-07-18 10:12:20 -0700144
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800145 status_t updateActiveBuffer() override;
Marissa Wall61c58622018-07-18 10:12:20 -0700146 status_t updateFrameNumber(nsecs_t latchTime) override;
147
chaviwb4c6e582019-08-16 14:35:07 -0700148 sp<Layer> createClone() override;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700149
chaviw4244e032019-09-04 11:27:49 -0700150 // Crop that applies to the buffer
151 Rect computeCrop(const State& s);
152
Marissa Walle2ffb422018-10-12 11:33:52 -0700153 bool willPresentCurrentTransaction() const;
Marissa Wall61c58622018-07-18 10:12:20 -0700154
Vishnu Naire7f79c52020-10-29 14:45:03 -0700155 bool bufferNeedsFiltering() const override;
156
Ady Abraham63a3e592021-01-06 10:47:15 -0800157 std::optional<nsecs_t> nextPredictedPresentTime() const override;
Ady Abrahamce4adf12020-12-15 18:45:12 -0800158
Lloyd Pique42ab75e2018-09-12 20:46:03 -0700159 static const std::array<float, 16> IDENTITY_MATRIX;
160
Peiyong Lin833074a2018-08-28 11:53:54 -0700161 std::unique_ptr<renderengine::Image> mTextureImage;
Marissa Wall61c58622018-07-18 10:12:20 -0700162
Valerie Hauda032562020-01-22 12:22:57 -0800163 mutable uint64_t mFrameNumber{0};
Valerie Hau2f54d642020-01-22 09:37:03 -0800164 uint64_t mFrameCounter{0};
Marissa Wall61c58622018-07-18 10:12:20 -0700165
Marissa Wallfda30bb2018-10-12 11:34:28 -0700166 sp<Fence> mPreviousReleaseFence;
Mikael Pessa2e1608f2019-07-19 11:25:35 -0700167 uint64_t mPreviousBufferId = 0;
Mikael Pessa2e1608f2019-07-19 11:25:35 -0700168 uint64_t mPreviousReleasedFrameNumber = 0;
Marissa Wallfda30bb2018-10-12 11:34:28 -0700169
Lloyd Piquef5275482019-01-29 18:42:42 -0800170 mutable bool mCurrentStateModified = false;
Marissa Wallfda30bb2018-10-12 11:34:28 -0700171 bool mReleasePreviousBuffer = false;
172 nsecs_t mCallbackHandleAcquireTime = -1;
Marissa Wall024a1912018-08-13 13:55:35 -0700173
Jorim Jaggi9c03b502020-11-24 23:51:31 +0100174 std::deque<std::shared_ptr<android::frametimeline::SurfaceFrame>> mPendingJankClassifications;
175
Robert Carr7121caf2020-12-15 13:07:32 -0800176 const std::string mBlastTransactionName{"BufferTX - " + mName};
177 // This integer is incremented everytime a buffer arrives at the server for this layer,
178 // and decremented when a buffer is dropped or latched. When changed the integer is exported
179 // to systrace with ATRACE_INT and mBlastTransactionName. This way when debugging perf it is
180 // possible to see when a buffer arrived at the server, and in which frame it latched.
181 //
182 // You can understand the trace this way:
183 // - If the integer increases, a buffer arrived at the server.
184 // - If the integer decreases in latchBuffer, that buffer was latched
185 // - If the integer decreases in setBuffer or doTransaction, a buffer was dropped
186 uint64_t mPendingBufferTransactions{0};
187
Marissa Wall61c58622018-07-18 10:12:20 -0700188 // TODO(marissaw): support sticky transform for LEGACY camera mode
Marissa Wall947d34e2019-03-29 14:03:53 -0700189
190 class HwcSlotGenerator : public ClientCache::ErasedRecipient {
191 public:
192 HwcSlotGenerator() {
193 for (uint32_t i = 0; i < BufferQueue::NUM_BUFFER_SLOTS; i++) {
194 mFreeHwcCacheSlots.push(i);
195 }
196 }
197
198 void bufferErased(const client_cache_t& clientCacheId);
199
200 uint32_t getHwcCacheSlot(const client_cache_t& clientCacheId);
201
202 private:
Valerie Hau64499682019-04-10 11:04:29 -0700203 friend class SlotGenerationTest;
Marissa Wall947d34e2019-03-29 14:03:53 -0700204 uint32_t addCachedBuffer(const client_cache_t& clientCacheId) REQUIRES(mMutex);
205 uint32_t getFreeHwcCacheSlot() REQUIRES(mMutex);
206 void evictLeastRecentlyUsed() REQUIRES(mMutex);
207 void eraseBufferLocked(const client_cache_t& clientCacheId) REQUIRES(mMutex);
208
209 struct CachedBufferHash {
210 std::size_t operator()(const client_cache_t& clientCacheId) const {
211 return std::hash<uint64_t>{}(clientCacheId.id);
212 }
213 };
214
215 std::mutex mMutex;
216
217 std::unordered_map<client_cache_t,
218 std::pair<uint32_t /*HwcCacheSlot*/, uint32_t /*counter*/>,
219 CachedBufferHash>
220 mCachedBuffers GUARDED_BY(mMutex);
221 std::stack<uint32_t /*HwcCacheSlot*/> mFreeHwcCacheSlots GUARDED_BY(mMutex);
222
223 // The cache increments this counter value when a slot is updated or used.
224 // Used to track the least recently-used buffer
225 uint64_t mCounter = 0;
226 };
227
228 sp<HwcSlotGenerator> mHwcSlotGenerator;
Marissa Wall61c58622018-07-18 10:12:20 -0700229};
230
231} // namespace android