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 void cleanupPostRender() override; cleanFramebufferCache()58 void cleanFramebufferCache() override{}; 59 int getContextPriority() override; isProtected()60 bool isProtected() const override { return mInProtectedContext; } 61 bool supportsProtectedContent() const override; 62 void useProtectedContext(bool useProtectedContext) override; supportsBackgroundBlur()63 bool supportsBackgroundBlur() override { return mBlurFilter != nullptr; } 64 void onActiveDisplaySizeChanged(ui::Size size) override; 65 int reportShadersCompiled() override; 66 67 protected: 68 void dump(std::string& result) override; 69 size_t getMaxTextureSize() const override; 70 size_t getMaxViewportDims() const override; 71 void mapExternalTextureBuffer(const sp<GraphicBuffer>& buffer, bool isRenderable) override; 72 void unmapExternalTextureBuffer(const sp<GraphicBuffer>& buffer) override; 73 bool canSkipPostRenderCleanup() const override; 74 void drawLayersInternal(const std::shared_ptr<std::promise<RenderEngineResult>>&& resultPromise, 75 const DisplaySettings& display, 76 const std::vector<LayerSettings>& layers, 77 const std::shared_ptr<ExternalTexture>& buffer, 78 const bool useFramebufferCache, base::unique_fd&& bufferFence) override; 79 80 private: 81 static EGLConfig chooseEglConfig(EGLDisplay display, int format, bool logConfig); 82 static EGLContext createEglContext(EGLDisplay display, EGLConfig config, 83 EGLContext shareContext, 84 std::optional<ContextPriority> contextPriority, 85 Protection protection); 86 static std::optional<RenderEngine::ContextPriority> createContextPriority( 87 const RenderEngineCreationArgs& args); 88 static EGLSurface createPlaceholderEglPbufferSurface(EGLDisplay display, EGLConfig config, 89 int hwcFormat, Protection protection); 90 inline SkRect getSkRect(const FloatRect& layer); 91 inline SkRect getSkRect(const Rect& layer); 92 inline std::pair<SkRRect, SkRRect> getBoundsAndClip(const FloatRect& bounds, 93 const FloatRect& crop, 94 const vec2& 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 // waitFence attempts to wait in the GPU, and if unable to waits on the CPU instead. 103 void waitFence(base::borrowed_fd fenceFd); 104 bool waitGpuFence(base::borrowed_fd fenceFd); 105 106 void initCanvas(SkCanvas* canvas, const DisplaySettings& display); 107 void drawShadow(SkCanvas* canvas, const SkRRect& casterRRect, 108 const ShadowSettings& shadowSettings); 109 110 // If requiresLinearEffect is true or the layer has a stretchEffect a new shader is returned. 111 // Otherwise it returns the input shader. 112 struct RuntimeEffectShaderParameters { 113 sk_sp<SkShader> shader; 114 const LayerSettings& layer; 115 const DisplaySettings& display; 116 bool undoPremultipliedAlpha; 117 bool requiresLinearEffect; 118 float layerDimmingRatio; 119 }; 120 sk_sp<SkShader> createRuntimeEffectShader(const RuntimeEffectShaderParameters&); 121 122 EGLDisplay mEGLDisplay; 123 EGLContext mEGLContext; 124 EGLSurface mPlaceholderSurface; 125 EGLContext mProtectedEGLContext; 126 EGLSurface mProtectedPlaceholderSurface; 127 BlurFilter* mBlurFilter = nullptr; 128 129 const PixelFormat mDefaultPixelFormat; 130 const bool mUseColorManagement; 131 132 // Identifier used or various mappings of layers to various 133 // textures or shaders 134 using GraphicBufferId = uint64_t; 135 136 // Number of external holders of ExternalTexture references, per GraphicBuffer ID. 137 std::unordered_map<GraphicBufferId, int32_t> mGraphicBufferExternalRefs 138 GUARDED_BY(mRenderingMutex); 139 // Cache of GL textures that we'll store per GraphicBuffer ID, shared between GPU contexts. 140 std::unordered_map<GraphicBufferId, std::shared_ptr<AutoBackendTexture::LocalRef>> mTextureCache 141 GUARDED_BY(mRenderingMutex); 142 std::unordered_map<shaders::LinearEffect, sk_sp<SkRuntimeEffect>, shaders::LinearEffectHasher> 143 mRuntimeEffects; 144 AutoBackendTexture::CleanupManager mTextureCleanupMgr GUARDED_BY(mRenderingMutex); 145 146 StretchShaderFactory mStretchShaderFactory; 147 // Mutex guarding rendering operations, so that: 148 // 1. GL operations aren't interleaved, and 149 // 2. Internal state related to rendering that is potentially modified by 150 // multiple threads is guaranteed thread-safe. 151 mutable std::mutex mRenderingMutex; 152 153 sp<Fence> mLastDrawFence; 154 155 // Graphics context used for creating surfaces and submitting commands 156 sk_sp<GrDirectContext> mGrContext; 157 // Same as above, but for protected content (eg. DRM) 158 sk_sp<GrDirectContext> mProtectedGrContext; 159 160 bool mInProtectedContext = false; 161 // Object to capture commands send to Skia. 162 std::unique_ptr<SkiaCapture> mCapture; 163 164 // Implements PersistentCache as a way to monitor what SkSL shaders Skia has 165 // cached. 166 class SkSLCacheMonitor : public GrContextOptions::PersistentCache { 167 public: 168 SkSLCacheMonitor() = default; 169 ~SkSLCacheMonitor() override = default; 170 171 sk_sp<SkData> load(const SkData& key) override; 172 173 void store(const SkData& key, const SkData& data, const SkString& description) override; 174 shadersCachedSinceLastCall()175 int shadersCachedSinceLastCall() { 176 const int shadersCachedSinceLastCall = mShadersCachedSinceLastCall; 177 mShadersCachedSinceLastCall = 0; 178 return shadersCachedSinceLastCall; 179 } 180 totalShadersCompiled()181 int totalShadersCompiled() const { return mTotalShadersCompiled; } 182 183 private: 184 int mShadersCachedSinceLastCall = 0; 185 int mTotalShadersCompiled = 0; 186 }; 187 188 SkSLCacheMonitor mSkSLCacheMonitor; 189 }; 190 191 } // namespace skia 192 } // namespace renderengine 193 } // namespace android 194 195 #endif /* SF_GLESRENDERENGINE_H_ */ 196