1 /* 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 19 #include <sys/types.h> 20 #include <cstdint> 21 #include <list> 22 23 #include <gui/ISurfaceComposerClient.h> 24 #include <gui/LayerState.h> 25 #include <renderengine/Image.h> 26 #include <renderengine/Mesh.h> 27 #include <renderengine/Texture.h> 28 #include <system/window.h> // For NATIVE_WINDOW_SCALING_MODE_FREEZE 29 #include <ui/FrameStats.h> 30 #include <ui/GraphicBuffer.h> 31 #include <ui/PixelFormat.h> 32 #include <ui/Region.h> 33 #include <utils/RefBase.h> 34 #include <utils/String8.h> 35 #include <utils/Timers.h> 36 37 #include "BufferLayerConsumer.h" 38 #include "Client.h" 39 #include "DisplayHardware/HWComposer.h" 40 #include "FrameTracker.h" 41 #include "Layer.h" 42 #include "LayerVector.h" 43 #include "MonitoredProducer.h" 44 #include "SurfaceFlinger.h" 45 46 namespace android { 47 48 class BufferLayer : public Layer { 49 public: 50 explicit BufferLayer(const LayerCreationArgs& args); 51 virtual ~BufferLayer() override; 52 53 // ----------------------------------------------------------------------- 54 // Overriden from Layer 55 // ----------------------------------------------------------------------- 56 public: 57 sp<compositionengine::LayerFE> getCompositionEngineLayerFE() const override; 58 compositionengine::LayerFECompositionState* editCompositionState() override; 59 60 // 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; 65 66 bool isOpaque(const Layer::State& s) const override; 67 68 // isVisible - true if this layer is visible, false otherwise 69 bool isVisible() const override; 70 71 // isProtected - true if the layer may contain protected content in the 72 // GRALLOC_USAGE_PROTECTED sense. 73 bool isProtected() const override; 74 75 // isFixedSize - true if content has a fixed size 76 bool isFixedSize() const override; 77 78 bool usesSourceCrop() const override; 79 80 bool isHdrY410() const override; 81 82 bool onPostComposition(const DisplayDevice*, const std::shared_ptr<FenceTime>& glDoneFence, 83 const std::shared_ptr<FenceTime>& presentFence, 84 const CompositorTiming&) override; 85 86 // latchBuffer - called each time the screen is redrawn and returns whether 87 // the visible regions need to be recomputed (this is a fairly heavy 88 // operation, so this should be set only if needed). Typically this is used 89 // to figure out if the content or size of a surface has changed. 90 bool latchBuffer(bool& recomputeVisibleRegions, nsecs_t latchTime, 91 nsecs_t expectedPresentTime) override; 92 isBufferLatched()93 bool isBufferLatched() const override { return mRefreshPending; } 94 95 void notifyAvailableFrames(nsecs_t expectedPresentTime) override; 96 97 bool hasReadyFrame() const override; 98 99 // Returns the current scaling mode, unless mOverrideScalingMode 100 // is set, in which case, it returns mOverrideScalingMode 101 uint32_t getEffectiveScalingMode() const override; 102 103 // Calls latchBuffer if the buffer has a frame queued and then releases the buffer. 104 // This is used if the buffer is just latched and releases to free up the buffer 105 // and will not be shown on screen. 106 // Should only be called on the main thread. 107 void latchAndReleaseBuffer() override; 108 109 bool getTransformToDisplayInverse() const override; 110 111 Rect getBufferCrop() const override; 112 113 uint32_t getBufferTransform() const override; 114 115 ui::Dataspace getDataSpace() const override; 116 117 sp<GraphicBuffer> getBuffer() const override; 118 getTransformHint()119 ui::Transform::RotationFlags getTransformHint() const override { return mTransformHint; } 120 121 // ----------------------------------------------------------------------- 122 // Functions that must be implemented by derived classes 123 // ----------------------------------------------------------------------- 124 private: 125 virtual bool fenceHasSignaled() const = 0; 126 virtual bool framePresentTimeIsCurrent(nsecs_t expectedPresentTime) const = 0; 127 128 PixelFormat getPixelFormat() const; 129 130 // Computes the transform matrix using the setFilteringEnabled to determine whether the 131 // transform matrix should be computed for use with bilinear filtering. 132 void getDrawingTransformMatrix(bool filteringEnabled, float outMatrix[16]); 133 134 virtual uint64_t getFrameNumber(nsecs_t expectedPresentTime) const = 0; 135 136 virtual bool getAutoRefresh() const = 0; 137 virtual bool getSidebandStreamChanged() const = 0; 138 139 // Latch sideband stream and returns true if the dirty region should be updated. 140 virtual bool latchSidebandStream(bool& recomputeVisibleRegions) = 0; 141 142 virtual bool hasFrameUpdate() const = 0; 143 144 virtual status_t bindTextureImage() = 0; 145 virtual status_t updateTexImage(bool& recomputeVisibleRegions, nsecs_t latchTime, 146 nsecs_t expectedPresentTime) = 0; 147 148 virtual status_t updateActiveBuffer() = 0; 149 virtual status_t updateFrameNumber(nsecs_t latchTime) = 0; 150 151 // We generate InputWindowHandles for all non-cursor buffered layers regardless of whether they 152 // have an InputChannel. This is to enable the InputDispatcher to do PID based occlusion 153 // detection. needsInputInfo()154 bool needsInputInfo() const override { return !mPotentialCursor; } 155 156 protected: 157 struct BufferInfo { 158 nsecs_t mDesiredPresentTime; 159 std::shared_ptr<FenceTime> mFenceTime; 160 sp<Fence> mFence; 161 uint32_t mTransform{0}; 162 ui::Dataspace mDataspace{ui::Dataspace::UNKNOWN}; 163 Rect mCrop; 164 uint32_t mScaleMode{NATIVE_WINDOW_SCALING_MODE_FREEZE}; 165 Region mSurfaceDamage; 166 HdrMetadata mHdrMetadata; 167 int mApi; 168 PixelFormat mPixelFormat{PIXEL_FORMAT_NONE}; 169 bool mTransformToDisplayInverse{false}; 170 171 sp<GraphicBuffer> mBuffer; 172 int mBufferSlot{BufferQueue::INVALID_BUFFER_SLOT}; 173 174 bool mFrameLatencyNeeded{false}; 175 }; 176 177 BufferInfo mBufferInfo; 178 virtual void gatherBufferInfo() = 0; 179 180 std::optional<compositionengine::LayerFE::LayerSettings> prepareClientComposition( 181 compositionengine::LayerFE::ClientCompositionTargetSettings&) override; 182 183 /* 184 * compositionengine::LayerFE overrides 185 */ 186 const compositionengine::LayerFECompositionState* getCompositionState() const override; 187 bool onPreComposition(nsecs_t) override; 188 void preparePerFrameCompositionState() override; 189 190 // Loads the corresponding system property once per process 191 static bool latchUnsignaledBuffers(); 192 193 // Check all of the local sync points to ensure that all transactions 194 // which need to have been applied prior to the frame which is about to 195 // be latched have signaled 196 bool allTransactionsSignaled(nsecs_t expectedPresentTime); 197 198 static bool getOpacityForFormat(uint32_t format); 199 200 // from graphics API 201 const uint32_t mTextureName; 202 203 bool mRefreshPending{false}; 204 205 ui::Dataspace translateDataspace(ui::Dataspace dataspace); 206 void setInitialValuesForClone(const sp<Layer>& clonedFrom); 207 void updateCloneBufferInfo() override; 208 uint64_t mPreviousFrameNumber = 0; 209 210 virtual uint64_t getHeadFrameNumber(nsecs_t expectedPresentTime) const; 211 212 void setTransformHint(ui::Transform::RotationFlags displayTransformHint) override; 213 214 // Transform hint provided to the producer. This must be accessed holding 215 /// the mStateLock. 216 ui::Transform::RotationFlags mTransformHint = ui::Transform::ROT_0; 217 218 private: 219 // Returns true if this layer requires filtering 220 bool needsFiltering(const DisplayDevice*) const override; 221 bool needsFilteringForScreenshots(const DisplayDevice*, 222 const ui::Transform& inverseParentTransform) const override; 223 224 // BufferStateLayers can return Rect::INVALID_RECT if the layer does not have a display frame 225 // and its parent layer is not bounded 226 Rect getBufferSize(const State& s) const override; 227 228 std::unique_ptr<compositionengine::LayerFECompositionState> mCompositionState; 229 230 FloatRect computeSourceBounds(const FloatRect& parentBounds) const override; 231 }; 232 233 } // namespace android 234