• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 
19 #include "BufferLayer.h"
20 #include "Layer.h"
21 
22 #include <renderengine/Image.h>
23 #include <renderengine/RenderEngine.h>
24 #include <system/window.h>
25 #include <utils/String8.h>
26 
27 #include <stack>
28 
29 namespace android {
30 
31 class SlotGenerationTest;
32 
33 class BufferStateLayer : public BufferLayer {
34 public:
35     explicit BufferStateLayer(const LayerCreationArgs&);
36 
37     ~BufferStateLayer() override;
38 
39     // Implements Layer.
getType()40     const char* getType() const override { return "BufferStateLayer"; }
41 
42     void onLayerDisplayed(ftl::SharedFuture<FenceResult>) override;
43 
44     void releasePendingBuffer(nsecs_t dequeueReadyTime) override;
45 
46     void finalizeFrameEventHistory(const std::shared_ptr<FenceTime>& glDoneFence,
47                                    const CompositorTiming& compositorTiming) override;
48 
isBufferDue(nsecs_t)49     bool isBufferDue(nsecs_t /*expectedPresentTime*/) const override { return true; }
50 
getActiveTransparentRegion(const Layer::State & s)51     Region getActiveTransparentRegion(const Layer::State& s) const override {
52         return s.transparentRegionHint;
53     }
54     Rect getCrop(const Layer::State& s) const;
55 
56     bool setTransform(uint32_t transform) override;
57     bool setTransformToDisplayInverse(bool transformToDisplayInverse) override;
58     bool setCrop(const Rect& crop) override;
59     bool setBuffer(std::shared_ptr<renderengine::ExternalTexture>& /* buffer */,
60                    const BufferData& bufferData, nsecs_t postTime, nsecs_t desiredPresentTime,
61                    bool isAutoTimestamp, std::optional<nsecs_t> dequeueTime,
62                    const FrameTimelineInfo& info) override;
63     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;
68     bool setTransactionCompletedListeners(const std::vector<sp<CallbackHandle>>& handles) override;
69     bool setPosition(float /*x*/, float /*y*/) override;
70     bool setMatrix(const layer_state_t::matrix22_t& /*matrix*/);
71 
72     // Override to ignore legacy layer state properties that are not used by BufferStateLayer
setSize(uint32_t,uint32_t)73     bool setSize(uint32_t /*w*/, uint32_t /*h*/) override { return false; }
74     bool setTransparentRegionHint(const Region& transparent) override;
75 
76     Rect getBufferSize(const State& s) const override;
77     FloatRect computeSourceBounds(const FloatRect& parentBounds) const override;
78     void setAutoRefresh(bool autoRefresh) override;
79 
80     bool setBufferCrop(const Rect& bufferCrop) override;
81     bool setDestinationFrame(const Rect& destinationFrame) override;
82     bool updateGeometry() override;
83 
84     // -----------------------------------------------------------------------
85 
86     // -----------------------------------------------------------------------
87     // Interface implementation for BufferLayer
88     // -----------------------------------------------------------------------
89     bool fenceHasSignaled() const override;
90     bool framePresentTimeIsCurrent(nsecs_t expectedPresentTime) const override;
91     bool onPreComposition(nsecs_t refreshStartTime) override;
92     uint32_t getEffectiveScalingMode() const override;
93 
94     // See mPendingBufferTransactions
95     void decrementPendingBufferCount();
getPendingBufferCounter()96     std::atomic<int32_t>* getPendingBufferCounter() override { return &mPendingBufferTransactions; }
getPendingBufferCounterName()97     std::string getPendingBufferCounterName() override { return mBlastTransactionName; }
98 
shouldPresentNow(nsecs_t)99     bool shouldPresentNow(nsecs_t /*expectedPresentTime*/) const override { return true; }
100 
101 protected:
102     void gatherBufferInfo() override;
103     void onSurfaceFrameCreated(const std::shared_ptr<frametimeline::SurfaceFrame>& surfaceFrame);
104     ui::Transform getInputTransform() const override;
105     Rect getInputBounds() const override;
106 
107 private:
108     friend class SlotGenerationTest;
109     friend class TransactionFrameTracerTest;
110     friend class TransactionSurfaceFrameTest;
111 
112     inline void tracePendingBufferCount(int32_t pendingBuffers);
113 
114     bool updateFrameEventHistory(const sp<Fence>& acquireFence, nsecs_t postedTime,
115                                  nsecs_t requestedPresentTime);
116 
117     bool latchSidebandStream(bool& recomputeVisibleRegions) override;
118 
119     bool hasFrameUpdate() const override;
120 
121     status_t updateTexImage(bool& recomputeVisibleRegions, nsecs_t latchTime,
122                             nsecs_t expectedPresentTime) override;
123 
124     status_t updateActiveBuffer() override;
125     status_t updateFrameNumber() override;
126 
127     sp<Layer> createClone() override;
128 
129     // Crop that applies to the buffer
130     Rect computeBufferCrop(const State& s);
131 
132     bool willPresentCurrentTransaction() const;
133 
134     bool bufferNeedsFiltering() const override;
135 
136     bool simpleBufferUpdate(const layer_state_t& s) const override;
137 
138     ReleaseCallbackId mPreviousReleaseCallbackId = ReleaseCallbackId::INVALID_ID;
139     uint64_t mPreviousReleasedFrameNumber = 0;
140 
141     uint64_t mPreviousBarrierFrameNumber = 0;
142 
143     bool mReleasePreviousBuffer = false;
144 
145     // Stores the last set acquire fence signal time used to populate the callback handle's acquire
146     // time.
147     std::variant<nsecs_t, sp<Fence>> mCallbackHandleAcquireTimeOrFence = -1;
148 
149     std::deque<std::shared_ptr<android::frametimeline::SurfaceFrame>> mPendingJankClassifications;
150     // An upper bound on the number of SurfaceFrames in the pending classifications deque.
151     static constexpr int kPendingClassificationMaxSurfaceFrames = 25;
152 
153     const std::string mBlastTransactionName{"BufferTX - " + mName};
154     // This integer is incremented everytime a buffer arrives at the server for this layer,
155     // and decremented when a buffer is dropped or latched. When changed the integer is exported
156     // to systrace with ATRACE_INT and mBlastTransactionName. This way when debugging perf it is
157     // possible to see when a buffer arrived at the server, and in which frame it latched.
158     //
159     // You can understand the trace this way:
160     //     - If the integer increases, a buffer arrived at the server.
161     //     - If the integer decreases in latchBuffer, that buffer was latched
162     //     - If the integer decreases in setBuffer or doTransaction, a buffer was dropped
163     std::atomic<int32_t> mPendingBufferTransactions{0};
164 
165     // Contains requested position and matrix updates. This will be applied if the client does
166     // not specify a destination frame.
167     ui::Transform mRequestedTransform;
168 
169     // TODO(marissaw): support sticky transform for LEGACY camera mode
170 
171     class HwcSlotGenerator : public ClientCache::ErasedRecipient {
172     public:
HwcSlotGenerator()173         HwcSlotGenerator() {
174             for (int i = 0; i < BufferQueue::NUM_BUFFER_SLOTS; i++) {
175                 mFreeHwcCacheSlots.push(i);
176             }
177         }
178 
179         void bufferErased(const client_cache_t& clientCacheId);
180 
181         int getHwcCacheSlot(const client_cache_t& clientCacheId);
182 
183     private:
184         friend class SlotGenerationTest;
185         int addCachedBuffer(const client_cache_t& clientCacheId) REQUIRES(mMutex);
186         int getFreeHwcCacheSlot() REQUIRES(mMutex);
187         void evictLeastRecentlyUsed() REQUIRES(mMutex);
188         void eraseBufferLocked(const client_cache_t& clientCacheId) REQUIRES(mMutex);
189 
190         struct CachedBufferHash {
operatorCachedBufferHash191             std::size_t operator()(const client_cache_t& clientCacheId) const {
192                 return std::hash<uint64_t>{}(clientCacheId.id);
193             }
194         };
195 
196         std::mutex mMutex;
197 
198         std::unordered_map<client_cache_t, std::pair<int /*HwcCacheSlot*/, uint64_t /*counter*/>,
199                            CachedBufferHash>
200                 mCachedBuffers GUARDED_BY(mMutex);
201         std::stack<int /*HwcCacheSlot*/> mFreeHwcCacheSlots GUARDED_BY(mMutex);
202 
203         // The cache increments this counter value when a slot is updated or used.
204         // Used to track the least recently-used buffer
205         uint64_t mCounter = 0;
206     };
207 
208     sp<HwcSlotGenerator> mHwcSlotGenerator;
209 };
210 
211 } // namespace android
212