• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 
20 #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/ExternalTexture.h>
27 #include <renderengine/RenderEngine.h>
28 #include <sys/types.h>
29 
30 #include <mutex>
31 #include <unordered_map>
32 
33 #include "AutoBackendTexture.h"
34 #include "EGL/egl.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 
44 namespace android {
45 namespace renderengine {
46 namespace skia {
47 
48 class SkiaGLRenderEngine : public skia::SkiaRenderEngine {
49 public:
50     static std::unique_ptr<SkiaGLRenderEngine> create(const RenderEngineCreationArgs& args);
51     SkiaGLRenderEngine(const RenderEngineCreationArgs& args, EGLDisplay display, EGLContext ctxt,
52                        EGLSurface placeholder, EGLContext protectedContext,
53                        EGLSurface protectedPlaceholder);
54     ~SkiaGLRenderEngine() override EXCLUDES(mRenderingMutex);
55 
56     std::future<void> primeCache() override;
57     status_t drawLayers(const DisplaySettings& display,
58                         const std::vector<const LayerSettings*>& layers,
59                         const std::shared_ptr<ExternalTexture>& buffer,
60                         const bool useFramebufferCache, base::unique_fd&& bufferFence,
61                         base::unique_fd* drawFence) override;
62     void cleanupPostRender() override;
cleanFramebufferCache()63     void cleanFramebufferCache() override{};
64     int getContextPriority() override;
isProtected()65     bool isProtected() const override { return mInProtectedContext; }
66     bool supportsProtectedContent() const override;
67     void useProtectedContext(bool useProtectedContext) override;
supportsBackgroundBlur()68     bool supportsBackgroundBlur() override { return mBlurFilter != nullptr; }
69     void assertShadersCompiled(int numShaders) override;
70     void onPrimaryDisplaySizeChanged(ui::Size size) override;
71     int reportShadersCompiled() override;
72 
73 protected:
74     void dump(std::string& result) override;
75     size_t getMaxTextureSize() const override;
76     size_t getMaxViewportDims() const override;
77     void mapExternalTextureBuffer(const sp<GraphicBuffer>& buffer, bool isRenderable) override;
78     void unmapExternalTextureBuffer(const sp<GraphicBuffer>& buffer) override;
79     bool canSkipPostRenderCleanup() const override;
80 
81 private:
82     static EGLConfig chooseEglConfig(EGLDisplay display, int format, bool logConfig);
83     static EGLContext createEglContext(EGLDisplay display, EGLConfig config,
84                                        EGLContext shareContext,
85                                        std::optional<ContextPriority> contextPriority,
86                                        Protection protection);
87     static std::optional<RenderEngine::ContextPriority> createContextPriority(
88             const RenderEngineCreationArgs& args);
89     static EGLSurface createPlaceholderEglPbufferSurface(EGLDisplay display, EGLConfig config,
90                                                          int hwcFormat, Protection protection);
91     inline SkRect getSkRect(const FloatRect& layer);
92     inline SkRect getSkRect(const Rect& layer);
93     inline std::pair<SkRRect, SkRRect> getBoundsAndClip(const FloatRect& bounds,
94                                                         const FloatRect& crop, float cornerRadius);
95     inline bool layerHasBlur(const LayerSettings* layer, bool colorTransformModifiesAlpha);
96     inline SkColor getSkColor(const vec4& color);
97     inline SkM44 getSkM44(const mat4& matrix);
98     inline SkPoint3 getSkPoint3(const vec3& vector);
99     inline GrDirectContext* getActiveGrContext() const;
100 
101     base::unique_fd flush();
102     bool waitFence(base::unique_fd fenceFd);
103     void initCanvas(SkCanvas* canvas, const DisplaySettings& display);
104     void drawShadow(SkCanvas* canvas, const SkRRect& casterRRect,
105                     const ShadowSettings& shadowSettings);
106     // If requiresLinearEffect is true or the layer has a stretchEffect a new shader is returned.
107     // Otherwise it returns the input shader.
108     sk_sp<SkShader> createRuntimeEffectShader(sk_sp<SkShader> shader,
109                                               const LayerSettings* layer,
110                                               const DisplaySettings& display,
111                                               bool undoPremultipliedAlpha,
112                                               bool requiresLinearEffect);
113 
114     EGLDisplay mEGLDisplay;
115     EGLContext mEGLContext;
116     EGLSurface mPlaceholderSurface;
117     EGLContext mProtectedEGLContext;
118     EGLSurface mProtectedPlaceholderSurface;
119     BlurFilter* mBlurFilter = nullptr;
120 
121     const PixelFormat mDefaultPixelFormat;
122     const bool mUseColorManagement;
123 
124     // Identifier used or various mappings of layers to various
125     // textures or shaders
126     using GraphicBufferId = uint64_t;
127 
128     // Number of external holders of ExternalTexture references, per GraphicBuffer ID.
129     std::unordered_map<GraphicBufferId, int32_t> mGraphicBufferExternalRefs
130             GUARDED_BY(mRenderingMutex);
131     // Cache of GL textures that we'll store per GraphicBuffer ID, shared between GPU contexts.
132     std::unordered_map<GraphicBufferId, std::shared_ptr<AutoBackendTexture::LocalRef>> mTextureCache
133             GUARDED_BY(mRenderingMutex);
134     std::unordered_map<LinearEffect, sk_sp<SkRuntimeEffect>, LinearEffectHasher> mRuntimeEffects;
135     AutoBackendTexture::CleanupManager mTextureCleanupMgr GUARDED_BY(mRenderingMutex);
136 
137     StretchShaderFactory mStretchShaderFactory;
138     // Mutex guarding rendering operations, so that:
139     // 1. GL operations aren't interleaved, and
140     // 2. Internal state related to rendering that is potentially modified by
141     // multiple threads is guaranteed thread-safe.
142     mutable std::mutex mRenderingMutex;
143 
144     sp<Fence> mLastDrawFence;
145 
146     // Graphics context used for creating surfaces and submitting commands
147     sk_sp<GrDirectContext> mGrContext;
148     // Same as above, but for protected content (eg. DRM)
149     sk_sp<GrDirectContext> mProtectedGrContext;
150 
151     bool mInProtectedContext = false;
152     // Object to capture commands send to Skia.
153     std::unique_ptr<SkiaCapture> mCapture;
154 
155     // Implements PersistentCache as a way to monitor what SkSL shaders Skia has
156     // cached.
157     class SkSLCacheMonitor : public GrContextOptions::PersistentCache {
158     public:
159         SkSLCacheMonitor() = default;
160         ~SkSLCacheMonitor() override = default;
161 
162         sk_sp<SkData> load(const SkData& key) override;
163 
164         void store(const SkData& key, const SkData& data, const SkString& description) override;
165 
shadersCachedSinceLastCall()166         int shadersCachedSinceLastCall() {
167             const int shadersCachedSinceLastCall = mShadersCachedSinceLastCall;
168             mShadersCachedSinceLastCall = 0;
169             return shadersCachedSinceLastCall;
170         }
171 
172     private:
173         int mShadersCachedSinceLastCall = 0;
174     };
175 
176     SkSLCacheMonitor mSkSLCacheMonitor;
177 };
178 
179 } // namespace skia
180 } // namespace renderengine
181 } // namespace android
182 
183 #endif /* SF_GLESRENDERENGINE_H_ */
184