| John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 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 |  | 
| Alec Mouri | b577745 | 2020-09-28 11:32:42 -0700 | [diff] [blame] | 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/RenderEngine.h> | 
| John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 27 | #include <sys/types.h> | 
| Alec Mouri | b577745 | 2020-09-28 11:32:42 -0700 | [diff] [blame] | 28 |  | 
| John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 29 | #include <mutex> | 
|  | 30 | #include <unordered_map> | 
|  | 31 |  | 
| Alec Mouri | c7f6c8b | 2020-11-09 18:35:20 -0800 | [diff] [blame] | 32 | #include "AutoBackendTexture.h" | 
| Alec Mouri | b577745 | 2020-09-28 11:32:42 -0700 | [diff] [blame] | 33 | #include "EGL/egl.h" | 
| Leon Scroggins III | 9f3072c | 2021-03-22 10:42:47 -0400 | [diff] [blame] | 34 | #include "GrContextOptions.h" | 
| Alec Mouri | c7f6c8b | 2020-11-09 18:35:20 -0800 | [diff] [blame] | 35 | #include "SkImageInfo.h" | 
| John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 36 | #include "SkiaRenderEngine.h" | 
| Alec Mouri | c7f6c8b | 2020-11-09 18:35:20 -0800 | [diff] [blame] | 37 | #include "android-base/macros.h" | 
| Alec Mouri | c0aae73 | 2021-01-12 13:32:18 -0800 | [diff] [blame] | 38 | #include "debug/SkiaCapture.h" | 
| Lucas Dupin | f4cb4a0 | 2020-09-22 14:19:26 -0700 | [diff] [blame] | 39 | #include "filters/BlurFilter.h" | 
| Alec Mouri | c0aae73 | 2021-01-12 13:32:18 -0800 | [diff] [blame] | 40 | #include "filters/LinearEffect.h" | 
| John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 41 |  | 
|  | 42 | namespace android { | 
|  | 43 | namespace renderengine { | 
|  | 44 | namespace skia { | 
|  | 45 |  | 
|  | 46 | class SkiaGLRenderEngine : public skia::SkiaRenderEngine { | 
|  | 47 | public: | 
|  | 48 | static std::unique_ptr<SkiaGLRenderEngine> create(const RenderEngineCreationArgs& args); | 
| Lucas Dupin | d508e47 | 2020-11-04 04:32:06 +0000 | [diff] [blame] | 49 | SkiaGLRenderEngine(const RenderEngineCreationArgs& args, EGLDisplay display, EGLContext ctxt, | 
|  | 50 | EGLSurface placeholder, EGLContext protectedContext, | 
| John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 51 | EGLSurface protectedPlaceholder); | 
| Alec Mouri | c0aae73 | 2021-01-12 13:32:18 -0800 | [diff] [blame] | 52 | ~SkiaGLRenderEngine() override EXCLUDES(mRenderingMutex); | 
| John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 53 |  | 
| Leon Scroggins III | b9216dc | 2021-03-08 17:19:01 -0500 | [diff] [blame] | 54 | void primeCache() override; | 
| Ana Krulec | dfec8f5 | 2021-01-13 12:51:47 -0800 | [diff] [blame] | 55 | void cacheExternalTextureBuffer(const sp<GraphicBuffer>& buffer) override; | 
| John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 56 | void unbindExternalTextureBuffer(uint64_t bufferId) override; | 
|  | 57 | status_t drawLayers(const DisplaySettings& display, | 
|  | 58 | const std::vector<const LayerSettings*>& layers, | 
|  | 59 | const sp<GraphicBuffer>& buffer, const bool useFramebufferCache, | 
|  | 60 | base::unique_fd&& bufferFence, base::unique_fd* drawFence) override; | 
|  | 61 | void cleanFramebufferCache() override; | 
| Alec Mouri | d6f0946 | 2020-12-07 11:18:17 -0800 | [diff] [blame] | 62 | int getContextPriority() override; | 
| Lucas Dupin | d508e47 | 2020-11-04 04:32:06 +0000 | [diff] [blame] | 63 | bool isProtected() const override { return mInProtectedContext; } | 
|  | 64 | bool supportsProtectedContent() const override; | 
|  | 65 | bool useProtectedContext(bool useProtectedContext) override; | 
| Derek Sollenberger | b399837 | 2021-02-16 15:16:56 -0500 | [diff] [blame] | 66 | bool supportsBackgroundBlur() override { return mBlurFilter != nullptr; } | 
| Leon Scroggins III | 9f3072c | 2021-03-22 10:42:47 -0400 | [diff] [blame] | 67 | void assertShadersCompiled(int numShaders) override; | 
| John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 68 |  | 
|  | 69 | protected: | 
| Ana Krulec | 1d12b3b | 2021-01-27 16:49:51 -0800 | [diff] [blame] | 70 | void dump(std::string& result) override; | 
| John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 71 | size_t getMaxTextureSize() const override; | 
|  | 72 | size_t getMaxViewportDims() const override; | 
|  | 73 |  | 
|  | 74 | private: | 
|  | 75 | static EGLConfig chooseEglConfig(EGLDisplay display, int format, bool logConfig); | 
|  | 76 | static EGLContext createEglContext(EGLDisplay display, EGLConfig config, | 
| Alec Mouri | d6f0946 | 2020-12-07 11:18:17 -0800 | [diff] [blame] | 77 | EGLContext shareContext, | 
|  | 78 | std::optional<ContextPriority> contextPriority, | 
| John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 79 | Protection protection); | 
| Alec Mouri | d6f0946 | 2020-12-07 11:18:17 -0800 | [diff] [blame] | 80 | static std::optional<RenderEngine::ContextPriority> createContextPriority( | 
|  | 81 | const RenderEngineCreationArgs& args); | 
| John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 82 | static EGLSurface createPlaceholderEglPbufferSurface(EGLDisplay display, EGLConfig config, | 
|  | 83 | int hwcFormat, Protection protection); | 
| Lucas Dupin | 3f11e92 | 2020-09-22 17:31:04 -0700 | [diff] [blame] | 84 | inline SkRect getSkRect(const FloatRect& layer); | 
|  | 85 | inline SkRect getSkRect(const Rect& layer); | 
| Lucas Dupin | 21f348e | 2020-09-16 17:31:26 -0700 | [diff] [blame] | 86 | inline SkRRect getRoundedRect(const LayerSettings* layer); | 
| Galia Peycheva | 80116e5 | 2020-11-06 11:57:25 +0100 | [diff] [blame] | 87 | inline BlurRegion getBlurRegion(const LayerSettings* layer); | 
| Derek Sollenberger | ecb2146 | 2021-01-29 16:53:49 -0500 | [diff] [blame] | 88 | inline bool layerHasBlur(const LayerSettings* layer); | 
| Lucas Dupin | 3f11e92 | 2020-09-22 17:31:04 -0700 | [diff] [blame] | 89 | inline SkColor getSkColor(const vec4& color); | 
| Lucas Dupin | bb1a1d4 | 2020-09-18 15:17:02 -0700 | [diff] [blame] | 90 | inline SkM44 getSkM44(const mat4& matrix); | 
| Lucas Dupin | 3f11e92 | 2020-09-22 17:31:04 -0700 | [diff] [blame] | 91 | inline SkPoint3 getSkPoint3(const vec3& vector); | 
| John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 92 |  | 
|  | 93 | base::unique_fd flush(); | 
|  | 94 | bool waitFence(base::unique_fd fenceFd); | 
| Derek Sollenberger | 3f77aa4 | 2021-02-04 11:06:34 -0500 | [diff] [blame] | 95 | void initCanvas(SkCanvas* canvas, const DisplaySettings& display); | 
| Lucas Dupin | 3f11e92 | 2020-09-22 17:31:04 -0700 | [diff] [blame] | 96 | void drawShadow(SkCanvas* canvas, const SkRect& casterRect, float casterCornerRadius, | 
|  | 97 | const ShadowSettings& shadowSettings); | 
| Derek Sollenberger | e2fe78c | 2021-02-23 13:22:54 -0500 | [diff] [blame] | 98 | // If requiresLinearEffect is true or the layer has a stretchEffect a new shader is returned. | 
|  | 99 | // Otherwise it returns the input shader. | 
| Ana Krulec | 4781421 | 2021-01-06 19:00:10 -0800 | [diff] [blame] | 100 | sk_sp<SkShader> createRuntimeEffectShader(sk_sp<SkShader> shader, const LayerSettings* layer, | 
|  | 101 | const DisplaySettings& display, | 
| Derek Sollenberger | e2fe78c | 2021-02-23 13:22:54 -0500 | [diff] [blame] | 102 | bool undoPremultipliedAlpha, | 
|  | 103 | bool requiresLinearEffect); | 
| John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 104 |  | 
|  | 105 | EGLDisplay mEGLDisplay; | 
| John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 106 | EGLContext mEGLContext; | 
|  | 107 | EGLSurface mPlaceholderSurface; | 
|  | 108 | EGLContext mProtectedEGLContext; | 
|  | 109 | EGLSurface mProtectedPlaceholderSurface; | 
| Lucas Dupin | f4cb4a0 | 2020-09-22 14:19:26 -0700 | [diff] [blame] | 110 | BlurFilter* mBlurFilter = nullptr; | 
| John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 111 |  | 
| Alec Mouri | b577745 | 2020-09-28 11:32:42 -0700 | [diff] [blame] | 112 | const bool mUseColorManagement; | 
|  | 113 |  | 
| Alec Mouri | c7f6c8b | 2020-11-09 18:35:20 -0800 | [diff] [blame] | 114 | // Cache of GL textures that we'll store per GraphicBuffer ID | 
|  | 115 | std::unordered_map<uint64_t, std::shared_ptr<AutoBackendTexture::LocalRef>> mTextureCache | 
|  | 116 | GUARDED_BY(mRenderingMutex); | 
|  | 117 | std::unordered_map<uint64_t, std::shared_ptr<AutoBackendTexture::LocalRef>> | 
|  | 118 | mProtectedTextureCache GUARDED_BY(mRenderingMutex); | 
| Alec Mouri | c16a77d | 2020-11-13 13:20:11 -0800 | [diff] [blame] | 119 | std::unordered_map<LinearEffect, sk_sp<SkRuntimeEffect>, LinearEffectHasher> mRuntimeEffects; | 
| John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 120 | // Mutex guarding rendering operations, so that: | 
|  | 121 | // 1. GL operations aren't interleaved, and | 
|  | 122 | // 2. Internal state related to rendering that is potentially modified by | 
|  | 123 | // multiple threads is guaranteed thread-safe. | 
|  | 124 | std::mutex mRenderingMutex; | 
|  | 125 |  | 
|  | 126 | sp<Fence> mLastDrawFence; | 
|  | 127 |  | 
| Lucas Dupin | d508e47 | 2020-11-04 04:32:06 +0000 | [diff] [blame] | 128 | // Graphics context used for creating surfaces and submitting commands | 
| John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 129 | sk_sp<GrDirectContext> mGrContext; | 
| Lucas Dupin | d508e47 | 2020-11-04 04:32:06 +0000 | [diff] [blame] | 130 | // Same as above, but for protected content (eg. DRM) | 
|  | 131 | sk_sp<GrDirectContext> mProtectedGrContext; | 
| John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 132 |  | 
| Lucas Dupin | d508e47 | 2020-11-04 04:32:06 +0000 | [diff] [blame] | 133 | bool mInProtectedContext = false; | 
| Ana Krulec | 70d15b1b | 2020-12-01 10:05:15 -0800 | [diff] [blame] | 134 | // Object to capture commands send to Skia. | 
| Alec Mouri | c0aae73 | 2021-01-12 13:32:18 -0800 | [diff] [blame] | 135 | std::unique_ptr<SkiaCapture> mCapture; | 
| Leon Scroggins III | 9f3072c | 2021-03-22 10:42:47 -0400 | [diff] [blame] | 136 |  | 
|  | 137 | // Implements PersistentCache as a way to monitor what SkSL shaders Skia has | 
|  | 138 | // cached. | 
|  | 139 | class SkSLCacheMonitor : public GrContextOptions::PersistentCache { | 
|  | 140 | public: | 
|  | 141 | SkSLCacheMonitor() = default; | 
|  | 142 | ~SkSLCacheMonitor() override = default; | 
|  | 143 |  | 
|  | 144 | sk_sp<SkData> load(const SkData& key) override; | 
|  | 145 |  | 
|  | 146 | void store(const SkData& key, const SkData& data, const SkString& description) override; | 
|  | 147 |  | 
|  | 148 | int shadersCachedSinceLastCall() { | 
|  | 149 | const int shadersCachedSinceLastCall = mShadersCachedSinceLastCall; | 
|  | 150 | mShadersCachedSinceLastCall = 0; | 
|  | 151 | return shadersCachedSinceLastCall; | 
|  | 152 | } | 
|  | 153 |  | 
|  | 154 | private: | 
|  | 155 | int mShadersCachedSinceLastCall = 0; | 
|  | 156 | }; | 
|  | 157 |  | 
|  | 158 | SkSLCacheMonitor mSkSLCacheMonitor; | 
| John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 159 | }; | 
|  | 160 |  | 
|  | 161 | } // namespace skia | 
|  | 162 | } // namespace renderengine | 
|  | 163 | } // namespace android | 
|  | 164 |  | 
| Galia Peycheva | 6c46065 | 2020-11-03 19:42:42 +0100 | [diff] [blame] | 165 | #endif /* SF_GLESRENDERENGINE_H_ */ |