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" |
Alec Mouri | c7f6c8b | 2020-11-09 18:35:20 -0800 | [diff] [blame] | 34 | #include "SkImageInfo.h" |
John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 35 | #include "SkiaRenderEngine.h" |
Alec Mouri | c7f6c8b | 2020-11-09 18:35:20 -0800 | [diff] [blame] | 36 | #include "android-base/macros.h" |
Alec Mouri | c0aae73 | 2021-01-12 13:32:18 -0800 | [diff] [blame] | 37 | #include "debug/SkiaCapture.h" |
Lucas Dupin | f4cb4a0 | 2020-09-22 14:19:26 -0700 | [diff] [blame] | 38 | #include "filters/BlurFilter.h" |
Alec Mouri | c0aae73 | 2021-01-12 13:32:18 -0800 | [diff] [blame] | 39 | #include "filters/LinearEffect.h" |
John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 40 | |
| 41 | namespace android { |
| 42 | namespace renderengine { |
| 43 | namespace skia { |
| 44 | |
| 45 | class SkiaGLRenderEngine : public skia::SkiaRenderEngine { |
| 46 | public: |
| 47 | static std::unique_ptr<SkiaGLRenderEngine> create(const RenderEngineCreationArgs& args); |
Lucas Dupin | d508e47 | 2020-11-04 04:32:06 +0000 | [diff] [blame] | 48 | SkiaGLRenderEngine(const RenderEngineCreationArgs& args, EGLDisplay display, EGLContext ctxt, |
| 49 | EGLSurface placeholder, EGLContext protectedContext, |
John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 50 | EGLSurface protectedPlaceholder); |
Alec Mouri | c0aae73 | 2021-01-12 13:32:18 -0800 | [diff] [blame] | 51 | ~SkiaGLRenderEngine() override EXCLUDES(mRenderingMutex); |
John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 52 | |
Ana Krulec | dfec8f5 | 2021-01-13 12:51:47 -0800 | [diff] [blame^] | 53 | void cacheExternalTextureBuffer(const sp<GraphicBuffer>& buffer) override; |
John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 54 | void unbindExternalTextureBuffer(uint64_t bufferId) override; |
| 55 | status_t drawLayers(const DisplaySettings& display, |
| 56 | const std::vector<const LayerSettings*>& layers, |
| 57 | const sp<GraphicBuffer>& buffer, const bool useFramebufferCache, |
| 58 | base::unique_fd&& bufferFence, base::unique_fd* drawFence) override; |
| 59 | void cleanFramebufferCache() override; |
Alec Mouri | d6f0946 | 2020-12-07 11:18:17 -0800 | [diff] [blame] | 60 | int getContextPriority() override; |
Lucas Dupin | d508e47 | 2020-11-04 04:32:06 +0000 | [diff] [blame] | 61 | bool isProtected() const override { return mInProtectedContext; } |
| 62 | bool supportsProtectedContent() const override; |
| 63 | bool useProtectedContext(bool useProtectedContext) override; |
John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 64 | |
| 65 | protected: |
| 66 | void dump(std::string& /*result*/) override{}; |
| 67 | size_t getMaxTextureSize() const override; |
| 68 | size_t getMaxViewportDims() const override; |
| 69 | |
| 70 | private: |
| 71 | static EGLConfig chooseEglConfig(EGLDisplay display, int format, bool logConfig); |
| 72 | static EGLContext createEglContext(EGLDisplay display, EGLConfig config, |
Alec Mouri | d6f0946 | 2020-12-07 11:18:17 -0800 | [diff] [blame] | 73 | EGLContext shareContext, |
| 74 | std::optional<ContextPriority> contextPriority, |
John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 75 | Protection protection); |
Alec Mouri | d6f0946 | 2020-12-07 11:18:17 -0800 | [diff] [blame] | 76 | static std::optional<RenderEngine::ContextPriority> createContextPriority( |
| 77 | const RenderEngineCreationArgs& args); |
John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 78 | static EGLSurface createPlaceholderEglPbufferSurface(EGLDisplay display, EGLConfig config, |
| 79 | int hwcFormat, Protection protection); |
Lucas Dupin | 3f11e92 | 2020-09-22 17:31:04 -0700 | [diff] [blame] | 80 | inline SkRect getSkRect(const FloatRect& layer); |
| 81 | inline SkRect getSkRect(const Rect& layer); |
Lucas Dupin | 21f348e | 2020-09-16 17:31:26 -0700 | [diff] [blame] | 82 | inline SkRRect getRoundedRect(const LayerSettings* layer); |
Galia Peycheva | 80116e5 | 2020-11-06 11:57:25 +0100 | [diff] [blame] | 83 | inline BlurRegion getBlurRegion(const LayerSettings* layer); |
Lucas Dupin | 3f11e92 | 2020-09-22 17:31:04 -0700 | [diff] [blame] | 84 | inline SkColor getSkColor(const vec4& color); |
Lucas Dupin | bb1a1d4 | 2020-09-18 15:17:02 -0700 | [diff] [blame] | 85 | inline SkM44 getSkM44(const mat4& matrix); |
Lucas Dupin | 3f11e92 | 2020-09-22 17:31:04 -0700 | [diff] [blame] | 86 | inline SkPoint3 getSkPoint3(const vec3& vector); |
John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 87 | |
| 88 | base::unique_fd flush(); |
| 89 | bool waitFence(base::unique_fd fenceFd); |
Lucas Dupin | 3f11e92 | 2020-09-22 17:31:04 -0700 | [diff] [blame] | 90 | void drawShadow(SkCanvas* canvas, const SkRect& casterRect, float casterCornerRadius, |
| 91 | const ShadowSettings& shadowSettings); |
Galia Peycheva | f7889b3 | 2020-11-25 22:22:40 +0100 | [diff] [blame] | 92 | void drawBlurRegion(SkCanvas* canvas, const BlurRegion& blurRegion, const SkRect& layerRect, |
| 93 | sk_sp<SkSurface> blurredSurface); |
| 94 | SkMatrix getBlurShaderTransform(const SkCanvas* canvas, const SkRect& layerRect); |
Ana Krulec | 4781421 | 2021-01-06 19:00:10 -0800 | [diff] [blame] | 95 | // If mUseColorManagement is correct and layer needsLinearEffect, it returns a linear runtime |
| 96 | // shader. Otherwise it returns the input shader. |
| 97 | sk_sp<SkShader> createRuntimeEffectShader(sk_sp<SkShader> shader, const LayerSettings* layer, |
| 98 | const DisplaySettings& display, |
| 99 | bool undoPremultipliedAlpha); |
John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 100 | |
| 101 | EGLDisplay mEGLDisplay; |
John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 102 | EGLContext mEGLContext; |
| 103 | EGLSurface mPlaceholderSurface; |
| 104 | EGLContext mProtectedEGLContext; |
| 105 | EGLSurface mProtectedPlaceholderSurface; |
Lucas Dupin | f4cb4a0 | 2020-09-22 14:19:26 -0700 | [diff] [blame] | 106 | BlurFilter* mBlurFilter = nullptr; |
John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 107 | |
Alec Mouri | b577745 | 2020-09-28 11:32:42 -0700 | [diff] [blame] | 108 | const bool mUseColorManagement; |
| 109 | |
Alec Mouri | c7f6c8b | 2020-11-09 18:35:20 -0800 | [diff] [blame] | 110 | // Cache of GL textures that we'll store per GraphicBuffer ID |
| 111 | std::unordered_map<uint64_t, std::shared_ptr<AutoBackendTexture::LocalRef>> mTextureCache |
| 112 | GUARDED_BY(mRenderingMutex); |
| 113 | std::unordered_map<uint64_t, std::shared_ptr<AutoBackendTexture::LocalRef>> |
| 114 | mProtectedTextureCache GUARDED_BY(mRenderingMutex); |
Alec Mouri | c16a77d | 2020-11-13 13:20:11 -0800 | [diff] [blame] | 115 | std::unordered_map<LinearEffect, sk_sp<SkRuntimeEffect>, LinearEffectHasher> mRuntimeEffects; |
John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 116 | // Mutex guarding rendering operations, so that: |
| 117 | // 1. GL operations aren't interleaved, and |
| 118 | // 2. Internal state related to rendering that is potentially modified by |
| 119 | // multiple threads is guaranteed thread-safe. |
| 120 | std::mutex mRenderingMutex; |
| 121 | |
| 122 | sp<Fence> mLastDrawFence; |
| 123 | |
Lucas Dupin | d508e47 | 2020-11-04 04:32:06 +0000 | [diff] [blame] | 124 | // Graphics context used for creating surfaces and submitting commands |
John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 125 | sk_sp<GrDirectContext> mGrContext; |
Lucas Dupin | d508e47 | 2020-11-04 04:32:06 +0000 | [diff] [blame] | 126 | // Same as above, but for protected content (eg. DRM) |
| 127 | sk_sp<GrDirectContext> mProtectedGrContext; |
John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 128 | |
Lucas Dupin | d508e47 | 2020-11-04 04:32:06 +0000 | [diff] [blame] | 129 | bool mInProtectedContext = false; |
Ana Krulec | 70d15b1b | 2020-12-01 10:05:15 -0800 | [diff] [blame] | 130 | // Object to capture commands send to Skia. |
Alec Mouri | c0aae73 | 2021-01-12 13:32:18 -0800 | [diff] [blame] | 131 | std::unique_ptr<SkiaCapture> mCapture; |
Ana Krulec | dfec8f5 | 2021-01-13 12:51:47 -0800 | [diff] [blame^] | 132 | |
| 133 | // Keep this information as a local variable to determine whether the access of the GL |
| 134 | // operations is working on the same threads. |
| 135 | const RenderEngineType mRenderEngineType = RenderEngineType::SKIA_GL; |
John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 136 | }; |
| 137 | |
| 138 | } // namespace skia |
| 139 | } // namespace renderengine |
| 140 | } // namespace android |
| 141 | |
Galia Peycheva | 6c46065 | 2020-11-03 19:42:42 +0100 | [diff] [blame] | 142 | #endif /* SF_GLESRENDERENGINE_H_ */ |