blob: cc670087ac080ed94022a97600593e12debb5f39 [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
Alec Mouri4545a8a2019-08-08 20:05:32 -070038 ~BufferStateLayer() override;
39
Marissa Wall61c58622018-07-18 10:12:20 -070040 // -----------------------------------------------------------------------
41 // Interface implementation for Layer
42 // -----------------------------------------------------------------------
chaviw8a01fa42019-08-19 12:39:31 -070043 const char* getType() const override { return "BufferStateLayer"; }
44
Marissa Wall61c58622018-07-18 10:12:20 -070045 void onLayerDisplayed(const sp<Fence>& releaseFence) override;
46 void setTransformHint(uint32_t orientation) const override;
47 void releasePendingBuffer(nsecs_t dequeueReadyTime) override;
48
Ana Krulec010d2192018-10-08 06:29:54 -070049 bool shouldPresentNow(nsecs_t expectedPresentTime) const override;
Marissa Wall61c58622018-07-18 10:12:20 -070050
Lloyd Pique0449b0f2018-12-20 16:23:45 -080051 bool getTransformToDisplayInverse() const override;
Marissa Wall61c58622018-07-18 10:12:20 -070052
53 uint32_t doTransactionResize(uint32_t flags, Layer::State* /*stateToCommit*/) override {
54 return flags;
55 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -080056 void pushPendingState() override;
57 bool applyPendingStates(Layer::State* stateToCommit) override;
Marissa Wall61c58622018-07-18 10:12:20 -070058
59 uint32_t getActiveWidth(const Layer::State& s) const override { return s.active.w; }
60 uint32_t getActiveHeight(const Layer::State& s) const override { return s.active.h; }
Peiyong Linefefaac2018-08-17 12:27:51 -070061 ui::Transform getActiveTransform(const Layer::State& s) const override {
Marissa Wall61c58622018-07-18 10:12:20 -070062 return s.active.transform;
63 }
64 Region getActiveTransparentRegion(const Layer::State& s) const override {
65 return s.transparentRegionHint;
66 }
67 Rect getCrop(const Layer::State& s) const;
Marissa Wall61c58622018-07-18 10:12:20 -070068
Lloyd Pique0449b0f2018-12-20 16:23:45 -080069 bool setTransform(uint32_t transform) override;
70 bool setTransformToDisplayInverse(bool transformToDisplayInverse) override;
71 bool setCrop(const Rect& crop) override;
72 bool setFrame(const Rect& frame) override;
Marissa Wall947d34e2019-03-29 14:03:53 -070073 bool setBuffer(const sp<GraphicBuffer>& buffer, nsecs_t postTime, nsecs_t desiredPresentTime,
74 const client_cache_t& clientCacheId) override;
Lloyd Pique0449b0f2018-12-20 16:23:45 -080075 bool setAcquireFence(const sp<Fence>& fence) override;
76 bool setDataspace(ui::Dataspace dataspace) override;
77 bool setHdrMetadata(const HdrMetadata& hdrMetadata) override;
78 bool setSurfaceDamageRegion(const Region& surfaceDamage) override;
79 bool setApi(int32_t api) override;
80 bool setSidebandStream(const sp<NativeHandle>& sidebandStream) override;
81 bool setTransactionCompletedListeners(const std::vector<sp<CallbackHandle>>& handles) override;
Marissa Wall61c58622018-07-18 10:12:20 -070082
Marissa Wall61c58622018-07-18 10:12:20 -070083 // Override to ignore legacy layer state properties that are not used by BufferStateLayer
Marissa Wall861616d2018-10-22 12:52:23 -070084 bool setSize(uint32_t /*w*/, uint32_t /*h*/) override { return false; }
85 bool setPosition(float /*x*/, float /*y*/, bool /*immediate*/) override { return false; }
86 bool setTransparentRegionHint(const Region& transparent) override;
87 bool setMatrix(const layer_state_t::matrix22_t& /*matrix*/,
88 bool /*allowNonRectPreservingTransforms*/) override {
89 return false;
90 }
91 bool setCrop_legacy(const Rect& /*crop*/, bool /*immediate*/) override { return false; }
92 bool setOverrideScalingMode(int32_t /*overrideScalingMode*/) override { return false; }
Marissa Wall61c58622018-07-18 10:12:20 -070093 void deferTransactionUntil_legacy(const sp<IBinder>& /*barrierHandle*/,
94 uint64_t /*frameNumber*/) override {}
95 void deferTransactionUntil_legacy(const sp<Layer>& /*barrierLayer*/,
96 uint64_t /*frameNumber*/) override {}
Marissa Wall861616d2018-10-22 12:52:23 -070097
Lloyd Pique0449b0f2018-12-20 16:23:45 -080098 Rect getBufferSize(const State& s) const override;
Vishnu Nair4351ad52019-02-11 14:13:02 -080099 FloatRect computeSourceBounds(const FloatRect& parentBounds) const override;
Valerie Haubc6ddb12019-03-08 11:10:15 -0800100
Marissa Wall61c58622018-07-18 10:12:20 -0700101 // -----------------------------------------------------------------------
102
103 // -----------------------------------------------------------------------
104 // Interface implementation for BufferLayer
105 // -----------------------------------------------------------------------
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800106 bool fenceHasSignaled() const override;
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700107 bool framePresentTimeIsCurrent(nsecs_t expectedPresentTime) const override;
Marissa Wall61c58622018-07-18 10:12:20 -0700108
109private:
110 nsecs_t getDesiredPresentTime() override;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800111 std::shared_ptr<FenceTime> getCurrentFenceTime() const override;
Marissa Wall61c58622018-07-18 10:12:20 -0700112
113 void getDrawingTransformMatrix(float *matrix) override;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800114 uint32_t getDrawingTransform() const override;
115 ui::Dataspace getDrawingDataSpace() const override;
116 Rect getDrawingCrop() const override;
Marissa Wall61c58622018-07-18 10:12:20 -0700117 uint32_t getDrawingScalingMode() const override;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800118 Region getDrawingSurfaceDamage() const override;
119 const HdrMetadata& getDrawingHdrMetadata() const override;
120 int getDrawingApi() const override;
Marissa Wall61c58622018-07-18 10:12:20 -0700121 PixelFormat getPixelFormat() const override;
122
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700123 uint64_t getFrameNumber(nsecs_t expectedPresentTime) const override;
Marissa Wall61c58622018-07-18 10:12:20 -0700124
125 bool getAutoRefresh() const override;
126 bool getSidebandStreamChanged() const override;
127
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800128 bool latchSidebandStream(bool& recomputeVisibleRegions) override;
Marissa Wall61c58622018-07-18 10:12:20 -0700129
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800130 bool hasFrameUpdate() const override;
Marissa Wall61c58622018-07-18 10:12:20 -0700131
132 void setFilteringEnabled(bool enabled) override;
133
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800134 status_t bindTextureImage() override;
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700135 status_t updateTexImage(bool& recomputeVisibleRegions, nsecs_t latchTime,
136 nsecs_t expectedPresentTime) override;
Marissa Wall61c58622018-07-18 10:12:20 -0700137
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800138 status_t updateActiveBuffer() override;
Marissa Wall61c58622018-07-18 10:12:20 -0700139 status_t updateFrameNumber(nsecs_t latchTime) override;
140
Lloyd Piquef5275482019-01-29 18:42:42 -0800141 void latchPerFrameState(compositionengine::LayerFECompositionState&) const override;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700142
Marissa Wall61c58622018-07-18 10:12:20 -0700143private:
Valerie Hau64499682019-04-10 11:04:29 -0700144 friend class SlotGenerationTest;
Marissa Wall61c58622018-07-18 10:12:20 -0700145 void onFirstRef() override;
Marissa Walle2ffb422018-10-12 11:33:52 -0700146 bool willPresentCurrentTransaction() const;
Marissa Wall61c58622018-07-18 10:12:20 -0700147
Lloyd Pique42ab75e2018-09-12 20:46:03 -0700148 static const std::array<float, 16> IDENTITY_MATRIX;
149
Peiyong Lin833074a2018-08-28 11:53:54 -0700150 std::unique_ptr<renderengine::Image> mTextureImage;
Marissa Wall61c58622018-07-18 10:12:20 -0700151
Lloyd Pique42ab75e2018-09-12 20:46:03 -0700152 std::array<float, 16> mTransformMatrix{IDENTITY_MATRIX};
Marissa Wall61c58622018-07-18 10:12:20 -0700153
Lloyd Pique42ab75e2018-09-12 20:46:03 -0700154 std::atomic<bool> mSidebandStreamChanged{false};
Marissa Wall61c58622018-07-18 10:12:20 -0700155
Lloyd Piquef5275482019-01-29 18:42:42 -0800156 mutable uint32_t mFrameNumber{0};
Marissa Wall61c58622018-07-18 10:12:20 -0700157
Marissa Wallfda30bb2018-10-12 11:34:28 -0700158 sp<Fence> mPreviousReleaseFence;
159
Lloyd Piquef5275482019-01-29 18:42:42 -0800160 mutable bool mCurrentStateModified = false;
Marissa Wallfda30bb2018-10-12 11:34:28 -0700161 bool mReleasePreviousBuffer = false;
162 nsecs_t mCallbackHandleAcquireTime = -1;
Marissa Wall024a1912018-08-13 13:55:35 -0700163
Marissa Wall61c58622018-07-18 10:12:20 -0700164 // TODO(marissaw): support sticky transform for LEGACY camera mode
Marissa Wall947d34e2019-03-29 14:03:53 -0700165
166 class HwcSlotGenerator : public ClientCache::ErasedRecipient {
167 public:
168 HwcSlotGenerator() {
169 for (uint32_t i = 0; i < BufferQueue::NUM_BUFFER_SLOTS; i++) {
170 mFreeHwcCacheSlots.push(i);
171 }
172 }
173
174 void bufferErased(const client_cache_t& clientCacheId);
175
176 uint32_t getHwcCacheSlot(const client_cache_t& clientCacheId);
177
178 private:
Valerie Hau64499682019-04-10 11:04:29 -0700179 friend class SlotGenerationTest;
Marissa Wall947d34e2019-03-29 14:03:53 -0700180 uint32_t addCachedBuffer(const client_cache_t& clientCacheId) REQUIRES(mMutex);
181 uint32_t getFreeHwcCacheSlot() REQUIRES(mMutex);
182 void evictLeastRecentlyUsed() REQUIRES(mMutex);
183 void eraseBufferLocked(const client_cache_t& clientCacheId) REQUIRES(mMutex);
184
185 struct CachedBufferHash {
186 std::size_t operator()(const client_cache_t& clientCacheId) const {
187 return std::hash<uint64_t>{}(clientCacheId.id);
188 }
189 };
190
191 std::mutex mMutex;
192
193 std::unordered_map<client_cache_t,
194 std::pair<uint32_t /*HwcCacheSlot*/, uint32_t /*counter*/>,
195 CachedBufferHash>
196 mCachedBuffers GUARDED_BY(mMutex);
197 std::stack<uint32_t /*HwcCacheSlot*/> mFreeHwcCacheSlots GUARDED_BY(mMutex);
198
199 // The cache increments this counter value when a slot is updated or used.
200 // Used to track the least recently-used buffer
201 uint64_t mCounter = 0;
202 };
203
204 sp<HwcSlotGenerator> mHwcSlotGenerator;
Marissa Wall61c58622018-07-18 10:12:20 -0700205};
206
207} // namespace android