blob: 7be4c253e76cbc36e581909562caa0edebb0df22 [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>
John Reck67b1e2b2020-08-26 13:17:24 -070021
Lingfeng Yang00c1ff62022-06-02 09:19:28 -070022#include <android-base/thread_annotations.h>
Nolan Scobiebc3f3602024-08-30 13:51:37 -040023#include <include/core/SkImageInfo.h>
24#include <include/core/SkSurface.h>
25#include <include/gpu/ganesh/GrBackendSemaphore.h>
26#include <include/gpu/ganesh/GrContextOptions.h>
Lingfeng Yang00c1ff62022-06-02 09:19:28 -070027#include <renderengine/ExternalTexture.h>
28#include <renderengine/RenderEngine.h>
29#include <sys/types.h>
30
Nolan Scobiefc125ec2024-03-11 20:08:27 -040031#include <memory>
Lingfeng Yang00c1ff62022-06-02 09:19:28 -070032#include <mutex>
33#include <unordered_map>
34
35#include "AutoBackendTexture.h"
Lingfeng Yang00c1ff62022-06-02 09:19:28 -070036#include "android-base/macros.h"
Nolan Scobiefc125ec2024-03-11 20:08:27 -040037#include "compat/SkiaGpuContext.h"
Lingfeng Yang00c1ff62022-06-02 09:19:28 -070038#include "debug/SkiaCapture.h"
39#include "filters/BlurFilter.h"
Marzia Favarodcc9d9b2024-01-10 10:17:00 +000040#include "filters/EdgeExtensionShaderFactory.h"
Lingfeng Yang00c1ff62022-06-02 09:19:28 -070041#include "filters/LinearEffect.h"
Sally Qief006582024-10-11 13:23:09 -070042#include "filters/LutShader.h"
Lingfeng Yang00c1ff62022-06-02 09:19:28 -070043#include "filters/StretchShaderFactory.h"
44
45class SkData;
46
47struct SkPoint3;
48
John Reck67b1e2b2020-08-26 13:17:24 -070049namespace android {
50
51namespace renderengine {
52
53class Mesh;
54class Texture;
55
56namespace skia {
57
58class BlurFilter;
59
Alec Mouri081be4c2020-09-16 10:24:47 -070060class SkiaRenderEngine : public RenderEngine {
John Reck67b1e2b2020-08-26 13:17:24 -070061public:
62 static std::unique_ptr<SkiaRenderEngine> create(const RenderEngineCreationArgs& args);
Robin Lee7338bd92024-04-04 14:05:07 +000063 SkiaRenderEngine(Threaded, PixelFormat pixelFormat, BlurAlgorithm);
Lingfeng Yang00c1ff62022-06-02 09:19:28 -070064 ~SkiaRenderEngine() override;
John Reck67b1e2b2020-08-26 13:17:24 -070065
Russell Myers3348c742024-04-29 20:22:42 +000066 std::future<void> primeCache(PrimeCacheConfig config) override final;
Lingfeng Yang00c1ff62022-06-02 09:19:28 -070067 void cleanupPostRender() override final;
Lingfeng Yang00c1ff62022-06-02 09:19:28 -070068 bool supportsBackgroundBlur() override final {
69 return mBlurFilter != nullptr;
70 }
71 void onActiveDisplaySizeChanged(ui::Size size) override final;
72 int reportShadersCompiled();
Alec Mouria90a5702021-04-16 16:36:21 +000073
Lingfeng Yang00c1ff62022-06-02 09:19:28 -070074 virtual void setEnableTracing(bool tracingEnabled) override final;
75
76 void useProtectedContext(bool useProtectedContext) override;
77 bool supportsProtectedContent() const override {
78 return supportsProtectedContentImpl();
79 }
Nolan Scobiefc125ec2024-03-11 20:08:27 -040080 void ensureContextsCreated();
81
Alec Mouria90a5702021-04-16 16:36:21 +000082protected:
Nolan Scobie2526b2f2024-04-16 15:12:22 -040083 // This is so backends can stop the generic rendering state first before cleaning up
84 // backend-specific state. SkiaGpuContexts are invalid after invocation.
85 void finishRenderingAndAbandonContexts();
Sally Qi4cabdd02021-08-05 16:45:57 -070086
Lingfeng Yang00c1ff62022-06-02 09:19:28 -070087 // Functions that a given backend (GLES, Vulkan) must implement
Nolan Scobiefc125ec2024-03-11 20:08:27 -040088 using Contexts = std::pair<unique_ptr<SkiaGpuContext>, unique_ptr<SkiaGpuContext>>;
89 virtual Contexts createContexts() = 0;
Lingfeng Yang00c1ff62022-06-02 09:19:28 -070090 virtual bool supportsProtectedContentImpl() const = 0;
91 virtual bool useProtectedContextImpl(GrProtected isProtected) = 0;
Nolan Scobiefc125ec2024-03-11 20:08:27 -040092 virtual void waitFence(SkiaGpuContext* context, base::borrowed_fd fenceFd) = 0;
Nolan Scobie1e06f2d2024-03-21 14:56:38 -040093 virtual base::unique_fd flushAndSubmit(SkiaGpuContext* context,
94 sk_sp<SkSurface> dstSurface) = 0;
Lingfeng Yang00c1ff62022-06-02 09:19:28 -070095 virtual void appendBackendSpecificInfoToDump(std::string& result) = 0;
96
97 size_t getMaxTextureSize() const override final;
98 size_t getMaxViewportDims() const override final;
Nolan Scobie02c160c2024-03-18 10:40:23 -040099 // TODO: b/293371537 - Return reference instead of pointer? (Cleanup)
Nolan Scobiefc125ec2024-03-11 20:08:27 -0400100 SkiaGpuContext* getActiveContext();
Lingfeng Yang00c1ff62022-06-02 09:19:28 -0700101
Patrick Williams8aed5d22022-10-31 22:18:10 +0000102 bool isProtected() const { return mInProtectedContext; }
103
Lingfeng Yang00c1ff62022-06-02 09:19:28 -0700104 // Implements PersistentCache as a way to monitor what SkSL shaders Skia has
105 // cached.
106 class SkSLCacheMonitor : public GrContextOptions::PersistentCache {
107 public:
108 SkSLCacheMonitor() = default;
109 ~SkSLCacheMonitor() override = default;
110
111 sk_sp<SkData> load(const SkData& key) override;
112
113 void store(const SkData& key, const SkData& data, const SkString& description) override;
114
115 int shadersCachedSinceLastCall() {
116 const int shadersCachedSinceLastCall = mShadersCachedSinceLastCall;
117 mShadersCachedSinceLastCall = 0;
118 return shadersCachedSinceLastCall;
119 }
120
121 int totalShadersCompiled() const { return mTotalShadersCompiled; }
122
123 private:
124 int mShadersCachedSinceLastCall = 0;
125 int mTotalShadersCompiled = 0;
Sally Qi4cabdd02021-08-05 16:45:57 -0700126 };
Lingfeng Yang00c1ff62022-06-02 09:19:28 -0700127
Nolan Scobiefc125ec2024-03-11 20:08:27 -0400128 SkSLCacheMonitor mSkSLCacheMonitor;
129
Lingfeng Yang00c1ff62022-06-02 09:19:28 -0700130private:
131 void mapExternalTextureBuffer(const sp<GraphicBuffer>& buffer,
132 bool isRenderable) override final;
Alec Mouri92f89fa2023-02-24 00:05:06 +0000133 void unmapExternalTextureBuffer(sp<GraphicBuffer>&& buffer) override final;
Lingfeng Yang00c1ff62022-06-02 09:19:28 -0700134 bool canSkipPostRenderCleanup() const override final;
135
Ian Elliott8506e362023-03-08 12:12:09 -0700136 std::shared_ptr<AutoBackendTexture::LocalRef> getOrCreateBackendTexture(
137 const sp<GraphicBuffer>& buffer, bool isOutputBuffer) REQUIRES(mRenderingMutex);
Lingfeng Yang00c1ff62022-06-02 09:19:28 -0700138 void initCanvas(SkCanvas* canvas, const DisplaySettings& display);
139 void drawShadow(SkCanvas* canvas, const SkRRect& casterRRect,
140 const ShadowSettings& shadowSettings);
Patrick Williams2e9748f2022-08-09 22:48:18 +0000141 void drawLayersInternal(const std::shared_ptr<std::promise<FenceResult>>&& resultPromise,
Lingfeng Yang00c1ff62022-06-02 09:19:28 -0700142 const DisplaySettings& display,
143 const std::vector<LayerSettings>& layers,
144 const std::shared_ptr<ExternalTexture>& buffer,
Lingfeng Yang00c1ff62022-06-02 09:19:28 -0700145 base::unique_fd&& bufferFence) override final;
Alec Mouri1b1853f2024-07-15 22:46:58 +0000146 void drawGainmapInternal(const std::shared_ptr<std::promise<FenceResult>>&& resultPromise,
147 const std::shared_ptr<ExternalTexture>& sdr,
148 base::borrowed_fd&& sdrFence,
149 const std::shared_ptr<ExternalTexture>& hdr,
150 base::borrowed_fd&& hdrFence, float hdrSdrRatio,
151 ui::Dataspace dataspace,
152 const std::shared_ptr<ExternalTexture>& gainmap) override final;
Lingfeng Yang00c1ff62022-06-02 09:19:28 -0700153
154 void dump(std::string& result) override final;
155
156 // If requiresLinearEffect is true or the layer has a stretchEffect a new shader is returned.
157 // Otherwise it returns the input shader.
158 struct RuntimeEffectShaderParameters {
159 sk_sp<SkShader> shader;
160 const LayerSettings& layer;
161 const DisplaySettings& display;
162 bool undoPremultipliedAlpha;
163 bool requiresLinearEffect;
164 float layerDimmingRatio;
Sally Qi628ef6e2023-03-30 14:49:03 -0700165 const ui::Dataspace outputDataSpace;
Alec Mourie0bb6f42023-08-02 22:41:52 +0000166 const ui::Dataspace fakeOutputDataspace;
Marzia Favarodcc9d9b2024-01-10 10:17:00 +0000167 const SkRect& imageBounds;
Lingfeng Yang00c1ff62022-06-02 09:19:28 -0700168 };
169 sk_sp<SkShader> createRuntimeEffectShader(const RuntimeEffectShaderParameters&);
170
171 const PixelFormat mDefaultPixelFormat;
Lingfeng Yang00c1ff62022-06-02 09:19:28 -0700172
173 // Identifier used for various mappings of layers to various
174 // textures or shaders
175 using GraphicBufferId = uint64_t;
176
177 // Number of external holders of ExternalTexture references, per GraphicBuffer ID.
178 std::unordered_map<GraphicBufferId, int32_t> mGraphicBufferExternalRefs
179 GUARDED_BY(mRenderingMutex);
Lingfeng Yang00c1ff62022-06-02 09:19:28 -0700180 std::unordered_map<GraphicBufferId, std::shared_ptr<AutoBackendTexture::LocalRef>> mTextureCache
181 GUARDED_BY(mRenderingMutex);
182 std::unordered_map<shaders::LinearEffect, sk_sp<SkRuntimeEffect>, shaders::LinearEffectHasher>
183 mRuntimeEffects;
184 AutoBackendTexture::CleanupManager mTextureCleanupMgr GUARDED_BY(mRenderingMutex);
185
186 StretchShaderFactory mStretchShaderFactory;
Marzia Favarodcc9d9b2024-01-10 10:17:00 +0000187 EdgeExtensionShaderFactory mEdgeExtensionShaderFactory;
Sally Qief006582024-10-11 13:23:09 -0700188 LutShader mLutShader;
Lingfeng Yang00c1ff62022-06-02 09:19:28 -0700189
190 sp<Fence> mLastDrawFence;
191 BlurFilter* mBlurFilter = nullptr;
192
193 // Object to capture commands send to Skia.
194 std::unique_ptr<SkiaCapture> mCapture;
195
196 // Mutex guarding rendering operations, so that internal state related to
197 // rendering that is potentially modified by multiple threads is guaranteed thread-safe.
198 mutable std::mutex mRenderingMutex;
Lingfeng Yang00c1ff62022-06-02 09:19:28 -0700199
200 // Graphics context used for creating surfaces and submitting commands
Nolan Scobiefc125ec2024-03-11 20:08:27 -0400201 unique_ptr<SkiaGpuContext> mContext;
Lingfeng Yang00c1ff62022-06-02 09:19:28 -0700202 // Same as above, but for protected content (eg. DRM)
Nolan Scobiefc125ec2024-03-11 20:08:27 -0400203 unique_ptr<SkiaGpuContext> mProtectedContext;
Lingfeng Yang00c1ff62022-06-02 09:19:28 -0700204 bool mInProtectedContext = false;
John Reck67b1e2b2020-08-26 13:17:24 -0700205};
206
207} // namespace skia
208} // namespace renderengine
209} // namespace android
210
Leon Scroggins IIIb9216dc2021-03-08 17:19:01 -0500211#endif /* SF_GLESRENDERENGINE_H_ */