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 "FrameTimeline.h" 41 #include "FrameTracker.h" 42 #include "Layer.h" 43 #include "LayerVector.h" 44 #include "MonitoredProducer.h" 45 #include "SurfaceFlinger.h" 46 47 namespace android { 48 49 class BufferLayer : public Layer { 50 public: 51 explicit BufferLayer(const LayerCreationArgs& args); 52 virtual ~BufferLayer() override; 53 54 // Implements Layer. 55 sp<compositionengine::LayerFE> getCompositionEngineLayerFE() const override; 56 compositionengine::LayerFECompositionState* editCompositionState() override; 57 58 // 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; 63 64 bool isOpaque(const Layer::State& s) const override; 65 bool canReceiveInput() const override; 66 67 // isVisible - true if this layer is visible, false otherwise 68 bool isVisible() const override; 69 70 // isProtected - true if the layer may contain protected content in the 71 // GRALLOC_USAGE_PROTECTED sense. 72 bool isProtected() const override; 73 74 // isFixedSize - true if content has a fixed size 75 bool isFixedSize() const override; 76 77 bool usesSourceCrop() const override; 78 79 bool isHdrY410() const override; 80 81 void onPostComposition(const DisplayDevice*, const std::shared_ptr<FenceTime>& glDoneFence, 82 const std::shared_ptr<FenceTime>& presentFence, 83 const CompositorTiming&) override; 84 85 // latchBuffer - called each time the screen is redrawn and returns whether 86 // the visible regions need to be recomputed (this is a fairly heavy 87 // operation, so this should be set only if needed). Typically this is used 88 // to figure out if the content or size of a surface has changed. 89 bool latchBuffer(bool& recomputeVisibleRegions, nsecs_t latchTime, 90 nsecs_t expectedPresentTime) override; 91 bool hasReadyFrame() const override; 92 93 // Returns the current scaling mode 94 uint32_t getEffectiveScalingMode() const override; 95 96 // Calls latchBuffer if the buffer has a frame queued and then releases the buffer. 97 // This is used if the buffer is just latched and releases to free up the buffer 98 // and will not be shown on screen. 99 // Should only be called on the main thread. 100 void latchAndReleaseBuffer() override; 101 102 bool getTransformToDisplayInverse() const override; 103 104 Rect getBufferCrop() const override; 105 106 uint32_t getBufferTransform() const override; 107 108 ui::Dataspace getDataSpace() const override; 109 110 sp<GraphicBuffer> getBuffer() const override; 111 const std::shared_ptr<renderengine::ExternalTexture>& getExternalTexture() const override; 112 getTransformHint()113 ui::Transform::RotationFlags getTransformHint() const override { return mTransformHint; } 114 115 // Returns true if the transformed buffer size does not match the layer size and we need 116 // to apply filtering. 117 virtual bool bufferNeedsFiltering() const; 118 119 protected: 120 struct BufferInfo { 121 nsecs_t mDesiredPresentTime; 122 std::shared_ptr<FenceTime> mFenceTime; 123 sp<Fence> mFence; 124 uint32_t mTransform{0}; 125 ui::Dataspace mDataspace{ui::Dataspace::UNKNOWN}; 126 Rect mCrop; 127 uint32_t mScaleMode{NATIVE_WINDOW_SCALING_MODE_FREEZE}; 128 Region mSurfaceDamage; 129 HdrMetadata mHdrMetadata; 130 int mApi; 131 PixelFormat mPixelFormat{PIXEL_FORMAT_NONE}; 132 bool mTransformToDisplayInverse{false}; 133 134 std::shared_ptr<renderengine::ExternalTexture> mBuffer; 135 uint64_t mFrameNumber; 136 int mBufferSlot{BufferQueue::INVALID_BUFFER_SLOT}; 137 138 bool mFrameLatencyNeeded{false}; 139 }; 140 141 BufferInfo mBufferInfo; 142 virtual void gatherBufferInfo() = 0; 143 144 std::optional<compositionengine::LayerFE::LayerSettings> prepareClientComposition( 145 compositionengine::LayerFE::ClientCompositionTargetSettings&) override; 146 147 /* 148 * compositionengine::LayerFE overrides 149 */ 150 const compositionengine::LayerFECompositionState* getCompositionState() const override; 151 bool onPreComposition(nsecs_t) override; 152 void preparePerFrameCompositionState() override; 153 154 static bool getOpacityForFormat(PixelFormat format); 155 156 // from graphics API 157 const uint32_t mTextureName; 158 ui::Dataspace translateDataspace(ui::Dataspace dataspace); 159 void setInitialValuesForClone(const sp<Layer>& clonedFrom); 160 void updateCloneBufferInfo() override; 161 uint64_t mPreviousFrameNumber = 0; 162 163 void setTransformHint(ui::Transform::RotationFlags displayTransformHint) override; 164 165 // Transform hint provided to the producer. This must be accessed holding 166 // the mStateLock. 167 ui::Transform::RotationFlags mTransformHint = ui::Transform::ROT_0; 168 getAutoRefresh()169 bool getAutoRefresh() const { return mDrawingState.autoRefresh; } getSidebandStreamChanged()170 bool getSidebandStreamChanged() const { return mSidebandStreamChanged; } 171 172 // Returns true if the next buffer should be presented at the expected present time 173 bool shouldPresentNow(nsecs_t expectedPresentTime) const; 174 175 // Returns true if the next buffer should be presented at the expected present time, 176 // overridden by BufferStateLayer and BufferQueueLayer for implementation 177 // specific logic 178 virtual bool isBufferDue(nsecs_t /*expectedPresentTime*/) const = 0; 179 180 std::atomic<bool> mSidebandStreamChanged{false}; 181 182 private: 183 virtual bool fenceHasSignaled() const = 0; 184 virtual bool framePresentTimeIsCurrent(nsecs_t expectedPresentTime) const = 0; 185 186 // Latch sideband stream and returns true if the dirty region should be updated. 187 virtual bool latchSidebandStream(bool& recomputeVisibleRegions) = 0; 188 189 virtual bool hasFrameUpdate() const = 0; 190 191 virtual status_t updateTexImage(bool& recomputeVisibleRegions, nsecs_t latchTime, 192 nsecs_t expectedPresentTime) = 0; 193 194 virtual status_t updateActiveBuffer() = 0; 195 virtual status_t updateFrameNumber() = 0; 196 197 // We generate InputWindowHandles for all non-cursor buffered layers regardless of whether they 198 // have an InputChannel. This is to enable the InputDispatcher to do PID based occlusion 199 // detection. needsInputInfo()200 bool needsInputInfo() const override { return !mPotentialCursor; } 201 202 // Returns true if this layer requires filtering 203 bool needsFiltering(const DisplayDevice*) const override; 204 bool needsFilteringForScreenshots(const DisplayDevice*, 205 const ui::Transform& inverseParentTransform) const override; 206 207 // BufferStateLayers can return Rect::INVALID_RECT if the layer does not have a display frame 208 // and its parent layer is not bounded 209 Rect getBufferSize(const State& s) const override; 210 211 PixelFormat getPixelFormat() const; 212 213 // Computes the transform matrix using the setFilteringEnabled to determine whether the 214 // transform matrix should be computed for use with bilinear filtering. 215 void getDrawingTransformMatrix(bool filteringEnabled, float outMatrix[16]); 216 217 std::unique_ptr<compositionengine::LayerFECompositionState> mCompositionState; 218 219 FloatRect computeSourceBounds(const FloatRect& parentBounds) const override; 220 }; 221 222 } // namespace android 223