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