blob: be16cf584d5b3b5b5b46ec211afce874f4e05be7 [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
Chia-I Wu0cb75ac2017-11-27 15:56:04 -080019#include "BufferLayerConsumer.h"
David Sodman0c69cad2017-08-21 12:12:51 -070020#include "Client.h"
David Sodman41fdfc92017-11-06 16:09:56 -080021#include "Layer.h"
David Sodman0c69cad2017-08-21 12:12:51 -070022#include "DisplayHardware/HWComposer.h"
23#include "DisplayHardware/HWComposerBufferCache.h"
24#include "FrameTracker.h"
25#include "LayerVector.h"
26#include "MonitoredProducer.h"
David Sodman0c69cad2017-08-21 12:12:51 -070027#include "SurfaceFlinger.h"
David Sodman0c69cad2017-08-21 12:12:51 -070028
29#include <gui/ISurfaceComposerClient.h>
30#include <gui/LayerState.h>
Peiyong Lincbc184f2018-08-22 13:24:10 -070031#include <renderengine/Mesh.h>
32#include <renderengine/Texture.h>
David Sodman0c69cad2017-08-21 12:12:51 -070033#include <ui/FrameStats.h>
34#include <ui/GraphicBuffer.h>
35#include <ui/PixelFormat.h>
36#include <ui/Region.h>
37
38#include <utils/RefBase.h>
39#include <utils/String8.h>
40#include <utils/Timers.h>
41
Lloyd Pique42ab75e2018-09-12 20:46:03 -070042#include <system/window.h> // For NATIVE_WINDOW_SCALING_MODE_FREEZE
43
David Sodman0c69cad2017-08-21 12:12:51 -070044#include <stdint.h>
45#include <sys/types.h>
46#include <list>
47
48namespace android {
49
Marissa Wallfd668622018-05-10 10:21:13 -070050class BufferLayer : public Layer {
David Sodman0c69cad2017-08-21 12:12:51 -070051public:
Lloyd Pique42ab75e2018-09-12 20:46:03 -070052 explicit BufferLayer(const LayerCreationArgs& args);
David Sodman0c69cad2017-08-21 12:12:51 -070053 ~BufferLayer() override;
54
55 // -----------------------------------------------------------------------
56 // Overriden from Layer
57 // -----------------------------------------------------------------------
Marissa Wallfd668622018-05-10 10:21:13 -070058public:
59 // 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 // getTypeId - Provide unique string for each class type in the Layer
66 // hierarchy
David Sodman0c69cad2017-08-21 12:12:51 -070067 const char* getTypeId() const override { return "BufferLayer"; }
68
Marissa Wallfd668622018-05-10 10:21:13 -070069 bool isOpaque(const Layer::State& s) const override;
David Sodman0c69cad2017-08-21 12:12:51 -070070
Marissa Wallfd668622018-05-10 10:21:13 -070071 // isVisible - true if this layer is visible, false otherwise
Lloyd Pique0449b0f2018-12-20 16:23:45 -080072 bool isVisible() const override;
David Sodman0c69cad2017-08-21 12:12:51 -070073
Peiyong Linfb530cf2018-12-15 05:07:38 +000074 // isProtected - true if the layer may contain protected content in the
75 // GRALLOC_USAGE_PROTECTED sense.
76 bool isProtected() const override;
77
Marissa Wallfd668622018-05-10 10:21:13 -070078 // isFixedSize - true if content has a fixed size
David Sodman0c69cad2017-08-21 12:12:51 -070079 bool isFixedSize() const override;
80
Marissa Wallfd668622018-05-10 10:21:13 -070081 // onDraw - draws the surface.
David Sodman0c69cad2017-08-21 12:12:51 -070082 void onDraw(const RenderArea& renderArea, const Region& clip,
Marissa Wall61c58622018-07-18 10:12:20 -070083 bool useIdentityTransform) override;
David Sodman0c69cad2017-08-21 12:12:51 -070084
Marissa Wallfd668622018-05-10 10:21:13 -070085 bool isHdrY410() const override;
David Sodmaneb085e02017-10-05 18:49:04 -070086
Dominik Laskowski075d3172018-05-24 15:50:06 -070087 void setPerFrameData(DisplayId displayId, const ui::Transform& transform, const Rect& viewport,
88 int32_t supportedPerFrameMetadata) override;
Marissa Wallfd668622018-05-10 10:21:13 -070089
90 bool onPreComposition(nsecs_t refreshStartTime) override;
Dominik Laskowski075d3172018-05-24 15:50:06 -070091 bool onPostComposition(const std::optional<DisplayId>& displayId,
92 const std::shared_ptr<FenceTime>& glDoneFence,
David Sodmaneb085e02017-10-05 18:49:04 -070093 const std::shared_ptr<FenceTime>& presentFence,
Lloyd Pique0449b0f2018-12-20 16:23:45 -080094 const CompositorTiming& compositorTiming) override;
David Sodmaneb085e02017-10-05 18:49:04 -070095
Marissa Wallfd668622018-05-10 10:21:13 -070096 // latchBuffer - called each time the screen is redrawn and returns whether
97 // the visible regions need to be recomputed (this is a fairly heavy
98 // operation, so this should be set only if needed). Typically this is used
99 // to figure out if the content or size of a surface has changed.
Alec Mouri86770e52018-09-24 22:40:58 +0000100 // If there was a GL composition step rendering the previous frame, then
101 // releaseFence will be populated with a native fence that fires when
102 // composition has completed.
103 Region latchBuffer(bool& recomputeVisibleRegions, nsecs_t latchTime,
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800104 const sp<Fence>& releaseFence) override;
Marissa Wallfd668622018-05-10 10:21:13 -0700105
David Sodman0c69cad2017-08-21 12:12:51 -0700106 bool isBufferLatched() const override { return mRefreshPending; }
107
Marissa Wallfd668622018-05-10 10:21:13 -0700108 void notifyAvailableFrames() override;
Chia-I Wu692e0832018-06-05 15:46:58 -0700109
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800110 bool hasReadyFrame() const override;
David Sodman0c69cad2017-08-21 12:12:51 -0700111
David Sodman0c69cad2017-08-21 12:12:51 -0700112 // Returns the current scaling mode, unless mOverrideScalingMode
113 // is set, in which case, it returns mOverrideScalingMode
114 uint32_t getEffectiveScalingMode() const override;
Marissa Wallfd668622018-05-10 10:21:13 -0700115 // -----------------------------------------------------------------------
116
117 // -----------------------------------------------------------------------
118 // Functions that must be implemented by derived classes
119 // -----------------------------------------------------------------------
120private:
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800121 virtual bool fenceHasSignaled() const = 0;
Marissa Wallfd668622018-05-10 10:21:13 -0700122
123 virtual nsecs_t getDesiredPresentTime() = 0;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800124 virtual std::shared_ptr<FenceTime> getCurrentFenceTime() const = 0;
Marissa Wallfd668622018-05-10 10:21:13 -0700125
Marissa Wall61c58622018-07-18 10:12:20 -0700126 virtual void getDrawingTransformMatrix(float *matrix) = 0;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800127 virtual uint32_t getDrawingTransform() const = 0;
128 virtual ui::Dataspace getDrawingDataSpace() const = 0;
129 virtual Rect getDrawingCrop() const = 0;
Marissa Wallfd668622018-05-10 10:21:13 -0700130 virtual uint32_t getDrawingScalingMode() const = 0;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800131 virtual Region getDrawingSurfaceDamage() const = 0;
132 virtual const HdrMetadata& getDrawingHdrMetadata() const = 0;
133 virtual int getDrawingApi() const = 0;
Marissa Wallfd668622018-05-10 10:21:13 -0700134 virtual PixelFormat getPixelFormat() const = 0;
135
136 virtual uint64_t getFrameNumber() const = 0;
137
138 virtual bool getAutoRefresh() const = 0;
139 virtual bool getSidebandStreamChanged() const = 0;
140
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800141 virtual std::optional<Region> 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
170private:
171 // needsLinearFiltering - true if this surface's state requires filtering
172 bool needsFiltering(const RenderArea& renderArea) const;
173
174 // drawing
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800175 void drawWithOpenGL(const RenderArea& renderArea, bool useIdentityTransform) const;
Marissa Wallfd668622018-05-10 10:21:13 -0700176
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800177 uint64_t getHeadFrameNumber() const;
Marissa Wallfd668622018-05-10 10:21:13 -0700178
Lloyd Pique42ab75e2018-09-12 20:46:03 -0700179 uint32_t mCurrentScalingMode{NATIVE_WINDOW_SCALING_MODE_FREEZE};
Marissa Wallfd668622018-05-10 10:21:13 -0700180
181 // main thread.
Lloyd Pique42ab75e2018-09-12 20:46:03 -0700182 bool mBufferLatched{false}; // TODO: Use mActiveBuffer?
Marissa Wallfd668622018-05-10 10:21:13 -0700183
David Sodman0c69cad2017-08-21 12:12:51 -0700184 // The texture used to draw the layer in GLES composition mode
Peiyong Lin833074a2018-08-28 11:53:54 -0700185 mutable renderengine::Texture mTexture;
David Sodman0c69cad2017-08-21 12:12:51 -0700186
Lloyd Pique42ab75e2018-09-12 20:46:03 -0700187 bool mRefreshPending{false};
Vishnu Nair60356342018-11-13 13:00:45 -0800188
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800189 Rect getBufferSize(const State& s) const override;
David Sodman0c69cad2017-08-21 12:12:51 -0700190};
191
192} // namespace android