blob: ba7431f0f820c5c371bff988205227c14f05cc16 [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
Lloyd Piquefeb73d72018-12-04 17:23:44 -080019#include <sys/types.h>
20#include <cstdint>
21#include <list>
David Sodman0c69cad2017-08-21 12:12:51 -070022
23#include <gui/ISurfaceComposerClient.h>
24#include <gui/LayerState.h>
Peiyong Lincbc184f2018-08-22 13:24:10 -070025#include <renderengine/Mesh.h>
26#include <renderengine/Texture.h>
Lloyd Piquefeb73d72018-12-04 17:23:44 -080027#include <system/window.h> // For NATIVE_WINDOW_SCALING_MODE_FREEZE
David Sodman0c69cad2017-08-21 12:12:51 -070028#include <ui/FrameStats.h>
29#include <ui/GraphicBuffer.h>
30#include <ui/PixelFormat.h>
31#include <ui/Region.h>
David Sodman0c69cad2017-08-21 12:12:51 -070032#include <utils/RefBase.h>
33#include <utils/String8.h>
34#include <utils/Timers.h>
35
Lloyd Piquefeb73d72018-12-04 17:23:44 -080036#include "BufferLayerConsumer.h"
37#include "Client.h"
38#include "DisplayHardware/HWComposer.h"
Lloyd Piquefeb73d72018-12-04 17:23:44 -080039#include "FrameTracker.h"
40#include "Layer.h"
41#include "LayerVector.h"
42#include "MonitoredProducer.h"
43#include "SurfaceFlinger.h"
David Sodman0c69cad2017-08-21 12:12:51 -070044
45namespace android {
46
Marissa Wallfd668622018-05-10 10:21:13 -070047class BufferLayer : public Layer {
David Sodman0c69cad2017-08-21 12:12:51 -070048public:
Lloyd Pique42ab75e2018-09-12 20:46:03 -070049 explicit BufferLayer(const LayerCreationArgs& args);
David Sodman0c69cad2017-08-21 12:12:51 -070050 ~BufferLayer() override;
51
52 // -----------------------------------------------------------------------
53 // Overriden from Layer
54 // -----------------------------------------------------------------------
Marissa Wallfd668622018-05-10 10:21:13 -070055public:
Lloyd Piquefeb73d72018-12-04 17:23:44 -080056 std::shared_ptr<compositionengine::Layer> getCompositionLayer() const override;
57
Marissa Wallfd668622018-05-10 10:21:13 -070058 // If we have received a new buffer this frame, we will pass its surface
59 // damage down to hardware composer. Otherwise, we must send a region with
60 // one empty rect.
61 void useSurfaceDamage() override;
62 void useEmptyDamage() override;
David Sodman0c69cad2017-08-21 12:12:51 -070063
Marissa Wallfd668622018-05-10 10:21:13 -070064 // getTypeId - Provide unique string for each class type in the Layer
65 // hierarchy
David Sodman0c69cad2017-08-21 12:12:51 -070066 const char* getTypeId() const override { return "BufferLayer"; }
67
Marissa Wallfd668622018-05-10 10:21:13 -070068 bool isOpaque(const Layer::State& s) const override;
David Sodman0c69cad2017-08-21 12:12:51 -070069
Marissa Wallfd668622018-05-10 10:21:13 -070070 // isVisible - true if this layer is visible, false otherwise
Lloyd Pique0449b0f2018-12-20 16:23:45 -080071 bool isVisible() const override;
David Sodman0c69cad2017-08-21 12:12:51 -070072
Peiyong Linfb530cf2018-12-15 05:07:38 +000073 // isProtected - true if the layer may contain protected content in the
74 // GRALLOC_USAGE_PROTECTED sense.
75 bool isProtected() const override;
76
Marissa Wallfd668622018-05-10 10:21:13 -070077 // isFixedSize - true if content has a fixed size
David Sodman0c69cad2017-08-21 12:12:51 -070078 bool isFixedSize() const override;
79
Alec Mouri79108df2019-02-04 19:33:44 +000080 // onDraw - draws the surface.
81 void onDraw(const RenderArea& renderArea, const Region& clip,
82 bool useIdentityTransform) override;
83
Marissa Wallfd668622018-05-10 10:21:13 -070084 bool isHdrY410() const override;
David Sodmaneb085e02017-10-05 18:49:04 -070085
Dominik Laskowski075d3172018-05-24 15:50:06 -070086 void setPerFrameData(DisplayId displayId, const ui::Transform& transform, const Rect& viewport,
87 int32_t supportedPerFrameMetadata) override;
Marissa Wallfd668622018-05-10 10:21:13 -070088
89 bool onPreComposition(nsecs_t refreshStartTime) override;
Dominik Laskowski075d3172018-05-24 15:50:06 -070090 bool onPostComposition(const std::optional<DisplayId>& displayId,
91 const std::shared_ptr<FenceTime>& glDoneFence,
David Sodmaneb085e02017-10-05 18:49:04 -070092 const std::shared_ptr<FenceTime>& presentFence,
Lloyd Pique0449b0f2018-12-20 16:23:45 -080093 const CompositorTiming& compositorTiming) override;
David Sodmaneb085e02017-10-05 18:49:04 -070094
Marissa Wallfd668622018-05-10 10:21:13 -070095 // latchBuffer - called each time the screen is redrawn and returns whether
96 // the visible regions need to be recomputed (this is a fairly heavy
97 // operation, so this should be set only if needed). Typically this is used
98 // to figure out if the content or size of a surface has changed.
Alec Mouri86770e52018-09-24 22:40:58 +000099 // If there was a GL composition step rendering the previous frame, then
100 // releaseFence will be populated with a native fence that fires when
101 // composition has completed.
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800102 bool latchBuffer(bool& recomputeVisibleRegions, nsecs_t latchTime,
103 const sp<Fence>& releaseFence) override;
Marissa Wallfd668622018-05-10 10:21:13 -0700104
David Sodman0c69cad2017-08-21 12:12:51 -0700105 bool isBufferLatched() const override { return mRefreshPending; }
106
Marissa Wallfd668622018-05-10 10:21:13 -0700107 void notifyAvailableFrames() override;
Chia-I Wu692e0832018-06-05 15:46:58 -0700108
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800109 bool hasReadyFrame() const override;
David Sodman0c69cad2017-08-21 12:12:51 -0700110
David Sodman0c69cad2017-08-21 12:12:51 -0700111 // Returns the current scaling mode, unless mOverrideScalingMode
112 // is set, in which case, it returns mOverrideScalingMode
113 uint32_t getEffectiveScalingMode() const override;
Marissa Wallfd668622018-05-10 10:21:13 -0700114 // -----------------------------------------------------------------------
115
116 // -----------------------------------------------------------------------
117 // Functions that must be implemented by derived classes
118 // -----------------------------------------------------------------------
119private:
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800120 virtual bool fenceHasSignaled() const = 0;
Marissa Wallfd668622018-05-10 10:21:13 -0700121
122 virtual nsecs_t getDesiredPresentTime() = 0;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800123 virtual std::shared_ptr<FenceTime> getCurrentFenceTime() const = 0;
Marissa Wallfd668622018-05-10 10:21:13 -0700124
Marissa Wall61c58622018-07-18 10:12:20 -0700125 virtual void getDrawingTransformMatrix(float *matrix) = 0;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800126 virtual uint32_t getDrawingTransform() const = 0;
127 virtual ui::Dataspace getDrawingDataSpace() const = 0;
128 virtual Rect getDrawingCrop() const = 0;
Marissa Wallfd668622018-05-10 10:21:13 -0700129 virtual uint32_t getDrawingScalingMode() const = 0;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800130 virtual Region getDrawingSurfaceDamage() const = 0;
131 virtual const HdrMetadata& getDrawingHdrMetadata() const = 0;
132 virtual int getDrawingApi() const = 0;
Marissa Wallfd668622018-05-10 10:21:13 -0700133 virtual PixelFormat getPixelFormat() const = 0;
134
135 virtual uint64_t getFrameNumber() const = 0;
136
137 virtual bool getAutoRefresh() const = 0;
138 virtual bool getSidebandStreamChanged() const = 0;
139
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800140 // Latch sideband stream and returns true if the dirty region should be updated.
141 virtual bool latchSidebandStream(bool& recomputeVisibleRegions) = 0;
Marissa Wallfd668622018-05-10 10:21:13 -0700142
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800143 virtual bool hasFrameUpdate() const = 0;
Marissa Wallfd668622018-05-10 10:21:13 -0700144
Marissa Wall61c58622018-07-18 10:12:20 -0700145 virtual void setFilteringEnabled(bool enabled) = 0;
Marissa Wallfd668622018-05-10 10:21:13 -0700146
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800147 virtual status_t bindTextureImage() = 0;
Alec Mouri86770e52018-09-24 22:40:58 +0000148 virtual status_t updateTexImage(bool& recomputeVisibleRegions, nsecs_t latchTime,
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800149 const sp<Fence>& flushFence) = 0;
Marissa Wallfd668622018-05-10 10:21:13 -0700150
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800151 virtual status_t updateActiveBuffer() = 0;
Marissa Wallfd668622018-05-10 10:21:13 -0700152 virtual status_t updateFrameNumber(nsecs_t latchTime) = 0;
153
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800154 virtual void setHwcLayerBuffer(DisplayId displayId) = 0;
Marissa Wallfd668622018-05-10 10:21:13 -0700155
Marissa Wallfd668622018-05-10 10:21:13 -0700156protected:
157 // Loads the corresponding system property once per process
158 static bool latchUnsignaledBuffers();
David Sodmaneb085e02017-10-05 18:49:04 -0700159
David Sodman0c69cad2017-08-21 12:12:51 -0700160 // Check all of the local sync points to ensure that all transactions
161 // which need to have been applied prior to the frame which is about to
162 // be latched have signaled
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800163 bool allTransactionsSignaled();
David Sodman0c69cad2017-08-21 12:12:51 -0700164
Marissa Wallfd668622018-05-10 10:21:13 -0700165 static bool getOpacityForFormat(uint32_t format);
David Sodman0c69cad2017-08-21 12:12:51 -0700166
Marissa Wallfd668622018-05-10 10:21:13 -0700167 // from GLES
168 const uint32_t mTextureName;
169
chaviwf206b662019-01-11 13:07:19 -0800170 bool mRefreshPending{false};
171
Marissa Wallfd668622018-05-10 10:21:13 -0700172private:
Peiyong Linc2020ca2019-01-10 11:36:12 -0800173 // Returns true if this layer requires filtering
174 bool needsFiltering() const;
Marissa Wallfd668622018-05-10 10:21:13 -0700175
Alec Mouri79108df2019-02-04 19:33:44 +0000176 // drawing
177 void drawWithOpenGL(const RenderArea& renderArea, bool useIdentityTransform) const;
178
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800179 uint64_t getHeadFrameNumber() const;
Marissa Wallfd668622018-05-10 10:21:13 -0700180
Lloyd Pique42ab75e2018-09-12 20:46:03 -0700181 uint32_t mCurrentScalingMode{NATIVE_WINDOW_SCALING_MODE_FREEZE};
Marissa Wallfd668622018-05-10 10:21:13 -0700182
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800183 bool mTransformToDisplayInverse{false};
184
Marissa Wallfd668622018-05-10 10:21:13 -0700185 // main thread.
Lloyd Pique42ab75e2018-09-12 20:46:03 -0700186 bool mBufferLatched{false}; // TODO: Use mActiveBuffer?
Marissa Wallfd668622018-05-10 10:21:13 -0700187
Alec Mouri79108df2019-02-04 19:33:44 +0000188 // The texture used to draw the layer in GLES composition mode
189 mutable renderengine::Texture mTexture;
190
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800191 Rect getBufferSize(const State& s) const override;
Lloyd Piquefeb73d72018-12-04 17:23:44 -0800192
193 std::shared_ptr<compositionengine::Layer> mCompositionLayer;
David Sodman0c69cad2017-08-21 12:12:51 -0700194};
195
196} // namespace android