blob: e88d44cca6ccf1dac1e30aae4438870a5d9c24ed [file] [log] [blame]
John Reck67b1e2b2020-08-26 13:17:24 -07001/*
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 Yang00c1ff62022-06-02 09:19:28 -070023#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
44class SkData;
45
46struct SkPoint3;
47
John Reck67b1e2b2020-08-26 13:17:24 -070048namespace android {
49
50namespace renderengine {
51
52class Mesh;
53class Texture;
54
55namespace skia {
56
57class BlurFilter;
58
Alec Mouri081be4c2020-09-16 10:24:47 -070059class SkiaRenderEngine : public RenderEngine {
John Reck67b1e2b2020-08-26 13:17:24 -070060public:
61 static std::unique_ptr<SkiaRenderEngine> create(const RenderEngineCreationArgs& args);
Leon Scroggins III696bf932024-01-24 15:21:05 -050062 SkiaRenderEngine(Threaded, PixelFormat pixelFormat, bool supportsBackgroundBlur);
Lingfeng Yang00c1ff62022-06-02 09:19:28 -070063 ~SkiaRenderEngine() override;
John Reck67b1e2b2020-08-26 13:17:24 -070064
Bruno BELANYIb9b5b702023-10-13 13:25:11 +000065 std::future<void> primeCache(bool shouldPrimeUltraHDR) override final;
Lingfeng Yang00c1ff62022-06-02 09:19:28 -070066 void cleanupPostRender() override final;
Lingfeng Yang00c1ff62022-06-02 09:19:28 -070067 bool supportsBackgroundBlur() override final {
68 return mBlurFilter != nullptr;
69 }
70 void onActiveDisplaySizeChanged(ui::Size size) override final;
71 int reportShadersCompiled();
Alec Mouria90a5702021-04-16 16:36:21 +000072
Lingfeng Yang00c1ff62022-06-02 09:19:28 -070073 virtual void setEnableTracing(bool tracingEnabled) override final;
74
75 void useProtectedContext(bool useProtectedContext) override;
76 bool supportsProtectedContent() const override {
77 return supportsProtectedContentImpl();
78 }
79 void ensureGrContextsCreated();
Alec Mouria90a5702021-04-16 16:36:21 +000080protected:
Lingfeng Yang00c1ff62022-06-02 09:19:28 -070081 // This is so backends can stop the generic rendering state first before
82 // cleaning up backend-specific state
83 void finishRenderingAndAbandonContext();
Sally Qi4cabdd02021-08-05 16:45:57 -070084
Lingfeng Yang00c1ff62022-06-02 09:19:28 -070085 // Functions that a given backend (GLES, Vulkan) must implement
86 using Contexts = std::pair<sk_sp<GrDirectContext>, sk_sp<GrDirectContext>>;
87 virtual Contexts createDirectContexts(const GrContextOptions& options) = 0;
88 virtual bool supportsProtectedContentImpl() const = 0;
89 virtual bool useProtectedContextImpl(GrProtected isProtected) = 0;
90 virtual void waitFence(GrDirectContext* grContext, base::borrowed_fd fenceFd) = 0;
91 virtual base::unique_fd flushAndSubmit(GrDirectContext* context) = 0;
92 virtual void appendBackendSpecificInfoToDump(std::string& result) = 0;
93
94 size_t getMaxTextureSize() const override final;
95 size_t getMaxViewportDims() const override final;
96 GrDirectContext* getActiveGrContext();
97
Patrick Williams8aed5d22022-10-31 22:18:10 +000098 bool isProtected() const { return mInProtectedContext; }
99
Lingfeng Yang00c1ff62022-06-02 09:19:28 -0700100 // Implements PersistentCache as a way to monitor what SkSL shaders Skia has
101 // cached.
102 class SkSLCacheMonitor : public GrContextOptions::PersistentCache {
103 public:
104 SkSLCacheMonitor() = default;
105 ~SkSLCacheMonitor() override = default;
106
107 sk_sp<SkData> load(const SkData& key) override;
108
109 void store(const SkData& key, const SkData& data, const SkString& description) override;
110
111 int shadersCachedSinceLastCall() {
112 const int shadersCachedSinceLastCall = mShadersCachedSinceLastCall;
113 mShadersCachedSinceLastCall = 0;
114 return shadersCachedSinceLastCall;
115 }
116
117 int totalShadersCompiled() const { return mTotalShadersCompiled; }
118
119 private:
120 int mShadersCachedSinceLastCall = 0;
121 int mTotalShadersCompiled = 0;
Sally Qi4cabdd02021-08-05 16:45:57 -0700122 };
Lingfeng Yang00c1ff62022-06-02 09:19:28 -0700123
124private:
125 void mapExternalTextureBuffer(const sp<GraphicBuffer>& buffer,
126 bool isRenderable) override final;
Alec Mouri92f89fa2023-02-24 00:05:06 +0000127 void unmapExternalTextureBuffer(sp<GraphicBuffer>&& buffer) override final;
Lingfeng Yang00c1ff62022-06-02 09:19:28 -0700128 bool canSkipPostRenderCleanup() const override final;
129
Ian Elliott8506e362023-03-08 12:12:09 -0700130 std::shared_ptr<AutoBackendTexture::LocalRef> getOrCreateBackendTexture(
131 const sp<GraphicBuffer>& buffer, bool isOutputBuffer) REQUIRES(mRenderingMutex);
Lingfeng Yang00c1ff62022-06-02 09:19:28 -0700132 void initCanvas(SkCanvas* canvas, const DisplaySettings& display);
133 void drawShadow(SkCanvas* canvas, const SkRRect& casterRRect,
134 const ShadowSettings& shadowSettings);
Patrick Williams2e9748f2022-08-09 22:48:18 +0000135 void drawLayersInternal(const std::shared_ptr<std::promise<FenceResult>>&& resultPromise,
Lingfeng Yang00c1ff62022-06-02 09:19:28 -0700136 const DisplaySettings& display,
137 const std::vector<LayerSettings>& layers,
138 const std::shared_ptr<ExternalTexture>& buffer,
Lingfeng Yang00c1ff62022-06-02 09:19:28 -0700139 base::unique_fd&& bufferFence) override final;
140
141 void dump(std::string& result) override final;
142
143 // If requiresLinearEffect is true or the layer has a stretchEffect a new shader is returned.
144 // Otherwise it returns the input shader.
145 struct RuntimeEffectShaderParameters {
146 sk_sp<SkShader> shader;
147 const LayerSettings& layer;
148 const DisplaySettings& display;
149 bool undoPremultipliedAlpha;
150 bool requiresLinearEffect;
151 float layerDimmingRatio;
Sally Qi628ef6e2023-03-30 14:49:03 -0700152 const ui::Dataspace outputDataSpace;
Alec Mourie0bb6f42023-08-02 22:41:52 +0000153 const ui::Dataspace fakeOutputDataspace;
Lingfeng Yang00c1ff62022-06-02 09:19:28 -0700154 };
155 sk_sp<SkShader> createRuntimeEffectShader(const RuntimeEffectShaderParameters&);
156
157 const PixelFormat mDefaultPixelFormat;
Lingfeng Yang00c1ff62022-06-02 09:19:28 -0700158
159 // Identifier used for various mappings of layers to various
160 // textures or shaders
161 using GraphicBufferId = uint64_t;
162
163 // Number of external holders of ExternalTexture references, per GraphicBuffer ID.
164 std::unordered_map<GraphicBufferId, int32_t> mGraphicBufferExternalRefs
165 GUARDED_BY(mRenderingMutex);
Ian Elliott8506e362023-03-08 12:12:09 -0700166 // For GL, this cache is shared between protected and unprotected contexts. For Vulkan, it is
167 // only used for the unprotected context, because Vulkan does not allow sharing between
168 // contexts, and protected is less common.
Lingfeng Yang00c1ff62022-06-02 09:19:28 -0700169 std::unordered_map<GraphicBufferId, std::shared_ptr<AutoBackendTexture::LocalRef>> mTextureCache
170 GUARDED_BY(mRenderingMutex);
171 std::unordered_map<shaders::LinearEffect, sk_sp<SkRuntimeEffect>, shaders::LinearEffectHasher>
172 mRuntimeEffects;
173 AutoBackendTexture::CleanupManager mTextureCleanupMgr GUARDED_BY(mRenderingMutex);
174
175 StretchShaderFactory mStretchShaderFactory;
176
177 sp<Fence> mLastDrawFence;
178 BlurFilter* mBlurFilter = nullptr;
179
180 // Object to capture commands send to Skia.
181 std::unique_ptr<SkiaCapture> mCapture;
182
183 // Mutex guarding rendering operations, so that internal state related to
184 // rendering that is potentially modified by multiple threads is guaranteed thread-safe.
185 mutable std::mutex mRenderingMutex;
186 SkSLCacheMonitor mSkSLCacheMonitor;
187
188 // Graphics context used for creating surfaces and submitting commands
189 sk_sp<GrDirectContext> mGrContext;
190 // Same as above, but for protected content (eg. DRM)
191 sk_sp<GrDirectContext> mProtectedGrContext;
192 bool mInProtectedContext = false;
John Reck67b1e2b2020-08-26 13:17:24 -0700193};
194
195} // namespace skia
196} // namespace renderengine
197} // namespace android
198
Leon Scroggins IIIb9216dc2021-03-08 17:19:01 -0500199#endif /* SF_GLESRENDERENGINE_H_ */