blob: 8e77c16b40ebdbba48eb3dc6f2d259ba6c4b0a0c [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>
26#include <renderengine/RenderEngine.h>
John Reck67b1e2b2020-08-26 13:17:24 -070027#include <sys/types.h>
Alec Mourib5777452020-09-28 11:32:42 -070028
John Reck67b1e2b2020-08-26 13:17:24 -070029#include <mutex>
30#include <unordered_map>
31
Alec Mouric7f6c8b2020-11-09 18:35:20 -080032#include "AutoBackendTexture.h"
Alec Mourib5777452020-09-28 11:32:42 -070033#include "EGL/egl.h"
Leon Scroggins III9f3072c2021-03-22 10:42:47 -040034#include "GrContextOptions.h"
Alec Mouric7f6c8b2020-11-09 18:35:20 -080035#include "SkImageInfo.h"
John Reck67b1e2b2020-08-26 13:17:24 -070036#include "SkiaRenderEngine.h"
Alec Mouric7f6c8b2020-11-09 18:35:20 -080037#include "android-base/macros.h"
Alec Mouric0aae732021-01-12 13:32:18 -080038#include "debug/SkiaCapture.h"
Lucas Dupinf4cb4a02020-09-22 14:19:26 -070039#include "filters/BlurFilter.h"
Alec Mouric0aae732021-01-12 13:32:18 -080040#include "filters/LinearEffect.h"
John Reck67b1e2b2020-08-26 13:17:24 -070041
42namespace android {
43namespace renderengine {
44namespace skia {
45
46class SkiaGLRenderEngine : public skia::SkiaRenderEngine {
47public:
48 static std::unique_ptr<SkiaGLRenderEngine> create(const RenderEngineCreationArgs& args);
Lucas Dupind508e472020-11-04 04:32:06 +000049 SkiaGLRenderEngine(const RenderEngineCreationArgs& args, EGLDisplay display, EGLContext ctxt,
50 EGLSurface placeholder, EGLContext protectedContext,
John Reck67b1e2b2020-08-26 13:17:24 -070051 EGLSurface protectedPlaceholder);
Alec Mouric0aae732021-01-12 13:32:18 -080052 ~SkiaGLRenderEngine() override EXCLUDES(mRenderingMutex);
John Reck67b1e2b2020-08-26 13:17:24 -070053
Leon Scroggins IIIb9216dc2021-03-08 17:19:01 -050054 void primeCache() override;
Alec Mouri617752f2021-04-15 16:27:01 +000055 void cacheExternalTextureBuffer(const sp<GraphicBuffer>& buffer) override;
56 void unbindExternalTextureBuffer(uint64_t bufferId) override;
John Reck67b1e2b2020-08-26 13:17:24 -070057 status_t drawLayers(const DisplaySettings& display,
58 const std::vector<const LayerSettings*>& layers,
Alec Mouri617752f2021-04-15 16:27:01 +000059 const sp<GraphicBuffer>& buffer, const bool useFramebufferCache,
60 base::unique_fd&& bufferFence, base::unique_fd* drawFence) override;
61 void cleanFramebufferCache() override;
Alec Mourid6f09462020-12-07 11:18:17 -080062 int getContextPriority() override;
Lucas Dupind508e472020-11-04 04:32:06 +000063 bool isProtected() const override { return mInProtectedContext; }
64 bool supportsProtectedContent() const override;
65 bool useProtectedContext(bool useProtectedContext) override;
Derek Sollenbergerb3998372021-02-16 15:16:56 -050066 bool supportsBackgroundBlur() override { return mBlurFilter != nullptr; }
Leon Scroggins III9f3072c2021-03-22 10:42:47 -040067 void assertShadersCompiled(int numShaders) override;
Derek Sollenbergerc4a05e12021-03-24 16:45:20 -040068 void onPrimaryDisplaySizeChanged(ui::Size size) override;
Nathaniel Nifongb9f27ef2021-04-01 16:44:12 -040069 int reportShadersCompiled() override;
John Reck67b1e2b2020-08-26 13:17:24 -070070
71protected:
Ana Krulec1d12b3b2021-01-27 16:49:51 -080072 void dump(std::string& result) override;
John Reck67b1e2b2020-08-26 13:17:24 -070073 size_t getMaxTextureSize() const override;
74 size_t getMaxViewportDims() const override;
75
76private:
77 static EGLConfig chooseEglConfig(EGLDisplay display, int format, bool logConfig);
78 static EGLContext createEglContext(EGLDisplay display, EGLConfig config,
Alec Mourid6f09462020-12-07 11:18:17 -080079 EGLContext shareContext,
80 std::optional<ContextPriority> contextPriority,
John Reck67b1e2b2020-08-26 13:17:24 -070081 Protection protection);
Alec Mourid6f09462020-12-07 11:18:17 -080082 static std::optional<RenderEngine::ContextPriority> createContextPriority(
83 const RenderEngineCreationArgs& args);
John Reck67b1e2b2020-08-26 13:17:24 -070084 static EGLSurface createPlaceholderEglPbufferSurface(EGLDisplay display, EGLConfig config,
85 int hwcFormat, Protection protection);
Lucas Dupin3f11e922020-09-22 17:31:04 -070086 inline SkRect getSkRect(const FloatRect& layer);
87 inline SkRect getSkRect(const Rect& layer);
Lucas Dupin21f348e2020-09-16 17:31:26 -070088 inline SkRRect getRoundedRect(const LayerSettings* layer);
Galia Peycheva80116e52020-11-06 11:57:25 +010089 inline BlurRegion getBlurRegion(const LayerSettings* layer);
Derek Sollenbergerecb21462021-01-29 16:53:49 -050090 inline bool layerHasBlur(const LayerSettings* layer);
Lucas Dupin3f11e922020-09-22 17:31:04 -070091 inline SkColor getSkColor(const vec4& color);
Lucas Dupinbb1a1d42020-09-18 15:17:02 -070092 inline SkM44 getSkM44(const mat4& matrix);
Lucas Dupin3f11e922020-09-22 17:31:04 -070093 inline SkPoint3 getSkPoint3(const vec3& vector);
John Reck67b1e2b2020-08-26 13:17:24 -070094
95 base::unique_fd flush();
96 bool waitFence(base::unique_fd fenceFd);
Derek Sollenberger3f77aa42021-02-04 11:06:34 -050097 void initCanvas(SkCanvas* canvas, const DisplaySettings& display);
Lucas Dupin3f11e922020-09-22 17:31:04 -070098 void drawShadow(SkCanvas* canvas, const SkRect& casterRect, float casterCornerRadius,
99 const ShadowSettings& shadowSettings);
Derek Sollenbergere2fe78c2021-02-23 13:22:54 -0500100 // If requiresLinearEffect is true or the layer has a stretchEffect a new shader is returned.
101 // Otherwise it returns the input shader.
Ana Krulec47814212021-01-06 19:00:10 -0800102 sk_sp<SkShader> createRuntimeEffectShader(sk_sp<SkShader> shader, const LayerSettings* layer,
103 const DisplaySettings& display,
Derek Sollenbergere2fe78c2021-02-23 13:22:54 -0500104 bool undoPremultipliedAlpha,
105 bool requiresLinearEffect);
John Reck67b1e2b2020-08-26 13:17:24 -0700106
107 EGLDisplay mEGLDisplay;
John Reck67b1e2b2020-08-26 13:17:24 -0700108 EGLContext mEGLContext;
109 EGLSurface mPlaceholderSurface;
110 EGLContext mProtectedEGLContext;
111 EGLSurface mProtectedPlaceholderSurface;
Lucas Dupinf4cb4a02020-09-22 14:19:26 -0700112 BlurFilter* mBlurFilter = nullptr;
John Reck67b1e2b2020-08-26 13:17:24 -0700113
Derek Sollenbergerc4a05e12021-03-24 16:45:20 -0400114 const PixelFormat mDefaultPixelFormat;
Alec Mourib5777452020-09-28 11:32:42 -0700115 const bool mUseColorManagement;
116
Alec Mouri617752f2021-04-15 16:27:01 +0000117 // Cache of GL textures that we'll store per GraphicBuffer ID
Alec Mouric7f6c8b2020-11-09 18:35:20 -0800118 std::unordered_map<uint64_t, std::shared_ptr<AutoBackendTexture::LocalRef>> mTextureCache
119 GUARDED_BY(mRenderingMutex);
120 std::unordered_map<uint64_t, std::shared_ptr<AutoBackendTexture::LocalRef>>
121 mProtectedTextureCache GUARDED_BY(mRenderingMutex);
Alec Mouric16a77d2020-11-13 13:20:11 -0800122 std::unordered_map<LinearEffect, sk_sp<SkRuntimeEffect>, LinearEffectHasher> mRuntimeEffects;
John Reck67b1e2b2020-08-26 13:17:24 -0700123 // Mutex guarding rendering operations, so that:
124 // 1. GL operations aren't interleaved, and
125 // 2. Internal state related to rendering that is potentially modified by
126 // multiple threads is guaranteed thread-safe.
127 std::mutex mRenderingMutex;
128
129 sp<Fence> mLastDrawFence;
130
Lucas Dupind508e472020-11-04 04:32:06 +0000131 // Graphics context used for creating surfaces and submitting commands
John Reck67b1e2b2020-08-26 13:17:24 -0700132 sk_sp<GrDirectContext> mGrContext;
Lucas Dupind508e472020-11-04 04:32:06 +0000133 // Same as above, but for protected content (eg. DRM)
134 sk_sp<GrDirectContext> mProtectedGrContext;
John Reck67b1e2b2020-08-26 13:17:24 -0700135
Lucas Dupind508e472020-11-04 04:32:06 +0000136 bool mInProtectedContext = false;
Ana Krulec70d15b1b2020-12-01 10:05:15 -0800137 // Object to capture commands send to Skia.
Alec Mouric0aae732021-01-12 13:32:18 -0800138 std::unique_ptr<SkiaCapture> mCapture;
Leon Scroggins III9f3072c2021-03-22 10:42:47 -0400139
140 // Implements PersistentCache as a way to monitor what SkSL shaders Skia has
141 // cached.
142 class SkSLCacheMonitor : public GrContextOptions::PersistentCache {
143 public:
144 SkSLCacheMonitor() = default;
145 ~SkSLCacheMonitor() override = default;
146
147 sk_sp<SkData> load(const SkData& key) override;
148
149 void store(const SkData& key, const SkData& data, const SkString& description) override;
150
151 int shadersCachedSinceLastCall() {
152 const int shadersCachedSinceLastCall = mShadersCachedSinceLastCall;
153 mShadersCachedSinceLastCall = 0;
154 return shadersCachedSinceLastCall;
155 }
156
157 private:
158 int mShadersCachedSinceLastCall = 0;
159 };
160
161 SkSLCacheMonitor mSkSLCacheMonitor;
John Reck67b1e2b2020-08-26 13:17:24 -0700162};
163
164} // namespace skia
165} // namespace renderengine
166} // namespace android
167
Galia Peycheva6c460652020-11-03 19:42:42 +0100168#endif /* SF_GLESRENDERENGINE_H_ */