1 /*
2  * Copyright (C) 2017 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 "BufferLayerConsumer.h"
20 #include "Client.h"
21 #include "Layer.h"
22 #include "DisplayHardware/HWComposer.h"
23 #include "DisplayHardware/HWComposerBufferCache.h"
24 #include "FrameTracker.h"
25 #include "LayerVector.h"
26 #include "MonitoredProducer.h"
27 #include "RenderEngine/Mesh.h"
28 #include "RenderEngine/Texture.h"
29 #include "SurfaceFlinger.h"
30 #include "Transform.h"
31 
32 #include <gui/ISurfaceComposerClient.h>
33 #include <gui/LayerState.h>
34 
35 #include <ui/FrameStats.h>
36 #include <ui/GraphicBuffer.h>
37 #include <ui/PixelFormat.h>
38 #include <ui/Region.h>
39 
40 #include <utils/RefBase.h>
41 #include <utils/String8.h>
42 #include <utils/Timers.h>
43 
44 #include <stdint.h>
45 #include <sys/types.h>
46 #include <list>
47 
48 namespace android {
49 
50 /*
51  * A new BufferQueue and a new BufferLayerConsumer are created when the
52  * BufferLayer is first referenced.
53  *
54  * This also implements onFrameAvailable(), which notifies SurfaceFlinger
55  * that new data has arrived.
56  */
57 class BufferLayer : public Layer, public BufferLayerConsumer::ContentsChangedListener {
58 public:
59     BufferLayer(SurfaceFlinger* flinger, const sp<Client>& client, const String8& name, uint32_t w,
60                 uint32_t h, uint32_t flags);
61 
62     ~BufferLayer() override;
63 
64     // If we have received a new buffer this frame, we will pass its surface
65     // damage down to hardware composer. Otherwise, we must send a region with
66     // one empty rect.
67     void useSurfaceDamage();
68     void useEmptyDamage();
69 
70     // -----------------------------------------------------------------------
71     // Overriden from Layer
72     // -----------------------------------------------------------------------
73 
74     /*
75      * getTypeId - Provide unique string for each class type in the Layer
76      * hierarchy
77      */
getTypeId()78     const char* getTypeId() const override { return "BufferLayer"; }
79 
80     /*
81      * isProtected - true if the layer may contain protected content in the
82      * GRALLOC_USAGE_PROTECTED sense.
83      */
84     bool isProtected() const;
85 
86     /*
87      * isVisible - true if this layer is visible, false otherwise
88      */
89     bool isVisible() const override;
90 
91     /*
92      * isFixedSize - true if content has a fixed size
93      */
94     bool isFixedSize() const override;
95 
96     // the this layer's size and format
97     status_t setBuffers(uint32_t w, uint32_t h, PixelFormat format, uint32_t flags);
98 
99     /*
100      * onDraw - draws the surface.
101      */
102     void onDraw(const RenderArea& renderArea, const Region& clip,
103                 bool useIdentityTransform) const override;
104 
105     void onLayerDisplayed(const sp<Fence>& releaseFence) override;
106 
107     void abandon() override;
108     bool shouldPresentNow(const DispSync& dispSync) const override;
109     void setTransformHint(uint32_t orientation) const override;
110     bool onPostComposition(const std::shared_ptr<FenceTime>& glDoneFence,
111                            const std::shared_ptr<FenceTime>& presentFence,
112                            const CompositorTiming& compositorTiming) override;
113     std::vector<OccupancyTracker::Segment> getOccupancyHistory(bool forceFlush) override;
114     bool getTransformToDisplayInverse() const override;
115 
116 public:
117     bool onPreComposition(nsecs_t refreshStartTime) override;
118 
119     // If a buffer was replaced this frame, release the former buffer
120     void releasePendingBuffer(nsecs_t dequeueReadyTime);
121 
122     /*
123      * latchBuffer - called each time the screen is redrawn and returns whether
124      * the visible regions need to be recomputed (this is a fairly heavy
125      * operation, so this should be set only if needed). Typically this is used
126      * to figure out if the content or size of a surface has changed.
127      */
128     Region latchBuffer(bool& recomputeVisibleRegions, nsecs_t latchTime) override;
isBufferLatched()129     bool isBufferLatched() const override { return mRefreshPending; }
130     void setDefaultBufferSize(uint32_t w, uint32_t h) override;
131 
132     bool isHdrY410() const override;
133 
134     void setPerFrameData(const sp<const DisplayDevice>& displayDevice) override;
135 
136     bool isOpaque(const Layer::State& s) const override;
137 
138 private:
139     void onFirstRef() override;
140 
141     // Interface implementation for
142     // BufferLayerConsumer::ContentsChangedListener
143     void onFrameAvailable(const BufferItem& item) override;
144     void onFrameReplaced(const BufferItem& item) override;
145     void onSidebandStreamChanged() override;
146 
147     // needsLinearFiltering - true if this surface's state requires filtering
148     bool needsFiltering(const RenderArea& renderArea) const;
149 
150     static bool getOpacityForFormat(uint32_t format);
151 
152     // drawing
153     void drawWithOpenGL(const RenderArea& renderArea, bool useIdentityTransform) const;
154 
155     // Temporary - Used only for LEGACY camera mode.
156     uint32_t getProducerStickyTransform() const;
157 
158     // Loads the corresponding system property once per process
159     static bool latchUnsignaledBuffers();
160 
161     uint64_t getHeadFrameNumber() const;
162     bool headFenceHasSignaled() const;
163 
164     // Returns the current scaling mode, unless mOverrideScalingMode
165     // is set, in which case, it returns mOverrideScalingMode
166     uint32_t getEffectiveScalingMode() const override;
167 
168 public:
169     void notifyAvailableFrames() override;
170 
getPixelFormat()171     PixelFormat getPixelFormat() const override { return mFormat; }
172     sp<IGraphicBufferProducer> getProducer() const;
173 
174 private:
175     sp<BufferLayerConsumer> mConsumer;
176 
177     // Check all of the local sync points to ensure that all transactions
178     // which need to have been applied prior to the frame which is about to
179     // be latched have signaled
180     bool allTransactionsSignaled();
181     sp<IGraphicBufferProducer> mProducer;
182 
183     // constants
184     uint32_t mTextureName; // from GLES
185     PixelFormat mFormat;
186 
187     // main thread
188     uint32_t mCurrentScalingMode;
189     bool mBufferLatched = false;   // TODO: Use mActiveBuffer?
190     uint64_t mPreviousFrameNumber; // Only accessed on the main thread.
191     // The texture used to draw the layer in GLES composition mode
192     mutable Texture mTexture;
193 
194     bool mUpdateTexImageFailed; // This is only accessed on the main thread.
195     bool mRefreshPending;
196 };
197 
198 } // namespace android
199