| 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_SKIARENDERENGINE_H_ | 
|  | 18 | #define SF_SKIARENDERENGINE_H_ | 
|  | 19 |  | 
|  | 20 | #include <renderengine/RenderEngine.h> | 
|  | 21 | #include <sys/types.h> | 
|  | 22 |  | 
| Lingfeng Yang | 00c1ff6 | 2022-06-02 09:19:28 -0700 | [diff] [blame] | 23 | #include <GrBackendSemaphore.h> | 
|  | 24 | #include <GrDirectContext.h> | 
|  | 25 | #include <SkSurface.h> | 
|  | 26 | #include <android-base/thread_annotations.h> | 
|  | 27 | #include <renderengine/ExternalTexture.h> | 
|  | 28 | #include <renderengine/RenderEngine.h> | 
|  | 29 | #include <sys/types.h> | 
|  | 30 |  | 
|  | 31 | #include <mutex> | 
|  | 32 | #include <unordered_map> | 
|  | 33 |  | 
|  | 34 | #include "AutoBackendTexture.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 | class SkData; | 
|  | 45 |  | 
|  | 46 | struct SkPoint3; | 
|  | 47 |  | 
| John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 48 | namespace android { | 
|  | 49 |  | 
|  | 50 | namespace renderengine { | 
|  | 51 |  | 
|  | 52 | class Mesh; | 
|  | 53 | class Texture; | 
|  | 54 |  | 
|  | 55 | namespace skia { | 
|  | 56 |  | 
|  | 57 | class BlurFilter; | 
|  | 58 |  | 
| Alec Mouri | 081be4c | 2020-09-16 10:24:47 -0700 | [diff] [blame] | 59 | class SkiaRenderEngine : public RenderEngine { | 
| John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 60 | public: | 
|  | 61 | static std::unique_ptr<SkiaRenderEngine> create(const RenderEngineCreationArgs& args); | 
| Lingfeng Yang | 00c1ff6 | 2022-06-02 09:19:28 -0700 | [diff] [blame] | 62 | SkiaRenderEngine(RenderEngineType type, | 
|  | 63 | PixelFormat pixelFormat, | 
|  | 64 | bool useColorManagement, | 
|  | 65 | bool supportsBackgroundBlur); | 
|  | 66 | ~SkiaRenderEngine() override; | 
| John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 67 |  | 
| Lingfeng Yang | 00c1ff6 | 2022-06-02 09:19:28 -0700 | [diff] [blame] | 68 | std::future<void> primeCache() override final; | 
|  | 69 | void cleanupPostRender() override final; | 
|  | 70 | void cleanFramebufferCache() override final{ } | 
|  | 71 | bool isProtected() const override final{ return mInProtectedContext; } | 
|  | 72 | bool supportsBackgroundBlur() override final { | 
|  | 73 | return mBlurFilter != nullptr; | 
|  | 74 | } | 
|  | 75 | void onActiveDisplaySizeChanged(ui::Size size) override final; | 
|  | 76 | int reportShadersCompiled(); | 
| Alec Mouri | a90a570 | 2021-04-16 16:36:21 +0000 | [diff] [blame] | 77 |  | 
| Lingfeng Yang | 00c1ff6 | 2022-06-02 09:19:28 -0700 | [diff] [blame] | 78 | virtual void genTextures(size_t /*count*/, uint32_t* /*names*/) override final{}; | 
|  | 79 | virtual void deleteTextures(size_t /*count*/, uint32_t const* /*names*/) override final{}; | 
|  | 80 | virtual void setEnableTracing(bool tracingEnabled) override final; | 
|  | 81 |  | 
|  | 82 | void useProtectedContext(bool useProtectedContext) override; | 
|  | 83 | bool supportsProtectedContent() const override { | 
|  | 84 | return supportsProtectedContentImpl(); | 
|  | 85 | } | 
|  | 86 | void ensureGrContextsCreated(); | 
| Alec Mouri | a90a570 | 2021-04-16 16:36:21 +0000 | [diff] [blame] | 87 | protected: | 
| Lingfeng Yang | 00c1ff6 | 2022-06-02 09:19:28 -0700 | [diff] [blame] | 88 | // This is so backends can stop the generic rendering state first before | 
|  | 89 | // cleaning up backend-specific state | 
|  | 90 | void finishRenderingAndAbandonContext(); | 
| Sally Qi | 4cabdd0 | 2021-08-05 16:45:57 -0700 | [diff] [blame] | 91 |  | 
| Lingfeng Yang | 00c1ff6 | 2022-06-02 09:19:28 -0700 | [diff] [blame] | 92 | // Functions that a given backend (GLES, Vulkan) must implement | 
|  | 93 | using Contexts = std::pair<sk_sp<GrDirectContext>, sk_sp<GrDirectContext>>; | 
|  | 94 | virtual Contexts createDirectContexts(const GrContextOptions& options) = 0; | 
|  | 95 | virtual bool supportsProtectedContentImpl() const = 0; | 
|  | 96 | virtual bool useProtectedContextImpl(GrProtected isProtected) = 0; | 
|  | 97 | virtual void waitFence(GrDirectContext* grContext, base::borrowed_fd fenceFd) = 0; | 
|  | 98 | virtual base::unique_fd flushAndSubmit(GrDirectContext* context) = 0; | 
|  | 99 | virtual void appendBackendSpecificInfoToDump(std::string& result) = 0; | 
|  | 100 |  | 
|  | 101 | size_t getMaxTextureSize() const override final; | 
|  | 102 | size_t getMaxViewportDims() const override final; | 
|  | 103 | GrDirectContext* getActiveGrContext(); | 
|  | 104 |  | 
|  | 105 | // Implements PersistentCache as a way to monitor what SkSL shaders Skia has | 
|  | 106 | // cached. | 
|  | 107 | class SkSLCacheMonitor : public GrContextOptions::PersistentCache { | 
|  | 108 | public: | 
|  | 109 | SkSLCacheMonitor() = default; | 
|  | 110 | ~SkSLCacheMonitor() override = default; | 
|  | 111 |  | 
|  | 112 | sk_sp<SkData> load(const SkData& key) override; | 
|  | 113 |  | 
|  | 114 | void store(const SkData& key, const SkData& data, const SkString& description) override; | 
|  | 115 |  | 
|  | 116 | int shadersCachedSinceLastCall() { | 
|  | 117 | const int shadersCachedSinceLastCall = mShadersCachedSinceLastCall; | 
|  | 118 | mShadersCachedSinceLastCall = 0; | 
|  | 119 | return shadersCachedSinceLastCall; | 
|  | 120 | } | 
|  | 121 |  | 
|  | 122 | int totalShadersCompiled() const { return mTotalShadersCompiled; } | 
|  | 123 |  | 
|  | 124 | private: | 
|  | 125 | int mShadersCachedSinceLastCall = 0; | 
|  | 126 | int mTotalShadersCompiled = 0; | 
| Sally Qi | 4cabdd0 | 2021-08-05 16:45:57 -0700 | [diff] [blame] | 127 | }; | 
| Lingfeng Yang | 00c1ff6 | 2022-06-02 09:19:28 -0700 | [diff] [blame] | 128 |  | 
|  | 129 | private: | 
|  | 130 | void mapExternalTextureBuffer(const sp<GraphicBuffer>& buffer, | 
|  | 131 | bool isRenderable) override final; | 
|  | 132 | void unmapExternalTextureBuffer(const sp<GraphicBuffer>& buffer) override final; | 
|  | 133 | bool canSkipPostRenderCleanup() const override final; | 
|  | 134 |  | 
|  | 135 | void initCanvas(SkCanvas* canvas, const DisplaySettings& display); | 
|  | 136 | void drawShadow(SkCanvas* canvas, const SkRRect& casterRRect, | 
|  | 137 | const ShadowSettings& shadowSettings); | 
|  | 138 | void drawLayersInternal(const std::shared_ptr<std::promise<RenderEngineResult>>&& resultPromise, | 
|  | 139 | const DisplaySettings& display, | 
|  | 140 | const std::vector<LayerSettings>& layers, | 
|  | 141 | const std::shared_ptr<ExternalTexture>& buffer, | 
|  | 142 | const bool useFramebufferCache, | 
|  | 143 | base::unique_fd&& bufferFence) override final; | 
|  | 144 |  | 
|  | 145 | void dump(std::string& result) override final; | 
|  | 146 |  | 
|  | 147 | // If requiresLinearEffect is true or the layer has a stretchEffect a new shader is returned. | 
|  | 148 | // Otherwise it returns the input shader. | 
|  | 149 | struct RuntimeEffectShaderParameters { | 
|  | 150 | sk_sp<SkShader> shader; | 
|  | 151 | const LayerSettings& layer; | 
|  | 152 | const DisplaySettings& display; | 
|  | 153 | bool undoPremultipliedAlpha; | 
|  | 154 | bool requiresLinearEffect; | 
|  | 155 | float layerDimmingRatio; | 
|  | 156 | }; | 
|  | 157 | sk_sp<SkShader> createRuntimeEffectShader(const RuntimeEffectShaderParameters&); | 
|  | 158 |  | 
|  | 159 | const PixelFormat mDefaultPixelFormat; | 
|  | 160 | const bool mUseColorManagement; | 
|  | 161 |  | 
|  | 162 | // Identifier used for various mappings of layers to various | 
|  | 163 | // textures or shaders | 
|  | 164 | using GraphicBufferId = uint64_t; | 
|  | 165 |  | 
|  | 166 | // Number of external holders of ExternalTexture references, per GraphicBuffer ID. | 
|  | 167 | std::unordered_map<GraphicBufferId, int32_t> mGraphicBufferExternalRefs | 
|  | 168 | GUARDED_BY(mRenderingMutex); | 
|  | 169 | // Cache of GL textures that we'll store per GraphicBuffer ID, shared between GPU contexts. | 
|  | 170 | std::unordered_map<GraphicBufferId, std::shared_ptr<AutoBackendTexture::LocalRef>> mTextureCache | 
|  | 171 | GUARDED_BY(mRenderingMutex); | 
|  | 172 | std::unordered_map<shaders::LinearEffect, sk_sp<SkRuntimeEffect>, shaders::LinearEffectHasher> | 
|  | 173 | mRuntimeEffects; | 
|  | 174 | AutoBackendTexture::CleanupManager mTextureCleanupMgr GUARDED_BY(mRenderingMutex); | 
|  | 175 |  | 
|  | 176 | StretchShaderFactory mStretchShaderFactory; | 
|  | 177 |  | 
|  | 178 | sp<Fence> mLastDrawFence; | 
|  | 179 | BlurFilter* mBlurFilter = nullptr; | 
|  | 180 |  | 
|  | 181 | // Object to capture commands send to Skia. | 
|  | 182 | std::unique_ptr<SkiaCapture> mCapture; | 
|  | 183 |  | 
|  | 184 | // Mutex guarding rendering operations, so that internal state related to | 
|  | 185 | // rendering that is potentially modified by multiple threads is guaranteed thread-safe. | 
|  | 186 | mutable std::mutex mRenderingMutex; | 
|  | 187 | SkSLCacheMonitor mSkSLCacheMonitor; | 
|  | 188 |  | 
|  | 189 | // Graphics context used for creating surfaces and submitting commands | 
|  | 190 | sk_sp<GrDirectContext> mGrContext; | 
|  | 191 | // Same as above, but for protected content (eg. DRM) | 
|  | 192 | sk_sp<GrDirectContext> mProtectedGrContext; | 
|  | 193 | bool mInProtectedContext = false; | 
| John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 194 | }; | 
|  | 195 |  | 
|  | 196 | } // namespace skia | 
|  | 197 | } // namespace renderengine | 
|  | 198 | } // namespace android | 
|  | 199 |  | 
| Leon Scroggins III | b9216dc | 2021-03-08 17:19:01 -0500 | [diff] [blame] | 200 | #endif /* SF_GLESRENDERENGINE_H_ */ |