blob: c891a708d9b4ad8ab35f237768b2012e80d70a3e [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_SKIAGLRENDERENGINE_H_
18#define SF_SKIAGLRENDERENGINE_H_
19
Alec Mourib5777452020-09-28 11:32:42 -070020#include <EGL/egl.h>
21#include <EGL/eglext.h>
22#include <GLES2/gl2.h>
23#include <GrDirectContext.h>
24#include <SkSurface.h>
25#include <android-base/thread_annotations.h>
Alec Mouria90a5702021-04-16 16:36:21 +000026#include <renderengine/ExternalTexture.h>
Alec Mourib5777452020-09-28 11:32:42 -070027#include <renderengine/RenderEngine.h>
John Reck67b1e2b2020-08-26 13:17:24 -070028#include <sys/types.h>
Alec Mourib5777452020-09-28 11:32:42 -070029
John Reck67b1e2b2020-08-26 13:17:24 -070030#include <mutex>
31#include <unordered_map>
32
Alec Mouric7f6c8b2020-11-09 18:35:20 -080033#include "AutoBackendTexture.h"
Alec Mourib5777452020-09-28 11:32:42 -070034#include "EGL/egl.h"
Leon Scroggins III9f3072c2021-03-22 10:42:47 -040035#include "GrContextOptions.h"
Alec Mouric7f6c8b2020-11-09 18:35:20 -080036#include "SkImageInfo.h"
John Reck67b1e2b2020-08-26 13:17:24 -070037#include "SkiaRenderEngine.h"
Alec Mouric7f6c8b2020-11-09 18:35:20 -080038#include "android-base/macros.h"
Alec Mouric0aae732021-01-12 13:32:18 -080039#include "debug/SkiaCapture.h"
Lucas Dupinf4cb4a02020-09-22 14:19:26 -070040#include "filters/BlurFilter.h"
Alec Mouric0aae732021-01-12 13:32:18 -080041#include "filters/LinearEffect.h"
Nader Jawad2dfc98b2021-04-08 20:35:39 -070042#include "filters/StretchShaderFactory.h"
John Reck67b1e2b2020-08-26 13:17:24 -070043
Kevin Lubick8e0ef682022-03-04 10:34:35 -050044class SkData;
45
John Reck67b1e2b2020-08-26 13:17:24 -070046namespace android {
47namespace renderengine {
48namespace skia {
49
50class SkiaGLRenderEngine : public skia::SkiaRenderEngine {
51public:
52 static std::unique_ptr<SkiaGLRenderEngine> create(const RenderEngineCreationArgs& args);
Lucas Dupind508e472020-11-04 04:32:06 +000053 SkiaGLRenderEngine(const RenderEngineCreationArgs& args, EGLDisplay display, EGLContext ctxt,
54 EGLSurface placeholder, EGLContext protectedContext,
John Reck67b1e2b2020-08-26 13:17:24 -070055 EGLSurface protectedPlaceholder);
Alec Mouric0aae732021-01-12 13:32:18 -080056 ~SkiaGLRenderEngine() override EXCLUDES(mRenderingMutex);
John Reck67b1e2b2020-08-26 13:17:24 -070057
Ady Abrahamfe2a6db2021-06-09 15:41:37 -070058 std::future<void> primeCache() override;
Derek Sollenbergerd3f60652021-06-11 15:34:36 -040059 void cleanupPostRender() override;
60 void cleanFramebufferCache() override{};
Alec Mourid6f09462020-12-07 11:18:17 -080061 int getContextPriority() override;
Lucas Dupind508e472020-11-04 04:32:06 +000062 bool isProtected() const override { return mInProtectedContext; }
63 bool supportsProtectedContent() const override;
Derek Sollenberger1ec2fb52021-06-16 15:11:27 -040064 void useProtectedContext(bool useProtectedContext) override;
Derek Sollenbergerb3998372021-02-16 15:16:56 -050065 bool supportsBackgroundBlur() override { return mBlurFilter != nullptr; }
Leon Scroggins III9f3072c2021-03-22 10:42:47 -040066 void assertShadersCompiled(int numShaders) override;
Ady Abrahamed3290f2021-05-17 15:12:14 -070067 void onActiveDisplaySizeChanged(ui::Size size) override;
Nathaniel Nifongb9f27ef2021-04-01 16:44:12 -040068 int reportShadersCompiled() override;
John Reck67b1e2b2020-08-26 13:17:24 -070069
70protected:
Ana Krulec1d12b3b2021-01-27 16:49:51 -080071 void dump(std::string& result) override;
John Reck67b1e2b2020-08-26 13:17:24 -070072 size_t getMaxTextureSize() const override;
73 size_t getMaxViewportDims() const override;
Alec Mouria90a5702021-04-16 16:36:21 +000074 void mapExternalTextureBuffer(const sp<GraphicBuffer>& buffer, bool isRenderable) override;
75 void unmapExternalTextureBuffer(const sp<GraphicBuffer>& buffer) override;
Derek Sollenbergerd3f60652021-06-11 15:34:36 -040076 bool canSkipPostRenderCleanup() const override;
Sally Qi4cabdd02021-08-05 16:45:57 -070077 void drawLayersInternal(const std::shared_ptr<std::promise<RenderEngineResult>>&& resultPromise,
78 const DisplaySettings& display,
Sally Qi59a9f502021-10-12 18:53:23 +000079 const std::vector<LayerSettings>& layers,
Sally Qi4cabdd02021-08-05 16:45:57 -070080 const std::shared_ptr<ExternalTexture>& buffer,
81 const bool useFramebufferCache, base::unique_fd&& bufferFence) override;
John Reck67b1e2b2020-08-26 13:17:24 -070082
83private:
84 static EGLConfig chooseEglConfig(EGLDisplay display, int format, bool logConfig);
85 static EGLContext createEglContext(EGLDisplay display, EGLConfig config,
Alec Mourid6f09462020-12-07 11:18:17 -080086 EGLContext shareContext,
87 std::optional<ContextPriority> contextPriority,
John Reck67b1e2b2020-08-26 13:17:24 -070088 Protection protection);
Alec Mourid6f09462020-12-07 11:18:17 -080089 static std::optional<RenderEngine::ContextPriority> createContextPriority(
90 const RenderEngineCreationArgs& args);
John Reck67b1e2b2020-08-26 13:17:24 -070091 static EGLSurface createPlaceholderEglPbufferSurface(EGLDisplay display, EGLConfig config,
92 int hwcFormat, Protection protection);
Lucas Dupin3f11e922020-09-22 17:31:04 -070093 inline SkRect getSkRect(const FloatRect& layer);
94 inline SkRect getSkRect(const Rect& layer);
Derek Sollenbergerc31985e2021-05-18 16:38:17 -040095 inline std::pair<SkRRect, SkRRect> getBoundsAndClip(const FloatRect& bounds,
96 const FloatRect& crop, float cornerRadius);
Sally Qi59a9f502021-10-12 18:53:23 +000097 inline bool layerHasBlur(const LayerSettings& layer, bool colorTransformModifiesAlpha);
Lucas Dupin3f11e922020-09-22 17:31:04 -070098 inline SkColor getSkColor(const vec4& color);
Lucas Dupinbb1a1d42020-09-18 15:17:02 -070099 inline SkM44 getSkM44(const mat4& matrix);
Lucas Dupin3f11e922020-09-22 17:31:04 -0700100 inline SkPoint3 getSkPoint3(const vec3& vector);
Derek Sollenbergerb24258c2021-05-04 13:47:34 -0400101 inline GrDirectContext* getActiveGrContext() const;
John Reck67b1e2b2020-08-26 13:17:24 -0700102
103 base::unique_fd flush();
Alec Mouri4ee7b492021-08-11 10:36:55 -0700104 // waitFence attempts to wait in the GPU, and if unable to waits on the CPU instead.
105 void waitFence(base::borrowed_fd fenceFd);
106 bool waitGpuFence(base::borrowed_fd fenceFd);
107
Derek Sollenberger3f77aa42021-02-04 11:06:34 -0500108 void initCanvas(SkCanvas* canvas, const DisplaySettings& display);
Derek Sollenbergerb0e764c2021-05-04 14:31:37 -0400109 void drawShadow(SkCanvas* canvas, const SkRRect& casterRRect,
Lucas Dupin3f11e922020-09-22 17:31:04 -0700110 const ShadowSettings& shadowSettings);
Alec Mouricdf6cbc2021-11-01 17:21:15 -0700111
Derek Sollenbergere2fe78c2021-02-23 13:22:54 -0500112 // If requiresLinearEffect is true or the layer has a stretchEffect a new shader is returned.
113 // Otherwise it returns the input shader.
Alec Mouricdf6cbc2021-11-01 17:21:15 -0700114 struct RuntimeEffectShaderParameters {
115 sk_sp<SkShader> shader;
116 const LayerSettings& layer;
117 const DisplaySettings& display;
118 bool undoPremultipliedAlpha;
119 bool requiresLinearEffect;
120 float layerDimmingRatio;
121 };
122 sk_sp<SkShader> createRuntimeEffectShader(const RuntimeEffectShaderParameters&);
John Reck67b1e2b2020-08-26 13:17:24 -0700123
124 EGLDisplay mEGLDisplay;
John Reck67b1e2b2020-08-26 13:17:24 -0700125 EGLContext mEGLContext;
126 EGLSurface mPlaceholderSurface;
127 EGLContext mProtectedEGLContext;
128 EGLSurface mProtectedPlaceholderSurface;
Lucas Dupinf4cb4a02020-09-22 14:19:26 -0700129 BlurFilter* mBlurFilter = nullptr;
John Reck67b1e2b2020-08-26 13:17:24 -0700130
Derek Sollenbergerc4a05e12021-03-24 16:45:20 -0400131 const PixelFormat mDefaultPixelFormat;
Alec Mourib5777452020-09-28 11:32:42 -0700132 const bool mUseColorManagement;
133
Nader Jawad2dfc98b2021-04-08 20:35:39 -0700134 // Identifier used or various mappings of layers to various
135 // textures or shaders
Nader Jawadc088bdc2021-05-10 13:24:46 -0700136 using GraphicBufferId = uint64_t;
Nader Jawad2dfc98b2021-04-08 20:35:39 -0700137
Alec Mouria90a5702021-04-16 16:36:21 +0000138 // Number of external holders of ExternalTexture references, per GraphicBuffer ID.
Nader Jawadc088bdc2021-05-10 13:24:46 -0700139 std::unordered_map<GraphicBufferId, int32_t> mGraphicBufferExternalRefs
Alec Mouric7f6c8b2020-11-09 18:35:20 -0800140 GUARDED_BY(mRenderingMutex);
Derek Sollenberger45007182021-06-10 14:47:21 -0400141 // Cache of GL textures that we'll store per GraphicBuffer ID, shared between GPU contexts.
Nader Jawadc088bdc2021-05-10 13:24:46 -0700142 std::unordered_map<GraphicBufferId, std::shared_ptr<AutoBackendTexture::LocalRef>> mTextureCache
143 GUARDED_BY(mRenderingMutex);
Alec Mouri492c85c2021-11-19 15:58:10 -0800144 std::unordered_map<shaders::LinearEffect, sk_sp<SkRuntimeEffect>, shaders::LinearEffectHasher>
145 mRuntimeEffects;
Derek Sollenbergerd3f60652021-06-11 15:34:36 -0400146 AutoBackendTexture::CleanupManager mTextureCleanupMgr GUARDED_BY(mRenderingMutex);
Nader Jawad2dfc98b2021-04-08 20:35:39 -0700147
148 StretchShaderFactory mStretchShaderFactory;
John Reck67b1e2b2020-08-26 13:17:24 -0700149 // Mutex guarding rendering operations, so that:
150 // 1. GL operations aren't interleaved, and
151 // 2. Internal state related to rendering that is potentially modified by
152 // multiple threads is guaranteed thread-safe.
Derek Sollenbergerd3f60652021-06-11 15:34:36 -0400153 mutable std::mutex mRenderingMutex;
John Reck67b1e2b2020-08-26 13:17:24 -0700154
155 sp<Fence> mLastDrawFence;
156
Lucas Dupind508e472020-11-04 04:32:06 +0000157 // Graphics context used for creating surfaces and submitting commands
John Reck67b1e2b2020-08-26 13:17:24 -0700158 sk_sp<GrDirectContext> mGrContext;
Lucas Dupind508e472020-11-04 04:32:06 +0000159 // Same as above, but for protected content (eg. DRM)
160 sk_sp<GrDirectContext> mProtectedGrContext;
John Reck67b1e2b2020-08-26 13:17:24 -0700161
Lucas Dupind508e472020-11-04 04:32:06 +0000162 bool mInProtectedContext = false;
Ana Krulec70d15b1b2020-12-01 10:05:15 -0800163 // Object to capture commands send to Skia.
Alec Mouric0aae732021-01-12 13:32:18 -0800164 std::unique_ptr<SkiaCapture> mCapture;
Leon Scroggins III9f3072c2021-03-22 10:42:47 -0400165
166 // Implements PersistentCache as a way to monitor what SkSL shaders Skia has
167 // cached.
168 class SkSLCacheMonitor : public GrContextOptions::PersistentCache {
169 public:
170 SkSLCacheMonitor() = default;
171 ~SkSLCacheMonitor() override = default;
172
173 sk_sp<SkData> load(const SkData& key) override;
174
175 void store(const SkData& key, const SkData& data, const SkString& description) override;
176
177 int shadersCachedSinceLastCall() {
178 const int shadersCachedSinceLastCall = mShadersCachedSinceLastCall;
179 mShadersCachedSinceLastCall = 0;
180 return shadersCachedSinceLastCall;
181 }
182
183 private:
184 int mShadersCachedSinceLastCall = 0;
185 };
186
187 SkSLCacheMonitor mSkSLCacheMonitor;
John Reck67b1e2b2020-08-26 13:17:24 -0700188};
189
190} // namespace skia
191} // namespace renderengine
192} // namespace android
193
Galia Peycheva6c460652020-11-03 19:42:42 +0100194#endif /* SF_GLESRENDERENGINE_H_ */