John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2020 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 | #ifndef SF_SKIARENDERENGINE_H_ |
| 18 | #define SF_SKIARENDERENGINE_H_ |
| 19 | |
| 20 | #include <renderengine/RenderEngine.h> |
| 21 | #include <sys/types.h> |
| 22 | |
Lingfeng Yang | 00c1ff6 | 2022-06-02 09:19:28 -0700 | [diff] [blame] | 23 | #include <GrBackendSemaphore.h> |
Lingfeng Yang | 00c1ff6 | 2022-06-02 09:19:28 -0700 | [diff] [blame] | 24 | #include <SkSurface.h> |
| 25 | #include <android-base/thread_annotations.h> |
| 26 | #include <renderengine/ExternalTexture.h> |
| 27 | #include <renderengine/RenderEngine.h> |
| 28 | #include <sys/types.h> |
| 29 | |
Nolan Scobie | fc125ec | 2024-03-11 20:08:27 -0400 | [diff] [blame] | 30 | #include <memory> |
Lingfeng Yang | 00c1ff6 | 2022-06-02 09:19:28 -0700 | [diff] [blame] | 31 | #include <mutex> |
| 32 | #include <unordered_map> |
| 33 | |
| 34 | #include "AutoBackendTexture.h" |
| 35 | #include "GrContextOptions.h" |
| 36 | #include "SkImageInfo.h" |
Lingfeng Yang | 00c1ff6 | 2022-06-02 09:19:28 -0700 | [diff] [blame] | 37 | #include "android-base/macros.h" |
Nolan Scobie | fc125ec | 2024-03-11 20:08:27 -0400 | [diff] [blame] | 38 | #include "compat/SkiaGpuContext.h" |
Lingfeng Yang | 00c1ff6 | 2022-06-02 09:19:28 -0700 | [diff] [blame] | 39 | #include "debug/SkiaCapture.h" |
| 40 | #include "filters/BlurFilter.h" |
| 41 | #include "filters/LinearEffect.h" |
| 42 | #include "filters/StretchShaderFactory.h" |
| 43 | |
| 44 | class SkData; |
| 45 | |
| 46 | struct SkPoint3; |
| 47 | |
John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 48 | namespace android { |
| 49 | |
| 50 | namespace renderengine { |
| 51 | |
| 52 | class Mesh; |
| 53 | class Texture; |
| 54 | |
| 55 | namespace skia { |
| 56 | |
| 57 | class BlurFilter; |
| 58 | |
Alec Mouri | 081be4c | 2020-09-16 10:24:47 -0700 | [diff] [blame] | 59 | class SkiaRenderEngine : public RenderEngine { |
John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 60 | public: |
| 61 | static std::unique_ptr<SkiaRenderEngine> create(const RenderEngineCreationArgs& args); |
Robin Lee | 7338bd9 | 2024-04-04 14:05:07 +0000 | [diff] [blame] | 62 | SkiaRenderEngine(Threaded, PixelFormat pixelFormat, BlurAlgorithm); |
Lingfeng Yang | 00c1ff6 | 2022-06-02 09:19:28 -0700 | [diff] [blame] | 63 | ~SkiaRenderEngine() override; |
John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 64 | |
Russell Myers | 3348c74 | 2024-04-29 20:22:42 +0000 | [diff] [blame] | 65 | std::future<void> primeCache(PrimeCacheConfig config) override final; |
Lingfeng Yang | 00c1ff6 | 2022-06-02 09:19:28 -0700 | [diff] [blame] | 66 | void cleanupPostRender() override final; |
Lingfeng Yang | 00c1ff6 | 2022-06-02 09:19:28 -0700 | [diff] [blame] | 67 | bool supportsBackgroundBlur() override final { |
| 68 | return mBlurFilter != nullptr; |
| 69 | } |
| 70 | void onActiveDisplaySizeChanged(ui::Size size) override final; |
| 71 | int reportShadersCompiled(); |
Alec Mouri | a90a570 | 2021-04-16 16:36:21 +0000 | [diff] [blame] | 72 | |
Lingfeng Yang | 00c1ff6 | 2022-06-02 09:19:28 -0700 | [diff] [blame] | 73 | virtual void setEnableTracing(bool tracingEnabled) override final; |
| 74 | |
| 75 | void useProtectedContext(bool useProtectedContext) override; |
| 76 | bool supportsProtectedContent() const override { |
| 77 | return supportsProtectedContentImpl(); |
| 78 | } |
Nolan Scobie | fc125ec | 2024-03-11 20:08:27 -0400 | [diff] [blame] | 79 | void ensureContextsCreated(); |
| 80 | |
Alec Mouri | a90a570 | 2021-04-16 16:36:21 +0000 | [diff] [blame] | 81 | protected: |
Nolan Scobie | 2526b2f | 2024-04-16 15:12:22 -0400 | [diff] [blame] | 82 | // This is so backends can stop the generic rendering state first before cleaning up |
| 83 | // backend-specific state. SkiaGpuContexts are invalid after invocation. |
| 84 | void finishRenderingAndAbandonContexts(); |
Sally Qi | 4cabdd0 | 2021-08-05 16:45:57 -0700 | [diff] [blame] | 85 | |
Lingfeng Yang | 00c1ff6 | 2022-06-02 09:19:28 -0700 | [diff] [blame] | 86 | // Functions that a given backend (GLES, Vulkan) must implement |
Nolan Scobie | fc125ec | 2024-03-11 20:08:27 -0400 | [diff] [blame] | 87 | using Contexts = std::pair<unique_ptr<SkiaGpuContext>, unique_ptr<SkiaGpuContext>>; |
| 88 | virtual Contexts createContexts() = 0; |
Lingfeng Yang | 00c1ff6 | 2022-06-02 09:19:28 -0700 | [diff] [blame] | 89 | virtual bool supportsProtectedContentImpl() const = 0; |
| 90 | virtual bool useProtectedContextImpl(GrProtected isProtected) = 0; |
Nolan Scobie | fc125ec | 2024-03-11 20:08:27 -0400 | [diff] [blame] | 91 | virtual void waitFence(SkiaGpuContext* context, base::borrowed_fd fenceFd) = 0; |
Nolan Scobie | 1e06f2d | 2024-03-21 14:56:38 -0400 | [diff] [blame] | 92 | virtual base::unique_fd flushAndSubmit(SkiaGpuContext* context, |
| 93 | sk_sp<SkSurface> dstSurface) = 0; |
Lingfeng Yang | 00c1ff6 | 2022-06-02 09:19:28 -0700 | [diff] [blame] | 94 | virtual void appendBackendSpecificInfoToDump(std::string& result) = 0; |
| 95 | |
| 96 | size_t getMaxTextureSize() const override final; |
| 97 | size_t getMaxViewportDims() const override final; |
Nolan Scobie | 02c160c | 2024-03-18 10:40:23 -0400 | [diff] [blame] | 98 | // TODO: b/293371537 - Return reference instead of pointer? (Cleanup) |
Nolan Scobie | fc125ec | 2024-03-11 20:08:27 -0400 | [diff] [blame] | 99 | SkiaGpuContext* getActiveContext(); |
Lingfeng Yang | 00c1ff6 | 2022-06-02 09:19:28 -0700 | [diff] [blame] | 100 | |
Patrick Williams | 8aed5d2 | 2022-10-31 22:18:10 +0000 | [diff] [blame] | 101 | bool isProtected() const { return mInProtectedContext; } |
| 102 | |
Lingfeng Yang | 00c1ff6 | 2022-06-02 09:19:28 -0700 | [diff] [blame] | 103 | // Implements PersistentCache as a way to monitor what SkSL shaders Skia has |
| 104 | // cached. |
| 105 | class SkSLCacheMonitor : public GrContextOptions::PersistentCache { |
| 106 | public: |
| 107 | SkSLCacheMonitor() = default; |
| 108 | ~SkSLCacheMonitor() override = default; |
| 109 | |
| 110 | sk_sp<SkData> load(const SkData& key) override; |
| 111 | |
| 112 | void store(const SkData& key, const SkData& data, const SkString& description) override; |
| 113 | |
| 114 | int shadersCachedSinceLastCall() { |
| 115 | const int shadersCachedSinceLastCall = mShadersCachedSinceLastCall; |
| 116 | mShadersCachedSinceLastCall = 0; |
| 117 | return shadersCachedSinceLastCall; |
| 118 | } |
| 119 | |
| 120 | int totalShadersCompiled() const { return mTotalShadersCompiled; } |
| 121 | |
| 122 | private: |
| 123 | int mShadersCachedSinceLastCall = 0; |
| 124 | int mTotalShadersCompiled = 0; |
Sally Qi | 4cabdd0 | 2021-08-05 16:45:57 -0700 | [diff] [blame] | 125 | }; |
Lingfeng Yang | 00c1ff6 | 2022-06-02 09:19:28 -0700 | [diff] [blame] | 126 | |
Nolan Scobie | fc125ec | 2024-03-11 20:08:27 -0400 | [diff] [blame] | 127 | SkSLCacheMonitor mSkSLCacheMonitor; |
| 128 | |
Lingfeng Yang | 00c1ff6 | 2022-06-02 09:19:28 -0700 | [diff] [blame] | 129 | private: |
| 130 | void mapExternalTextureBuffer(const sp<GraphicBuffer>& buffer, |
| 131 | bool isRenderable) override final; |
Alec Mouri | 92f89fa | 2023-02-24 00:05:06 +0000 | [diff] [blame] | 132 | void unmapExternalTextureBuffer(sp<GraphicBuffer>&& buffer) override final; |
Lingfeng Yang | 00c1ff6 | 2022-06-02 09:19:28 -0700 | [diff] [blame] | 133 | bool canSkipPostRenderCleanup() const override final; |
| 134 | |
Ian Elliott | 8506e36 | 2023-03-08 12:12:09 -0700 | [diff] [blame] | 135 | std::shared_ptr<AutoBackendTexture::LocalRef> getOrCreateBackendTexture( |
| 136 | const sp<GraphicBuffer>& buffer, bool isOutputBuffer) REQUIRES(mRenderingMutex); |
Lingfeng Yang | 00c1ff6 | 2022-06-02 09:19:28 -0700 | [diff] [blame] | 137 | void initCanvas(SkCanvas* canvas, const DisplaySettings& display); |
| 138 | void drawShadow(SkCanvas* canvas, const SkRRect& casterRRect, |
| 139 | const ShadowSettings& shadowSettings); |
Patrick Williams | 2e9748f | 2022-08-09 22:48:18 +0000 | [diff] [blame] | 140 | void drawLayersInternal(const std::shared_ptr<std::promise<FenceResult>>&& resultPromise, |
Lingfeng Yang | 00c1ff6 | 2022-06-02 09:19:28 -0700 | [diff] [blame] | 141 | const DisplaySettings& display, |
| 142 | const std::vector<LayerSettings>& layers, |
| 143 | const std::shared_ptr<ExternalTexture>& buffer, |
Lingfeng Yang | 00c1ff6 | 2022-06-02 09:19:28 -0700 | [diff] [blame] | 144 | base::unique_fd&& bufferFence) override final; |
| 145 | |
| 146 | void dump(std::string& result) override final; |
| 147 | |
| 148 | // If requiresLinearEffect is true or the layer has a stretchEffect a new shader is returned. |
| 149 | // Otherwise it returns the input shader. |
| 150 | struct RuntimeEffectShaderParameters { |
| 151 | sk_sp<SkShader> shader; |
| 152 | const LayerSettings& layer; |
| 153 | const DisplaySettings& display; |
| 154 | bool undoPremultipliedAlpha; |
| 155 | bool requiresLinearEffect; |
| 156 | float layerDimmingRatio; |
Sally Qi | 628ef6e | 2023-03-30 14:49:03 -0700 | [diff] [blame] | 157 | const ui::Dataspace outputDataSpace; |
Alec Mouri | e0bb6f4 | 2023-08-02 22:41:52 +0000 | [diff] [blame] | 158 | const ui::Dataspace fakeOutputDataspace; |
Lingfeng Yang | 00c1ff6 | 2022-06-02 09:19:28 -0700 | [diff] [blame] | 159 | }; |
| 160 | sk_sp<SkShader> createRuntimeEffectShader(const RuntimeEffectShaderParameters&); |
| 161 | |
| 162 | const PixelFormat mDefaultPixelFormat; |
Lingfeng Yang | 00c1ff6 | 2022-06-02 09:19:28 -0700 | [diff] [blame] | 163 | |
| 164 | // Identifier used for various mappings of layers to various |
| 165 | // textures or shaders |
| 166 | using GraphicBufferId = uint64_t; |
| 167 | |
| 168 | // Number of external holders of ExternalTexture references, per GraphicBuffer ID. |
| 169 | std::unordered_map<GraphicBufferId, int32_t> mGraphicBufferExternalRefs |
| 170 | GUARDED_BY(mRenderingMutex); |
Lingfeng Yang | 00c1ff6 | 2022-06-02 09:19:28 -0700 | [diff] [blame] | 171 | std::unordered_map<GraphicBufferId, std::shared_ptr<AutoBackendTexture::LocalRef>> mTextureCache |
| 172 | GUARDED_BY(mRenderingMutex); |
| 173 | std::unordered_map<shaders::LinearEffect, sk_sp<SkRuntimeEffect>, shaders::LinearEffectHasher> |
| 174 | mRuntimeEffects; |
| 175 | AutoBackendTexture::CleanupManager mTextureCleanupMgr GUARDED_BY(mRenderingMutex); |
| 176 | |
| 177 | StretchShaderFactory mStretchShaderFactory; |
| 178 | |
| 179 | sp<Fence> mLastDrawFence; |
| 180 | BlurFilter* mBlurFilter = nullptr; |
| 181 | |
| 182 | // Object to capture commands send to Skia. |
| 183 | std::unique_ptr<SkiaCapture> mCapture; |
| 184 | |
| 185 | // Mutex guarding rendering operations, so that internal state related to |
| 186 | // rendering that is potentially modified by multiple threads is guaranteed thread-safe. |
| 187 | mutable std::mutex mRenderingMutex; |
Lingfeng Yang | 00c1ff6 | 2022-06-02 09:19:28 -0700 | [diff] [blame] | 188 | |
| 189 | // Graphics context used for creating surfaces and submitting commands |
Nolan Scobie | fc125ec | 2024-03-11 20:08:27 -0400 | [diff] [blame] | 190 | unique_ptr<SkiaGpuContext> mContext; |
Lingfeng Yang | 00c1ff6 | 2022-06-02 09:19:28 -0700 | [diff] [blame] | 191 | // Same as above, but for protected content (eg. DRM) |
Nolan Scobie | fc125ec | 2024-03-11 20:08:27 -0400 | [diff] [blame] | 192 | unique_ptr<SkiaGpuContext> mProtectedContext; |
Lingfeng Yang | 00c1ff6 | 2022-06-02 09:19:28 -0700 | [diff] [blame] | 193 | bool mInProtectedContext = false; |
John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 194 | }; |
| 195 | |
| 196 | } // namespace skia |
| 197 | } // namespace renderengine |
| 198 | } // namespace android |
| 199 | |
Leon Scroggins III | b9216dc | 2021-03-08 17:19:01 -0500 | [diff] [blame] | 200 | #endif /* SF_GLESRENDERENGINE_H_ */ |