blob: e8838e812c867598a04b3098df16a9cd9b7be751 [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>
Alec Mourie7d1d4a2019-02-05 01:13:46 +000025#include <renderengine/Image.h>
Peiyong Lincbc184f2018-08-22 13:24:10 -070026#include <renderengine/Mesh.h>
27#include <renderengine/Texture.h>
Lloyd Piquefeb73d72018-12-04 17:23:44 -080028#include <system/window.h> // For NATIVE_WINDOW_SCALING_MODE_FREEZE
David Sodman0c69cad2017-08-21 12:12:51 -070029#include <ui/FrameStats.h>
30#include <ui/GraphicBuffer.h>
31#include <ui/PixelFormat.h>
32#include <ui/Region.h>
David Sodman0c69cad2017-08-21 12:12:51 -070033#include <utils/RefBase.h>
34#include <utils/String8.h>
35#include <utils/Timers.h>
36
Lloyd Piquefeb73d72018-12-04 17:23:44 -080037#include "BufferLayerConsumer.h"
38#include "Client.h"
39#include "DisplayHardware/HWComposer.h"
Lloyd Piquefeb73d72018-12-04 17:23:44 -080040#include "FrameTracker.h"
41#include "Layer.h"
42#include "LayerVector.h"
43#include "MonitoredProducer.h"
44#include "SurfaceFlinger.h"
David Sodman0c69cad2017-08-21 12:12:51 -070045
46namespace android {
47
Marissa Wallfd668622018-05-10 10:21:13 -070048class BufferLayer : public Layer {
David Sodman0c69cad2017-08-21 12:12:51 -070049public:
Lloyd Pique42ab75e2018-09-12 20:46:03 -070050 explicit BufferLayer(const LayerCreationArgs& args);
Alec Mouri4545a8a2019-08-08 20:05:32 -070051 virtual ~BufferLayer() override;
David Sodman0c69cad2017-08-21 12:12:51 -070052
53 // -----------------------------------------------------------------------
54 // Overriden from Layer
55 // -----------------------------------------------------------------------
Marissa Wallfd668622018-05-10 10:21:13 -070056public:
Lloyd Piquefeb73d72018-12-04 17:23:44 -080057 std::shared_ptr<compositionengine::Layer> getCompositionLayer() const override;
58
Marissa Wallfd668622018-05-10 10:21:13 -070059 // 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 bool isOpaque(const Layer::State& s) const override;
David Sodman0c69cad2017-08-21 12:12:51 -070066
Marissa Wallfd668622018-05-10 10:21:13 -070067 // isVisible - true if this layer is visible, false otherwise
Lloyd Pique0449b0f2018-12-20 16:23:45 -080068 bool isVisible() const override;
David Sodman0c69cad2017-08-21 12:12:51 -070069
Peiyong Linfb530cf2018-12-15 05:07:38 +000070 // isProtected - true if the layer may contain protected content in the
71 // GRALLOC_USAGE_PROTECTED sense.
72 bool isProtected() 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
Lloyd Piquea83776c2019-01-29 18:42:32 -080077 bool usesSourceCrop() const override;
78
Marissa Wallfd668622018-05-10 10:21:13 -070079 bool isHdrY410() const override;
David Sodmaneb085e02017-10-05 18:49:04 -070080
Dominik Laskowski075d3172018-05-24 15:50:06 -070081 bool onPostComposition(const std::optional<DisplayId>& displayId,
82 const std::shared_ptr<FenceTime>& glDoneFence,
David Sodmaneb085e02017-10-05 18:49:04 -070083 const std::shared_ptr<FenceTime>& presentFence,
Lloyd Pique0449b0f2018-12-20 16:23:45 -080084 const CompositorTiming& compositorTiming) override;
David Sodmaneb085e02017-10-05 18:49:04 -070085
Marissa Wallfd668622018-05-10 10:21:13 -070086 // latchBuffer - called each time the screen is redrawn and returns whether
87 // the visible regions need to be recomputed (this is a fairly heavy
88 // operation, so this should be set only if needed). Typically this is used
89 // to figure out if the content or size of a surface has changed.
Dominik Laskowskia8955dd2019-07-10 10:19:09 -070090 bool latchBuffer(bool& recomputeVisibleRegions, nsecs_t latchTime,
91 nsecs_t expectedPresentTime) override;
Marissa Wallfd668622018-05-10 10:21:13 -070092
David Sodman0c69cad2017-08-21 12:12:51 -070093 bool isBufferLatched() const override { return mRefreshPending; }
94
Dominik Laskowskia8955dd2019-07-10 10:19:09 -070095 void notifyAvailableFrames(nsecs_t expectedPresentTime) override;
Chia-I Wu692e0832018-06-05 15:46:58 -070096
Lloyd Pique0449b0f2018-12-20 16:23:45 -080097 bool hasReadyFrame() const override;
David Sodman0c69cad2017-08-21 12:12:51 -070098
David Sodman0c69cad2017-08-21 12:12:51 -070099 // Returns the current scaling mode, unless mOverrideScalingMode
100 // is set, in which case, it returns mOverrideScalingMode
101 uint32_t getEffectiveScalingMode() const override;
chaviw49a108c2019-08-12 11:23:06 -0700102
103 // Calls latchBuffer if the buffer has a frame queued and then releases the buffer.
104 // This is used if the buffer is just latched and releases to free up the buffer
105 // and will not be shown on screen.
106 // Should only be called on the main thread.
107 void latchAndReleaseBuffer() override;
108
chaviw4244e032019-09-04 11:27:49 -0700109 bool getTransformToDisplayInverse() const override;
110
111 Rect getBufferCrop() const override;
112
113 uint32_t getBufferTransform() const override;
114
115 ui::Dataspace getDataSpace() const override;
116
Marissa Wallfd668622018-05-10 10:21:13 -0700117 // -----------------------------------------------------------------------
118
119 // -----------------------------------------------------------------------
120 // Functions that must be implemented by derived classes
121 // -----------------------------------------------------------------------
122private:
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800123 virtual bool fenceHasSignaled() const = 0;
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700124 virtual bool framePresentTimeIsCurrent(nsecs_t expectedPresentTime) const = 0;
Marissa Wallfd668622018-05-10 10:21:13 -0700125
chaviw4244e032019-09-04 11:27:49 -0700126 PixelFormat getPixelFormat() const;
Marissa Wallfd668622018-05-10 10:21:13 -0700127
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700128 virtual uint64_t getFrameNumber(nsecs_t expectedPresentTime) const = 0;
Marissa Wallfd668622018-05-10 10:21:13 -0700129
130 virtual bool getAutoRefresh() const = 0;
131 virtual bool getSidebandStreamChanged() const = 0;
132
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800133 // Latch sideband stream and returns true if the dirty region should be updated.
134 virtual bool latchSidebandStream(bool& recomputeVisibleRegions) = 0;
Marissa Wallfd668622018-05-10 10:21:13 -0700135
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800136 virtual bool hasFrameUpdate() const = 0;
Marissa Wallfd668622018-05-10 10:21:13 -0700137
Marissa Wall61c58622018-07-18 10:12:20 -0700138 virtual void setFilteringEnabled(bool enabled) = 0;
Marissa Wallfd668622018-05-10 10:21:13 -0700139
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800140 virtual status_t bindTextureImage() = 0;
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700141 virtual status_t updateTexImage(bool& recomputeVisibleRegions, nsecs_t latchTime,
142 nsecs_t expectedPresentTime) = 0;
Marissa Wallfd668622018-05-10 10:21:13 -0700143
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800144 virtual status_t updateActiveBuffer() = 0;
Marissa Wallfd668622018-05-10 10:21:13 -0700145 virtual status_t updateFrameNumber(nsecs_t latchTime) = 0;
146
Marissa Wallfd668622018-05-10 10:21:13 -0700147protected:
chaviw4244e032019-09-04 11:27:49 -0700148 struct BufferInfo {
149 nsecs_t mDesiredPresentTime;
150 std::shared_ptr<FenceTime> mFenceTime;
151 sp<Fence> mFence;
152 float mTransformMatrix[16];
153 uint32_t mTransform{0};
154 ui::Dataspace mDataspace;
155 Rect mCrop;
156 uint32_t mScaleMode{NATIVE_WINDOW_SCALING_MODE_FREEZE};
157 Region mSurfaceDamage;
158 HdrMetadata mHdrMetadata;
159 int mApi;
160 PixelFormat mPixelFormat;
161 bool mTransformToDisplayInverse{false};
162 };
163
164 BufferInfo mBufferInfo;
165 virtual void gatherBufferInfo() = 0;
166
Lloyd Piquef16688f2019-02-19 17:47:57 -0800167 /*
168 * compositionengine::LayerFE overrides
169 */
170 bool onPreComposition(nsecs_t) override;
171 void latchPerFrameState(compositionengine::LayerFECompositionState&) const override;
172 std::optional<renderengine::LayerSettings> prepareClientComposition(
173 compositionengine::LayerFE::ClientCompositionTargetSettings&) override;
Lloyd Piquef5275482019-01-29 18:42:42 -0800174
Marissa Wallfd668622018-05-10 10:21:13 -0700175 // Loads the corresponding system property once per process
176 static bool latchUnsignaledBuffers();
David Sodmaneb085e02017-10-05 18:49:04 -0700177
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
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700181 bool allTransactionsSignaled(nsecs_t expectedPresentTime);
David Sodman0c69cad2017-08-21 12:12:51 -0700182
Marissa Wallfd668622018-05-10 10:21:13 -0700183 static bool getOpacityForFormat(uint32_t format);
David Sodman0c69cad2017-08-21 12:12:51 -0700184
Marissa Wallfd668622018-05-10 10:21:13 -0700185 // from GLES
186 const uint32_t mTextureName;
187
chaviwf206b662019-01-11 13:07:19 -0800188 bool mRefreshPending{false};
189
chaviw4244e032019-09-04 11:27:49 -0700190 ui::Dataspace translateDataspace(ui::Dataspace dataspace);
191
Marissa Wallfd668622018-05-10 10:21:13 -0700192private:
Peiyong Linc2020ca2019-01-10 11:36:12 -0800193 // Returns true if this layer requires filtering
Lloyd Piquef16688f2019-02-19 17:47:57 -0800194 bool needsFiltering(const sp<const DisplayDevice>& displayDevice) const override;
Marissa Wallfd668622018-05-10 10:21:13 -0700195
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700196 uint64_t getHeadFrameNumber(nsecs_t expectedPresentTime) const;
Marissa Wallfd668622018-05-10 10:21:13 -0700197
Marissa Wallfd668622018-05-10 10:21:13 -0700198 // main thread.
Lloyd Pique42ab75e2018-09-12 20:46:03 -0700199 bool mBufferLatched{false}; // TODO: Use mActiveBuffer?
Marissa Wallfd668622018-05-10 10:21:13 -0700200
Marissa Wall290ad082019-03-06 13:23:47 -0800201 // BufferStateLayers can return Rect::INVALID_RECT if the layer does not have a display frame
202 // and its parent layer is not bounded
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800203 Rect getBufferSize(const State& s) const override;
Lloyd Piquefeb73d72018-12-04 17:23:44 -0800204
205 std::shared_ptr<compositionengine::Layer> mCompositionLayer;
Vishnu Nair4351ad52019-02-11 14:13:02 -0800206
207 FloatRect computeSourceBounds(const FloatRect& parentBounds) const override;
David Sodman0c69cad2017-08-21 12:12:51 -0700208};
209
210} // namespace android