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 | |
| 19 | #include "Layer.h" |
| 20 | #include "Client.h" |
| 21 | #include "DisplayHardware/HWComposer.h" |
| 22 | #include "DisplayHardware/HWComposerBufferCache.h" |
| 23 | #include "FrameTracker.h" |
| 24 | #include "LayerVector.h" |
| 25 | #include "MonitoredProducer.h" |
| 26 | #include "RenderEngine/Mesh.h" |
| 27 | #include "RenderEngine/Texture.h" |
| 28 | #include "SurfaceFlinger.h" |
| 29 | #include "SurfaceFlingerConsumer.h" |
| 30 | #include "Transform.h" |
| 31 | |
| 32 | #include <gui/ISurfaceComposerClient.h> |
| 33 | #include <gui/LayerState.h> |
| 34 | |
| 35 | #include <EGL/egl.h> |
| 36 | #include <EGL/eglext.h> |
| 37 | |
| 38 | #include <ui/FrameStats.h> |
| 39 | #include <ui/GraphicBuffer.h> |
| 40 | #include <ui/PixelFormat.h> |
| 41 | #include <ui/Region.h> |
| 42 | |
| 43 | #include <utils/RefBase.h> |
| 44 | #include <utils/String8.h> |
| 45 | #include <utils/Timers.h> |
| 46 | |
| 47 | #include <stdint.h> |
| 48 | #include <sys/types.h> |
| 49 | #include <list> |
| 50 | |
| 51 | namespace android { |
| 52 | |
| 53 | /* |
| 54 | * A new BufferQueue and a new SurfaceFlingerConsumer are created when the |
| 55 | * BufferLayer is first referenced. |
| 56 | * |
| 57 | * This also implements onFrameAvailable(), which notifies SurfaceFlinger |
| 58 | * that new data has arrived. |
| 59 | */ |
| 60 | class BufferLayer : public Layer, public SurfaceFlingerConsumer::ContentsChangedListener { |
| 61 | public: |
| 62 | BufferLayer(SurfaceFlinger* flinger, const sp<Client>& client, const String8& name, uint32_t w, |
| 63 | uint32_t h, uint32_t flags); |
| 64 | |
| 65 | ~BufferLayer() override; |
| 66 | |
| 67 | // ----------------------------------------------------------------------- |
| 68 | // Overriden from Layer |
| 69 | // ----------------------------------------------------------------------- |
| 70 | |
| 71 | /* |
| 72 | * getTypeId - Provide unique string for each class type in the Layer |
| 73 | * hierarchy |
| 74 | */ |
| 75 | const char* getTypeId() const override { return "BufferLayer"; } |
| 76 | |
| 77 | /* |
| 78 | * isProtected - true if the layer may contain protected content in the |
| 79 | * GRALLOC_USAGE_PROTECTED sense. |
| 80 | */ |
| 81 | bool isProtected() const; |
| 82 | |
| 83 | /* |
| 84 | * isVisible - true if this layer is visible, false otherwise |
| 85 | */ |
| 86 | bool isVisible() const override; |
| 87 | |
| 88 | /* |
| 89 | * isFixedSize - true if content has a fixed size |
| 90 | */ |
| 91 | bool isFixedSize() const override; |
| 92 | |
| 93 | // the this layer's size and format |
| 94 | status_t setBuffers(uint32_t w, uint32_t h, PixelFormat format, uint32_t flags); |
| 95 | |
| 96 | /* |
| 97 | * onDraw - draws the surface. |
| 98 | */ |
| 99 | void onDraw(const RenderArea& renderArea, const Region& clip, |
| 100 | bool useIdentityTransform) const override; |
| 101 | |
| 102 | bool onPreComposition(nsecs_t refreshStartTime) override; |
| 103 | |
| 104 | #ifdef USE_HWC2 |
| 105 | // If a buffer was replaced this frame, release the former buffer |
| 106 | void releasePendingBuffer(nsecs_t dequeueReadyTime); |
| 107 | #endif |
| 108 | |
| 109 | /* |
| 110 | * latchBuffer - called each time the screen is redrawn and returns whether |
| 111 | * the visible regions need to be recomputed (this is a fairly heavy |
| 112 | * operation, so this should be set only if needed). Typically this is used |
| 113 | * to figure out if the content or size of a surface has changed. |
| 114 | */ |
| 115 | Region latchBuffer(bool& recomputeVisibleRegions, nsecs_t latchTime) override; |
| 116 | bool isBufferLatched() const override { return mRefreshPending; } |
| 117 | |
| 118 | #ifdef USE_HWC2 |
| 119 | void setPerFrameData(const sp<const DisplayDevice>& displayDevice); |
| 120 | #else |
| 121 | void setPerFrameData(const sp<const DisplayDevice>& hw, HWComposer::HWCLayerInterface& layer); |
| 122 | #endif |
| 123 | bool isOpaque(const Layer::State& s) const override; |
| 124 | |
| 125 | private: |
| 126 | void onFirstRef() override; |
| 127 | |
| 128 | // Interface implementation for |
| 129 | // SurfaceFlingerConsumer::ContentsChangedListener |
| 130 | void onFrameAvailable(const BufferItem& item) override; |
| 131 | void onFrameReplaced(const BufferItem& item) override; |
| 132 | void onSidebandStreamChanged() override; |
| 133 | |
| 134 | // needsLinearFiltering - true if this surface's state requires filtering |
| 135 | bool needsFiltering(const RenderArea& renderArea) const; |
| 136 | |
| 137 | static bool getOpacityForFormat(uint32_t format); |
| 138 | |
| 139 | // drawing |
| 140 | void drawWithOpenGL(const RenderArea& renderArea, |
| 141 | bool useIdentityTransform) const; |
| 142 | |
| 143 | // Temporary - Used only for LEGACY camera mode. |
| 144 | uint32_t getProducerStickyTransform() const; |
| 145 | |
| 146 | // Loads the corresponding system property once per process |
| 147 | static bool latchUnsignaledBuffers(); |
| 148 | |
| 149 | uint64_t getHeadFrameNumber() const; |
| 150 | bool headFenceHasSignaled() const; |
| 151 | |
| 152 | // Returns the current scaling mode, unless mOverrideScalingMode |
| 153 | // is set, in which case, it returns mOverrideScalingMode |
| 154 | uint32_t getEffectiveScalingMode() const override; |
| 155 | |
| 156 | public: |
| 157 | void notifyAvailableFrames() override; |
| 158 | |
| 159 | PixelFormat getPixelFormat() const override { return mFormat; } |
| 160 | sp<IGraphicBufferProducer> getProducer() const; |
| 161 | |
| 162 | private: |
| 163 | // Check all of the local sync points to ensure that all transactions |
| 164 | // which need to have been applied prior to the frame which is about to |
| 165 | // be latched have signaled |
| 166 | bool allTransactionsSignaled(); |
| 167 | sp<IGraphicBufferProducer> mProducer; |
| 168 | |
| 169 | // constants |
| 170 | uint32_t mTextureName; // from GLES |
| 171 | PixelFormat mFormat; |
| 172 | |
| 173 | // main thread |
| 174 | uint32_t mCurrentScalingMode; |
| 175 | bool mBufferLatched = false; // TODO: Use mActiveBuffer? |
| 176 | uint64_t mPreviousFrameNumber; // Only accessed on the main thread. |
| 177 | // The texture used to draw the layer in GLES composition mode |
| 178 | mutable Texture mTexture; |
| 179 | |
| 180 | bool mUpdateTexImageFailed; // This is only accessed on the main thread. |
| 181 | bool mRefreshPending; |
| 182 | }; |
| 183 | |
| 184 | } // namespace android |