BufferStateLayer.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. #pragma once
  17. #include "BufferLayer.h"
  18. #include "Layer.h"
  19. #include <gui/GLConsumer.h>
  20. #include <renderengine/Image.h>
  21. #include <renderengine/RenderEngine.h>
  22. #include <system/window.h>
  23. #include <utils/String8.h>
  24. #include <stack>
  25. namespace android {
  26. class SlotGenerationTest;
  27. class BufferStateLayer : public BufferLayer {
  28. public:
  29. explicit BufferStateLayer(const LayerCreationArgs&);
  30. ~BufferStateLayer() override;
  31. // -----------------------------------------------------------------------
  32. // Interface implementation for Layer
  33. // -----------------------------------------------------------------------
  34. void onLayerDisplayed(const sp<Fence>& releaseFence) override;
  35. void setTransformHint(uint32_t orientation) const override;
  36. void releasePendingBuffer(nsecs_t dequeueReadyTime) override;
  37. bool shouldPresentNow(nsecs_t expectedPresentTime) const override;
  38. bool getTransformToDisplayInverse() const override;
  39. uint32_t doTransactionResize(uint32_t flags, Layer::State* /*stateToCommit*/) override {
  40. return flags;
  41. }
  42. void pushPendingState() override;
  43. bool applyPendingStates(Layer::State* stateToCommit) override;
  44. uint32_t getActiveWidth(const Layer::State& s) const override { return s.active.w; }
  45. uint32_t getActiveHeight(const Layer::State& s) const override { return s.active.h; }
  46. ui::Transform getActiveTransform(const Layer::State& s) const override {
  47. return s.active.transform;
  48. }
  49. Region getActiveTransparentRegion(const Layer::State& s) const override {
  50. return s.transparentRegionHint;
  51. }
  52. Rect getCrop(const Layer::State& s) const;
  53. bool setTransform(uint32_t transform) override;
  54. bool setTransformToDisplayInverse(bool transformToDisplayInverse) override;
  55. bool setCrop(const Rect& crop) override;
  56. bool setFrame(const Rect& frame) override;
  57. bool setBuffer(const sp<GraphicBuffer>& buffer, nsecs_t postTime, nsecs_t desiredPresentTime,
  58. const client_cache_t& clientCacheId) override;
  59. bool setAcquireFence(const sp<Fence>& fence) override;
  60. bool setDataspace(ui::Dataspace dataspace) override;
  61. bool setHdrMetadata(const HdrMetadata& hdrMetadata) override;
  62. bool setSurfaceDamageRegion(const Region& surfaceDamage) override;
  63. bool setApi(int32_t api) override;
  64. bool setSidebandStream(const sp<NativeHandle>& sidebandStream) override;
  65. bool setTransactionCompletedListeners(const std::vector<sp<CallbackHandle>>& handles) override;
  66. // Override to ignore legacy layer state properties that are not used by BufferStateLayer
  67. bool setSize(uint32_t /*w*/, uint32_t /*h*/) override { return false; }
  68. bool setPosition(float /*x*/, float /*y*/, bool /*immediate*/) override { return false; }
  69. bool setTransparentRegionHint(const Region& transparent) override;
  70. bool setMatrix(const layer_state_t::matrix22_t& /*matrix*/,
  71. bool /*allowNonRectPreservingTransforms*/) override {
  72. return false;
  73. }
  74. bool setCrop_legacy(const Rect& /*crop*/, bool /*immediate*/) override { return false; }
  75. bool setOverrideScalingMode(int32_t /*overrideScalingMode*/) override { return false; }
  76. void deferTransactionUntil_legacy(const sp<IBinder>& /*barrierHandle*/,
  77. uint64_t /*frameNumber*/) override {}
  78. void deferTransactionUntil_legacy(const sp<Layer>& /*barrierLayer*/,
  79. uint64_t /*frameNumber*/) override {}
  80. Rect getBufferSize(const State& s) const override;
  81. FloatRect computeSourceBounds(const FloatRect& parentBounds) const override;
  82. // -----------------------------------------------------------------------
  83. // -----------------------------------------------------------------------
  84. // Interface implementation for BufferLayer
  85. // -----------------------------------------------------------------------
  86. bool fenceHasSignaled() const override;
  87. bool framePresentTimeIsCurrent() const override;
  88. private:
  89. nsecs_t getDesiredPresentTime() override;
  90. std::shared_ptr<FenceTime> getCurrentFenceTime() const override;
  91. void getDrawingTransformMatrix(float *matrix) override;
  92. uint32_t getDrawingTransform() const override;
  93. ui::Dataspace getDrawingDataSpace() const override;
  94. Rect getDrawingCrop() const override;
  95. uint32_t getDrawingScalingMode() const override;
  96. Region getDrawingSurfaceDamage() const override;
  97. const HdrMetadata& getDrawingHdrMetadata() const override;
  98. int getDrawingApi() const override;
  99. PixelFormat getPixelFormat() const override;
  100. uint64_t getFrameNumber() const override;
  101. bool getAutoRefresh() const override;
  102. bool getSidebandStreamChanged() const override;
  103. bool latchSidebandStream(bool& recomputeVisibleRegions) override;
  104. bool hasFrameUpdate() const override;
  105. void setFilteringEnabled(bool enabled) override;
  106. status_t bindTextureImage() override;
  107. status_t updateTexImage(bool& recomputeVisibleRegions, nsecs_t latchTime) override;
  108. status_t updateActiveBuffer() override;
  109. status_t updateFrameNumber(nsecs_t latchTime) override;
  110. void setHwcLayerBuffer(const sp<const DisplayDevice>& display) override;
  111. private:
  112. friend class SlotGenerationTest;
  113. void onFirstRef() override;
  114. bool willPresentCurrentTransaction() const;
  115. static const std::array<float, 16> IDENTITY_MATRIX;
  116. std::unique_ptr<renderengine::Image> mTextureImage;
  117. std::array<float, 16> mTransformMatrix{IDENTITY_MATRIX};
  118. std::atomic<bool> mSidebandStreamChanged{false};
  119. uint32_t mFrameNumber{0};
  120. sp<Fence> mPreviousReleaseFence;
  121. bool mCurrentStateModified = false;
  122. bool mReleasePreviousBuffer = false;
  123. nsecs_t mCallbackHandleAcquireTime = -1;
  124. nsecs_t mDesiredPresentTime = -1;
  125. // TODO(marissaw): support sticky transform for LEGACY camera mode
  126. class HwcSlotGenerator : public ClientCache::ErasedRecipient {
  127. public:
  128. HwcSlotGenerator() {
  129. for (uint32_t i = 0; i < BufferQueue::NUM_BUFFER_SLOTS; i++) {
  130. mFreeHwcCacheSlots.push(i);
  131. }
  132. }
  133. void bufferErased(const client_cache_t& clientCacheId);
  134. uint32_t getHwcCacheSlot(const client_cache_t& clientCacheId);
  135. private:
  136. friend class SlotGenerationTest;
  137. uint32_t addCachedBuffer(const client_cache_t& clientCacheId) REQUIRES(mMutex);
  138. uint32_t getFreeHwcCacheSlot() REQUIRES(mMutex);
  139. void evictLeastRecentlyUsed() REQUIRES(mMutex);
  140. void eraseBufferLocked(const client_cache_t& clientCacheId) REQUIRES(mMutex);
  141. struct CachedBufferHash {
  142. std::size_t operator()(const client_cache_t& clientCacheId) const {
  143. return std::hash<uint64_t>{}(clientCacheId.id);
  144. }
  145. };
  146. std::mutex mMutex;
  147. std::unordered_map<client_cache_t,
  148. std::pair<uint32_t /*HwcCacheSlot*/, uint32_t /*counter*/>,
  149. CachedBufferHash>
  150. mCachedBuffers GUARDED_BY(mMutex);
  151. std::stack<uint32_t /*HwcCacheSlot*/> mFreeHwcCacheSlots GUARDED_BY(mMutex);
  152. // The cache increments this counter value when a slot is updated or used.
  153. // Used to track the least recently-used buffer
  154. uint64_t mCounter = 0;
  155. };
  156. sp<HwcSlotGenerator> mHwcSlotGenerator;
  157. };
  158. } // namespace android