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> |
| 24 | #include <GrDirectContext.h> |
| 25 | #include <SkSurface.h> |
| 26 | #include <android-base/thread_annotations.h> |
| 27 | #include <renderengine/ExternalTexture.h> |
| 28 | #include <renderengine/RenderEngine.h> |
| 29 | #include <sys/types.h> |
| 30 | |
| 31 | #include <mutex> |
| 32 | #include <unordered_map> |
| 33 | |
| 34 | #include "AutoBackendTexture.h" |
| 35 | #include "GrContextOptions.h" |
| 36 | #include "SkImageInfo.h" |
| 37 | #include "SkiaRenderEngine.h" |
| 38 | #include "android-base/macros.h" |
| 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); |
Lingfeng Yang | 00c1ff6 | 2022-06-02 09:19:28 -0700 | [diff] [blame] | 62 | SkiaRenderEngine(RenderEngineType type, |
| 63 | PixelFormat pixelFormat, |
| 64 | bool useColorManagement, |
| 65 | bool supportsBackgroundBlur); |
| 66 | ~SkiaRenderEngine() override; |
John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 67 | |
Lingfeng Yang | 00c1ff6 | 2022-06-02 09:19:28 -0700 | [diff] [blame] | 68 | std::future<void> primeCache() override final; |
| 69 | void cleanupPostRender() override final; |
| 70 | void cleanFramebufferCache() override final{ } |
Lingfeng Yang | 00c1ff6 | 2022-06-02 09:19:28 -0700 | [diff] [blame] | 71 | bool supportsBackgroundBlur() override final { |
| 72 | return mBlurFilter != nullptr; |
| 73 | } |
| 74 | void onActiveDisplaySizeChanged(ui::Size size) override final; |
| 75 | int reportShadersCompiled(); |
Alec Mouri | a90a570 | 2021-04-16 16:36:21 +0000 | [diff] [blame] | 76 | |
Lingfeng Yang | 00c1ff6 | 2022-06-02 09:19:28 -0700 | [diff] [blame] | 77 | virtual void genTextures(size_t /*count*/, uint32_t* /*names*/) override final{}; |
| 78 | virtual void deleteTextures(size_t /*count*/, uint32_t const* /*names*/) override final{}; |
| 79 | virtual void setEnableTracing(bool tracingEnabled) override final; |
| 80 | |
| 81 | void useProtectedContext(bool useProtectedContext) override; |
| 82 | bool supportsProtectedContent() const override { |
| 83 | return supportsProtectedContentImpl(); |
| 84 | } |
| 85 | void ensureGrContextsCreated(); |
Alec Mouri | a90a570 | 2021-04-16 16:36:21 +0000 | [diff] [blame] | 86 | protected: |
Lingfeng Yang | 00c1ff6 | 2022-06-02 09:19:28 -0700 | [diff] [blame] | 87 | // This is so backends can stop the generic rendering state first before |
| 88 | // cleaning up backend-specific state |
| 89 | void finishRenderingAndAbandonContext(); |
Sally Qi | 4cabdd0 | 2021-08-05 16:45:57 -0700 | [diff] [blame] | 90 | |
Lingfeng Yang | 00c1ff6 | 2022-06-02 09:19:28 -0700 | [diff] [blame] | 91 | // Functions that a given backend (GLES, Vulkan) must implement |
| 92 | using Contexts = std::pair<sk_sp<GrDirectContext>, sk_sp<GrDirectContext>>; |
| 93 | virtual Contexts createDirectContexts(const GrContextOptions& options) = 0; |
| 94 | virtual bool supportsProtectedContentImpl() const = 0; |
| 95 | virtual bool useProtectedContextImpl(GrProtected isProtected) = 0; |
| 96 | virtual void waitFence(GrDirectContext* grContext, base::borrowed_fd fenceFd) = 0; |
| 97 | virtual base::unique_fd flushAndSubmit(GrDirectContext* context) = 0; |
| 98 | virtual void appendBackendSpecificInfoToDump(std::string& result) = 0; |
| 99 | |
| 100 | size_t getMaxTextureSize() const override final; |
| 101 | size_t getMaxViewportDims() const override final; |
| 102 | GrDirectContext* getActiveGrContext(); |
| 103 | |
Patrick Williams | 8aed5d2 | 2022-10-31 22:18:10 +0000 | [diff] [blame] | 104 | bool isProtected() const { return mInProtectedContext; } |
| 105 | |
Lingfeng Yang | 00c1ff6 | 2022-06-02 09:19:28 -0700 | [diff] [blame] | 106 | // Implements PersistentCache as a way to monitor what SkSL shaders Skia has |
| 107 | // cached. |
| 108 | class SkSLCacheMonitor : public GrContextOptions::PersistentCache { |
| 109 | public: |
| 110 | SkSLCacheMonitor() = default; |
| 111 | ~SkSLCacheMonitor() override = default; |
| 112 | |
| 113 | sk_sp<SkData> load(const SkData& key) override; |
| 114 | |
| 115 | void store(const SkData& key, const SkData& data, const SkString& description) override; |
| 116 | |
| 117 | int shadersCachedSinceLastCall() { |
| 118 | const int shadersCachedSinceLastCall = mShadersCachedSinceLastCall; |
| 119 | mShadersCachedSinceLastCall = 0; |
| 120 | return shadersCachedSinceLastCall; |
| 121 | } |
| 122 | |
| 123 | int totalShadersCompiled() const { return mTotalShadersCompiled; } |
| 124 | |
| 125 | private: |
| 126 | int mShadersCachedSinceLastCall = 0; |
| 127 | int mTotalShadersCompiled = 0; |
Sally Qi | 4cabdd0 | 2021-08-05 16:45:57 -0700 | [diff] [blame] | 128 | }; |
Lingfeng Yang | 00c1ff6 | 2022-06-02 09:19:28 -0700 | [diff] [blame] | 129 | |
| 130 | private: |
| 131 | void mapExternalTextureBuffer(const sp<GraphicBuffer>& buffer, |
| 132 | bool isRenderable) override final; |
Alec Mouri | 92f89fa | 2023-02-24 00:05:06 +0000 | [diff] [blame] | 133 | void unmapExternalTextureBuffer(sp<GraphicBuffer>&& buffer) override final; |
Lingfeng Yang | 00c1ff6 | 2022-06-02 09:19:28 -0700 | [diff] [blame] | 134 | bool canSkipPostRenderCleanup() const override final; |
| 135 | |
Ian Elliott | 8506e36 | 2023-03-08 12:12:09 -0700 | [diff] [blame] | 136 | std::shared_ptr<AutoBackendTexture::LocalRef> getOrCreateBackendTexture( |
| 137 | const sp<GraphicBuffer>& buffer, bool isOutputBuffer) REQUIRES(mRenderingMutex); |
Lingfeng Yang | 00c1ff6 | 2022-06-02 09:19:28 -0700 | [diff] [blame] | 138 | void initCanvas(SkCanvas* canvas, const DisplaySettings& display); |
| 139 | void drawShadow(SkCanvas* canvas, const SkRRect& casterRRect, |
| 140 | const ShadowSettings& shadowSettings); |
Patrick Williams | 2e9748f | 2022-08-09 22:48:18 +0000 | [diff] [blame] | 141 | void drawLayersInternal(const std::shared_ptr<std::promise<FenceResult>>&& resultPromise, |
Lingfeng Yang | 00c1ff6 | 2022-06-02 09:19:28 -0700 | [diff] [blame] | 142 | const DisplaySettings& display, |
| 143 | const std::vector<LayerSettings>& layers, |
| 144 | const std::shared_ptr<ExternalTexture>& buffer, |
| 145 | const bool useFramebufferCache, |
| 146 | base::unique_fd&& bufferFence) override final; |
| 147 | |
| 148 | void dump(std::string& result) override final; |
| 149 | |
| 150 | // If requiresLinearEffect is true or the layer has a stretchEffect a new shader is returned. |
| 151 | // Otherwise it returns the input shader. |
| 152 | struct RuntimeEffectShaderParameters { |
| 153 | sk_sp<SkShader> shader; |
| 154 | const LayerSettings& layer; |
| 155 | const DisplaySettings& display; |
| 156 | bool undoPremultipliedAlpha; |
| 157 | bool requiresLinearEffect; |
| 158 | float layerDimmingRatio; |
Sally Qi | 628ef6e | 2023-03-30 14:49:03 -0700 | [diff] [blame] | 159 | const ui::Dataspace outputDataSpace; |
Alec Mouri | e0bb6f4 | 2023-08-02 22:41:52 +0000 | [diff] [blame^] | 160 | const ui::Dataspace fakeOutputDataspace; |
Lingfeng Yang | 00c1ff6 | 2022-06-02 09:19:28 -0700 | [diff] [blame] | 161 | }; |
| 162 | sk_sp<SkShader> createRuntimeEffectShader(const RuntimeEffectShaderParameters&); |
| 163 | |
| 164 | const PixelFormat mDefaultPixelFormat; |
| 165 | const bool mUseColorManagement; |
| 166 | |
| 167 | // Identifier used for various mappings of layers to various |
| 168 | // textures or shaders |
| 169 | using GraphicBufferId = uint64_t; |
| 170 | |
| 171 | // Number of external holders of ExternalTexture references, per GraphicBuffer ID. |
| 172 | std::unordered_map<GraphicBufferId, int32_t> mGraphicBufferExternalRefs |
| 173 | GUARDED_BY(mRenderingMutex); |
Ian Elliott | 8506e36 | 2023-03-08 12:12:09 -0700 | [diff] [blame] | 174 | // For GL, this cache is shared between protected and unprotected contexts. For Vulkan, it is |
| 175 | // only used for the unprotected context, because Vulkan does not allow sharing between |
| 176 | // contexts, and protected is less common. |
Lingfeng Yang | 00c1ff6 | 2022-06-02 09:19:28 -0700 | [diff] [blame] | 177 | std::unordered_map<GraphicBufferId, std::shared_ptr<AutoBackendTexture::LocalRef>> mTextureCache |
| 178 | GUARDED_BY(mRenderingMutex); |
| 179 | std::unordered_map<shaders::LinearEffect, sk_sp<SkRuntimeEffect>, shaders::LinearEffectHasher> |
| 180 | mRuntimeEffects; |
| 181 | AutoBackendTexture::CleanupManager mTextureCleanupMgr GUARDED_BY(mRenderingMutex); |
| 182 | |
| 183 | StretchShaderFactory mStretchShaderFactory; |
| 184 | |
| 185 | sp<Fence> mLastDrawFence; |
| 186 | BlurFilter* mBlurFilter = nullptr; |
| 187 | |
| 188 | // Object to capture commands send to Skia. |
| 189 | std::unique_ptr<SkiaCapture> mCapture; |
| 190 | |
| 191 | // Mutex guarding rendering operations, so that internal state related to |
| 192 | // rendering that is potentially modified by multiple threads is guaranteed thread-safe. |
| 193 | mutable std::mutex mRenderingMutex; |
| 194 | SkSLCacheMonitor mSkSLCacheMonitor; |
| 195 | |
| 196 | // Graphics context used for creating surfaces and submitting commands |
| 197 | sk_sp<GrDirectContext> mGrContext; |
| 198 | // Same as above, but for protected content (eg. DRM) |
| 199 | sk_sp<GrDirectContext> mProtectedGrContext; |
| 200 | bool mInProtectedContext = false; |
John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 201 | }; |
| 202 | |
| 203 | } // namespace skia |
| 204 | } // namespace renderengine |
| 205 | } // namespace android |
| 206 | |
Leon Scroggins III | b9216dc | 2021-03-08 17:19:01 -0500 | [diff] [blame] | 207 | #endif /* SF_GLESRENDERENGINE_H_ */ |