blob: c6e072780676fe7cfa3989e41c8591288c50d6b7 [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
Adithya Srinivasan5f683cf2020-09-15 14:21:04 -070025namespace frametimeline {
26class SurfaceFrame;
27}
28
Marissa Wallfd668622018-05-10 10:21:13 -070029/*
30 * A new BufferQueue and a new BufferLayerConsumer are created when the
31 * BufferLayer is first referenced.
32 *
33 * This also implements onFrameAvailable(), which notifies SurfaceFlinger
34 * that new data has arrived.
35 */
chaviw2d9a4292019-12-02 16:00:24 -080036class BufferQueueLayer : public BufferLayer {
Marissa Wallfd668622018-05-10 10:21:13 -070037public:
chaviwb4c6e582019-08-16 14:35:07 -070038 // Only call while mStateLock is held
Lloyd Pique42ab75e2018-09-12 20:46:03 -070039 explicit BufferQueueLayer(const LayerCreationArgs&);
40 ~BufferQueueLayer() override;
Marissa Wallfd668622018-05-10 10:21:13 -070041
Peiyong Linf243e5e2020-08-22 17:40:59 -070042 // Implements Layer.
chaviw8a01fa42019-08-19 12:39:31 -070043 const char* getType() const override { return "BufferQueueLayer"; }
44
Sally Qi59a9f502021-10-12 18:53:23 +000045 void onLayerDisplayed(
46 std::shared_future<renderengine::RenderEngineResult> futureRenderEngineResult) override;
Marissa Wallfd668622018-05-10 10:21:13 -070047
Marissa Wallfd668622018-05-10 10:21:13 -070048 // If a buffer was replaced this frame, release the former buffer
49 void releasePendingBuffer(nsecs_t dequeueReadyTime) override;
50
51 void setDefaultBufferSize(uint32_t w, uint32_t h) override;
52
53 int32_t getQueuedFrameCount() const override;
54
Ady Abraham63a3e592021-01-06 10:47:15 -080055 // Returns true if the next buffer should be presented at the expected present time
56 bool isBufferDue(nsecs_t expectedPresentTime) const override;
Steven Thomas3172e202020-01-06 19:25:30 -080057
Peiyong Linf243e5e2020-08-22 17:40:59 -070058 // Implements BufferLayer.
Marissa Wallfd668622018-05-10 10:21:13 -070059 bool fenceHasSignaled() const override;
Dominik Laskowskia8955dd2019-07-10 10:19:09 -070060 bool framePresentTimeIsCurrent(nsecs_t expectedPresentTime) const override;
Marissa Wallfd668622018-05-10 10:21:13 -070061
Peiyong Linf243e5e2020-08-22 17:40:59 -070062 status_t setDefaultBufferProperties(uint32_t w, uint32_t h, PixelFormat format);
63 sp<IGraphicBufferProducer> getProducer() const;
chaviw2d9a4292019-12-02 16:00:24 -080064
Vishnu Nair7fb9e5a2021-11-08 12:44:05 -080065 void setSizeForTest(uint32_t w, uint32_t h) {
66 mDrawingState.active_legacy.w = w;
67 mDrawingState.active_legacy.h = h;
68 }
69
Marissa Wallfd668622018-05-10 10:21:13 -070070protected:
chaviw4244e032019-09-04 11:27:49 -070071 void gatherBufferInfo() override;
72
chaviw2d9a4292019-12-02 16:00:24 -080073 // -----------------------------------------------------------------------
74 // Interface implementation for BufferLayerConsumer::ContentsChangedListener
75 // -----------------------------------------------------------------------
76 class ContentsChangedListener : public BufferLayerConsumer::ContentsChangedListener {
77 public:
78 ContentsChangedListener(BufferQueueLayer* bufferQueueLayer)
79 : mBufferQueueLayer(bufferQueueLayer) {}
80 void abandon();
81
82 protected:
83 void onFrameAvailable(const BufferItem& item) override;
84 void onFrameReplaced(const BufferItem& item) override;
85 void onSidebandStreamChanged() override;
86 void onFrameDequeued(const uint64_t bufferId) override;
87 void onFrameDetached(const uint64_t bufferId) override;
88 void onFrameCancelled(const uint64_t bufferId) override;
89
90 private:
91 BufferQueueLayer* mBufferQueueLayer = nullptr;
92 Mutex mMutex;
93 };
Marissa Wallfd668622018-05-10 10:21:13 -070094
95private:
Peiyong Linf243e5e2020-08-22 17:40:59 -070096
Peiyong Linf243e5e2020-08-22 17:40:59 -070097 bool latchSidebandStream(bool& recomputeVisibleRegions) override;
98 void setTransformHint(ui::Transform::RotationFlags displayTransformHint) override;
99
100 bool hasFrameUpdate() const override;
101
Peiyong Linf243e5e2020-08-22 17:40:59 -0700102 status_t updateTexImage(bool& recomputeVisibleRegions, nsecs_t latchTime,
103 nsecs_t expectedPresentTime) override;
104
105 status_t updateActiveBuffer() override;
106 status_t updateFrameNumber(nsecs_t latchTime) override;
Siarhei Vishniakoufc434ac2021-01-13 10:28:00 -1000107 void setFrameTimelineInfoForBuffer(const FrameTimelineInfo& frameTimelineInfo) override;
Peiyong Linf243e5e2020-08-22 17:40:59 -0700108
109 sp<Layer> createClone() override;
Marissa Wallfd668622018-05-10 10:21:13 -0700110
111 void onFirstRef() override;
112
Peiyong Linf243e5e2020-08-22 17:40:59 -0700113 void onFrameAvailable(const BufferItem& item);
114 void onFrameReplaced(const BufferItem& item);
115 void onSidebandStreamChanged();
116 void onFrameDequeued(const uint64_t bufferId);
117 void onFrameDetached(const uint64_t bufferId);
118 void onFrameCancelled(const uint64_t bufferId);
119
120 // Temporary - Used only for LEGACY camera mode.
121 uint32_t getProducerStickyTransform() const;
122
Marissa Wallfd668622018-05-10 10:21:13 -0700123 sp<BufferLayerConsumer> mConsumer;
124 sp<IGraphicBufferProducer> mProducer;
125
Lloyd Pique42ab75e2018-09-12 20:46:03 -0700126 bool mUpdateTexImageFailed{false};
Marissa Wallfd668622018-05-10 10:21:13 -0700127
Mikael Pessa2e1608f2019-07-19 11:25:35 -0700128 uint64_t mPreviousBufferId = 0;
129 uint64_t mPreviousReleasedFrameNumber = 0;
130
Marissa Wallfd668622018-05-10 10:21:13 -0700131 // Local copy of the queued contents of the incoming BufferQueue
132 mutable Mutex mQueueItemLock;
133 Condition mQueueItemCondition;
Adithya Srinivasan5f683cf2020-09-15 14:21:04 -0700134
135 struct BufferData {
Jorim Jaggi9c03b502020-11-24 23:51:31 +0100136 BufferData(BufferItem item, std::shared_ptr<frametimeline::SurfaceFrame> surfaceFrame)
137 : item(item), surfaceFrame(surfaceFrame) {}
Adithya Srinivasan5f683cf2020-09-15 14:21:04 -0700138 BufferItem item;
Jorim Jaggi9c03b502020-11-24 23:51:31 +0100139 std::shared_ptr<frametimeline::SurfaceFrame> surfaceFrame;
Adithya Srinivasan5f683cf2020-09-15 14:21:04 -0700140 };
141 std::vector<BufferData> mQueueItems;
Lloyd Pique42ab75e2018-09-12 20:46:03 -0700142 std::atomic<uint64_t> mLastFrameNumberReceived{0};
Marissa Wallfd668622018-05-10 10:21:13 -0700143
Marissa Wallfd668622018-05-10 10:21:13 -0700144 // thread-safe
Lloyd Piquef1c675b2018-09-12 20:45:39 -0700145 std::atomic<int32_t> mQueuedFrames{0};
chaviw2d9a4292019-12-02 16:00:24 -0800146
147 sp<ContentsChangedListener> mContentsChangedListener;
Ady Abraham22c7b5c2020-09-22 19:33:40 -0700148
Siarhei Vishniakoufc434ac2021-01-13 10:28:00 -1000149 // The last vsync info received on this layer. This will be used when we get
Ady Abraham22c7b5c2020-09-22 19:33:40 -0700150 // a buffer to correlate the buffer with the vsync id. Can only be accessed
151 // with the SF state lock held.
Siarhei Vishniakoufc434ac2021-01-13 10:28:00 -1000152 FrameTimelineInfo mFrameTimelineInfo;
Marissa Wallfd668622018-05-10 10:21:13 -0700153};
154
155} // namespace android