David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 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 | |
Chia-I Wu | 0cb75ac | 2017-11-27 15:56:04 -0800 | [diff] [blame^] | 19 | #include "BufferLayerConsumer.h" |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 20 | #include "Client.h" |
David Sodman | 41fdfc9 | 2017-11-06 16:09:56 -0800 | [diff] [blame] | 21 | #include "Layer.h" |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 22 | #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 Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 30 | #include "Transform.h" |
| 31 | |
| 32 | #include <gui/ISurfaceComposerClient.h> |
| 33 | #include <gui/LayerState.h> |
| 34 | |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 35 | #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 | |
| 48 | namespace android { |
| 49 | |
| 50 | /* |
Chia-I Wu | 0cb75ac | 2017-11-27 15:56:04 -0800 | [diff] [blame^] | 51 | * A new BufferQueue and a new BufferLayerConsumer are created when the |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 52 | * BufferLayer is first referenced. |
| 53 | * |
| 54 | * This also implements onFrameAvailable(), which notifies SurfaceFlinger |
| 55 | * that new data has arrived. |
| 56 | */ |
Chia-I Wu | 0cb75ac | 2017-11-27 15:56:04 -0800 | [diff] [blame^] | 57 | class BufferLayer : public Layer, public BufferLayerConsumer::ContentsChangedListener { |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 58 | public: |
| 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 Sodman | eb085e0 | 2017-10-05 18:49:04 -0700 | [diff] [blame] | 64 | // 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 Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 70 | // ----------------------------------------------------------------------- |
| 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; |
| 104 | |
David Sodman | eb085e0 | 2017-10-05 18:49:04 -0700 | [diff] [blame] | 105 | void onLayerDisplayed(const sp<Fence>& releaseFence) override; |
David Sodman | eb085e0 | 2017-10-05 18:49:04 -0700 | [diff] [blame] | 106 | |
| 107 | void abandon() override; |
| 108 | bool shouldPresentNow(const DispSync& dispSync) const override; |
| 109 | void setTransformHint(uint32_t orientation) const override; |
| 110 | bool onPostComposition(const std::shared_ptr<FenceTime>& glDoneFence, |
| 111 | const std::shared_ptr<FenceTime>& presentFence, |
| 112 | const CompositorTiming& compositorTiming) override; |
| 113 | std::vector<OccupancyTracker::Segment> getOccupancyHistory(bool forceFlush) override; |
| 114 | bool getTransformToDisplayInverse() const override; |
| 115 | |
| 116 | public: |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 117 | bool onPreComposition(nsecs_t refreshStartTime) override; |
| 118 | |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 119 | // If a buffer was replaced this frame, release the former buffer |
| 120 | void releasePendingBuffer(nsecs_t dequeueReadyTime); |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 121 | |
| 122 | /* |
| 123 | * latchBuffer - called each time the screen is redrawn and returns whether |
| 124 | * the visible regions need to be recomputed (this is a fairly heavy |
| 125 | * operation, so this should be set only if needed). Typically this is used |
| 126 | * to figure out if the content or size of a surface has changed. |
| 127 | */ |
| 128 | Region latchBuffer(bool& recomputeVisibleRegions, nsecs_t latchTime) override; |
| 129 | bool isBufferLatched() const override { return mRefreshPending; } |
David Sodman | eb085e0 | 2017-10-05 18:49:04 -0700 | [diff] [blame] | 130 | void setDefaultBufferSize(uint32_t w, uint32_t h) override; |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 131 | |
David Sodman | eb085e0 | 2017-10-05 18:49:04 -0700 | [diff] [blame] | 132 | void setPerFrameData(const sp<const DisplayDevice>& displayDevice) override; |
David Sodman | eb085e0 | 2017-10-05 18:49:04 -0700 | [diff] [blame] | 133 | |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 134 | bool isOpaque(const Layer::State& s) const override; |
| 135 | |
| 136 | private: |
| 137 | void onFirstRef() override; |
| 138 | |
| 139 | // Interface implementation for |
Chia-I Wu | 0cb75ac | 2017-11-27 15:56:04 -0800 | [diff] [blame^] | 140 | // BufferLayerConsumer::ContentsChangedListener |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 141 | void onFrameAvailable(const BufferItem& item) override; |
| 142 | void onFrameReplaced(const BufferItem& item) override; |
| 143 | void onSidebandStreamChanged() override; |
| 144 | |
| 145 | // needsLinearFiltering - true if this surface's state requires filtering |
| 146 | bool needsFiltering(const RenderArea& renderArea) const; |
| 147 | |
| 148 | static bool getOpacityForFormat(uint32_t format); |
| 149 | |
| 150 | // drawing |
David Sodman | 41fdfc9 | 2017-11-06 16:09:56 -0800 | [diff] [blame] | 151 | void drawWithOpenGL(const RenderArea& renderArea, bool useIdentityTransform) const; |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 152 | |
| 153 | // Temporary - Used only for LEGACY camera mode. |
| 154 | uint32_t getProducerStickyTransform() const; |
| 155 | |
| 156 | // Loads the corresponding system property once per process |
| 157 | static bool latchUnsignaledBuffers(); |
| 158 | |
| 159 | uint64_t getHeadFrameNumber() const; |
| 160 | bool headFenceHasSignaled() const; |
| 161 | |
| 162 | // Returns the current scaling mode, unless mOverrideScalingMode |
| 163 | // is set, in which case, it returns mOverrideScalingMode |
| 164 | uint32_t getEffectiveScalingMode() const override; |
| 165 | |
| 166 | public: |
| 167 | void notifyAvailableFrames() override; |
| 168 | |
| 169 | PixelFormat getPixelFormat() const override { return mFormat; } |
| 170 | sp<IGraphicBufferProducer> getProducer() const; |
| 171 | |
| 172 | private: |
Chia-I Wu | 0cb75ac | 2017-11-27 15:56:04 -0800 | [diff] [blame^] | 173 | sp<BufferLayerConsumer> mSurfaceFlingerConsumer; |
David Sodman | eb085e0 | 2017-10-05 18:49:04 -0700 | [diff] [blame] | 174 | |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 175 | // Check all of the local sync points to ensure that all transactions |
| 176 | // which need to have been applied prior to the frame which is about to |
| 177 | // be latched have signaled |
| 178 | bool allTransactionsSignaled(); |
| 179 | sp<IGraphicBufferProducer> mProducer; |
| 180 | |
| 181 | // constants |
| 182 | uint32_t mTextureName; // from GLES |
| 183 | PixelFormat mFormat; |
| 184 | |
| 185 | // main thread |
| 186 | uint32_t mCurrentScalingMode; |
| 187 | bool mBufferLatched = false; // TODO: Use mActiveBuffer? |
| 188 | uint64_t mPreviousFrameNumber; // Only accessed on the main thread. |
| 189 | // The texture used to draw the layer in GLES composition mode |
| 190 | mutable Texture mTexture; |
| 191 | |
| 192 | bool mUpdateTexImageFailed; // This is only accessed on the main thread. |
| 193 | bool mRefreshPending; |
| 194 | }; |
| 195 | |
| 196 | } // namespace android |