blob: 9bcb63aca2b364d41d554b4ee4c24bcb68f4e884 [file] [log] [blame]
Marissa Wallfd668622018-05-10 10:21:13 -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
19#include "BufferLayer.h"
20
21#include <utils/String8.h>
22
23namespace android {
24
25/*
26 * A new BufferQueue and a new BufferLayerConsumer are created when the
27 * BufferLayer is first referenced.
28 *
29 * This also implements onFrameAvailable(), which notifies SurfaceFlinger
30 * that new data has arrived.
31 */
chaviw2d9a4292019-12-02 16:00:24 -080032class BufferQueueLayer : public BufferLayer {
Marissa Wallfd668622018-05-10 10:21:13 -070033public:
chaviwb4c6e582019-08-16 14:35:07 -070034 // Only call while mStateLock is held
Lloyd Pique42ab75e2018-09-12 20:46:03 -070035 explicit BufferQueueLayer(const LayerCreationArgs&);
36 ~BufferQueueLayer() override;
Marissa Wallfd668622018-05-10 10:21:13 -070037
38 // -----------------------------------------------------------------------
39 // Interface implementation for Layer
40 // -----------------------------------------------------------------------
41public:
chaviw8a01fa42019-08-19 12:39:31 -070042 const char* getType() const override { return "BufferQueueLayer"; }
43
Marissa Wallfd668622018-05-10 10:21:13 -070044 void onLayerDisplayed(const sp<Fence>& releaseFence) override;
45
Vishnu Nair6213bd92020-05-08 17:42:25 -070046 void setTransformHint(ui::Transform::RotationFlags displayTransformHint) const override;
Marissa Wallfd668622018-05-10 10:21:13 -070047
48 std::vector<OccupancyTracker::Segment> getOccupancyHistory(bool forceFlush) override;
49
Marissa Wallfd668622018-05-10 10:21:13 -070050 // If a buffer was replaced this frame, release the former buffer
51 void releasePendingBuffer(nsecs_t dequeueReadyTime) override;
52
53 void setDefaultBufferSize(uint32_t w, uint32_t h) override;
54
55 int32_t getQueuedFrameCount() const override;
56
Ana Krulec010d2192018-10-08 06:29:54 -070057 bool shouldPresentNow(nsecs_t expectedPresentTime) const override;
Steven Thomas3172e202020-01-06 19:25:30 -080058
Marissa Wallfd668622018-05-10 10:21:13 -070059 // -----------------------------------------------------------------------
60
61 // -----------------------------------------------------------------------
62 // Interface implementation for BufferLayer
63 // -----------------------------------------------------------------------
64public:
65 bool fenceHasSignaled() const override;
Dominik Laskowskia8955dd2019-07-10 10:19:09 -070066 bool framePresentTimeIsCurrent(nsecs_t expectedPresentTime) const override;
Marissa Wallfd668622018-05-10 10:21:13 -070067
68private:
Dominik Laskowskia8955dd2019-07-10 10:19:09 -070069 uint64_t getFrameNumber(nsecs_t expectedPresentTime) const override;
Marissa Wallfd668622018-05-10 10:21:13 -070070
71 bool getAutoRefresh() const override;
72 bool getSidebandStreamChanged() const override;
73
Vishnu Nair6194e2e2019-02-06 12:58:39 -080074 bool latchSidebandStream(bool& recomputeVisibleRegions) override;
Marissa Wallfd668622018-05-10 10:21:13 -070075
Lloyd Pique0449b0f2018-12-20 16:23:45 -080076 bool hasFrameUpdate() const override;
Marissa Wallfd668622018-05-10 10:21:13 -070077
Alec Mouri39801c02018-10-10 10:44:47 -070078 status_t bindTextureImage() override;
Dominik Laskowskia8955dd2019-07-10 10:19:09 -070079 status_t updateTexImage(bool& recomputeVisibleRegions, nsecs_t latchTime,
80 nsecs_t expectedPresentTime) override;
Marissa Wallfd668622018-05-10 10:21:13 -070081
Lloyd Pique0449b0f2018-12-20 16:23:45 -080082 status_t updateActiveBuffer() override;
Marissa Wallfd668622018-05-10 10:21:13 -070083 status_t updateFrameNumber(nsecs_t latchTime) override;
84
chaviwb4c6e582019-08-16 14:35:07 -070085 sp<Layer> createClone() override;
Marissa Wallfd668622018-05-10 10:21:13 -070086
chaviw2d9a4292019-12-02 16:00:24 -080087 void onFrameAvailable(const BufferItem& item);
88 void onFrameReplaced(const BufferItem& item);
89 void onSidebandStreamChanged();
90 void onFrameDequeued(const uint64_t bufferId);
91 void onFrameDetached(const uint64_t bufferId);
92 void onFrameCancelled(const uint64_t bufferId);
93
Marissa Wallfd668622018-05-10 10:21:13 -070094protected:
chaviw4244e032019-09-04 11:27:49 -070095 void gatherBufferInfo() override;
96
chaviw2d9a4292019-12-02 16:00:24 -080097 // -----------------------------------------------------------------------
98 // Interface implementation for BufferLayerConsumer::ContentsChangedListener
99 // -----------------------------------------------------------------------
100 class ContentsChangedListener : public BufferLayerConsumer::ContentsChangedListener {
101 public:
102 ContentsChangedListener(BufferQueueLayer* bufferQueueLayer)
103 : mBufferQueueLayer(bufferQueueLayer) {}
104 void abandon();
105
106 protected:
107 void onFrameAvailable(const BufferItem& item) override;
108 void onFrameReplaced(const BufferItem& item) override;
109 void onSidebandStreamChanged() override;
110 void onFrameDequeued(const uint64_t bufferId) override;
111 void onFrameDetached(const uint64_t bufferId) override;
112 void onFrameCancelled(const uint64_t bufferId) override;
113
114 private:
115 BufferQueueLayer* mBufferQueueLayer = nullptr;
116 Mutex mMutex;
117 };
Marissa Wallfd668622018-05-10 10:21:13 -0700118 // -----------------------------------------------------------------------
119
120public:
121 status_t setDefaultBufferProperties(uint32_t w, uint32_t h, PixelFormat format);
122
123 sp<IGraphicBufferProducer> getProducer() const;
124
125private:
126 // Temporary - Used only for LEGACY camera mode.
127 uint32_t getProducerStickyTransform() const;
128
129 void onFirstRef() override;
130
131 sp<BufferLayerConsumer> mConsumer;
132 sp<IGraphicBufferProducer> mProducer;
133
Lloyd Pique42ab75e2018-09-12 20:46:03 -0700134 bool mUpdateTexImageFailed{false};
Marissa Wallfd668622018-05-10 10:21:13 -0700135
Mikael Pessa2e1608f2019-07-19 11:25:35 -0700136 uint64_t mPreviousBufferId = 0;
137 uint64_t mPreviousReleasedFrameNumber = 0;
138
Marissa Wallfd668622018-05-10 10:21:13 -0700139 // Local copy of the queued contents of the incoming BufferQueue
140 mutable Mutex mQueueItemLock;
141 Condition mQueueItemCondition;
142 Vector<BufferItem> mQueueItems;
Lloyd Pique42ab75e2018-09-12 20:46:03 -0700143 std::atomic<uint64_t> mLastFrameNumberReceived{0};
Marissa Wallfd668622018-05-10 10:21:13 -0700144
Lloyd Pique42ab75e2018-09-12 20:46:03 -0700145 bool mAutoRefresh{false};
Marissa Wallfd668622018-05-10 10:21:13 -0700146
147 // thread-safe
Lloyd Piquef1c675b2018-09-12 20:45:39 -0700148 std::atomic<int32_t> mQueuedFrames{0};
149 std::atomic<bool> mSidebandStreamChanged{false};
chaviw2d9a4292019-12-02 16:00:24 -0800150
151 sp<ContentsChangedListener> mContentsChangedListener;
Marissa Wallfd668622018-05-10 10:21:13 -0700152};
153
154} // namespace android