blob: 13186dd343246ef8ebfa12d1c21719465e09d560 [file] [log] [blame]
Marissa Wall61c58622018-07-18 10:12:20 -07001/*
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
Marissa Wall61c58622018-07-18 10:12:20 -070019#include "BufferLayer.h"
20#include "Layer.h"
21
22#include <gui/GLConsumer.h>
Peiyong Lincbc184f2018-08-22 13:24:10 -070023#include <renderengine/Image.h>
24#include <renderengine/RenderEngine.h>
Marissa Wall61c58622018-07-18 10:12:20 -070025#include <system/window.h>
26#include <utils/String8.h>
27
Marissa Wall947d34e2019-03-29 14:03:53 -070028#include <stack>
29
Marissa Wall61c58622018-07-18 10:12:20 -070030namespace android {
31
Valerie Hau64499682019-04-10 11:04:29 -070032class SlotGenerationTest;
33
Marissa Wall61c58622018-07-18 10:12:20 -070034class BufferStateLayer : public BufferLayer {
35public:
Lloyd Pique42ab75e2018-09-12 20:46:03 -070036 explicit BufferStateLayer(const LayerCreationArgs&);
37 ~BufferStateLayer() override;
Marissa Wall61c58622018-07-18 10:12:20 -070038
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
Ana Krulec010d2192018-10-08 06:29:54 -070046 bool shouldPresentNow(nsecs_t expectedPresentTime) const override;
Marissa Wall61c58622018-07-18 10:12:20 -070047
Lloyd Pique0449b0f2018-12-20 16:23:45 -080048 bool getTransformToDisplayInverse() const override;
Marissa Wall61c58622018-07-18 10:12:20 -070049
50 uint32_t doTransactionResize(uint32_t flags, Layer::State* /*stateToCommit*/) override {
51 return flags;
52 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -080053 void pushPendingState() override;
54 bool applyPendingStates(Layer::State* stateToCommit) override;
Marissa Wall61c58622018-07-18 10:12:20 -070055
56 uint32_t getActiveWidth(const Layer::State& s) const override { return s.active.w; }
57 uint32_t getActiveHeight(const Layer::State& s) const override { return s.active.h; }
Peiyong Linefefaac2018-08-17 12:27:51 -070058 ui::Transform getActiveTransform(const Layer::State& s) const override {
Marissa Wall61c58622018-07-18 10:12:20 -070059 return s.active.transform;
60 }
61 Region getActiveTransparentRegion(const Layer::State& s) const override {
62 return s.transparentRegionHint;
63 }
64 Rect getCrop(const Layer::State& s) const;
Marissa Wall61c58622018-07-18 10:12:20 -070065
Lloyd Pique0449b0f2018-12-20 16:23:45 -080066 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;
Marissa Wall947d34e2019-03-29 14:03:53 -070070 bool setBuffer(const sp<GraphicBuffer>& buffer, nsecs_t postTime, nsecs_t desiredPresentTime,
71 const client_cache_t& clientCacheId) override;
Lloyd Pique0449b0f2018-12-20 16:23:45 -080072 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;
Marissa Wall61c58622018-07-18 10:12:20 -070079
Marissa Wall61c58622018-07-18 10:12:20 -070080 // Override to ignore legacy layer state properties that are not used by BufferStateLayer
Marissa Wall861616d2018-10-22 12:52:23 -070081 bool setSize(uint32_t /*w*/, uint32_t /*h*/) override { return false; }
82 bool setPosition(float /*x*/, float /*y*/, bool /*immediate*/) override { return false; }
83 bool setTransparentRegionHint(const Region& transparent) override;
84 bool setMatrix(const layer_state_t::matrix22_t& /*matrix*/,
85 bool /*allowNonRectPreservingTransforms*/) override {
86 return false;
87 }
88 bool setCrop_legacy(const Rect& /*crop*/, bool /*immediate*/) override { return false; }
89 bool setOverrideScalingMode(int32_t /*overrideScalingMode*/) override { return false; }
Marissa Wall61c58622018-07-18 10:12:20 -070090 void deferTransactionUntil_legacy(const sp<IBinder>& /*barrierHandle*/,
91 uint64_t /*frameNumber*/) override {}
92 void deferTransactionUntil_legacy(const sp<Layer>& /*barrierLayer*/,
93 uint64_t /*frameNumber*/) override {}
Marissa Wall861616d2018-10-22 12:52:23 -070094
Lloyd Pique0449b0f2018-12-20 16:23:45 -080095 Rect getBufferSize(const State& s) const override;
Vishnu Nair4351ad52019-02-11 14:13:02 -080096 FloatRect computeSourceBounds(const FloatRect& parentBounds) const override;
Valerie Haubc6ddb12019-03-08 11:10:15 -080097
Marissa Wall61c58622018-07-18 10:12:20 -070098 // -----------------------------------------------------------------------
99
100 // -----------------------------------------------------------------------
101 // Interface implementation for BufferLayer
102 // -----------------------------------------------------------------------
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800103 bool fenceHasSignaled() const override;
Marissa Wall61c58622018-07-18 10:12:20 -0700104
105private:
106 nsecs_t getDesiredPresentTime() override;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800107 std::shared_ptr<FenceTime> getCurrentFenceTime() const override;
Marissa Wall61c58622018-07-18 10:12:20 -0700108
109 void getDrawingTransformMatrix(float *matrix) override;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800110 uint32_t getDrawingTransform() const override;
111 ui::Dataspace getDrawingDataSpace() const override;
112 Rect getDrawingCrop() const override;
Marissa Wall61c58622018-07-18 10:12:20 -0700113 uint32_t getDrawingScalingMode() const override;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800114 Region getDrawingSurfaceDamage() const override;
115 const HdrMetadata& getDrawingHdrMetadata() const override;
116 int getDrawingApi() const override;
Marissa Wall61c58622018-07-18 10:12:20 -0700117 PixelFormat getPixelFormat() const override;
118
119 uint64_t getFrameNumber() const override;
120
121 bool getAutoRefresh() const override;
122 bool getSidebandStreamChanged() const override;
123
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800124 bool latchSidebandStream(bool& recomputeVisibleRegions) override;
Marissa Wall61c58622018-07-18 10:12:20 -0700125
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800126 bool hasFrameUpdate() const override;
Marissa Wall61c58622018-07-18 10:12:20 -0700127
128 void setFilteringEnabled(bool enabled) override;
129
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800130 status_t bindTextureImage() override;
Alec Mouri56e538f2019-01-14 15:22:01 -0800131 status_t updateTexImage(bool& recomputeVisibleRegions, nsecs_t latchTime) override;
Marissa Wall61c58622018-07-18 10:12:20 -0700132
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800133 status_t updateActiveBuffer() override;
Marissa Wall61c58622018-07-18 10:12:20 -0700134 status_t updateFrameNumber(nsecs_t latchTime) override;
135
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800136 void setHwcLayerBuffer(const sp<const DisplayDevice>& display) override;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700137
Marissa Wall61c58622018-07-18 10:12:20 -0700138private:
Valerie Hau64499682019-04-10 11:04:29 -0700139 friend class SlotGenerationTest;
Marissa Wall61c58622018-07-18 10:12:20 -0700140 void onFirstRef() override;
Marissa Walle2ffb422018-10-12 11:33:52 -0700141 bool willPresentCurrentTransaction() const;
Marissa Wall61c58622018-07-18 10:12:20 -0700142
Lloyd Pique42ab75e2018-09-12 20:46:03 -0700143 static const std::array<float, 16> IDENTITY_MATRIX;
144
Peiyong Lin833074a2018-08-28 11:53:54 -0700145 std::unique_ptr<renderengine::Image> mTextureImage;
Marissa Wall61c58622018-07-18 10:12:20 -0700146
Lloyd Pique42ab75e2018-09-12 20:46:03 -0700147 std::array<float, 16> mTransformMatrix{IDENTITY_MATRIX};
Marissa Wall61c58622018-07-18 10:12:20 -0700148
Lloyd Pique42ab75e2018-09-12 20:46:03 -0700149 std::atomic<bool> mSidebandStreamChanged{false};
Marissa Wall61c58622018-07-18 10:12:20 -0700150
Lloyd Pique42ab75e2018-09-12 20:46:03 -0700151 uint32_t mFrameNumber{0};
Marissa Wall61c58622018-07-18 10:12:20 -0700152
Marissa Wallfda30bb2018-10-12 11:34:28 -0700153 sp<Fence> mPreviousReleaseFence;
154
Marissa Wall024a1912018-08-13 13:55:35 -0700155 bool mCurrentStateModified = false;
Marissa Wallfda30bb2018-10-12 11:34:28 -0700156 bool mReleasePreviousBuffer = false;
157 nsecs_t mCallbackHandleAcquireTime = -1;
Marissa Wall024a1912018-08-13 13:55:35 -0700158
Valerie Haubc6ddb12019-03-08 11:10:15 -0800159 nsecs_t mDesiredPresentTime = -1;
160
Marissa Wall61c58622018-07-18 10:12:20 -0700161 // TODO(marissaw): support sticky transform for LEGACY camera mode
Marissa Wall947d34e2019-03-29 14:03:53 -0700162
163 class HwcSlotGenerator : public ClientCache::ErasedRecipient {
164 public:
165 HwcSlotGenerator() {
166 for (uint32_t i = 0; i < BufferQueue::NUM_BUFFER_SLOTS; i++) {
167 mFreeHwcCacheSlots.push(i);
168 }
169 }
170
171 void bufferErased(const client_cache_t& clientCacheId);
172
173 uint32_t getHwcCacheSlot(const client_cache_t& clientCacheId);
174
175 private:
Valerie Hau64499682019-04-10 11:04:29 -0700176 friend class SlotGenerationTest;
Marissa Wall947d34e2019-03-29 14:03:53 -0700177 uint32_t addCachedBuffer(const client_cache_t& clientCacheId) REQUIRES(mMutex);
178 uint32_t getFreeHwcCacheSlot() REQUIRES(mMutex);
179 void evictLeastRecentlyUsed() REQUIRES(mMutex);
180 void eraseBufferLocked(const client_cache_t& clientCacheId) REQUIRES(mMutex);
181
182 struct CachedBufferHash {
183 std::size_t operator()(const client_cache_t& clientCacheId) const {
184 return std::hash<uint64_t>{}(clientCacheId.id);
185 }
186 };
187
188 std::mutex mMutex;
189
190 std::unordered_map<client_cache_t,
191 std::pair<uint32_t /*HwcCacheSlot*/, uint32_t /*counter*/>,
192 CachedBufferHash>
193 mCachedBuffers GUARDED_BY(mMutex);
194 std::stack<uint32_t /*HwcCacheSlot*/> mFreeHwcCacheSlots GUARDED_BY(mMutex);
195
196 // The cache increments this counter value when a slot is updated or used.
197 // Used to track the least recently-used buffer
198 uint64_t mCounter = 0;
199 };
200
201 sp<HwcSlotGenerator> mHwcSlotGenerator;
Marissa Wall61c58622018-07-18 10:12:20 -0700202};
203
204} // namespace android