blob: 12f0141d5cc453d2b9aa1aad7d30360f54e7013b [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
32class BufferStateLayer : public BufferLayer {
33public:
Lloyd Pique42ab75e2018-09-12 20:46:03 -070034 explicit BufferStateLayer(const LayerCreationArgs&);
35 ~BufferStateLayer() override;
Marissa Wall61c58622018-07-18 10:12:20 -070036
37 // -----------------------------------------------------------------------
38 // Interface implementation for Layer
39 // -----------------------------------------------------------------------
40 void onLayerDisplayed(const sp<Fence>& releaseFence) override;
41 void setTransformHint(uint32_t orientation) const override;
42 void releasePendingBuffer(nsecs_t dequeueReadyTime) override;
43
Ana Krulec010d2192018-10-08 06:29:54 -070044 bool shouldPresentNow(nsecs_t expectedPresentTime) const override;
Marissa Wall61c58622018-07-18 10:12:20 -070045
Lloyd Pique0449b0f2018-12-20 16:23:45 -080046 bool getTransformToDisplayInverse() const override;
Marissa Wall61c58622018-07-18 10:12:20 -070047
48 uint32_t doTransactionResize(uint32_t flags, Layer::State* /*stateToCommit*/) override {
49 return flags;
50 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -080051 void pushPendingState() override;
52 bool applyPendingStates(Layer::State* stateToCommit) override;
Marissa Wall61c58622018-07-18 10:12:20 -070053
54 uint32_t getActiveWidth(const Layer::State& s) const override { return s.active.w; }
55 uint32_t getActiveHeight(const Layer::State& s) const override { return s.active.h; }
Peiyong Linefefaac2018-08-17 12:27:51 -070056 ui::Transform getActiveTransform(const Layer::State& s) const override {
Marissa Wall61c58622018-07-18 10:12:20 -070057 return s.active.transform;
58 }
59 Region getActiveTransparentRegion(const Layer::State& s) const override {
60 return s.transparentRegionHint;
61 }
62 Rect getCrop(const Layer::State& s) const;
Marissa Wall61c58622018-07-18 10:12:20 -070063
Lloyd Pique0449b0f2018-12-20 16:23:45 -080064 bool setTransform(uint32_t transform) override;
65 bool setTransformToDisplayInverse(bool transformToDisplayInverse) override;
66 bool setCrop(const Rect& crop) override;
67 bool setFrame(const Rect& frame) override;
Marissa Wall947d34e2019-03-29 14:03:53 -070068 bool setBuffer(const sp<GraphicBuffer>& buffer, nsecs_t postTime, nsecs_t desiredPresentTime,
69 const client_cache_t& clientCacheId) override;
Lloyd Pique0449b0f2018-12-20 16:23:45 -080070 bool setAcquireFence(const sp<Fence>& fence) override;
71 bool setDataspace(ui::Dataspace dataspace) override;
72 bool setHdrMetadata(const HdrMetadata& hdrMetadata) override;
73 bool setSurfaceDamageRegion(const Region& surfaceDamage) override;
74 bool setApi(int32_t api) override;
75 bool setSidebandStream(const sp<NativeHandle>& sidebandStream) override;
76 bool setTransactionCompletedListeners(const std::vector<sp<CallbackHandle>>& handles) override;
Marissa Wall61c58622018-07-18 10:12:20 -070077
Marissa Wall61c58622018-07-18 10:12:20 -070078 // Override to ignore legacy layer state properties that are not used by BufferStateLayer
Marissa Wall861616d2018-10-22 12:52:23 -070079 bool setSize(uint32_t /*w*/, uint32_t /*h*/) override { return false; }
80 bool setPosition(float /*x*/, float /*y*/, bool /*immediate*/) override { return false; }
81 bool setTransparentRegionHint(const Region& transparent) override;
82 bool setMatrix(const layer_state_t::matrix22_t& /*matrix*/,
83 bool /*allowNonRectPreservingTransforms*/) override {
84 return false;
85 }
86 bool setCrop_legacy(const Rect& /*crop*/, bool /*immediate*/) override { return false; }
87 bool setOverrideScalingMode(int32_t /*overrideScalingMode*/) override { return false; }
Marissa Wall61c58622018-07-18 10:12:20 -070088 void deferTransactionUntil_legacy(const sp<IBinder>& /*barrierHandle*/,
89 uint64_t /*frameNumber*/) override {}
90 void deferTransactionUntil_legacy(const sp<Layer>& /*barrierLayer*/,
91 uint64_t /*frameNumber*/) override {}
Marissa Wall861616d2018-10-22 12:52:23 -070092
Lloyd Pique0449b0f2018-12-20 16:23:45 -080093 Rect getBufferSize(const State& s) const override;
Vishnu Nair4351ad52019-02-11 14:13:02 -080094 FloatRect computeSourceBounds(const FloatRect& parentBounds) const override;
Valerie Haubc6ddb12019-03-08 11:10:15 -080095
Marissa Wall61c58622018-07-18 10:12:20 -070096 // -----------------------------------------------------------------------
97
98 // -----------------------------------------------------------------------
99 // Interface implementation for BufferLayer
100 // -----------------------------------------------------------------------
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800101 bool fenceHasSignaled() const override;
Marissa Wall61c58622018-07-18 10:12:20 -0700102
103private:
104 nsecs_t getDesiredPresentTime() override;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800105 std::shared_ptr<FenceTime> getCurrentFenceTime() const override;
Marissa Wall61c58622018-07-18 10:12:20 -0700106
107 void getDrawingTransformMatrix(float *matrix) override;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800108 uint32_t getDrawingTransform() const override;
109 ui::Dataspace getDrawingDataSpace() const override;
110 Rect getDrawingCrop() const override;
Marissa Wall61c58622018-07-18 10:12:20 -0700111 uint32_t getDrawingScalingMode() const override;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800112 Region getDrawingSurfaceDamage() const override;
113 const HdrMetadata& getDrawingHdrMetadata() const override;
114 int getDrawingApi() const override;
Marissa Wall61c58622018-07-18 10:12:20 -0700115 PixelFormat getPixelFormat() const override;
116
117 uint64_t getFrameNumber() const override;
118
119 bool getAutoRefresh() const override;
120 bool getSidebandStreamChanged() const override;
121
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800122 bool latchSidebandStream(bool& recomputeVisibleRegions) override;
Marissa Wall61c58622018-07-18 10:12:20 -0700123
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800124 bool hasFrameUpdate() const override;
Marissa Wall61c58622018-07-18 10:12:20 -0700125
126 void setFilteringEnabled(bool enabled) override;
127
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800128 status_t bindTextureImage() override;
Alec Mouri56e538f2019-01-14 15:22:01 -0800129 status_t updateTexImage(bool& recomputeVisibleRegions, nsecs_t latchTime) override;
Marissa Wall61c58622018-07-18 10:12:20 -0700130
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800131 status_t updateActiveBuffer() override;
Marissa Wall61c58622018-07-18 10:12:20 -0700132 status_t updateFrameNumber(nsecs_t latchTime) override;
133
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800134 void setHwcLayerBuffer(const sp<const DisplayDevice>& display) override;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700135
Marissa Wall61c58622018-07-18 10:12:20 -0700136private:
137 void onFirstRef() override;
Marissa Walle2ffb422018-10-12 11:33:52 -0700138 bool willPresentCurrentTransaction() const;
Marissa Wall61c58622018-07-18 10:12:20 -0700139
Lloyd Pique42ab75e2018-09-12 20:46:03 -0700140 static const std::array<float, 16> IDENTITY_MATRIX;
141
Peiyong Lin833074a2018-08-28 11:53:54 -0700142 std::unique_ptr<renderengine::Image> mTextureImage;
Marissa Wall61c58622018-07-18 10:12:20 -0700143
Lloyd Pique42ab75e2018-09-12 20:46:03 -0700144 std::array<float, 16> mTransformMatrix{IDENTITY_MATRIX};
Marissa Wall61c58622018-07-18 10:12:20 -0700145
Lloyd Pique42ab75e2018-09-12 20:46:03 -0700146 std::atomic<bool> mSidebandStreamChanged{false};
Marissa Wall61c58622018-07-18 10:12:20 -0700147
Lloyd Pique42ab75e2018-09-12 20:46:03 -0700148 uint32_t mFrameNumber{0};
Marissa Wall61c58622018-07-18 10:12:20 -0700149
Marissa Wallfda30bb2018-10-12 11:34:28 -0700150 sp<Fence> mPreviousReleaseFence;
151
Marissa Wall024a1912018-08-13 13:55:35 -0700152 bool mCurrentStateModified = false;
Marissa Wallfda30bb2018-10-12 11:34:28 -0700153 bool mReleasePreviousBuffer = false;
154 nsecs_t mCallbackHandleAcquireTime = -1;
Marissa Wall024a1912018-08-13 13:55:35 -0700155
Valerie Haubc6ddb12019-03-08 11:10:15 -0800156 nsecs_t mDesiredPresentTime = -1;
157
Marissa Wall61c58622018-07-18 10:12:20 -0700158 // TODO(marissaw): support sticky transform for LEGACY camera mode
Marissa Wall947d34e2019-03-29 14:03:53 -0700159
160 class HwcSlotGenerator : public ClientCache::ErasedRecipient {
161 public:
162 HwcSlotGenerator() {
163 for (uint32_t i = 0; i < BufferQueue::NUM_BUFFER_SLOTS; i++) {
164 mFreeHwcCacheSlots.push(i);
165 }
166 }
167
168 void bufferErased(const client_cache_t& clientCacheId);
169
170 uint32_t getHwcCacheSlot(const client_cache_t& clientCacheId);
171
172 private:
173 uint32_t addCachedBuffer(const client_cache_t& clientCacheId) REQUIRES(mMutex);
174 uint32_t getFreeHwcCacheSlot() REQUIRES(mMutex);
175 void evictLeastRecentlyUsed() REQUIRES(mMutex);
176 void eraseBufferLocked(const client_cache_t& clientCacheId) REQUIRES(mMutex);
177
178 struct CachedBufferHash {
179 std::size_t operator()(const client_cache_t& clientCacheId) const {
180 return std::hash<uint64_t>{}(clientCacheId.id);
181 }
182 };
183
184 std::mutex mMutex;
185
186 std::unordered_map<client_cache_t,
187 std::pair<uint32_t /*HwcCacheSlot*/, uint32_t /*counter*/>,
188 CachedBufferHash>
189 mCachedBuffers GUARDED_BY(mMutex);
190 std::stack<uint32_t /*HwcCacheSlot*/> mFreeHwcCacheSlots GUARDED_BY(mMutex);
191
192 // The cache increments this counter value when a slot is updated or used.
193 // Used to track the least recently-used buffer
194 uint64_t mCounter = 0;
195 };
196
197 sp<HwcSlotGenerator> mHwcSlotGenerator;
Marissa Wall61c58622018-07-18 10:12:20 -0700198};
199
200} // namespace android