blob: f678910813d3cda307744c00f23446efe718e431 [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 Piquede196652020-01-22 17:29:58 -080057 sp<compositionengine::LayerFE> getCompositionEngineLayerFE() const override;
58 compositionengine::LayerFECompositionState* editCompositionState() override;
Lloyd Piquefeb73d72018-12-04 17:23:44 -080059
Marissa Wallfd668622018-05-10 10:21:13 -070060 // If we have received a new buffer this frame, we will pass its surface
61 // damage down to hardware composer. Otherwise, we must send a region with
62 // one empty rect.
63 void useSurfaceDamage() override;
64 void useEmptyDamage() override;
David Sodman0c69cad2017-08-21 12:12:51 -070065
Marissa Wallfd668622018-05-10 10:21:13 -070066 bool isOpaque(const Layer::State& s) const override;
David Sodman0c69cad2017-08-21 12:12:51 -070067
Marissa Wallfd668622018-05-10 10:21:13 -070068 // isVisible - true if this layer is visible, false otherwise
Lloyd Pique0449b0f2018-12-20 16:23:45 -080069 bool isVisible() const override;
David Sodman0c69cad2017-08-21 12:12:51 -070070
Peiyong Linfb530cf2018-12-15 05:07:38 +000071 // isProtected - true if the layer may contain protected content in the
72 // GRALLOC_USAGE_PROTECTED sense.
73 bool isProtected() const override;
74
Marissa Wallfd668622018-05-10 10:21:13 -070075 // isFixedSize - true if content has a fixed size
David Sodman0c69cad2017-08-21 12:12:51 -070076 bool isFixedSize() const override;
77
Lloyd Piquea83776c2019-01-29 18:42:32 -080078 bool usesSourceCrop() const override;
79
Marissa Wallfd668622018-05-10 10:21:13 -070080 bool isHdrY410() const override;
David Sodmaneb085e02017-10-05 18:49:04 -070081
Adithya Srinivasanb69e0762019-11-11 18:39:53 -080082 bool onPostComposition(sp<const DisplayDevice> displayDevice,
Dominik Laskowski075d3172018-05-24 15:50:06 -070083 const std::shared_ptr<FenceTime>& glDoneFence,
David Sodmaneb085e02017-10-05 18:49:04 -070084 const std::shared_ptr<FenceTime>& presentFence,
Lloyd Pique0449b0f2018-12-20 16:23:45 -080085 const CompositorTiming& compositorTiming) override;
David Sodmaneb085e02017-10-05 18:49:04 -070086
Marissa Wallfd668622018-05-10 10:21:13 -070087 // latchBuffer - called each time the screen is redrawn and returns whether
88 // the visible regions need to be recomputed (this is a fairly heavy
89 // operation, so this should be set only if needed). Typically this is used
90 // to figure out if the content or size of a surface has changed.
Dominik Laskowskia8955dd2019-07-10 10:19:09 -070091 bool latchBuffer(bool& recomputeVisibleRegions, nsecs_t latchTime,
92 nsecs_t expectedPresentTime) override;
Marissa Wallfd668622018-05-10 10:21:13 -070093
David Sodman0c69cad2017-08-21 12:12:51 -070094 bool isBufferLatched() const override { return mRefreshPending; }
95
Dominik Laskowskia8955dd2019-07-10 10:19:09 -070096 void notifyAvailableFrames(nsecs_t expectedPresentTime) override;
Chia-I Wu692e0832018-06-05 15:46:58 -070097
Lloyd Pique0449b0f2018-12-20 16:23:45 -080098 bool hasReadyFrame() const override;
David Sodman0c69cad2017-08-21 12:12:51 -070099
David Sodman0c69cad2017-08-21 12:12:51 -0700100 // Returns the current scaling mode, unless mOverrideScalingMode
101 // is set, in which case, it returns mOverrideScalingMode
102 uint32_t getEffectiveScalingMode() const override;
chaviw49a108c2019-08-12 11:23:06 -0700103
104 // Calls latchBuffer if the buffer has a frame queued and then releases the buffer.
105 // This is used if the buffer is just latched and releases to free up the buffer
106 // and will not be shown on screen.
107 // Should only be called on the main thread.
108 void latchAndReleaseBuffer() override;
109
chaviw4244e032019-09-04 11:27:49 -0700110 bool getTransformToDisplayInverse() const override;
111
112 Rect getBufferCrop() const override;
113
114 uint32_t getBufferTransform() const override;
115
116 ui::Dataspace getDataSpace() const override;
117
chaviwd62d3062019-09-04 14:48:02 -0700118 sp<GraphicBuffer> getBuffer() const override;
119
Marissa Wallfd668622018-05-10 10:21:13 -0700120 // -----------------------------------------------------------------------
121
122 // -----------------------------------------------------------------------
123 // Functions that must be implemented by derived classes
124 // -----------------------------------------------------------------------
125private:
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800126 virtual bool fenceHasSignaled() const = 0;
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700127 virtual bool framePresentTimeIsCurrent(nsecs_t expectedPresentTime) const = 0;
Marissa Wallfd668622018-05-10 10:21:13 -0700128
chaviw4244e032019-09-04 11:27:49 -0700129 PixelFormat getPixelFormat() const;
Marissa Wallfd668622018-05-10 10:21:13 -0700130
chaviwf83ce182019-09-12 14:43:08 -0700131 // Computes the transform matrix using the setFilteringEnabled to determine whether the
132 // transform matrix should be computed for use with bilinear filtering.
133 void getDrawingTransformMatrix(bool filteringEnabled, float outMatrix[16]);
134
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700135 virtual uint64_t getFrameNumber(nsecs_t expectedPresentTime) const = 0;
Marissa Wallfd668622018-05-10 10:21:13 -0700136
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
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800145 virtual status_t bindTextureImage() = 0;
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700146 virtual status_t updateTexImage(bool& recomputeVisibleRegions, nsecs_t latchTime,
147 nsecs_t expectedPresentTime) = 0;
Marissa Wallfd668622018-05-10 10:21:13 -0700148
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800149 virtual status_t updateActiveBuffer() = 0;
Marissa Wallfd668622018-05-10 10:21:13 -0700150 virtual status_t updateFrameNumber(nsecs_t latchTime) = 0;
151
Marissa Wallfd668622018-05-10 10:21:13 -0700152protected:
chaviw4244e032019-09-04 11:27:49 -0700153 struct BufferInfo {
154 nsecs_t mDesiredPresentTime;
155 std::shared_ptr<FenceTime> mFenceTime;
156 sp<Fence> mFence;
chaviw4244e032019-09-04 11:27:49 -0700157 uint32_t mTransform{0};
chaviwf83ce182019-09-12 14:43:08 -0700158 ui::Dataspace mDataspace{ui::Dataspace::UNKNOWN};
chaviw4244e032019-09-04 11:27:49 -0700159 Rect mCrop;
160 uint32_t mScaleMode{NATIVE_WINDOW_SCALING_MODE_FREEZE};
161 Region mSurfaceDamage;
162 HdrMetadata mHdrMetadata;
163 int mApi;
164 PixelFormat mPixelFormat;
165 bool mTransformToDisplayInverse{false};
chaviwd62d3062019-09-04 14:48:02 -0700166
167 sp<GraphicBuffer> mBuffer;
168 int mBufferSlot{BufferQueue::INVALID_BUFFER_SLOT};
chaviw74b03172019-08-19 11:09:03 -0700169
170 bool mFrameLatencyNeeded{false};
chaviw4244e032019-09-04 11:27:49 -0700171 };
172
173 BufferInfo mBufferInfo;
174 virtual void gatherBufferInfo() = 0;
175
Vishnu Nairb87d94f2020-02-13 09:17:36 -0800176 std::optional<compositionengine::LayerFE::LayerSettings> prepareClientComposition(
177 compositionengine::LayerFE::ClientCompositionTargetSettings&) override;
178
Lloyd Piquef16688f2019-02-19 17:47:57 -0800179 /*
180 * compositionengine::LayerFE overrides
181 */
Lloyd Piquede196652020-01-22 17:29:58 -0800182 const compositionengine::LayerFECompositionState* getCompositionState() const override;
Lloyd Piquef16688f2019-02-19 17:47:57 -0800183 bool onPreComposition(nsecs_t) override;
Lloyd Piquede196652020-01-22 17:29:58 -0800184 void preparePerFrameCompositionState() override;
Lloyd Piquef5275482019-01-29 18:42:42 -0800185
Marissa Wallfd668622018-05-10 10:21:13 -0700186 // Loads the corresponding system property once per process
187 static bool latchUnsignaledBuffers();
David Sodmaneb085e02017-10-05 18:49:04 -0700188
David Sodman0c69cad2017-08-21 12:12:51 -0700189 // Check all of the local sync points to ensure that all transactions
190 // which need to have been applied prior to the frame which is about to
191 // be latched have signaled
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700192 bool allTransactionsSignaled(nsecs_t expectedPresentTime);
David Sodman0c69cad2017-08-21 12:12:51 -0700193
Marissa Wallfd668622018-05-10 10:21:13 -0700194 static bool getOpacityForFormat(uint32_t format);
David Sodman0c69cad2017-08-21 12:12:51 -0700195
Peiyong Linf3ffc4e2019-12-13 00:46:24 -0800196 // from graphics API
Marissa Wallfd668622018-05-10 10:21:13 -0700197 const uint32_t mTextureName;
198
chaviwf206b662019-01-11 13:07:19 -0800199 bool mRefreshPending{false};
200
chaviw4244e032019-09-04 11:27:49 -0700201 ui::Dataspace translateDataspace(ui::Dataspace dataspace);
chaviwb4c6e582019-08-16 14:35:07 -0700202 void setInitialValuesForClone(const sp<Layer>& clonedFrom);
chaviw74b03172019-08-19 11:09:03 -0700203 void updateCloneBufferInfo() override;
204 uint64_t mPreviousFrameNumber = 0;
chaviw4244e032019-09-04 11:27:49 -0700205
Robert Carrfe1209c2020-02-11 12:25:35 -0800206 virtual uint64_t getHeadFrameNumber(nsecs_t expectedPresentTime) const;
207
Marissa Wallfd668622018-05-10 10:21:13 -0700208private:
Peiyong Linc2020ca2019-01-10 11:36:12 -0800209 // Returns true if this layer requires filtering
Lloyd Piquef16688f2019-02-19 17:47:57 -0800210 bool needsFiltering(const sp<const DisplayDevice>& displayDevice) const override;
Marissa Wallfd668622018-05-10 10:21:13 -0700211
Marissa Wall290ad082019-03-06 13:23:47 -0800212 // BufferStateLayers can return Rect::INVALID_RECT if the layer does not have a display frame
213 // and its parent layer is not bounded
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800214 Rect getBufferSize(const State& s) const override;
Lloyd Piquefeb73d72018-12-04 17:23:44 -0800215
Lloyd Piquede196652020-01-22 17:29:58 -0800216 std::unique_ptr<compositionengine::LayerFECompositionState> mCompositionState;
Vishnu Nair4351ad52019-02-11 14:13:02 -0800217
218 FloatRect computeSourceBounds(const FloatRect& parentBounds) const override;
David Sodman0c69cad2017-08-21 12:12:51 -0700219};
220
221} // namespace android