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 
38     ~BufferStateLayer() override;
39 
40     // -----------------------------------------------------------------------
41     // Interface implementation for Layer
42     // -----------------------------------------------------------------------
43     void onLayerDisplayed(const sp<Fence>& releaseFence) override;
44     void setTransformHint(uint32_t orientation) const override;
45     void releasePendingBuffer(nsecs_t dequeueReadyTime) override;
46 
47     bool shouldPresentNow(nsecs_t expectedPresentTime) const override;
48 
49     bool getTransformToDisplayInverse() const override;
50 
doTransactionResize(uint32_t flags,Layer::State *)51     uint32_t doTransactionResize(uint32_t flags, Layer::State* /*stateToCommit*/) override {
52         return flags;
53     }
54     void pushPendingState() override;
55     bool applyPendingStates(Layer::State* stateToCommit) override;
56 
getActiveWidth(const Layer::State & s)57     uint32_t getActiveWidth(const Layer::State& s) const override { return s.active.w; }
getActiveHeight(const Layer::State & s)58     uint32_t getActiveHeight(const Layer::State& s) const override { return s.active.h; }
getActiveTransform(const Layer::State & s)59     ui::Transform getActiveTransform(const Layer::State& s) const override {
60         return s.active.transform;
61     }
getActiveTransparentRegion(const Layer::State & s)62     Region getActiveTransparentRegion(const Layer::State& s) const override {
63         return s.transparentRegionHint;
64     }
65     Rect getCrop(const Layer::State& s) const;
66 
67     bool setTransform(uint32_t transform) override;
68     bool setTransformToDisplayInverse(bool transformToDisplayInverse) override;
69     bool setCrop(const Rect& crop) override;
70     bool setFrame(const Rect& frame) override;
71     bool setBuffer(const sp<GraphicBuffer>& buffer, nsecs_t postTime, nsecs_t desiredPresentTime,
72                    const client_cache_t& clientCacheId) override;
73     bool setAcquireFence(const sp<Fence>& fence) override;
74     bool setDataspace(ui::Dataspace dataspace) override;
75     bool setHdrMetadata(const HdrMetadata& hdrMetadata) override;
76     bool setSurfaceDamageRegion(const Region& surfaceDamage) override;
77     bool setApi(int32_t api) override;
78     bool setSidebandStream(const sp<NativeHandle>& sidebandStream) override;
79     bool setTransactionCompletedListeners(const std::vector<sp<CallbackHandle>>& handles) override;
80 
81     // Override to ignore legacy layer state properties that are not used by BufferStateLayer
setSize(uint32_t,uint32_t)82     bool setSize(uint32_t /*w*/, uint32_t /*h*/) override { return false; }
setPosition(float,float,bool)83     bool setPosition(float /*x*/, float /*y*/, bool /*immediate*/) override { return false; }
84     bool setTransparentRegionHint(const Region& transparent) override;
setMatrix(const layer_state_t::matrix22_t &,bool)85     bool setMatrix(const layer_state_t::matrix22_t& /*matrix*/,
86                    bool /*allowNonRectPreservingTransforms*/) override {
87         return false;
88     }
setCrop_legacy(const Rect &,bool)89     bool setCrop_legacy(const Rect& /*crop*/, bool /*immediate*/) override { return false; }
setOverrideScalingMode(int32_t)90     bool setOverrideScalingMode(int32_t /*overrideScalingMode*/) override { return false; }
deferTransactionUntil_legacy(const sp<IBinder> &,uint64_t)91     void deferTransactionUntil_legacy(const sp<IBinder>& /*barrierHandle*/,
92                                       uint64_t /*frameNumber*/) override {}
deferTransactionUntil_legacy(const sp<Layer> &,uint64_t)93     void deferTransactionUntil_legacy(const sp<Layer>& /*barrierLayer*/,
94                                       uint64_t /*frameNumber*/) override {}
95 
96     Rect getBufferSize(const State& s) const override;
97     FloatRect computeSourceBounds(const FloatRect& parentBounds) const override;
98 
99     // -----------------------------------------------------------------------
100 
101     // -----------------------------------------------------------------------
102     // Interface implementation for BufferLayer
103     // -----------------------------------------------------------------------
104     bool fenceHasSignaled() const override;
105     bool framePresentTimeIsCurrent() const override;
106 
107 private:
108     nsecs_t getDesiredPresentTime() override;
109     std::shared_ptr<FenceTime> getCurrentFenceTime() const override;
110 
111     void getDrawingTransformMatrix(float *matrix) override;
112     uint32_t getDrawingTransform() const override;
113     ui::Dataspace getDrawingDataSpace() const override;
114     Rect getDrawingCrop() const override;
115     uint32_t getDrawingScalingMode() const override;
116     Region getDrawingSurfaceDamage() const override;
117     const HdrMetadata& getDrawingHdrMetadata() const override;
118     int getDrawingApi() 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