blob: 2118f4ad18aa9f81acd84735f29c1f5a495cb99f [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"
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;
David Sodman0c69cad2017-08-21 12:12:51 -070065
Marissa Wallfd668622018-05-10 10:21:13 -070066 // isVisible - true if this layer is visible, false otherwise
Lloyd Pique0449b0f2018-12-20 16:23:45 -080067 bool isVisible() const override;
David Sodman0c69cad2017-08-21 12:12:51 -070068
Peiyong Linfb530cf2018-12-15 05:07:38 +000069 // isProtected - true if the layer may contain protected content in the
70 // GRALLOC_USAGE_PROTECTED sense.
71 bool isProtected() const override;
72
Marissa Wallfd668622018-05-10 10:21:13 -070073 // isFixedSize - true if content has a fixed size
David Sodman0c69cad2017-08-21 12:12:51 -070074 bool isFixedSize() const override;
75
Lloyd Piquea83776c2019-01-29 18:42:32 -080076 bool usesSourceCrop() const override;
77
Marissa Wallfd668622018-05-10 10:21:13 -070078 bool isHdrY410() const override;
David Sodmaneb085e02017-10-05 18:49:04 -070079
Dominik Laskowskib7251f42020-04-20 17:42:59 -070080 bool onPostComposition(const DisplayDevice*, const std::shared_ptr<FenceTime>& glDoneFence,
David Sodmaneb085e02017-10-05 18:49:04 -070081 const std::shared_ptr<FenceTime>& presentFence,
Dominik Laskowskib7251f42020-04-20 17:42:59 -070082 const CompositorTiming&) override;
David Sodmaneb085e02017-10-05 18:49:04 -070083
Marissa Wallfd668622018-05-10 10:21:13 -070084 // latchBuffer - called each time the screen is redrawn and returns whether
85 // the visible regions need to be recomputed (this is a fairly heavy
86 // operation, so this should be set only if needed). Typically this is used
87 // to figure out if the content or size of a surface has changed.
Dominik Laskowskia8955dd2019-07-10 10:19:09 -070088 bool latchBuffer(bool& recomputeVisibleRegions, nsecs_t latchTime,
89 nsecs_t expectedPresentTime) override;
Marissa Wallfd668622018-05-10 10:21:13 -070090
David Sodman0c69cad2017-08-21 12:12:51 -070091 bool isBufferLatched() const override { return mRefreshPending; }
92
Dominik Laskowskia8955dd2019-07-10 10:19:09 -070093 void notifyAvailableFrames(nsecs_t expectedPresentTime) override;
Chia-I Wu692e0832018-06-05 15:46:58 -070094
Lloyd Pique0449b0f2018-12-20 16:23:45 -080095 bool hasReadyFrame() const override;
David Sodman0c69cad2017-08-21 12:12:51 -070096
Robert Carr916b0362020-10-06 13:53:03 -070097 // Returns the current scaling mode
David Sodman0c69cad2017-08-21 12:12:51 -070098 uint32_t getEffectiveScalingMode() const override;
chaviw49a108c2019-08-12 11:23:06 -070099
100 // Calls latchBuffer if the buffer has a frame queued and then releases the buffer.
101 // This is used if the buffer is just latched and releases to free up the buffer
102 // and will not be shown on screen.
103 // Should only be called on the main thread.
104 void latchAndReleaseBuffer() override;
105
chaviw4244e032019-09-04 11:27:49 -0700106 bool getTransformToDisplayInverse() const override;
107
108 Rect getBufferCrop() const override;
109
110 uint32_t getBufferTransform() const override;
111
112 ui::Dataspace getDataSpace() const override;
113
chaviwd62d3062019-09-04 14:48:02 -0700114 sp<GraphicBuffer> getBuffer() const override;
115
Vishnu Nair6213bd92020-05-08 17:42:25 -0700116 ui::Transform::RotationFlags getTransformHint() const override { return mTransformHint; }
117
Vishnu Naire7f79c52020-10-29 14:45:03 -0700118 // Returns true if the transformed buffer size does not match the layer size and we need
119 // to apply filtering.
120 virtual bool bufferNeedsFiltering() const;
121
Marissa Wallfd668622018-05-10 10:21:13 -0700122protected:
chaviw4244e032019-09-04 11:27:49 -0700123 struct BufferInfo {
124 nsecs_t mDesiredPresentTime;
125 std::shared_ptr<FenceTime> mFenceTime;
126 sp<Fence> mFence;
chaviw4244e032019-09-04 11:27:49 -0700127 uint32_t mTransform{0};
chaviwf83ce182019-09-12 14:43:08 -0700128 ui::Dataspace mDataspace{ui::Dataspace::UNKNOWN};
chaviw4244e032019-09-04 11:27:49 -0700129 Rect mCrop;
130 uint32_t mScaleMode{NATIVE_WINDOW_SCALING_MODE_FREEZE};
131 Region mSurfaceDamage;
132 HdrMetadata mHdrMetadata;
133 int mApi;
chaviwdebadb82020-03-26 14:57:24 -0700134 PixelFormat mPixelFormat{PIXEL_FORMAT_NONE};
chaviw4244e032019-09-04 11:27:49 -0700135 bool mTransformToDisplayInverse{false};
chaviwd62d3062019-09-04 14:48:02 -0700136
137 sp<GraphicBuffer> mBuffer;
138 int mBufferSlot{BufferQueue::INVALID_BUFFER_SLOT};
chaviw74b03172019-08-19 11:09:03 -0700139
140 bool mFrameLatencyNeeded{false};
chaviw4244e032019-09-04 11:27:49 -0700141 };
142
143 BufferInfo mBufferInfo;
144 virtual void gatherBufferInfo() = 0;
145
Vishnu Nairb87d94f2020-02-13 09:17:36 -0800146 std::optional<compositionengine::LayerFE::LayerSettings> prepareClientComposition(
147 compositionengine::LayerFE::ClientCompositionTargetSettings&) override;
148
Lloyd Piquef16688f2019-02-19 17:47:57 -0800149 /*
150 * compositionengine::LayerFE overrides
151 */
Lloyd Piquede196652020-01-22 17:29:58 -0800152 const compositionengine::LayerFECompositionState* getCompositionState() const override;
Lloyd Piquef16688f2019-02-19 17:47:57 -0800153 bool onPreComposition(nsecs_t) override;
Lloyd Piquede196652020-01-22 17:29:58 -0800154 void preparePerFrameCompositionState() override;
Lloyd Piquef5275482019-01-29 18:42:42 -0800155
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
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700159 bool allTransactionsSignaled(nsecs_t expectedPresentTime);
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
Peiyong Linf3ffc4e2019-12-13 00:46:24 -0800163 // from graphics API
Marissa Wallfd668622018-05-10 10:21:13 -0700164 const uint32_t mTextureName;
165
chaviwf206b662019-01-11 13:07:19 -0800166 bool mRefreshPending{false};
167
chaviw4244e032019-09-04 11:27:49 -0700168 ui::Dataspace translateDataspace(ui::Dataspace dataspace);
chaviwb4c6e582019-08-16 14:35:07 -0700169 void setInitialValuesForClone(const sp<Layer>& clonedFrom);
chaviw74b03172019-08-19 11:09:03 -0700170 void updateCloneBufferInfo() override;
171 uint64_t mPreviousFrameNumber = 0;
chaviw4244e032019-09-04 11:27:49 -0700172
Vishnu Nair6b7c5c92020-09-29 17:27:05 -0700173 uint64_t getHeadFrameNumber(nsecs_t expectedPresentTime) const override;
Robert Carrfe1209c2020-02-11 12:25:35 -0800174
Dominik Laskowskib7251f42020-04-20 17:42:59 -0700175 void setTransformHint(ui::Transform::RotationFlags displayTransformHint) override;
176
Vishnu Nair6213bd92020-05-08 17:42:25 -0700177 // Transform hint provided to the producer. This must be accessed holding
Ady Abraham63a3e592021-01-06 10:47:15 -0800178 // the mStateLock.
Dominik Laskowskib7251f42020-04-20 17:42:59 -0700179 ui::Transform::RotationFlags mTransformHint = ui::Transform::ROT_0;
Vishnu Nair6213bd92020-05-08 17:42:25 -0700180
Vishnu Naircf26a0a2020-11-13 12:56:20 -0800181 bool getAutoRefresh() const { return mAutoRefresh; }
182 bool getSidebandStreamChanged() const { return mSidebandStreamChanged; }
183
Ady Abraham63a3e592021-01-06 10:47:15 -0800184 // Returns true if the next buffer should be presented at the expected present time
185 bool shouldPresentNow(nsecs_t expectedPresentTime) const final;
186
187 // Returns true if the next buffer should be presented at the expected present time,
188 // overridden by BufferStateLayer and BufferQueueLayer for implementation
189 // specific logic
190 virtual bool isBufferDue(nsecs_t /*expectedPresentTime*/) const = 0;
191
192 // Returns true if the next frame is considered too early to present
193 // at the given expectedPresentTime
194 bool frameIsEarly(nsecs_t expectedPresentTime) const;
195
Vishnu Naircf26a0a2020-11-13 12:56:20 -0800196 std::atomic<bool> mAutoRefresh{false};
197 std::atomic<bool> mSidebandStreamChanged{false};
198
Marissa Wallfd668622018-05-10 10:21:13 -0700199private:
Peiyong Linf243e5e2020-08-22 17:40:59 -0700200 virtual bool fenceHasSignaled() const = 0;
201 virtual bool framePresentTimeIsCurrent(nsecs_t expectedPresentTime) const = 0;
202 virtual uint64_t getFrameNumber(nsecs_t expectedPresentTime) const = 0;
203
Peiyong Linf243e5e2020-08-22 17:40:59 -0700204
205 // Latch sideband stream and returns true if the dirty region should be updated.
206 virtual bool latchSidebandStream(bool& recomputeVisibleRegions) = 0;
207
208 virtual bool hasFrameUpdate() const = 0;
209
Peiyong Linf243e5e2020-08-22 17:40:59 -0700210 virtual status_t updateTexImage(bool& recomputeVisibleRegions, nsecs_t latchTime,
211 nsecs_t expectedPresentTime) = 0;
212
213 virtual status_t updateActiveBuffer() = 0;
214 virtual status_t updateFrameNumber(nsecs_t latchTime) = 0;
215
216 // We generate InputWindowHandles for all non-cursor buffered layers regardless of whether they
217 // have an InputChannel. This is to enable the InputDispatcher to do PID based occlusion
218 // detection.
219 bool needsInputInfo() const override { return !mPotentialCursor; }
220
Peiyong Linc2020ca2019-01-10 11:36:12 -0800221 // Returns true if this layer requires filtering
Dominik Laskowskib7251f42020-04-20 17:42:59 -0700222 bool needsFiltering(const DisplayDevice*) const override;
223 bool needsFilteringForScreenshots(const DisplayDevice*,
Alec Mouri5a6d8572020-03-23 23:56:15 -0700224 const ui::Transform& inverseParentTransform) const override;
Marissa Wallfd668622018-05-10 10:21:13 -0700225
Marissa Wall290ad082019-03-06 13:23:47 -0800226 // BufferStateLayers can return Rect::INVALID_RECT if the layer does not have a display frame
227 // and its parent layer is not bounded
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800228 Rect getBufferSize(const State& s) const override;
Lloyd Piquefeb73d72018-12-04 17:23:44 -0800229
Peiyong Linf243e5e2020-08-22 17:40:59 -0700230 PixelFormat getPixelFormat() const;
231
232 // Computes the transform matrix using the setFilteringEnabled to determine whether the
233 // transform matrix should be computed for use with bilinear filtering.
234 void getDrawingTransformMatrix(bool filteringEnabled, float outMatrix[16]);
235
Lloyd Piquede196652020-01-22 17:29:58 -0800236 std::unique_ptr<compositionengine::LayerFECompositionState> mCompositionState;
Vishnu Nair4351ad52019-02-11 14:13:02 -0800237
238 FloatRect computeSourceBounds(const FloatRect& parentBounds) const override;
Ady Abrahamce4adf12020-12-15 18:45:12 -0800239
Ady Abraham63a3e592021-01-06 10:47:15 -0800240 // Returns the predicted present time of the next frame if available
241 virtual std::optional<nsecs_t> nextPredictedPresentTime() const = 0;
Ady Abrahamce4adf12020-12-15 18:45:12 -0800242
243 // The amount of time SF can delay a frame if it is considered early based
244 // on the VsyncModulator::VsyncConfig::appWorkDuration
245 static constexpr std::chrono::nanoseconds kEarlyLatchMaxThreshold = 100ms;
David Sodman0c69cad2017-08-21 12:12:51 -0700246};
247
248} // namespace android