| 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> | 
| Alec Mouri | a90a570 | 2021-04-16 16:36:21 +0000 | [diff] [blame] | 26 | #include <renderengine/ExternalTexture.h> | 
| Alec Mouri | b577745 | 2020-09-28 11:32:42 -0700 | [diff] [blame] | 27 | #include <renderengine/RenderEngine.h> | 
| John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 28 | #include <sys/types.h> | 
| Alec Mouri | b577745 | 2020-09-28 11:32:42 -0700 | [diff] [blame] | 29 |  | 
| John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 30 | #include <mutex> | 
|  | 31 | #include <unordered_map> | 
|  | 32 |  | 
| Alec Mouri | c7f6c8b | 2020-11-09 18:35:20 -0800 | [diff] [blame] | 33 | #include "AutoBackendTexture.h" | 
| Alec Mouri | b577745 | 2020-09-28 11:32:42 -0700 | [diff] [blame] | 34 | #include "EGL/egl.h" | 
| Leon Scroggins III | 9f3072c | 2021-03-22 10:42:47 -0400 | [diff] [blame] | 35 | #include "GrContextOptions.h" | 
| Alec Mouri | c7f6c8b | 2020-11-09 18:35:20 -0800 | [diff] [blame] | 36 | #include "SkImageInfo.h" | 
| John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 37 | #include "SkiaRenderEngine.h" | 
| Alec Mouri | c7f6c8b | 2020-11-09 18:35:20 -0800 | [diff] [blame] | 38 | #include "android-base/macros.h" | 
| Alec Mouri | c0aae73 | 2021-01-12 13:32:18 -0800 | [diff] [blame] | 39 | #include "debug/SkiaCapture.h" | 
| Lucas Dupin | f4cb4a0 | 2020-09-22 14:19:26 -0700 | [diff] [blame] | 40 | #include "filters/BlurFilter.h" | 
| Alec Mouri | c0aae73 | 2021-01-12 13:32:18 -0800 | [diff] [blame] | 41 | #include "filters/LinearEffect.h" | 
| Nader Jawad | 2dfc98b | 2021-04-08 20:35:39 -0700 | [diff] [blame] | 42 | #include "filters/StretchShaderFactory.h" | 
| John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 43 |  | 
| Kevin Lubick | 8e0ef68 | 2022-03-04 10:34:35 -0500 | [diff] [blame] | 44 | class SkData; | 
|  | 45 |  | 
| John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 46 | namespace android { | 
|  | 47 | namespace renderengine { | 
|  | 48 | namespace skia { | 
|  | 49 |  | 
|  | 50 | class SkiaGLRenderEngine : public skia::SkiaRenderEngine { | 
|  | 51 | public: | 
|  | 52 | static std::unique_ptr<SkiaGLRenderEngine> create(const RenderEngineCreationArgs& args); | 
| Lucas Dupin | d508e47 | 2020-11-04 04:32:06 +0000 | [diff] [blame] | 53 | SkiaGLRenderEngine(const RenderEngineCreationArgs& args, EGLDisplay display, EGLContext ctxt, | 
|  | 54 | EGLSurface placeholder, EGLContext protectedContext, | 
| John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 55 | EGLSurface protectedPlaceholder); | 
| Alec Mouri | c0aae73 | 2021-01-12 13:32:18 -0800 | [diff] [blame] | 56 | ~SkiaGLRenderEngine() override EXCLUDES(mRenderingMutex); | 
| John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 57 |  | 
| Ady Abraham | fe2a6db | 2021-06-09 15:41:37 -0700 | [diff] [blame] | 58 | std::future<void> primeCache() override; | 
| Derek Sollenberger | d3f6065 | 2021-06-11 15:34:36 -0400 | [diff] [blame] | 59 | void cleanupPostRender() override; | 
|  | 60 | void cleanFramebufferCache() override{}; | 
| Alec Mouri | d6f0946 | 2020-12-07 11:18:17 -0800 | [diff] [blame] | 61 | int getContextPriority() override; | 
| Lucas Dupin | d508e47 | 2020-11-04 04:32:06 +0000 | [diff] [blame] | 62 | bool isProtected() const override { return mInProtectedContext; } | 
|  | 63 | bool supportsProtectedContent() const override; | 
| Derek Sollenberger | 1ec2fb5 | 2021-06-16 15:11:27 -0400 | [diff] [blame] | 64 | void useProtectedContext(bool useProtectedContext) override; | 
| Derek Sollenberger | b399837 | 2021-02-16 15:16:56 -0500 | [diff] [blame] | 65 | bool supportsBackgroundBlur() override { return mBlurFilter != nullptr; } | 
| Leon Scroggins III | 9f3072c | 2021-03-22 10:42:47 -0400 | [diff] [blame] | 66 | void assertShadersCompiled(int numShaders) override; | 
| Ady Abraham | ed3290f | 2021-05-17 15:12:14 -0700 | [diff] [blame] | 67 | void onActiveDisplaySizeChanged(ui::Size size) override; | 
| Nathaniel Nifong | b9f27ef | 2021-04-01 16:44:12 -0400 | [diff] [blame] | 68 | int reportShadersCompiled() override; | 
| John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 69 |  | 
|  | 70 | protected: | 
| Ana Krulec | 1d12b3b | 2021-01-27 16:49:51 -0800 | [diff] [blame] | 71 | void dump(std::string& result) override; | 
| John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 72 | size_t getMaxTextureSize() const override; | 
|  | 73 | size_t getMaxViewportDims() const override; | 
| Alec Mouri | a90a570 | 2021-04-16 16:36:21 +0000 | [diff] [blame] | 74 | void mapExternalTextureBuffer(const sp<GraphicBuffer>& buffer, bool isRenderable) override; | 
|  | 75 | void unmapExternalTextureBuffer(const sp<GraphicBuffer>& buffer) override; | 
| Derek Sollenberger | d3f6065 | 2021-06-11 15:34:36 -0400 | [diff] [blame] | 76 | bool canSkipPostRenderCleanup() const override; | 
| Sally Qi | 4cabdd0 | 2021-08-05 16:45:57 -0700 | [diff] [blame] | 77 | void drawLayersInternal(const std::shared_ptr<std::promise<RenderEngineResult>>&& resultPromise, | 
|  | 78 | const DisplaySettings& display, | 
| Sally Qi | 59a9f50 | 2021-10-12 18:53:23 +0000 | [diff] [blame] | 79 | const std::vector<LayerSettings>& layers, | 
| Sally Qi | 4cabdd0 | 2021-08-05 16:45:57 -0700 | [diff] [blame] | 80 | const std::shared_ptr<ExternalTexture>& buffer, | 
|  | 81 | const bool useFramebufferCache, base::unique_fd&& bufferFence) override; | 
| John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 82 |  | 
|  | 83 | private: | 
|  | 84 | static EGLConfig chooseEglConfig(EGLDisplay display, int format, bool logConfig); | 
|  | 85 | static EGLContext createEglContext(EGLDisplay display, EGLConfig config, | 
| Alec Mouri | d6f0946 | 2020-12-07 11:18:17 -0800 | [diff] [blame] | 86 | EGLContext shareContext, | 
|  | 87 | std::optional<ContextPriority> contextPriority, | 
| John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 88 | Protection protection); | 
| Alec Mouri | d6f0946 | 2020-12-07 11:18:17 -0800 | [diff] [blame] | 89 | static std::optional<RenderEngine::ContextPriority> createContextPriority( | 
|  | 90 | const RenderEngineCreationArgs& args); | 
| John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 91 | static EGLSurface createPlaceholderEglPbufferSurface(EGLDisplay display, EGLConfig config, | 
|  | 92 | int hwcFormat, Protection protection); | 
| Lucas Dupin | 3f11e92 | 2020-09-22 17:31:04 -0700 | [diff] [blame] | 93 | inline SkRect getSkRect(const FloatRect& layer); | 
|  | 94 | inline SkRect getSkRect(const Rect& layer); | 
| Derek Sollenberger | c31985e | 2021-05-18 16:38:17 -0400 | [diff] [blame] | 95 | inline std::pair<SkRRect, SkRRect> getBoundsAndClip(const FloatRect& bounds, | 
|  | 96 | const FloatRect& crop, float cornerRadius); | 
| Sally Qi | 59a9f50 | 2021-10-12 18:53:23 +0000 | [diff] [blame] | 97 | inline bool layerHasBlur(const LayerSettings& layer, bool colorTransformModifiesAlpha); | 
| Lucas Dupin | 3f11e92 | 2020-09-22 17:31:04 -0700 | [diff] [blame] | 98 | inline SkColor getSkColor(const vec4& color); | 
| Lucas Dupin | bb1a1d4 | 2020-09-18 15:17:02 -0700 | [diff] [blame] | 99 | inline SkM44 getSkM44(const mat4& matrix); | 
| Lucas Dupin | 3f11e92 | 2020-09-22 17:31:04 -0700 | [diff] [blame] | 100 | inline SkPoint3 getSkPoint3(const vec3& vector); | 
| Derek Sollenberger | b24258c | 2021-05-04 13:47:34 -0400 | [diff] [blame] | 101 | inline GrDirectContext* getActiveGrContext() const; | 
| John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 102 |  | 
|  | 103 | base::unique_fd flush(); | 
| Alec Mouri | 4ee7b49 | 2021-08-11 10:36:55 -0700 | [diff] [blame] | 104 | // waitFence attempts to wait in the GPU, and if unable to waits on the CPU instead. | 
|  | 105 | void waitFence(base::borrowed_fd fenceFd); | 
|  | 106 | bool waitGpuFence(base::borrowed_fd fenceFd); | 
|  | 107 |  | 
| Derek Sollenberger | 3f77aa4 | 2021-02-04 11:06:34 -0500 | [diff] [blame] | 108 | void initCanvas(SkCanvas* canvas, const DisplaySettings& display); | 
| Derek Sollenberger | b0e764c | 2021-05-04 14:31:37 -0400 | [diff] [blame] | 109 | void drawShadow(SkCanvas* canvas, const SkRRect& casterRRect, | 
| Lucas Dupin | 3f11e92 | 2020-09-22 17:31:04 -0700 | [diff] [blame] | 110 | const ShadowSettings& shadowSettings); | 
| Alec Mouri | cdf6cbc | 2021-11-01 17:21:15 -0700 | [diff] [blame] | 111 |  | 
| Derek Sollenberger | e2fe78c | 2021-02-23 13:22:54 -0500 | [diff] [blame] | 112 | // If requiresLinearEffect is true or the layer has a stretchEffect a new shader is returned. | 
|  | 113 | // Otherwise it returns the input shader. | 
| Alec Mouri | cdf6cbc | 2021-11-01 17:21:15 -0700 | [diff] [blame] | 114 | struct RuntimeEffectShaderParameters { | 
|  | 115 | sk_sp<SkShader> shader; | 
|  | 116 | const LayerSettings& layer; | 
|  | 117 | const DisplaySettings& display; | 
|  | 118 | bool undoPremultipliedAlpha; | 
|  | 119 | bool requiresLinearEffect; | 
|  | 120 | float layerDimmingRatio; | 
|  | 121 | }; | 
|  | 122 | sk_sp<SkShader> createRuntimeEffectShader(const RuntimeEffectShaderParameters&); | 
| John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 123 |  | 
|  | 124 | EGLDisplay mEGLDisplay; | 
| John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 125 | EGLContext mEGLContext; | 
|  | 126 | EGLSurface mPlaceholderSurface; | 
|  | 127 | EGLContext mProtectedEGLContext; | 
|  | 128 | EGLSurface mProtectedPlaceholderSurface; | 
| Lucas Dupin | f4cb4a0 | 2020-09-22 14:19:26 -0700 | [diff] [blame] | 129 | BlurFilter* mBlurFilter = nullptr; | 
| John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 130 |  | 
| Derek Sollenberger | c4a05e1 | 2021-03-24 16:45:20 -0400 | [diff] [blame] | 131 | const PixelFormat mDefaultPixelFormat; | 
| Alec Mouri | b577745 | 2020-09-28 11:32:42 -0700 | [diff] [blame] | 132 | const bool mUseColorManagement; | 
|  | 133 |  | 
| Nader Jawad | 2dfc98b | 2021-04-08 20:35:39 -0700 | [diff] [blame] | 134 | // Identifier used or various mappings of layers to various | 
|  | 135 | // textures or shaders | 
| Nader Jawad | c088bdc | 2021-05-10 13:24:46 -0700 | [diff] [blame] | 136 | using GraphicBufferId = uint64_t; | 
| Nader Jawad | 2dfc98b | 2021-04-08 20:35:39 -0700 | [diff] [blame] | 137 |  | 
| Alec Mouri | a90a570 | 2021-04-16 16:36:21 +0000 | [diff] [blame] | 138 | // Number of external holders of ExternalTexture references, per GraphicBuffer ID. | 
| Nader Jawad | c088bdc | 2021-05-10 13:24:46 -0700 | [diff] [blame] | 139 | std::unordered_map<GraphicBufferId, int32_t> mGraphicBufferExternalRefs | 
| Alec Mouri | c7f6c8b | 2020-11-09 18:35:20 -0800 | [diff] [blame] | 140 | GUARDED_BY(mRenderingMutex); | 
| Derek Sollenberger | 4500718 | 2021-06-10 14:47:21 -0400 | [diff] [blame] | 141 | // Cache of GL textures that we'll store per GraphicBuffer ID, shared between GPU contexts. | 
| Nader Jawad | c088bdc | 2021-05-10 13:24:46 -0700 | [diff] [blame] | 142 | std::unordered_map<GraphicBufferId, std::shared_ptr<AutoBackendTexture::LocalRef>> mTextureCache | 
|  | 143 | GUARDED_BY(mRenderingMutex); | 
| Alec Mouri | 492c85c | 2021-11-19 15:58:10 -0800 | [diff] [blame] | 144 | std::unordered_map<shaders::LinearEffect, sk_sp<SkRuntimeEffect>, shaders::LinearEffectHasher> | 
|  | 145 | mRuntimeEffects; | 
| Derek Sollenberger | d3f6065 | 2021-06-11 15:34:36 -0400 | [diff] [blame] | 146 | AutoBackendTexture::CleanupManager mTextureCleanupMgr GUARDED_BY(mRenderingMutex); | 
| Nader Jawad | 2dfc98b | 2021-04-08 20:35:39 -0700 | [diff] [blame] | 147 |  | 
|  | 148 | StretchShaderFactory mStretchShaderFactory; | 
| John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 149 | // Mutex guarding rendering operations, so that: | 
|  | 150 | // 1. GL operations aren't interleaved, and | 
|  | 151 | // 2. Internal state related to rendering that is potentially modified by | 
|  | 152 | // multiple threads is guaranteed thread-safe. | 
| Derek Sollenberger | d3f6065 | 2021-06-11 15:34:36 -0400 | [diff] [blame] | 153 | mutable std::mutex mRenderingMutex; | 
| John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 154 |  | 
|  | 155 | sp<Fence> mLastDrawFence; | 
|  | 156 |  | 
| Lucas Dupin | d508e47 | 2020-11-04 04:32:06 +0000 | [diff] [blame] | 157 | // Graphics context used for creating surfaces and submitting commands | 
| John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 158 | sk_sp<GrDirectContext> mGrContext; | 
| Lucas Dupin | d508e47 | 2020-11-04 04:32:06 +0000 | [diff] [blame] | 159 | // Same as above, but for protected content (eg. DRM) | 
|  | 160 | sk_sp<GrDirectContext> mProtectedGrContext; | 
| John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 161 |  | 
| Lucas Dupin | d508e47 | 2020-11-04 04:32:06 +0000 | [diff] [blame] | 162 | bool mInProtectedContext = false; | 
| Ana Krulec | 70d15b1b | 2020-12-01 10:05:15 -0800 | [diff] [blame] | 163 | // Object to capture commands send to Skia. | 
| Alec Mouri | c0aae73 | 2021-01-12 13:32:18 -0800 | [diff] [blame] | 164 | std::unique_ptr<SkiaCapture> mCapture; | 
| Leon Scroggins III | 9f3072c | 2021-03-22 10:42:47 -0400 | [diff] [blame] | 165 |  | 
|  | 166 | // Implements PersistentCache as a way to monitor what SkSL shaders Skia has | 
|  | 167 | // cached. | 
|  | 168 | class SkSLCacheMonitor : public GrContextOptions::PersistentCache { | 
|  | 169 | public: | 
|  | 170 | SkSLCacheMonitor() = default; | 
|  | 171 | ~SkSLCacheMonitor() override = default; | 
|  | 172 |  | 
|  | 173 | sk_sp<SkData> load(const SkData& key) override; | 
|  | 174 |  | 
|  | 175 | void store(const SkData& key, const SkData& data, const SkString& description) override; | 
|  | 176 |  | 
|  | 177 | int shadersCachedSinceLastCall() { | 
|  | 178 | const int shadersCachedSinceLastCall = mShadersCachedSinceLastCall; | 
|  | 179 | mShadersCachedSinceLastCall = 0; | 
|  | 180 | return shadersCachedSinceLastCall; | 
|  | 181 | } | 
|  | 182 |  | 
|  | 183 | private: | 
|  | 184 | int mShadersCachedSinceLastCall = 0; | 
|  | 185 | }; | 
|  | 186 |  | 
|  | 187 | SkSLCacheMonitor mSkSLCacheMonitor; | 
| John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 188 | }; | 
|  | 189 |  | 
|  | 190 | } // namespace skia | 
|  | 191 | } // namespace renderengine | 
|  | 192 | } // namespace android | 
|  | 193 |  | 
| Galia Peycheva | 6c46065 | 2020-11-03 19:42:42 +0100 | [diff] [blame] | 194 | #endif /* SF_GLESRENDERENGINE_H_ */ |