blob: 8e22215b4aa7fdae9c0887492e09a25a2c301b4f [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
Peiyong Lincbc184f2018-08-22 13:24:10 -070022#include <renderengine/Image.h>
23#include <renderengine/RenderEngine.h>
Marissa Wall61c58622018-07-18 10:12:20 -070024#include <system/window.h>
25#include <utils/String8.h>
26
Marissa Wall947d34e2019-03-29 14:03:53 -070027#include <stack>
28
Marissa Wall61c58622018-07-18 10:12:20 -070029namespace android {
30
Valerie Hau64499682019-04-10 11:04:29 -070031class SlotGenerationTest;
32
Marissa Wall61c58622018-07-18 10:12:20 -070033class BufferStateLayer : public BufferLayer {
34public:
Lloyd Pique42ab75e2018-09-12 20:46:03 -070035 explicit BufferStateLayer(const LayerCreationArgs&);
Marissa Wall61c58622018-07-18 10:12:20 -070036
Alec Mouri4545a8a2019-08-08 20:05:32 -070037 ~BufferStateLayer() override;
38
Marissa Wall61c58622018-07-18 10:12:20 -070039 // -----------------------------------------------------------------------
40 // Interface implementation for Layer
41 // -----------------------------------------------------------------------
chaviw8a01fa42019-08-19 12:39:31 -070042 const char* getType() const override { return "BufferStateLayer"; }
43
Marissa Wall61c58622018-07-18 10:12:20 -070044 void onLayerDisplayed(const sp<Fence>& releaseFence) override;
45 void setTransformHint(uint32_t orientation) const override;
46 void releasePendingBuffer(nsecs_t dequeueReadyTime) override;
47
Valerie Hau871d6352020-01-29 08:44:02 -080048 void finalizeFrameEventHistory(const std::shared_ptr<FenceTime>& glDoneFence,
49 const CompositorTiming& compositorTiming) override;
50
Ana Krulec010d2192018-10-08 06:29:54 -070051 bool shouldPresentNow(nsecs_t expectedPresentTime) const override;
Marissa Wall61c58622018-07-18 10:12:20 -070052
Marissa Wall61c58622018-07-18 10:12:20 -070053 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
Valerie Hau1acd6962019-10-28 16:35:48 -070069 uint32_t getTransformHint() const { return mTransformHint; }
Lloyd Pique0449b0f2018-12-20 16:23:45 -080070 bool setTransform(uint32_t transform) override;
71 bool setTransformToDisplayInverse(bool transformToDisplayInverse) override;
72 bool setCrop(const Rect& crop) override;
73 bool setFrame(const Rect& frame) override;
Valerie Haubf784642020-01-29 07:25:23 -080074 bool setBuffer(const sp<GraphicBuffer>& buffer, const sp<Fence>& acquireFence, nsecs_t postTime,
75 nsecs_t desiredPresentTime, const client_cache_t& clientCacheId) override;
Lloyd Pique0449b0f2018-12-20 16:23:45 -080076 bool setAcquireFence(const sp<Fence>& fence) override;
77 bool setDataspace(ui::Dataspace dataspace) override;
78 bool setHdrMetadata(const HdrMetadata& hdrMetadata) override;
79 bool setSurfaceDamageRegion(const Region& surfaceDamage) override;
80 bool setApi(int32_t api) override;
81 bool setSidebandStream(const sp<NativeHandle>& sidebandStream) override;
82 bool setTransactionCompletedListeners(const std::vector<sp<CallbackHandle>>& handles) override;
Valerie Hau7618b232020-01-09 16:03:08 -080083 void forceSendCallbacks() override;
Valerie Hau871d6352020-01-29 08:44:02 -080084 bool addFrameEvent(const sp<Fence>& acquireFence, nsecs_t postedTime,
85 nsecs_t requestedPresentTime) override;
Marissa Wall61c58622018-07-18 10:12:20 -070086
Marissa Wall61c58622018-07-18 10:12:20 -070087 // Override to ignore legacy layer state properties that are not used by BufferStateLayer
Marissa Wall861616d2018-10-22 12:52:23 -070088 bool setSize(uint32_t /*w*/, uint32_t /*h*/) override { return false; }
chaviw214c89d2019-09-04 16:03:53 -070089 bool setPosition(float /*x*/, float /*y*/) override { return false; }
Marissa Wall861616d2018-10-22 12:52:23 -070090 bool setTransparentRegionHint(const Region& transparent) override;
91 bool setMatrix(const layer_state_t::matrix22_t& /*matrix*/,
92 bool /*allowNonRectPreservingTransforms*/) override {
93 return false;
94 }
chaviw214c89d2019-09-04 16:03:53 -070095 bool setCrop_legacy(const Rect& /*crop*/) override { return false; }
Marissa Wall861616d2018-10-22 12:52:23 -070096 bool setOverrideScalingMode(int32_t /*overrideScalingMode*/) override { return false; }
Marissa Wall61c58622018-07-18 10:12:20 -070097 void deferTransactionUntil_legacy(const sp<IBinder>& /*barrierHandle*/,
98 uint64_t /*frameNumber*/) override {}
99 void deferTransactionUntil_legacy(const sp<Layer>& /*barrierLayer*/,
100 uint64_t /*frameNumber*/) override {}
Marissa Wall861616d2018-10-22 12:52:23 -0700101
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800102 Rect getBufferSize(const State& s) const override;
Vishnu Nair4351ad52019-02-11 14:13:02 -0800103 FloatRect computeSourceBounds(const FloatRect& parentBounds) const override;
Valerie Haubc6ddb12019-03-08 11:10:15 -0800104
Marissa Wall61c58622018-07-18 10:12:20 -0700105 // -----------------------------------------------------------------------
106
107 // -----------------------------------------------------------------------
108 // Interface implementation for BufferLayer
109 // -----------------------------------------------------------------------
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800110 bool fenceHasSignaled() const override;
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700111 bool framePresentTimeIsCurrent(nsecs_t expectedPresentTime) const override;
Valerie Hau871d6352020-01-29 08:44:02 -0800112 bool onPreComposition(nsecs_t refreshStartTime) override;
Marissa Wall61c58622018-07-18 10:12:20 -0700113
chaviw4244e032019-09-04 11:27:49 -0700114protected:
115 void gatherBufferInfo() override;
Marissa Wall61c58622018-07-18 10:12:20 -0700116
chaviw4244e032019-09-04 11:27:49 -0700117private:
Valerie Haubf784642020-01-29 07:25:23 -0800118 bool updateFrameEventHistory(const sp<Fence>& acquireFence, nsecs_t postedTime,
119 nsecs_t requestedPresentTime);
120
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700121 uint64_t getFrameNumber(nsecs_t expectedPresentTime) const override;
Marissa Wall61c58622018-07-18 10:12:20 -0700122
123 bool getAutoRefresh() const override;
124 bool getSidebandStreamChanged() const override;
125
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800126 bool latchSidebandStream(bool& recomputeVisibleRegions) override;
Marissa Wall61c58622018-07-18 10:12:20 -0700127
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800128 bool hasFrameUpdate() const override;
Marissa Wall61c58622018-07-18 10:12:20 -0700129
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800130 status_t bindTextureImage() override;
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700131 status_t updateTexImage(bool& recomputeVisibleRegions, nsecs_t latchTime,
132 nsecs_t expectedPresentTime) override;
Marissa Wall61c58622018-07-18 10:12:20 -0700133
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800134 status_t updateActiveBuffer() override;
Marissa Wall61c58622018-07-18 10:12:20 -0700135 status_t updateFrameNumber(nsecs_t latchTime) override;
136
Lloyd Piquede196652020-01-22 17:29:58 -0800137 void preparePerFrameCompositionState() override;
chaviwb4c6e582019-08-16 14:35:07 -0700138 sp<Layer> createClone() override;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700139
chaviw4244e032019-09-04 11:27:49 -0700140 // Crop that applies to the buffer
141 Rect computeCrop(const State& s);
142
Marissa Wall61c58622018-07-18 10:12:20 -0700143private:
Valerie Hau64499682019-04-10 11:04:29 -0700144 friend class SlotGenerationTest;
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::atomic<bool> mSidebandStreamChanged{false};
Marissa Wall61c58622018-07-18 10:12:20 -0700152
Valerie Hauda032562020-01-22 12:22:57 -0800153 mutable uint64_t mFrameNumber{0};
Valerie Hau2f54d642020-01-22 09:37:03 -0800154 uint64_t mFrameCounter{0};
Marissa Wall61c58622018-07-18 10:12:20 -0700155
Marissa Wallfda30bb2018-10-12 11:34:28 -0700156 sp<Fence> mPreviousReleaseFence;
Mikael Pessa2e1608f2019-07-19 11:25:35 -0700157 uint64_t mPreviousBufferId = 0;
Mikael Pessa2e1608f2019-07-19 11:25:35 -0700158 uint64_t mPreviousReleasedFrameNumber = 0;
Marissa Wallfda30bb2018-10-12 11:34:28 -0700159
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
Valerie Hau32cdc1f2019-10-21 14:45:54 -0700164 mutable uint32_t mTransformHint = 0;
165
Marissa Wall61c58622018-07-18 10:12:20 -0700166 // TODO(marissaw): support sticky transform for LEGACY camera mode
Marissa Wall947d34e2019-03-29 14:03:53 -0700167
168 class HwcSlotGenerator : public ClientCache::ErasedRecipient {
169 public:
170 HwcSlotGenerator() {
171 for (uint32_t i = 0; i < BufferQueue::NUM_BUFFER_SLOTS; i++) {
172 mFreeHwcCacheSlots.push(i);
173 }
174 }
175
176 void bufferErased(const client_cache_t& clientCacheId);
177
178 uint32_t getHwcCacheSlot(const client_cache_t& clientCacheId);
179
180 private:
Valerie Hau64499682019-04-10 11:04:29 -0700181 friend class SlotGenerationTest;
Marissa Wall947d34e2019-03-29 14:03:53 -0700182 uint32_t addCachedBuffer(const client_cache_t& clientCacheId) REQUIRES(mMutex);
183 uint32_t getFreeHwcCacheSlot() REQUIRES(mMutex);
184 void evictLeastRecentlyUsed() REQUIRES(mMutex);
185 void eraseBufferLocked(const client_cache_t& clientCacheId) REQUIRES(mMutex);
186
187 struct CachedBufferHash {
188 std::size_t operator()(const client_cache_t& clientCacheId) const {
189 return std::hash<uint64_t>{}(clientCacheId.id);
190 }
191 };
192
193 std::mutex mMutex;
194
195 std::unordered_map<client_cache_t,
196 std::pair<uint32_t /*HwcCacheSlot*/, uint32_t /*counter*/>,
197 CachedBufferHash>
198 mCachedBuffers GUARDED_BY(mMutex);
199 std::stack<uint32_t /*HwcCacheSlot*/> mFreeHwcCacheSlots GUARDED_BY(mMutex);
200
201 // The cache increments this counter value when a slot is updated or used.
202 // Used to track the least recently-used buffer
203 uint64_t mCounter = 0;
204 };
205
206 sp<HwcSlotGenerator> mHwcSlotGenerator;
Marissa Wall61c58622018-07-18 10:12:20 -0700207};
208
209} // namespace android