blob: 078302bc75b8a7aeecd23d74d33196791130ad40 [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 void onLayerDisplayed(const sp<Fence>& releaseFence) override;
David Sodmaneb085e02017-10-05 18:49:04 -0700109
110 void abandon() override;
111 bool shouldPresentNow(const DispSync& dispSync) const override;
112 void setTransformHint(uint32_t orientation) const override;
113 bool onPostComposition(const std::shared_ptr<FenceTime>& glDoneFence,
114 const std::shared_ptr<FenceTime>& presentFence,
115 const CompositorTiming& compositorTiming) override;
116 std::vector<OccupancyTracker::Segment> getOccupancyHistory(bool forceFlush) override;
117 bool getTransformToDisplayInverse() const override;
118
119public:
David Sodman0c69cad2017-08-21 12:12:51 -0700120 bool onPreComposition(nsecs_t refreshStartTime) override;
121
David Sodman0c69cad2017-08-21 12:12:51 -0700122 // If a buffer was replaced this frame, release the former buffer
123 void releasePendingBuffer(nsecs_t dequeueReadyTime);
David Sodman0c69cad2017-08-21 12:12:51 -0700124
125 /*
126 * latchBuffer - called each time the screen is redrawn and returns whether
127 * the visible regions need to be recomputed (this is a fairly heavy
128 * operation, so this should be set only if needed). Typically this is used
129 * to figure out if the content or size of a surface has changed.
130 */
131 Region latchBuffer(bool& recomputeVisibleRegions, nsecs_t latchTime) override;
132 bool isBufferLatched() const override { return mRefreshPending; }
David Sodmaneb085e02017-10-05 18:49:04 -0700133 void setDefaultBufferSize(uint32_t w, uint32_t h) override;
David Sodman0c69cad2017-08-21 12:12:51 -0700134
David Sodmaneb085e02017-10-05 18:49:04 -0700135 void setPerFrameData(const sp<const DisplayDevice>& displayDevice) override;
David Sodmaneb085e02017-10-05 18:49:04 -0700136
David Sodman0c69cad2017-08-21 12:12:51 -0700137 bool isOpaque(const Layer::State& s) const override;
138
139private:
140 void onFirstRef() override;
141
142 // Interface implementation for
143 // SurfaceFlingerConsumer::ContentsChangedListener
144 void onFrameAvailable(const BufferItem& item) override;
145 void onFrameReplaced(const BufferItem& item) override;
146 void onSidebandStreamChanged() override;
147
148 // needsLinearFiltering - true if this surface's state requires filtering
149 bool needsFiltering(const RenderArea& renderArea) const;
150
151 static bool getOpacityForFormat(uint32_t format);
152
153 // drawing
David Sodman41fdfc92017-11-06 16:09:56 -0800154 void drawWithOpenGL(const RenderArea& renderArea, bool useIdentityTransform) const;
David Sodman0c69cad2017-08-21 12:12:51 -0700155
156 // Temporary - Used only for LEGACY camera mode.
157 uint32_t getProducerStickyTransform() const;
158
159 // Loads the corresponding system property once per process
160 static bool latchUnsignaledBuffers();
161
162 uint64_t getHeadFrameNumber() const;
163 bool headFenceHasSignaled() const;
164
165 // Returns the current scaling mode, unless mOverrideScalingMode
166 // is set, in which case, it returns mOverrideScalingMode
167 uint32_t getEffectiveScalingMode() const override;
168
169public:
170 void notifyAvailableFrames() override;
171
172 PixelFormat getPixelFormat() const override { return mFormat; }
173 sp<IGraphicBufferProducer> getProducer() const;
174
175private:
David Sodmaneb085e02017-10-05 18:49:04 -0700176 sp<SurfaceFlingerConsumer> mSurfaceFlingerConsumer;
177
David Sodman0c69cad2017-08-21 12:12:51 -0700178 // Check all of the local sync points to ensure that all transactions
179 // which need to have been applied prior to the frame which is about to
180 // be latched have signaled
181 bool allTransactionsSignaled();
182 sp<IGraphicBufferProducer> mProducer;
183
184 // constants
185 uint32_t mTextureName; // from GLES
186 PixelFormat mFormat;
187
188 // main thread
189 uint32_t mCurrentScalingMode;
190 bool mBufferLatched = false; // TODO: Use mActiveBuffer?
191 uint64_t mPreviousFrameNumber; // Only accessed on the main thread.
192 // The texture used to draw the layer in GLES composition mode
193 mutable Texture mTexture;
194
195 bool mUpdateTexImageFailed; // This is only accessed on the main thread.
196 bool mRefreshPending;
197};
198
199} // namespace android