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