blob: 3bb0fb3eb46240f63781a5feaeeeaca26e76ae12 [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
Huihong Luod3d8f8e2022-03-08 14:48:46 -080023#include <android/gui/ISurfaceComposerClient.h>
David Sodman0c69cad2017-08-21 12:12:51 -070024#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"
Adithya Srinivasan5f683cf2020-09-15 14:21:04 -070040#include "FrameTimeline.h"
Lloyd Piquefeb73d72018-12-04 17:23:44 -080041#include "FrameTracker.h"
42#include "Layer.h"
43#include "LayerVector.h"
44#include "MonitoredProducer.h"
45#include "SurfaceFlinger.h"
David Sodman0c69cad2017-08-21 12:12:51 -070046
47namespace android {
48
Marissa Wallfd668622018-05-10 10:21:13 -070049class BufferLayer : public Layer {
David Sodman0c69cad2017-08-21 12:12:51 -070050public:
Lloyd Pique42ab75e2018-09-12 20:46:03 -070051 explicit BufferLayer(const LayerCreationArgs& args);
Alec Mouri4545a8a2019-08-08 20:05:32 -070052 virtual ~BufferLayer() override;
David Sodman0c69cad2017-08-21 12:12:51 -070053
Peiyong Linf243e5e2020-08-22 17:40:59 -070054 // Implements Layer.
Lloyd Piquede196652020-01-22 17:29:58 -080055 sp<compositionengine::LayerFE> getCompositionEngineLayerFE() const override;
56 compositionengine::LayerFECompositionState* editCompositionState() override;
Lloyd Piquefeb73d72018-12-04 17:23:44 -080057
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 bool isOpaque(const Layer::State& s) const override;
Arthur Hungfb2ebce2021-10-04 14:08:48 +000065 bool canReceiveInput() 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
Alec Mouri1dc4bfa2021-10-01 16:31:08 -070081 void onPostComposition(const DisplayDevice*, const std::shared_ptr<FenceTime>& glDoneFence,
David Sodmaneb085e02017-10-05 18:49:04 -070082 const std::shared_ptr<FenceTime>& presentFence,
Dominik Laskowskib7251f42020-04-20 17:42:59 -070083 const CompositorTiming&) override;
David Sodmaneb085e02017-10-05 18:49:04 -070084
Marissa Wallfd668622018-05-10 10:21:13 -070085 // latchBuffer - called each time the screen is redrawn and returns whether
86 // the visible regions need to be recomputed (this is a fairly heavy
87 // operation, so this should be set only if needed). Typically this is used
88 // to figure out if the content or size of a surface has changed.
Dominik Laskowskia8955dd2019-07-10 10:19:09 -070089 bool latchBuffer(bool& recomputeVisibleRegions, nsecs_t latchTime,
90 nsecs_t expectedPresentTime) override;
Lloyd Pique0449b0f2018-12-20 16:23:45 -080091 bool hasReadyFrame() const override;
David Sodman0c69cad2017-08-21 12:12:51 -070092
Robert Carr916b0362020-10-06 13:53:03 -070093 // Returns the current scaling mode
David Sodman0c69cad2017-08-21 12:12:51 -070094 uint32_t getEffectiveScalingMode() const override;
chaviw49a108c2019-08-12 11:23:06 -070095
96 // Calls latchBuffer if the buffer has a frame queued and then releases the buffer.
97 // This is used if the buffer is just latched and releases to free up the buffer
98 // and will not be shown on screen.
99 // Should only be called on the main thread.
100 void latchAndReleaseBuffer() override;
101
chaviw4244e032019-09-04 11:27:49 -0700102 bool getTransformToDisplayInverse() const override;
103
104 Rect getBufferCrop() const override;
105
106 uint32_t getBufferTransform() const override;
107
108 ui::Dataspace getDataSpace() const override;
109
chaviwd62d3062019-09-04 14:48:02 -0700110 sp<GraphicBuffer> getBuffer() const override;
Vishnu Naird37343b2022-01-12 16:18:56 -0800111 const std::shared_ptr<renderengine::ExternalTexture>& getExternalTexture() const override;
chaviwd62d3062019-09-04 14:48:02 -0700112
Vishnu Nair6213bd92020-05-08 17:42:25 -0700113 ui::Transform::RotationFlags getTransformHint() const override { return mTransformHint; }
114
Vishnu Naire7f79c52020-10-29 14:45:03 -0700115 // Returns true if the transformed buffer size does not match the layer size and we need
116 // to apply filtering.
117 virtual bool bufferNeedsFiltering() const;
118
Marissa Wallfd668622018-05-10 10:21:13 -0700119protected:
chaviw4244e032019-09-04 11:27:49 -0700120 struct BufferInfo {
121 nsecs_t mDesiredPresentTime;
122 std::shared_ptr<FenceTime> mFenceTime;
123 sp<Fence> mFence;
chaviw4244e032019-09-04 11:27:49 -0700124 uint32_t mTransform{0};
chaviwf83ce182019-09-12 14:43:08 -0700125 ui::Dataspace mDataspace{ui::Dataspace::UNKNOWN};
chaviw4244e032019-09-04 11:27:49 -0700126 Rect mCrop;
127 uint32_t mScaleMode{NATIVE_WINDOW_SCALING_MODE_FREEZE};
128 Region mSurfaceDamage;
129 HdrMetadata mHdrMetadata;
130 int mApi;
chaviwdebadb82020-03-26 14:57:24 -0700131 PixelFormat mPixelFormat{PIXEL_FORMAT_NONE};
chaviw4244e032019-09-04 11:27:49 -0700132 bool mTransformToDisplayInverse{false};
chaviwd62d3062019-09-04 14:48:02 -0700133
Alec Mouria90a5702021-04-16 16:36:21 +0000134 std::shared_ptr<renderengine::ExternalTexture> mBuffer;
Vishnu Nair4ba0c2e2021-06-24 11:27:17 -0700135 uint64_t mFrameNumber;
chaviwd62d3062019-09-04 14:48:02 -0700136 int mBufferSlot{BufferQueue::INVALID_BUFFER_SLOT};
chaviw74b03172019-08-19 11:09:03 -0700137
138 bool mFrameLatencyNeeded{false};
chaviw4244e032019-09-04 11:27:49 -0700139 };
140
141 BufferInfo mBufferInfo;
142 virtual void gatherBufferInfo() = 0;
143
Vishnu Nairb87d94f2020-02-13 09:17:36 -0800144 std::optional<compositionengine::LayerFE::LayerSettings> prepareClientComposition(
145 compositionengine::LayerFE::ClientCompositionTargetSettings&) override;
146
Lloyd Piquef16688f2019-02-19 17:47:57 -0800147 /*
148 * compositionengine::LayerFE overrides
149 */
Lloyd Piquede196652020-01-22 17:29:58 -0800150 const compositionengine::LayerFECompositionState* getCompositionState() const override;
Lloyd Piquef16688f2019-02-19 17:47:57 -0800151 bool onPreComposition(nsecs_t) override;
Lloyd Piquede196652020-01-22 17:29:58 -0800152 void preparePerFrameCompositionState() override;
Lloyd Piquef5275482019-01-29 18:42:42 -0800153
Leon Scroggins III4d258852021-11-24 14:40:53 -0500154 static bool getOpacityForFormat(PixelFormat format);
David Sodman0c69cad2017-08-21 12:12:51 -0700155
Peiyong Linf3ffc4e2019-12-13 00:46:24 -0800156 // from graphics API
Marissa Wallfd668622018-05-10 10:21:13 -0700157 const uint32_t mTextureName;
chaviw4244e032019-09-04 11:27:49 -0700158 ui::Dataspace translateDataspace(ui::Dataspace dataspace);
chaviwb4c6e582019-08-16 14:35:07 -0700159 void setInitialValuesForClone(const sp<Layer>& clonedFrom);
chaviw74b03172019-08-19 11:09:03 -0700160 void updateCloneBufferInfo() override;
161 uint64_t mPreviousFrameNumber = 0;
chaviw4244e032019-09-04 11:27:49 -0700162
Dominik Laskowskib7251f42020-04-20 17:42:59 -0700163 void setTransformHint(ui::Transform::RotationFlags displayTransformHint) override;
164
Vishnu Nair6213bd92020-05-08 17:42:25 -0700165 // Transform hint provided to the producer. This must be accessed holding
Ady Abraham63a3e592021-01-06 10:47:15 -0800166 // the mStateLock.
Dominik Laskowskib7251f42020-04-20 17:42:59 -0700167 ui::Transform::RotationFlags mTransformHint = ui::Transform::ROT_0;
Vishnu Nair6213bd92020-05-08 17:42:25 -0700168
Vishnu Nair86653e92021-11-03 17:19:36 -0700169 bool getAutoRefresh() const { return mDrawingState.autoRefresh; }
Vishnu Naircf26a0a2020-11-13 12:56:20 -0800170 bool getSidebandStreamChanged() const { return mSidebandStreamChanged; }
171
Ady Abraham63a3e592021-01-06 10:47:15 -0800172 // Returns true if the next buffer should be presented at the expected present time
Robert Carr6a5dc1f2021-03-11 12:36:40 -0800173 bool shouldPresentNow(nsecs_t expectedPresentTime) const;
Ady Abraham63a3e592021-01-06 10:47:15 -0800174
175 // Returns true if the next buffer should be presented at the expected present time,
176 // overridden by BufferStateLayer and BufferQueueLayer for implementation
177 // specific logic
178 virtual bool isBufferDue(nsecs_t /*expectedPresentTime*/) const = 0;
179
Vishnu Naircf26a0a2020-11-13 12:56:20 -0800180 std::atomic<bool> mSidebandStreamChanged{false};
181
Marissa Wallfd668622018-05-10 10:21:13 -0700182private:
Peiyong Linf243e5e2020-08-22 17:40:59 -0700183 virtual bool fenceHasSignaled() const = 0;
184 virtual bool framePresentTimeIsCurrent(nsecs_t expectedPresentTime) const = 0;
Peiyong Linf243e5e2020-08-22 17:40:59 -0700185
186 // Latch sideband stream and returns true if the dirty region should be updated.
187 virtual bool latchSidebandStream(bool& recomputeVisibleRegions) = 0;
188
189 virtual bool hasFrameUpdate() const = 0;
190
Peiyong Linf243e5e2020-08-22 17:40:59 -0700191 virtual status_t updateTexImage(bool& recomputeVisibleRegions, nsecs_t latchTime,
192 nsecs_t expectedPresentTime) = 0;
193
194 virtual status_t updateActiveBuffer() = 0;
Alec Mouri8977ce92022-05-07 00:34:50 +0000195 virtual status_t updateFrameNumber() = 0;
Peiyong Linf243e5e2020-08-22 17:40:59 -0700196
197 // We generate InputWindowHandles for all non-cursor buffered layers regardless of whether they
198 // have an InputChannel. This is to enable the InputDispatcher to do PID based occlusion
199 // detection.
200 bool needsInputInfo() const override { return !mPotentialCursor; }
201
Peiyong Linc2020ca2019-01-10 11:36:12 -0800202 // Returns true if this layer requires filtering
Dominik Laskowskib7251f42020-04-20 17:42:59 -0700203 bool needsFiltering(const DisplayDevice*) const override;
204 bool needsFilteringForScreenshots(const DisplayDevice*,
Alec Mouri5a6d8572020-03-23 23:56:15 -0700205 const ui::Transform& inverseParentTransform) const override;
Marissa Wallfd668622018-05-10 10:21:13 -0700206
Marissa Wall290ad082019-03-06 13:23:47 -0800207 // BufferStateLayers can return Rect::INVALID_RECT if the layer does not have a display frame
208 // and its parent layer is not bounded
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800209 Rect getBufferSize(const State& s) const override;
Lloyd Piquefeb73d72018-12-04 17:23:44 -0800210
Peiyong Linf243e5e2020-08-22 17:40:59 -0700211 PixelFormat getPixelFormat() const;
212
213 // Computes the transform matrix using the setFilteringEnabled to determine whether the
214 // transform matrix should be computed for use with bilinear filtering.
215 void getDrawingTransformMatrix(bool filteringEnabled, float outMatrix[16]);
216
Lloyd Piquede196652020-01-22 17:29:58 -0800217 std::unique_ptr<compositionengine::LayerFECompositionState> mCompositionState;
Vishnu Nair4351ad52019-02-11 14:13:02 -0800218
219 FloatRect computeSourceBounds(const FloatRect& parentBounds) const override;
David Sodman0c69cad2017-08-21 12:12:51 -0700220};
221
222} // namespace android