blob: bb8625e7336b87375f784adc8332e2f957360480 [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
44namespace android {
45namespace renderengine {
46namespace skia {
47
48class SkiaGLRenderEngine : public skia::SkiaRenderEngine {
49public:
50 static std::unique_ptr<SkiaGLRenderEngine> create(const RenderEngineCreationArgs& args);
Lucas Dupind508e472020-11-04 04:32:06 +000051 SkiaGLRenderEngine(const RenderEngineCreationArgs& args, EGLDisplay display, EGLContext ctxt,
52 EGLSurface placeholder, EGLContext protectedContext,
John Reck67b1e2b2020-08-26 13:17:24 -070053 EGLSurface protectedPlaceholder);
Alec Mouric0aae732021-01-12 13:32:18 -080054 ~SkiaGLRenderEngine() override EXCLUDES(mRenderingMutex);
John Reck67b1e2b2020-08-26 13:17:24 -070055
Leon Scroggins IIIb9216dc2021-03-08 17:19:01 -050056 void primeCache() override;
John Reck67b1e2b2020-08-26 13:17:24 -070057 status_t drawLayers(const DisplaySettings& display,
58 const std::vector<const LayerSettings*>& layers,
Alec Mouria90a5702021-04-16 16:36:21 +000059 const std::shared_ptr<ExternalTexture>& buffer,
60 const bool useFramebufferCache, base::unique_fd&& bufferFence,
61 base::unique_fd* drawFence) override;
62 void cleanFramebufferCache() override {}
Alec Mourid6f09462020-12-07 11:18:17 -080063 int getContextPriority() override;
Lucas Dupind508e472020-11-04 04:32:06 +000064 bool isProtected() const override { return mInProtectedContext; }
65 bool supportsProtectedContent() const override;
66 bool useProtectedContext(bool useProtectedContext) override;
Derek Sollenbergerb3998372021-02-16 15:16:56 -050067 bool supportsBackgroundBlur() override { return mBlurFilter != nullptr; }
Leon Scroggins III9f3072c2021-03-22 10:42:47 -040068 void assertShadersCompiled(int numShaders) override;
Derek Sollenbergerc4a05e12021-03-24 16:45:20 -040069 void onPrimaryDisplaySizeChanged(ui::Size size) override;
Nathaniel Nifongb9f27ef2021-04-01 16:44:12 -040070 int reportShadersCompiled() override;
John Reck67b1e2b2020-08-26 13:17:24 -070071
72protected:
Ana Krulec1d12b3b2021-01-27 16:49:51 -080073 void dump(std::string& result) override;
John Reck67b1e2b2020-08-26 13:17:24 -070074 size_t getMaxTextureSize() const override;
75 size_t getMaxViewportDims() const override;
Alec Mouria90a5702021-04-16 16:36:21 +000076 void mapExternalTextureBuffer(const sp<GraphicBuffer>& buffer, bool isRenderable) override;
77 void unmapExternalTextureBuffer(const sp<GraphicBuffer>& buffer) override;
John Reck67b1e2b2020-08-26 13:17:24 -070078
79private:
80 static EGLConfig chooseEglConfig(EGLDisplay display, int format, bool logConfig);
81 static EGLContext createEglContext(EGLDisplay display, EGLConfig config,
Alec Mourid6f09462020-12-07 11:18:17 -080082 EGLContext shareContext,
83 std::optional<ContextPriority> contextPriority,
John Reck67b1e2b2020-08-26 13:17:24 -070084 Protection protection);
Alec Mourid6f09462020-12-07 11:18:17 -080085 static std::optional<RenderEngine::ContextPriority> createContextPriority(
86 const RenderEngineCreationArgs& args);
John Reck67b1e2b2020-08-26 13:17:24 -070087 static EGLSurface createPlaceholderEglPbufferSurface(EGLDisplay display, EGLConfig config,
88 int hwcFormat, Protection protection);
Lucas Dupin3f11e922020-09-22 17:31:04 -070089 inline SkRect getSkRect(const FloatRect& layer);
90 inline SkRect getSkRect(const Rect& layer);
Derek Sollenberger8e8b3bf2021-04-29 15:35:28 -040091 inline std::pair<SkRRect, SkRRect> getBoundsAndClip(const LayerSettings* layer);
Derek Sollenbergerecb21462021-01-29 16:53:49 -050092 inline bool layerHasBlur(const LayerSettings* layer);
Lucas Dupin3f11e922020-09-22 17:31:04 -070093 inline SkColor getSkColor(const vec4& color);
Lucas Dupinbb1a1d42020-09-18 15:17:02 -070094 inline SkM44 getSkM44(const mat4& matrix);
Lucas Dupin3f11e922020-09-22 17:31:04 -070095 inline SkPoint3 getSkPoint3(const vec3& vector);
Derek Sollenbergerb24258c2021-05-04 13:47:34 -040096 inline GrDirectContext* getActiveGrContext() const;
John Reck67b1e2b2020-08-26 13:17:24 -070097
98 base::unique_fd flush();
99 bool waitFence(base::unique_fd fenceFd);
Derek Sollenberger3f77aa42021-02-04 11:06:34 -0500100 void initCanvas(SkCanvas* canvas, const DisplaySettings& display);
Lucas Dupin3f11e922020-09-22 17:31:04 -0700101 void drawShadow(SkCanvas* canvas, const SkRect& casterRect, float casterCornerRadius,
102 const ShadowSettings& shadowSettings);
Derek Sollenbergere2fe78c2021-02-23 13:22:54 -0500103 // If requiresLinearEffect is true or the layer has a stretchEffect a new shader is returned.
104 // Otherwise it returns the input shader.
Nader Jawad2dfc98b2021-04-08 20:35:39 -0700105 sk_sp<SkShader> createRuntimeEffectShader(sk_sp<SkShader> shader,
106 const LayerSettings* layer,
Ana Krulec47814212021-01-06 19:00:10 -0800107 const DisplaySettings& display,
Derek Sollenbergere2fe78c2021-02-23 13:22:54 -0500108 bool undoPremultipliedAlpha,
109 bool requiresLinearEffect);
John Reck67b1e2b2020-08-26 13:17:24 -0700110
111 EGLDisplay mEGLDisplay;
John Reck67b1e2b2020-08-26 13:17:24 -0700112 EGLContext mEGLContext;
113 EGLSurface mPlaceholderSurface;
114 EGLContext mProtectedEGLContext;
115 EGLSurface mProtectedPlaceholderSurface;
Lucas Dupinf4cb4a02020-09-22 14:19:26 -0700116 BlurFilter* mBlurFilter = nullptr;
John Reck67b1e2b2020-08-26 13:17:24 -0700117
Derek Sollenbergerc4a05e12021-03-24 16:45:20 -0400118 const PixelFormat mDefaultPixelFormat;
Alec Mourib5777452020-09-28 11:32:42 -0700119 const bool mUseColorManagement;
120
Nader Jawad2dfc98b2021-04-08 20:35:39 -0700121 // Identifier used or various mappings of layers to various
122 // textures or shaders
Nader Jawadc088bdc2021-05-10 13:24:46 -0700123 using GraphicBufferId = uint64_t;
Nader Jawad2dfc98b2021-04-08 20:35:39 -0700124
Alec Mouria90a5702021-04-16 16:36:21 +0000125 // Number of external holders of ExternalTexture references, per GraphicBuffer ID.
Nader Jawadc088bdc2021-05-10 13:24:46 -0700126 std::unordered_map<GraphicBufferId, int32_t> mGraphicBufferExternalRefs
Alec Mouric7f6c8b2020-11-09 18:35:20 -0800127 GUARDED_BY(mRenderingMutex);
Nader Jawadc088bdc2021-05-10 13:24:46 -0700128 // Cache of GL textures that we'll store per GraphicBuffer ID, sliced by GPU context.
129 std::unordered_map<GraphicBufferId, std::shared_ptr<AutoBackendTexture::LocalRef>> mTextureCache
130 GUARDED_BY(mRenderingMutex);
131 std::unordered_map<GraphicBufferId, std::shared_ptr<AutoBackendTexture::LocalRef>>
Alec Mouric7f6c8b2020-11-09 18:35:20 -0800132 mProtectedTextureCache GUARDED_BY(mRenderingMutex);
Alec Mouric16a77d2020-11-13 13:20:11 -0800133 std::unordered_map<LinearEffect, sk_sp<SkRuntimeEffect>, LinearEffectHasher> mRuntimeEffects;
Nader Jawad2dfc98b2021-04-08 20:35:39 -0700134
135 StretchShaderFactory mStretchShaderFactory;
John Reck67b1e2b2020-08-26 13:17:24 -0700136 // Mutex guarding rendering operations, so that:
137 // 1. GL operations aren't interleaved, and
138 // 2. Internal state related to rendering that is potentially modified by
139 // multiple threads is guaranteed thread-safe.
140 std::mutex mRenderingMutex;
141
142 sp<Fence> mLastDrawFence;
143
Lucas Dupind508e472020-11-04 04:32:06 +0000144 // Graphics context used for creating surfaces and submitting commands
John Reck67b1e2b2020-08-26 13:17:24 -0700145 sk_sp<GrDirectContext> mGrContext;
Lucas Dupind508e472020-11-04 04:32:06 +0000146 // Same as above, but for protected content (eg. DRM)
147 sk_sp<GrDirectContext> mProtectedGrContext;
John Reck67b1e2b2020-08-26 13:17:24 -0700148
Lucas Dupind508e472020-11-04 04:32:06 +0000149 bool mInProtectedContext = false;
Ana Krulec70d15b1b2020-12-01 10:05:15 -0800150 // Object to capture commands send to Skia.
Alec Mouric0aae732021-01-12 13:32:18 -0800151 std::unique_ptr<SkiaCapture> mCapture;
Leon Scroggins III9f3072c2021-03-22 10:42:47 -0400152
153 // Implements PersistentCache as a way to monitor what SkSL shaders Skia has
154 // cached.
155 class SkSLCacheMonitor : public GrContextOptions::PersistentCache {
156 public:
157 SkSLCacheMonitor() = default;
158 ~SkSLCacheMonitor() override = default;
159
160 sk_sp<SkData> load(const SkData& key) override;
161
162 void store(const SkData& key, const SkData& data, const SkString& description) override;
163
164 int shadersCachedSinceLastCall() {
165 const int shadersCachedSinceLastCall = mShadersCachedSinceLastCall;
166 mShadersCachedSinceLastCall = 0;
167 return shadersCachedSinceLastCall;
168 }
169
170 private:
171 int mShadersCachedSinceLastCall = 0;
172 };
173
174 SkSLCacheMonitor mSkSLCacheMonitor;
John Reck67b1e2b2020-08-26 13:17:24 -0700175};
176
177} // namespace skia
178} // namespace renderengine
179} // namespace android
180
Galia Peycheva6c460652020-11-03 19:42:42 +0100181#endif /* SF_GLESRENDERENGINE_H_ */