blob: 45906ff223d2e7b3538a9bb701737885a1a0211f [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"
David Sodman0c69cad2017-08-21 12:12:51 -070027#include "SurfaceFlinger.h"
David Sodman0c69cad2017-08-21 12:12:51 -070028
29#include <gui/ISurfaceComposerClient.h>
30#include <gui/LayerState.h>
Peiyong Lincbc184f2018-08-22 13:24:10 -070031#include <renderengine/Mesh.h>
32#include <renderengine/Texture.h>
David Sodman0c69cad2017-08-21 12:12:51 -070033#include <ui/FrameStats.h>
34#include <ui/GraphicBuffer.h>
35#include <ui/PixelFormat.h>
36#include <ui/Region.h>
37
38#include <utils/RefBase.h>
39#include <utils/String8.h>
40#include <utils/Timers.h>
41
42#include <stdint.h>
43#include <sys/types.h>
44#include <list>
45
46namespace android {
47
Marissa Wallfd668622018-05-10 10:21:13 -070048class BufferLayer : public Layer {
David Sodman0c69cad2017-08-21 12:12:51 -070049public:
50 BufferLayer(SurfaceFlinger* flinger, const sp<Client>& client, const String8& name, uint32_t w,
51 uint32_t h, uint32_t flags);
52
53 ~BufferLayer() override;
54
55 // -----------------------------------------------------------------------
56 // Overriden from Layer
57 // -----------------------------------------------------------------------
Marissa Wallfd668622018-05-10 10:21:13 -070058public:
59 // If we have received a new buffer this frame, we will pass its surface
60 // damage down to hardware composer. Otherwise, we must send a region with
61 // one empty rect.
62 void useSurfaceDamage() override;
63 void useEmptyDamage() override;
David Sodman0c69cad2017-08-21 12:12:51 -070064
Marissa Wallfd668622018-05-10 10:21:13 -070065 // getTypeId - Provide unique string for each class type in the Layer
66 // hierarchy
David Sodman0c69cad2017-08-21 12:12:51 -070067 const char* getTypeId() const override { return "BufferLayer"; }
68
Marissa Wallfd668622018-05-10 10:21:13 -070069 bool isOpaque(const Layer::State& s) const override;
David Sodman0c69cad2017-08-21 12:12:51 -070070
Marissa Wallfd668622018-05-10 10:21:13 -070071 // isVisible - true if this layer is visible, false otherwise
David Sodman0c69cad2017-08-21 12:12:51 -070072 bool isVisible() const override;
73
Marissa Wallfd668622018-05-10 10:21:13 -070074 // isFixedSize - true if content has a fixed size
David Sodman0c69cad2017-08-21 12:12:51 -070075 bool isFixedSize() const override;
76
Marissa Wallfd668622018-05-10 10:21:13 -070077 // onDraw - draws the surface.
David Sodman0c69cad2017-08-21 12:12:51 -070078 void onDraw(const RenderArea& renderArea, const Region& clip,
Marissa Wall61c58622018-07-18 10:12:20 -070079 bool useIdentityTransform) override;
David Sodman0c69cad2017-08-21 12:12:51 -070080
Marissa Wallfd668622018-05-10 10:21:13 -070081 bool isHdrY410() const override;
David Sodmaneb085e02017-10-05 18:49:04 -070082
Marissa Wallfd668622018-05-10 10:21:13 -070083 void setPerFrameData(const sp<const DisplayDevice>& displayDevice) override;
84
85 bool onPreComposition(nsecs_t refreshStartTime) override;
David Sodmaneb085e02017-10-05 18:49:04 -070086 bool onPostComposition(const std::shared_ptr<FenceTime>& glDoneFence,
87 const std::shared_ptr<FenceTime>& presentFence,
88 const CompositorTiming& compositorTiming) override;
David Sodmaneb085e02017-10-05 18:49:04 -070089
Marissa Wallfd668622018-05-10 10:21:13 -070090 // latchBuffer - called each time the screen is redrawn and returns whether
91 // the visible regions need to be recomputed (this is a fairly heavy
92 // operation, so this should be set only if needed). Typically this is used
93 // to figure out if the content or size of a surface has changed.
David Sodman0c69cad2017-08-21 12:12:51 -070094 Region latchBuffer(bool& recomputeVisibleRegions, nsecs_t latchTime) override;
Marissa Wallfd668622018-05-10 10:21:13 -070095
David Sodman0c69cad2017-08-21 12:12:51 -070096 bool isBufferLatched() const override { return mRefreshPending; }
97
Marissa Wallfd668622018-05-10 10:21:13 -070098 void notifyAvailableFrames() override;
Chia-I Wu692e0832018-06-05 15:46:58 -070099
Marissa Wallfd668622018-05-10 10:21:13 -0700100 bool hasReadyFrame() const override;
David Sodman0c69cad2017-08-21 12:12:51 -0700101
David Sodman0c69cad2017-08-21 12:12:51 -0700102 // Returns the current scaling mode, unless mOverrideScalingMode
103 // is set, in which case, it returns mOverrideScalingMode
104 uint32_t getEffectiveScalingMode() const override;
Marissa Wallfd668622018-05-10 10:21:13 -0700105 // -----------------------------------------------------------------------
106
107 // -----------------------------------------------------------------------
108 // Functions that must be implemented by derived classes
109 // -----------------------------------------------------------------------
110private:
111 virtual bool fenceHasSignaled() const = 0;
112
113 virtual nsecs_t getDesiredPresentTime() = 0;
114 virtual std::shared_ptr<FenceTime> getCurrentFenceTime() const = 0;
115
Marissa Wall61c58622018-07-18 10:12:20 -0700116 virtual void getDrawingTransformMatrix(float *matrix) = 0;
Marissa Wallfd668622018-05-10 10:21:13 -0700117 virtual uint32_t getDrawingTransform() const = 0;
118 virtual ui::Dataspace getDrawingDataSpace() const = 0;
119 virtual Rect getDrawingCrop() const = 0;
120 virtual uint32_t getDrawingScalingMode() const = 0;
121 virtual Region getDrawingSurfaceDamage() const = 0;
122 virtual const HdrMetadata& getDrawingHdrMetadata() const = 0;
123 virtual int getDrawingApi() const = 0;
124 virtual PixelFormat getPixelFormat() const = 0;
125
126 virtual uint64_t getFrameNumber() const = 0;
127
128 virtual bool getAutoRefresh() const = 0;
129 virtual bool getSidebandStreamChanged() const = 0;
130
131 virtual std::optional<Region> latchSidebandStream(bool& recomputeVisibleRegions) = 0;
132
133 virtual bool hasDrawingBuffer() const = 0;
134
Marissa Wall61c58622018-07-18 10:12:20 -0700135 virtual void setFilteringEnabled(bool enabled) = 0;
Marissa Wallfd668622018-05-10 10:21:13 -0700136
137 virtual status_t bindTextureImage() const = 0;
138 virtual status_t updateTexImage(bool& recomputeVisibleRegions, nsecs_t latchTime) = 0;
139
140 virtual status_t updateActiveBuffer() = 0;
141 virtual status_t updateFrameNumber(nsecs_t latchTime) = 0;
142
143 virtual void setHwcLayerBuffer(const sp<const DisplayDevice>& display) = 0;
144
145 // -----------------------------------------------------------------------
David Sodman0c69cad2017-08-21 12:12:51 -0700146
147public:
Marissa Wallfd668622018-05-10 10:21:13 -0700148 // isProtected - true if the layer may contain protected content in the
149 // GRALLOC_USAGE_PROTECTED sense.
150 bool isProtected() const;
David Sodman0c69cad2017-08-21 12:12:51 -0700151
Marissa Wallfd668622018-05-10 10:21:13 -0700152protected:
153 // Loads the corresponding system property once per process
154 static bool latchUnsignaledBuffers();
David Sodmaneb085e02017-10-05 18:49:04 -0700155
David Sodman0c69cad2017-08-21 12:12:51 -0700156 // Check all of the local sync points to ensure that all transactions
157 // which need to have been applied prior to the frame which is about to
158 // be latched have signaled
159 bool allTransactionsSignaled();
David Sodman0c69cad2017-08-21 12:12:51 -0700160
Marissa Wallfd668622018-05-10 10:21:13 -0700161 static bool getOpacityForFormat(uint32_t format);
David Sodman0c69cad2017-08-21 12:12:51 -0700162
Marissa Wallfd668622018-05-10 10:21:13 -0700163 // from GLES
164 const uint32_t mTextureName;
165
166private:
167 // needsLinearFiltering - true if this surface's state requires filtering
168 bool needsFiltering(const RenderArea& renderArea) const;
169
170 // drawing
171 void drawWithOpenGL(const RenderArea& renderArea, bool useIdentityTransform) const;
172
173 uint64_t getHeadFrameNumber() const;
174
David Sodman0c69cad2017-08-21 12:12:51 -0700175 uint32_t mCurrentScalingMode;
Marissa Wallfd668622018-05-10 10:21:13 -0700176
177 // main thread.
178 bool mBufferLatched; // TODO: Use mActiveBuffer?
179
David Sodman0c69cad2017-08-21 12:12:51 -0700180 // The texture used to draw the layer in GLES composition mode
Peiyong Lin833074a2018-08-28 11:53:54 -0700181 mutable renderengine::Texture mTexture;
David Sodman0c69cad2017-08-21 12:12:51 -0700182
David Sodman0c69cad2017-08-21 12:12:51 -0700183 bool mRefreshPending;
184};
185
186} // namespace android