blob: fbe63673060981e57f97ce8860c29f094afb43c0 [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
Chia-I Wu0cb75ac2017-11-27 15:56:04 -080019#include "BufferLayerConsumer.h"
David Sodman0c69cad2017-08-21 12:12:51 -070020#include "Client.h"
David Sodman41fdfc92017-11-06 16:09:56 -080021#include "Layer.h"
David Sodman0c69cad2017-08-21 12:12:51 -070022#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"
David Sodman0c69cad2017-08-21 12:12:51 -070030#include "Transform.h"
31
32#include <gui/ISurfaceComposerClient.h>
33#include <gui/LayerState.h>
34
David Sodman0c69cad2017-08-21 12:12:51 -070035#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
48namespace android {
49
50/*
Chia-I Wu0cb75ac2017-11-27 15:56:04 -080051 * A new BufferQueue and a new BufferLayerConsumer are created when the
David Sodman0c69cad2017-08-21 12:12:51 -070052 * BufferLayer is first referenced.
53 *
54 * This also implements onFrameAvailable(), which notifies SurfaceFlinger
55 * that new data has arrived.
56 */
Chia-I Wu0cb75ac2017-11-27 15:56:04 -080057class BufferLayer : public Layer, public BufferLayerConsumer::ContentsChangedListener {
David Sodman0c69cad2017-08-21 12:12:51 -070058public:
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
David Sodmaneb085e02017-10-05 18:49:04 -070064 // 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
David Sodman0c69cad2017-08-21 12:12:51 -070070 // -----------------------------------------------------------------------
71 // Overriden from Layer
72 // -----------------------------------------------------------------------
73
74 /*
75 * getTypeId - Provide unique string for each class type in the Layer
76 * hierarchy
77 */
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
David Sodmaneb085e02017-10-05 18:49:04 -0700105 void onLayerDisplayed(const sp<Fence>& releaseFence) override;
David Sodmaneb085e02017-10-05 18:49:04 -0700106
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
116public:
David Sodman0c69cad2017-08-21 12:12:51 -0700117 bool onPreComposition(nsecs_t refreshStartTime) override;
118
David Sodman0c69cad2017-08-21 12:12:51 -0700119 // If a buffer was replaced this frame, release the former buffer
120 void releasePendingBuffer(nsecs_t dequeueReadyTime);
David Sodman0c69cad2017-08-21 12:12:51 -0700121
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;
129 bool isBufferLatched() const override { return mRefreshPending; }
David Sodmaneb085e02017-10-05 18:49:04 -0700130 void setDefaultBufferSize(uint32_t w, uint32_t h) override;
David Sodman0c69cad2017-08-21 12:12:51 -0700131
David Sodmaneb085e02017-10-05 18:49:04 -0700132 void setPerFrameData(const sp<const DisplayDevice>& displayDevice) override;
David Sodmaneb085e02017-10-05 18:49:04 -0700133
David Sodman0c69cad2017-08-21 12:12:51 -0700134 bool isOpaque(const Layer::State& s) const override;
135
136private:
137 void onFirstRef() override;
138
139 // Interface implementation for
Chia-I Wu0cb75ac2017-11-27 15:56:04 -0800140 // BufferLayerConsumer::ContentsChangedListener
David Sodman0c69cad2017-08-21 12:12:51 -0700141 void onFrameAvailable(const BufferItem& item) override;
142 void onFrameReplaced(const BufferItem& item) override;
143 void onSidebandStreamChanged() override;
144
145 // needsLinearFiltering - true if this surface's state requires filtering
146 bool needsFiltering(const RenderArea& renderArea) const;
147
148 static bool getOpacityForFormat(uint32_t format);
149
150 // drawing
David Sodman41fdfc92017-11-06 16:09:56 -0800151 void drawWithOpenGL(const RenderArea& renderArea, bool useIdentityTransform) const;
David Sodman0c69cad2017-08-21 12:12:51 -0700152
153 // Temporary - Used only for LEGACY camera mode.
154 uint32_t getProducerStickyTransform() const;
155
156 // Loads the corresponding system property once per process
157 static bool latchUnsignaledBuffers();
158
159 uint64_t getHeadFrameNumber() const;
160 bool headFenceHasSignaled() const;
161
162 // Returns the current scaling mode, unless mOverrideScalingMode
163 // is set, in which case, it returns mOverrideScalingMode
164 uint32_t getEffectiveScalingMode() const override;
165
166public:
167 void notifyAvailableFrames() override;
168
169 PixelFormat getPixelFormat() const override { return mFormat; }
170 sp<IGraphicBufferProducer> getProducer() const;
171
172private:
Chia-I Wu0cb75ac2017-11-27 15:56:04 -0800173 sp<BufferLayerConsumer> mSurfaceFlingerConsumer;
David Sodmaneb085e02017-10-05 18:49:04 -0700174
David Sodman0c69cad2017-08-21 12:12:51 -0700175 // Check all of the local sync points to ensure that all transactions
176 // which need to have been applied prior to the frame which is about to
177 // be latched have signaled
178 bool allTransactionsSignaled();
179 sp<IGraphicBufferProducer> mProducer;
180
181 // constants
182 uint32_t mTextureName; // from GLES
183 PixelFormat mFormat;
184
185 // main thread
186 uint32_t mCurrentScalingMode;
187 bool mBufferLatched = false; // TODO: Use mActiveBuffer?
188 uint64_t mPreviousFrameNumber; // Only accessed on the main thread.
189 // The texture used to draw the layer in GLES composition mode
190 mutable Texture mTexture;
191
192 bool mUpdateTexImageFailed; // This is only accessed on the main thread.
193 bool mRefreshPending;
194};
195
196} // namespace android