blob: 1ac0453ba63c0712bb506ee3d90c703f63163cd1 [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
Peiyong Linf243e5e2020-08-22 17:40:59 -070038 // Implements Layer.
chaviw8a01fa42019-08-19 12:39:31 -070039 const char* getType() const override { return "BufferQueueLayer"; }
40
Marissa Wallfd668622018-05-10 10:21:13 -070041 void onLayerDisplayed(const sp<Fence>& releaseFence) override;
42
Marissa Wallfd668622018-05-10 10:21:13 -070043 std::vector<OccupancyTracker::Segment> getOccupancyHistory(bool forceFlush) override;
44
Marissa Wallfd668622018-05-10 10:21:13 -070045 // If a buffer was replaced this frame, release the former buffer
46 void releasePendingBuffer(nsecs_t dequeueReadyTime) override;
47
48 void setDefaultBufferSize(uint32_t w, uint32_t h) override;
49
50 int32_t getQueuedFrameCount() const override;
51
Ana Krulec010d2192018-10-08 06:29:54 -070052 bool shouldPresentNow(nsecs_t expectedPresentTime) const override;
Steven Thomas3172e202020-01-06 19:25:30 -080053
Peiyong Linf243e5e2020-08-22 17:40:59 -070054 // Implements BufferLayer.
Marissa Wallfd668622018-05-10 10:21:13 -070055 bool fenceHasSignaled() const override;
Dominik Laskowskia8955dd2019-07-10 10:19:09 -070056 bool framePresentTimeIsCurrent(nsecs_t expectedPresentTime) const override;
Marissa Wallfd668622018-05-10 10:21:13 -070057
Peiyong Linf243e5e2020-08-22 17:40:59 -070058 status_t setDefaultBufferProperties(uint32_t w, uint32_t h, PixelFormat format);
59 sp<IGraphicBufferProducer> getProducer() const;
chaviw2d9a4292019-12-02 16:00:24 -080060
Marissa Wallfd668622018-05-10 10:21:13 -070061protected:
chaviw4244e032019-09-04 11:27:49 -070062 void gatherBufferInfo() override;
63
chaviw2d9a4292019-12-02 16:00:24 -080064 // -----------------------------------------------------------------------
65 // Interface implementation for BufferLayerConsumer::ContentsChangedListener
66 // -----------------------------------------------------------------------
67 class ContentsChangedListener : public BufferLayerConsumer::ContentsChangedListener {
68 public:
69 ContentsChangedListener(BufferQueueLayer* bufferQueueLayer)
70 : mBufferQueueLayer(bufferQueueLayer) {}
71 void abandon();
72
73 protected:
74 void onFrameAvailable(const BufferItem& item) override;
75 void onFrameReplaced(const BufferItem& item) override;
76 void onSidebandStreamChanged() override;
77 void onFrameDequeued(const uint64_t bufferId) override;
78 void onFrameDetached(const uint64_t bufferId) override;
79 void onFrameCancelled(const uint64_t bufferId) override;
80
81 private:
82 BufferQueueLayer* mBufferQueueLayer = nullptr;
83 Mutex mMutex;
84 };
Marissa Wallfd668622018-05-10 10:21:13 -070085
86private:
Peiyong Linf243e5e2020-08-22 17:40:59 -070087 uint64_t getFrameNumber(nsecs_t expectedPresentTime) const override;
88
89 bool getAutoRefresh() const override;
90 bool getSidebandStreamChanged() const override;
91
92 bool latchSidebandStream(bool& recomputeVisibleRegions) override;
93 void setTransformHint(ui::Transform::RotationFlags displayTransformHint) override;
94
95 bool hasFrameUpdate() const override;
96
97 status_t bindTextureImage() override;
98 status_t updateTexImage(bool& recomputeVisibleRegions, nsecs_t latchTime,
99 nsecs_t expectedPresentTime) override;
100
101 status_t updateActiveBuffer() override;
102 status_t updateFrameNumber(nsecs_t latchTime) override;
103
104 sp<Layer> createClone() override;
Marissa Wallfd668622018-05-10 10:21:13 -0700105
106 void onFirstRef() override;
107
Peiyong Linf243e5e2020-08-22 17:40:59 -0700108 void onFrameAvailable(const BufferItem& item);
109 void onFrameReplaced(const BufferItem& item);
110 void onSidebandStreamChanged();
111 void onFrameDequeued(const uint64_t bufferId);
112 void onFrameDetached(const uint64_t bufferId);
113 void onFrameCancelled(const uint64_t bufferId);
114
115 // Temporary - Used only for LEGACY camera mode.
116 uint32_t getProducerStickyTransform() const;
117
Marissa Wallfd668622018-05-10 10:21:13 -0700118 sp<BufferLayerConsumer> mConsumer;
119 sp<IGraphicBufferProducer> mProducer;
120
Lloyd Pique42ab75e2018-09-12 20:46:03 -0700121 bool mUpdateTexImageFailed{false};
Marissa Wallfd668622018-05-10 10:21:13 -0700122
Mikael Pessa2e1608f2019-07-19 11:25:35 -0700123 uint64_t mPreviousBufferId = 0;
124 uint64_t mPreviousReleasedFrameNumber = 0;
125
Marissa Wallfd668622018-05-10 10:21:13 -0700126 // Local copy of the queued contents of the incoming BufferQueue
127 mutable Mutex mQueueItemLock;
128 Condition mQueueItemCondition;
129 Vector<BufferItem> mQueueItems;
Lloyd Pique42ab75e2018-09-12 20:46:03 -0700130 std::atomic<uint64_t> mLastFrameNumberReceived{0};
Marissa Wallfd668622018-05-10 10:21:13 -0700131
Lloyd Pique42ab75e2018-09-12 20:46:03 -0700132 bool mAutoRefresh{false};
Marissa Wallfd668622018-05-10 10:21:13 -0700133
134 // thread-safe
Lloyd Piquef1c675b2018-09-12 20:45:39 -0700135 std::atomic<int32_t> mQueuedFrames{0};
136 std::atomic<bool> mSidebandStreamChanged{false};
chaviw2d9a4292019-12-02 16:00:24 -0800137
138 sp<ContentsChangedListener> mContentsChangedListener;
Marissa Wallfd668622018-05-10 10:21:13 -0700139};
140
141} // namespace android