1 /*
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 
23 namespace 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  */
32 class BufferQueueLayer : public BufferLayer {
33 public:
34     // Only call while mStateLock is held
35     explicit BufferQueueLayer(const LayerCreationArgs&);
36     ~BufferQueueLayer() override;
37 
38     // -----------------------------------------------------------------------
39     // Interface implementation for Layer
40     // -----------------------------------------------------------------------
41 public:
getType()42     const char* getType() const override { return "BufferQueueLayer"; }
43 
44     void onLayerDisplayed(const sp<Fence>& releaseFence) override;
45 
46     std::vector<OccupancyTracker::Segment> getOccupancyHistory(bool forceFlush) override;
47 
48     // 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 
55     bool shouldPresentNow(nsecs_t expectedPresentTime) const override;
56 
57     // -----------------------------------------------------------------------
58 
59     // -----------------------------------------------------------------------
60     // Interface implementation for BufferLayer
61     // -----------------------------------------------------------------------
62 public:
63     bool fenceHasSignaled() const override;
64     bool framePresentTimeIsCurrent(nsecs_t expectedPresentTime) const override;
65 
66 private:
67     uint64_t getFrameNumber(nsecs_t expectedPresentTime) const override;
68 
69     bool getAutoRefresh() const override;
70     bool getSidebandStreamChanged() const override;
71 
72     bool latchSidebandStream(bool& recomputeVisibleRegions) override;
73     void setTransformHint(ui::Transform::RotationFlags displayTransformHint) override;
74 
75     bool hasFrameUpdate() const override;
76 
77     status_t bindTextureImage() override;
78     status_t updateTexImage(bool& recomputeVisibleRegions, nsecs_t latchTime,
79                             nsecs_t expectedPresentTime) override;
80 
81     status_t updateActiveBuffer() override;
82     status_t updateFrameNumber(nsecs_t latchTime) override;
83 
84     sp<Layer> createClone() override;
85 
86     void onFrameAvailable(const BufferItem& item);
87     void onFrameReplaced(const BufferItem& item);
88     void onSidebandStreamChanged();
89     void onFrameDequeued(const uint64_t bufferId);
90     void onFrameDetached(const uint64_t bufferId);
91     void onFrameCancelled(const uint64_t bufferId);
92 
93 protected:
94     void gatherBufferInfo() override;
95 
96     // -----------------------------------------------------------------------
97     // Interface implementation for BufferLayerConsumer::ContentsChangedListener
98     // -----------------------------------------------------------------------
99     class ContentsChangedListener : public BufferLayerConsumer::ContentsChangedListener {
100     public:
ContentsChangedListener(BufferQueueLayer * bufferQueueLayer)101         ContentsChangedListener(BufferQueueLayer* bufferQueueLayer)
102               : mBufferQueueLayer(bufferQueueLayer) {}
103         void abandon();
104 
105     protected:
106         void onFrameAvailable(const BufferItem& item) override;
107         void onFrameReplaced(const BufferItem& item) override;
108         void onSidebandStreamChanged() override;
109         void onFrameDequeued(const uint64_t bufferId) override;
110         void onFrameDetached(const uint64_t bufferId) override;
111         void onFrameCancelled(const uint64_t bufferId) override;
112 
113     private:
114         BufferQueueLayer* mBufferQueueLayer = nullptr;
115         Mutex mMutex;
116     };
117     // -----------------------------------------------------------------------
118 
119 public:
120     status_t setDefaultBufferProperties(uint32_t w, uint32_t h, PixelFormat format);
121 
122     sp<IGraphicBufferProducer> getProducer() const;
123 
124 private:
125     // Temporary - Used only for LEGACY camera mode.
126     uint32_t getProducerStickyTransform() const;
127 
128     void onFirstRef() override;
129 
130     sp<BufferLayerConsumer> mConsumer;
131     sp<IGraphicBufferProducer> mProducer;
132 
133     bool mUpdateTexImageFailed{false};
134 
135     uint64_t mPreviousBufferId = 0;
136     uint64_t mPreviousReleasedFrameNumber = 0;
137 
138     // Local copy of the queued contents of the incoming BufferQueue
139     mutable Mutex mQueueItemLock;
140     Condition mQueueItemCondition;
141     Vector<BufferItem> mQueueItems;
142     std::atomic<uint64_t> mLastFrameNumberReceived{0};
143 
144     bool mAutoRefresh{false};
145 
146     // thread-safe
147     std::atomic<int32_t> mQueuedFrames{0};
148     std::atomic<bool> mSidebandStreamChanged{false};
149 
150     sp<ContentsChangedListener> mContentsChangedListener;
151 };
152 
153 } // namespace android
154