blob: 18e8cfa481c64eaea00eb93d4edaf99b5e43000a [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&);
Marissa Wall61c58622018-07-18 10:12:20 -070037
38 // -----------------------------------------------------------------------
39 // Interface implementation for Layer
40 // -----------------------------------------------------------------------
41 void onLayerDisplayed(const sp<Fence>& releaseFence) override;
42 void setTransformHint(uint32_t orientation) const override;
43 void releasePendingBuffer(nsecs_t dequeueReadyTime) override;
44
Ana Krulec010d2192018-10-08 06:29:54 -070045 bool shouldPresentNow(nsecs_t expectedPresentTime) const override;
Marissa Wall61c58622018-07-18 10:12:20 -070046
Lloyd Pique0449b0f2018-12-20 16:23:45 -080047 bool getTransformToDisplayInverse() const override;
Marissa Wall61c58622018-07-18 10:12:20 -070048
49 uint32_t doTransactionResize(uint32_t flags, Layer::State* /*stateToCommit*/) override {
50 return flags;
51 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -080052 void pushPendingState() override;
53 bool applyPendingStates(Layer::State* stateToCommit) override;
Marissa Wall61c58622018-07-18 10:12:20 -070054
55 uint32_t getActiveWidth(const Layer::State& s) const override { return s.active.w; }
56 uint32_t getActiveHeight(const Layer::State& s) const override { return s.active.h; }
Peiyong Linefefaac2018-08-17 12:27:51 -070057 ui::Transform getActiveTransform(const Layer::State& s) const override {
Marissa Wall61c58622018-07-18 10:12:20 -070058 return s.active.transform;
59 }
60 Region getActiveTransparentRegion(const Layer::State& s) const override {
61 return s.transparentRegionHint;
62 }
63 Rect getCrop(const Layer::State& s) const;
Marissa Wall61c58622018-07-18 10:12:20 -070064
Lloyd Pique0449b0f2018-12-20 16:23:45 -080065 bool setTransform(uint32_t transform) override;
66 bool setTransformToDisplayInverse(bool transformToDisplayInverse) override;
67 bool setCrop(const Rect& crop) override;
68 bool setFrame(const Rect& frame) override;
Marissa Wall947d34e2019-03-29 14:03:53 -070069 bool setBuffer(const sp<GraphicBuffer>& buffer, nsecs_t postTime, nsecs_t desiredPresentTime,
70 const client_cache_t& clientCacheId) override;
Lloyd Pique0449b0f2018-12-20 16:23:45 -080071 bool setAcquireFence(const sp<Fence>& fence) override;
72 bool setDataspace(ui::Dataspace dataspace) override;
73 bool setHdrMetadata(const HdrMetadata& hdrMetadata) override;
74 bool setSurfaceDamageRegion(const Region& surfaceDamage) override;
75 bool setApi(int32_t api) override;
76 bool setSidebandStream(const sp<NativeHandle>& sidebandStream) override;
77 bool setTransactionCompletedListeners(const std::vector<sp<CallbackHandle>>& handles) override;
Marissa Wall61c58622018-07-18 10:12:20 -070078
Marissa Wall61c58622018-07-18 10:12:20 -070079 // Override to ignore legacy layer state properties that are not used by BufferStateLayer
Marissa Wall861616d2018-10-22 12:52:23 -070080 bool setSize(uint32_t /*w*/, uint32_t /*h*/) override { return false; }
81 bool setPosition(float /*x*/, float /*y*/, bool /*immediate*/) override { return false; }
82 bool setTransparentRegionHint(const Region& transparent) override;
83 bool setMatrix(const layer_state_t::matrix22_t& /*matrix*/,
84 bool /*allowNonRectPreservingTransforms*/) override {
85 return false;
86 }
87 bool setCrop_legacy(const Rect& /*crop*/, bool /*immediate*/) override { return false; }
88 bool setOverrideScalingMode(int32_t /*overrideScalingMode*/) override { return false; }
Marissa Wall61c58622018-07-18 10:12:20 -070089 void deferTransactionUntil_legacy(const sp<IBinder>& /*barrierHandle*/,
90 uint64_t /*frameNumber*/) override {}
91 void deferTransactionUntil_legacy(const sp<Layer>& /*barrierLayer*/,
92 uint64_t /*frameNumber*/) override {}
Marissa Wall861616d2018-10-22 12:52:23 -070093
Lloyd Pique0449b0f2018-12-20 16:23:45 -080094 Rect getBufferSize(const State& s) const override;
Vishnu Nair4351ad52019-02-11 14:13:02 -080095 FloatRect computeSourceBounds(const FloatRect& parentBounds) const override;
Valerie Haubc6ddb12019-03-08 11:10:15 -080096
Marissa Wall61c58622018-07-18 10:12:20 -070097 // -----------------------------------------------------------------------
98
99 // -----------------------------------------------------------------------
100 // Interface implementation for BufferLayer
101 // -----------------------------------------------------------------------
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800102 bool fenceHasSignaled() const override;
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700103 bool framePresentTimeIsCurrent(nsecs_t expectedPresentTime) const override;
Marissa Wall61c58622018-07-18 10:12:20 -0700104
Alec Mouri1c8d7202019-06-01 18:51:35 -0700105 // Inherit from ClientCache::ErasedRecipient
106 void bufferErased(const client_cache_t& clientCacheId) override;
107
Marissa Wall61c58622018-07-18 10:12:20 -0700108private:
109 nsecs_t getDesiredPresentTime() override;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800110 std::shared_ptr<FenceTime> getCurrentFenceTime() const override;
Marissa Wall61c58622018-07-18 10:12:20 -0700111
112 void getDrawingTransformMatrix(float *matrix) override;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800113 uint32_t getDrawingTransform() const override;
114 ui::Dataspace getDrawingDataSpace() const override;
115 Rect getDrawingCrop() const override;
Marissa Wall61c58622018-07-18 10:12:20 -0700116 uint32_t getDrawingScalingMode() const override;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800117 Region getDrawingSurfaceDamage() const override;
118 const HdrMetadata& getDrawingHdrMetadata() const override;
119 int getDrawingApi() const override;
Marissa Wall61c58622018-07-18 10:12:20 -0700120 PixelFormat getPixelFormat() const override;
121
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700122 uint64_t getFrameNumber(nsecs_t expectedPresentTime) const override;
Marissa Wall61c58622018-07-18 10:12:20 -0700123
124 bool getAutoRefresh() const override;
125 bool getSidebandStreamChanged() const override;
126
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800127 bool latchSidebandStream(bool& recomputeVisibleRegions) override;
Marissa Wall61c58622018-07-18 10:12:20 -0700128
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800129 bool hasFrameUpdate() const override;
Marissa Wall61c58622018-07-18 10:12:20 -0700130
131 void setFilteringEnabled(bool enabled) override;
132
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800133 status_t bindTextureImage() override;
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700134 status_t updateTexImage(bool& recomputeVisibleRegions, nsecs_t latchTime,
135 nsecs_t expectedPresentTime) override;
Marissa Wall61c58622018-07-18 10:12:20 -0700136
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800137 status_t updateActiveBuffer() override;
Marissa Wall61c58622018-07-18 10:12:20 -0700138 status_t updateFrameNumber(nsecs_t latchTime) override;
139
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800140 void setHwcLayerBuffer(const sp<const DisplayDevice>& display) override;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700141
Marissa Wall61c58622018-07-18 10:12:20 -0700142private:
Valerie Hau64499682019-04-10 11:04:29 -0700143 friend class SlotGenerationTest;
Marissa Wall61c58622018-07-18 10:12:20 -0700144 void onFirstRef() override;
Marissa Walle2ffb422018-10-12 11:33:52 -0700145 bool willPresentCurrentTransaction() const;
Marissa Wall61c58622018-07-18 10:12:20 -0700146
Lloyd Pique42ab75e2018-09-12 20:46:03 -0700147 static const std::array<float, 16> IDENTITY_MATRIX;
148
Peiyong Lin833074a2018-08-28 11:53:54 -0700149 std::unique_ptr<renderengine::Image> mTextureImage;
Marissa Wall61c58622018-07-18 10:12:20 -0700150
Lloyd Pique42ab75e2018-09-12 20:46:03 -0700151 std::array<float, 16> mTransformMatrix{IDENTITY_MATRIX};
Marissa Wall61c58622018-07-18 10:12:20 -0700152
Lloyd Pique42ab75e2018-09-12 20:46:03 -0700153 std::atomic<bool> mSidebandStreamChanged{false};
Marissa Wall61c58622018-07-18 10:12:20 -0700154
Lloyd Pique42ab75e2018-09-12 20:46:03 -0700155 uint32_t mFrameNumber{0};
Marissa Wall61c58622018-07-18 10:12:20 -0700156
Marissa Wallfda30bb2018-10-12 11:34:28 -0700157 sp<Fence> mPreviousReleaseFence;
158
Marissa Wall024a1912018-08-13 13:55:35 -0700159 bool mCurrentStateModified = false;
Marissa Wallfda30bb2018-10-12 11:34:28 -0700160 bool mReleasePreviousBuffer = false;
161 nsecs_t mCallbackHandleAcquireTime = -1;
Marissa Wall024a1912018-08-13 13:55:35 -0700162
Valerie Haubc6ddb12019-03-08 11:10:15 -0800163 nsecs_t mDesiredPresentTime = -1;
164
Marissa Wall61c58622018-07-18 10:12:20 -0700165 // TODO(marissaw): support sticky transform for LEGACY camera mode
Marissa Wall947d34e2019-03-29 14:03:53 -0700166
167 class HwcSlotGenerator : public ClientCache::ErasedRecipient {
168 public:
169 HwcSlotGenerator() {
170 for (uint32_t i = 0; i < BufferQueue::NUM_BUFFER_SLOTS; i++) {
171 mFreeHwcCacheSlots.push(i);
172 }
173 }
174
175 void bufferErased(const client_cache_t& clientCacheId);
176
177 uint32_t getHwcCacheSlot(const client_cache_t& clientCacheId);
178
179 private:
Valerie Hau64499682019-04-10 11:04:29 -0700180 friend class SlotGenerationTest;
Marissa Wall947d34e2019-03-29 14:03:53 -0700181 uint32_t addCachedBuffer(const client_cache_t& clientCacheId) REQUIRES(mMutex);
182 uint32_t getFreeHwcCacheSlot() REQUIRES(mMutex);
183 void evictLeastRecentlyUsed() REQUIRES(mMutex);
184 void eraseBufferLocked(const client_cache_t& clientCacheId) REQUIRES(mMutex);
185
186 struct CachedBufferHash {
187 std::size_t operator()(const client_cache_t& clientCacheId) const {
188 return std::hash<uint64_t>{}(clientCacheId.id);
189 }
190 };
191
192 std::mutex mMutex;
193
194 std::unordered_map<client_cache_t,
195 std::pair<uint32_t /*HwcCacheSlot*/, uint32_t /*counter*/>,
196 CachedBufferHash>
197 mCachedBuffers GUARDED_BY(mMutex);
198 std::stack<uint32_t /*HwcCacheSlot*/> mFreeHwcCacheSlots GUARDED_BY(mMutex);
199
200 // The cache increments this counter value when a slot is updated or used.
201 // Used to track the least recently-used buffer
202 uint64_t mCounter = 0;
203 };
204
205 sp<HwcSlotGenerator> mHwcSlotGenerator;
Marissa Wall61c58622018-07-18 10:12:20 -0700206};
207
208} // namespace android