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