blob: 0886f17e91c598c9c6a4faa4e760e8c9c22edeb8 [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"
27#include "RenderEngine/Mesh.h"
28#include "RenderEngine/Texture.h"
29#include "SurfaceFlinger.h"
David Sodman0c69cad2017-08-21 12:12:51 -070030#include "Transform.h"
31
32#include <gui/ISurfaceComposerClient.h>
33#include <gui/LayerState.h>
34
David Sodman0c69cad2017-08-21 12:12:51 -070035#include <ui/FrameStats.h>
36#include <ui/GraphicBuffer.h>
37#include <ui/PixelFormat.h>
38#include <ui/Region.h>
39
40#include <utils/RefBase.h>
41#include <utils/String8.h>
42#include <utils/Timers.h>
43
44#include <stdint.h>
45#include <sys/types.h>
46#include <list>
47
48namespace android {
49
50/*
Chia-I Wu0cb75ac2017-11-27 15:56:04 -080051 * A new BufferQueue and a new BufferLayerConsumer are created when the
David Sodman0c69cad2017-08-21 12:12:51 -070052 * BufferLayer is first referenced.
53 *
54 * This also implements onFrameAvailable(), which notifies SurfaceFlinger
55 * that new data has arrived.
56 */
Chia-I Wu0cb75ac2017-11-27 15:56:04 -080057class BufferLayer : public Layer, public BufferLayerConsumer::ContentsChangedListener {
David Sodman0c69cad2017-08-21 12:12:51 -070058public:
59 BufferLayer(SurfaceFlinger* flinger, const sp<Client>& client, const String8& name, uint32_t w,
60 uint32_t h, uint32_t flags);
61
62 ~BufferLayer() override;
63
David Sodmaneb085e02017-10-05 18:49:04 -070064 // If we have received a new buffer this frame, we will pass its surface
65 // damage down to hardware composer. Otherwise, we must send a region with
66 // one empty rect.
67 void useSurfaceDamage();
68 void useEmptyDamage();
69
David Sodman0c69cad2017-08-21 12:12:51 -070070 // -----------------------------------------------------------------------
71 // Overriden from Layer
72 // -----------------------------------------------------------------------
73
74 /*
75 * getTypeId - Provide unique string for each class type in the Layer
76 * hierarchy
77 */
78 const char* getTypeId() const override { return "BufferLayer"; }
79
80 /*
81 * isProtected - true if the layer may contain protected content in the
82 * GRALLOC_USAGE_PROTECTED sense.
83 */
84 bool isProtected() const;
85
86 /*
87 * isVisible - true if this layer is visible, false otherwise
88 */
89 bool isVisible() const override;
90
91 /*
92 * isFixedSize - true if content has a fixed size
93 */
94 bool isFixedSize() const override;
95
96 // the this layer's size and format
97 status_t setBuffers(uint32_t w, uint32_t h, PixelFormat format, uint32_t flags);
98
99 /*
100 * onDraw - draws the surface.
101 */
102 void onDraw(const RenderArea& renderArea, const Region& clip,
103 bool useIdentityTransform) const override;
David Sodmanca10ed22018-04-16 14:10:25 -0700104 void drawNow(const RenderArea& renderArea, bool useIdentityTransform) const;
David Sodman0c69cad2017-08-21 12:12:51 -0700105
David Sodmaneb085e02017-10-05 18:49:04 -0700106 void onLayerDisplayed(const sp<Fence>& releaseFence) override;
David Sodmaneb085e02017-10-05 18:49:04 -0700107
108 void abandon() override;
109 bool shouldPresentNow(const DispSync& dispSync) const override;
110 void setTransformHint(uint32_t orientation) const override;
111 bool onPostComposition(const std::shared_ptr<FenceTime>& glDoneFence,
112 const std::shared_ptr<FenceTime>& presentFence,
113 const CompositorTiming& compositorTiming) override;
114 std::vector<OccupancyTracker::Segment> getOccupancyHistory(bool forceFlush) override;
115 bool getTransformToDisplayInverse() const override;
116
117public:
David Sodman0c69cad2017-08-21 12:12:51 -0700118 bool onPreComposition(nsecs_t refreshStartTime) override;
119
David Sodman0c69cad2017-08-21 12:12:51 -0700120 // If a buffer was replaced this frame, release the former buffer
121 void releasePendingBuffer(nsecs_t dequeueReadyTime);
David Sodman0c69cad2017-08-21 12:12:51 -0700122
123 /*
124 * latchBuffer - called each time the screen is redrawn and returns whether
125 * the visible regions need to be recomputed (this is a fairly heavy
126 * operation, so this should be set only if needed). Typically this is used
127 * to figure out if the content or size of a surface has changed.
128 */
129 Region latchBuffer(bool& recomputeVisibleRegions, nsecs_t latchTime) override;
130 bool isBufferLatched() const override { return mRefreshPending; }
David Sodmaneb085e02017-10-05 18:49:04 -0700131 void setDefaultBufferSize(uint32_t w, uint32_t h) override;
David Sodman0c69cad2017-08-21 12:12:51 -0700132
Chia-I Wu692e0832018-06-05 15:46:58 -0700133 bool isHdrY410() const override;
134
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700135 void setPerFrameData(const sp<const DisplayDevice>& display) override;
David Sodmaneb085e02017-10-05 18:49:04 -0700136
David Sodman0c69cad2017-08-21 12:12:51 -0700137 bool isOpaque(const Layer::State& s) const override;
138
139private:
140 void onFirstRef() override;
141
142 // Interface implementation for
Chia-I Wu0cb75ac2017-11-27 15:56:04 -0800143 // BufferLayerConsumer::ContentsChangedListener
David Sodman0c69cad2017-08-21 12:12:51 -0700144 void onFrameAvailable(const BufferItem& item) override;
145 void onFrameReplaced(const BufferItem& item) override;
146 void onSidebandStreamChanged() override;
147
148 // needsLinearFiltering - true if this surface's state requires filtering
149 bool needsFiltering(const RenderArea& renderArea) const;
150
151 static bool getOpacityForFormat(uint32_t format);
152
153 // drawing
David Sodman41fdfc92017-11-06 16:09:56 -0800154 void drawWithOpenGL(const RenderArea& renderArea, bool useIdentityTransform) const;
David Sodman0c69cad2017-08-21 12:12:51 -0700155
156 // Temporary - Used only for LEGACY camera mode.
157 uint32_t getProducerStickyTransform() const;
158
159 // Loads the corresponding system property once per process
160 static bool latchUnsignaledBuffers();
161
162 uint64_t getHeadFrameNumber() const;
163 bool headFenceHasSignaled() const;
164
165 // Returns the current scaling mode, unless mOverrideScalingMode
166 // is set, in which case, it returns mOverrideScalingMode
167 uint32_t getEffectiveScalingMode() const override;
168
169public:
170 void notifyAvailableFrames() override;
171
172 PixelFormat getPixelFormat() const override { return mFormat; }
173 sp<IGraphicBufferProducer> getProducer() const;
174
175private:
Chia-I Wub28c6742017-12-27 10:59:54 -0800176 sp<BufferLayerConsumer> mConsumer;
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
181 bool allTransactionsSignaled();
182 sp<IGraphicBufferProducer> mProducer;
183
184 // constants
185 uint32_t mTextureName; // from GLES
186 PixelFormat mFormat;
187
188 // main thread
189 uint32_t mCurrentScalingMode;
190 bool mBufferLatched = false; // TODO: Use mActiveBuffer?
191 uint64_t mPreviousFrameNumber; // Only accessed on the main thread.
192 // The texture used to draw the layer in GLES composition mode
193 mutable Texture mTexture;
194
195 bool mUpdateTexImageFailed; // This is only accessed on the main thread.
196 bool mRefreshPending;
197};
198
199} // namespace android