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 ~BufferLayer() override; 52 53 // ----------------------------------------------------------------------- 54 // Overriden from Layer 55 // ----------------------------------------------------------------------- 56 public: 57 std::shared_ptr<compositionengine::Layer> getCompositionLayer() const override; 58 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; 64 65 // getTypeId - Provide unique string for each class type in the Layer 66 // hierarchy getTypeId()67 const char* getTypeId() const override { return "BufferLayer"; } 68 69 bool isOpaque(const Layer::State& s) const override; 70 71 // isVisible - true if this layer is visible, false otherwise 72 bool isVisible() const override; 73 74 // isProtected - true if the layer may contain protected content in the 75 // GRALLOC_USAGE_PROTECTED sense. 76 bool isProtected() const override; 77 78 // isFixedSize - true if content has a fixed size 79 bool isFixedSize() const override; 80 81 bool usesSourceCrop() const override; 82 83 bool isHdrY410() const override; 84 85 void setPerFrameData(const sp<const DisplayDevice>& display, const ui::Transform& transform, 86 const Rect& viewport, int32_t supportedPerFrameMetadata, 87 const ui::Dataspace targetDataspace) override; 88 89 bool onPreComposition(nsecs_t refreshStartTime) override; 90 bool onPostComposition(const std::optional<DisplayId>& displayId, 91 const std::shared_ptr<FenceTime>& glDoneFence, 92 const std::shared_ptr<FenceTime>& presentFence, 93 const CompositorTiming& compositorTiming) override; 94 95 // latchBuffer - called each time the screen is redrawn and returns whether 96 // the visible regions need to be recomputed (this is a fairly heavy 97 // operation, so this should be set only if needed). Typically this is used 98 // to figure out if the content or size of a surface has changed. 99 bool latchBuffer(bool& recomputeVisibleRegions, nsecs_t latchTime) override; 100 isBufferLatched()101 bool isBufferLatched() const override { return mRefreshPending; } 102 103 void notifyAvailableFrames() override; 104 105 bool hasReadyFrame() const override; 106 107 // Returns the current scaling mode, unless mOverrideScalingMode 108 // is set, in which case, it returns mOverrideScalingMode 109 uint32_t getEffectiveScalingMode() const override; 110 // ----------------------------------------------------------------------- 111 112 // ----------------------------------------------------------------------- 113 // Functions that must be implemented by derived classes 114 // ----------------------------------------------------------------------- 115 private: 116 virtual bool fenceHasSignaled() const = 0; 117 virtual bool framePresentTimeIsCurrent() const = 0; 118 119 virtual nsecs_t getDesiredPresentTime() = 0; 120 virtual std::shared_ptr<FenceTime> getCurrentFenceTime() const = 0; 121 122 virtual void getDrawingTransformMatrix(float *matrix) = 0; 123 virtual uint32_t getDrawingTransform() const = 0; 124 virtual ui::Dataspace getDrawingDataSpace() const = 0; 125 virtual Rect getDrawingCrop() const = 0; 126 virtual uint32_t getDrawingScalingMode() const = 0; 127 virtual Region getDrawingSurfaceDamage() const = 0; 128 virtual const HdrMetadata& getDrawingHdrMetadata() const = 0; 129 virtual int getDrawingApi() const = 0; 130 virtual PixelFormat getPixelFormat() const = 0; 131 132 virtual uint64_t getFrameNumber() const = 0; 133 134 virtual bool getAutoRefresh() const = 0; 135 virtual bool getSidebandStreamChanged() const = 0; 136 137 // Latch sideband stream and returns true if the dirty region should be updated. 138 virtual bool latchSidebandStream(bool& recomputeVisibleRegions) = 0; 139 140 virtual bool hasFrameUpdate() const = 0; 141 142 virtual void setFilteringEnabled(bool enabled) = 0; 143 144 virtual status_t bindTextureImage() = 0; 145 virtual status_t updateTexImage(bool& recomputeVisibleRegions, nsecs_t latchTime) = 0; 146 147 virtual status_t updateActiveBuffer() = 0; 148 virtual status_t updateFrameNumber(nsecs_t latchTime) = 0; 149 150 virtual void setHwcLayerBuffer(const sp<const DisplayDevice>& displayDevice) = 0; 151 152 protected: 153 // Loads the corresponding system property once per process 154 static bool latchUnsignaledBuffers(); 155 156 // 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 159 bool allTransactionsSignaled(); 160 161 static bool getOpacityForFormat(uint32_t format); 162 163 // from GLES 164 const uint32_t mTextureName; 165 166 bool mRefreshPending{false}; 167 168 // prepareClientLayer - constructs a RenderEngine layer for GPU composition. 169 bool prepareClientLayer(const RenderArea& renderArea, const Region& clip, 170 bool useIdentityTransform, Region& clearRegion, 171 const bool supportProtectedContent, 172 renderengine::LayerSettings& layer) override; 173 174 private: 175 // Returns true if this layer requires filtering 176 bool needsFiltering(const sp<const DisplayDevice>& displayDevice) const; 177 178 uint64_t getHeadFrameNumber() const; 179 180 uint32_t mCurrentScalingMode{NATIVE_WINDOW_SCALING_MODE_FREEZE}; 181 182 bool mTransformToDisplayInverse{false}; 183 184 // main thread. 185 bool mBufferLatched{false}; // TODO: Use mActiveBuffer? 186 187 // BufferStateLayers can return Rect::INVALID_RECT if the layer does not have a display frame 188 // and its parent layer is not bounded 189 Rect getBufferSize(const State& s) const override; 190 191 std::shared_ptr<compositionengine::Layer> mCompositionLayer; 192 193 FloatRect computeSourceBounds(const FloatRect& parentBounds) const override; 194 }; 195 196 } // namespace android 197