blob: fe864de594cad8b90d09cf22991905a425805a87 [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
Marissa Wallfd668622018-05-10 10:21:13 -070045 void onLayerDisplayed(const sp<Fence>& releaseFence) override;
46
Marissa Wallfd668622018-05-10 10:21:13 -070047 std::vector<OccupancyTracker::Segment> getOccupancyHistory(bool forceFlush) override;
48
Marissa Wallfd668622018-05-10 10:21:13 -070049 // If a buffer was replaced this frame, release the former buffer
50 void releasePendingBuffer(nsecs_t dequeueReadyTime) override;
51
52 void setDefaultBufferSize(uint32_t w, uint32_t h) override;
53
54 int32_t getQueuedFrameCount() const override;
55
Ana Krulec010d2192018-10-08 06:29:54 -070056 bool shouldPresentNow(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
Marissa Wallfd668622018-05-10 10:21:13 -070065protected:
chaviw4244e032019-09-04 11:27:49 -070066 void gatherBufferInfo() override;
67
chaviw2d9a4292019-12-02 16:00:24 -080068 // -----------------------------------------------------------------------
69 // Interface implementation for BufferLayerConsumer::ContentsChangedListener
70 // -----------------------------------------------------------------------
71 class ContentsChangedListener : public BufferLayerConsumer::ContentsChangedListener {
72 public:
73 ContentsChangedListener(BufferQueueLayer* bufferQueueLayer)
74 : mBufferQueueLayer(bufferQueueLayer) {}
75 void abandon();
76
77 protected:
78 void onFrameAvailable(const BufferItem& item) override;
79 void onFrameReplaced(const BufferItem& item) override;
80 void onSidebandStreamChanged() override;
81 void onFrameDequeued(const uint64_t bufferId) override;
82 void onFrameDetached(const uint64_t bufferId) override;
83 void onFrameCancelled(const uint64_t bufferId) override;
84
85 private:
86 BufferQueueLayer* mBufferQueueLayer = nullptr;
87 Mutex mMutex;
88 };
Marissa Wallfd668622018-05-10 10:21:13 -070089
90private:
Peiyong Linf243e5e2020-08-22 17:40:59 -070091 uint64_t getFrameNumber(nsecs_t expectedPresentTime) const override;
92
93 bool getAutoRefresh() const override;
94 bool getSidebandStreamChanged() const override;
95
96 bool latchSidebandStream(bool& recomputeVisibleRegions) override;
97 void setTransformHint(ui::Transform::RotationFlags displayTransformHint) override;
98
99 bool hasFrameUpdate() const override;
100
101 status_t bindTextureImage() override;
102 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;
107
108 sp<Layer> createClone() override;
Marissa Wallfd668622018-05-10 10:21:13 -0700109
110 void onFirstRef() override;
111
Peiyong Linf243e5e2020-08-22 17:40:59 -0700112 void onFrameAvailable(const BufferItem& item);
113 void onFrameReplaced(const BufferItem& item);
114 void onSidebandStreamChanged();
115 void onFrameDequeued(const uint64_t bufferId);
116 void onFrameDetached(const uint64_t bufferId);
117 void onFrameCancelled(const uint64_t bufferId);
118
119 // Temporary - Used only for LEGACY camera mode.
120 uint32_t getProducerStickyTransform() const;
121
Marissa Wallfd668622018-05-10 10:21:13 -0700122 sp<BufferLayerConsumer> mConsumer;
123 sp<IGraphicBufferProducer> mProducer;
124
Lloyd Pique42ab75e2018-09-12 20:46:03 -0700125 bool mUpdateTexImageFailed{false};
Marissa Wallfd668622018-05-10 10:21:13 -0700126
Mikael Pessa2e1608f2019-07-19 11:25:35 -0700127 uint64_t mPreviousBufferId = 0;
128 uint64_t mPreviousReleasedFrameNumber = 0;
129
Marissa Wallfd668622018-05-10 10:21:13 -0700130 // Local copy of the queued contents of the incoming BufferQueue
131 mutable Mutex mQueueItemLock;
132 Condition mQueueItemCondition;
Adithya Srinivasan5f683cf2020-09-15 14:21:04 -0700133
134 struct BufferData {
135 BufferData(BufferItem item, std::unique_ptr<frametimeline::SurfaceFrame> surfaceFrame)
136 : item(item), surfaceFrame(std::move(surfaceFrame)) {}
137 BufferItem item;
138 std::unique_ptr<frametimeline::SurfaceFrame> surfaceFrame;
139 };
140 std::vector<BufferData> mQueueItems;
Lloyd Pique42ab75e2018-09-12 20:46:03 -0700141 std::atomic<uint64_t> mLastFrameNumberReceived{0};
Marissa Wallfd668622018-05-10 10:21:13 -0700142
Lloyd Pique42ab75e2018-09-12 20:46:03 -0700143 bool mAutoRefresh{false};
Marissa Wallfd668622018-05-10 10:21:13 -0700144
145 // thread-safe
Lloyd Piquef1c675b2018-09-12 20:45:39 -0700146 std::atomic<int32_t> mQueuedFrames{0};
147 std::atomic<bool> mSidebandStreamChanged{false};
chaviw2d9a4292019-12-02 16:00:24 -0800148
149 sp<ContentsChangedListener> mContentsChangedListener;
Marissa Wallfd668622018-05-10 10:21:13 -0700150};
151
152} // namespace android