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 | |
| 20 | #include <sys/types.h> |
| 21 | #include <mutex> |
| 22 | #include <unordered_map> |
| 23 | |
| 24 | #include <android-base/thread_annotations.h> |
| 25 | #include <renderengine/RenderEngine.h> |
| 26 | |
| 27 | #include <GrDirectContext.h> |
| 28 | #include <SkSurface.h> |
| 29 | |
| 30 | #include "SkiaRenderEngine.h" |
Lucas Dupin | f4cb4a0 | 2020-09-22 14:19:26 -0700 | [diff] [blame^] | 31 | #include "filters/BlurFilter.h" |
John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 32 | |
| 33 | namespace android { |
| 34 | namespace renderengine { |
| 35 | namespace skia { |
| 36 | |
| 37 | class SkiaGLRenderEngine : public skia::SkiaRenderEngine { |
| 38 | public: |
| 39 | static std::unique_ptr<SkiaGLRenderEngine> create(const RenderEngineCreationArgs& args); |
Lucas Dupin | f4cb4a0 | 2020-09-22 14:19:26 -0700 | [diff] [blame^] | 40 | SkiaGLRenderEngine(const RenderEngineCreationArgs& args, EGLDisplay display, EGLConfig config, |
| 41 | EGLContext ctxt, EGLSurface placeholder, EGLContext protectedContext, |
John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 42 | EGLSurface protectedPlaceholder); |
| 43 | ~SkiaGLRenderEngine() override{}; |
| 44 | |
| 45 | void unbindExternalTextureBuffer(uint64_t bufferId) override; |
| 46 | status_t drawLayers(const DisplaySettings& display, |
| 47 | const std::vector<const LayerSettings*>& layers, |
| 48 | const sp<GraphicBuffer>& buffer, const bool useFramebufferCache, |
| 49 | base::unique_fd&& bufferFence, base::unique_fd* drawFence) override; |
| 50 | void cleanFramebufferCache() override; |
| 51 | |
| 52 | protected: |
| 53 | void dump(std::string& /*result*/) override{}; |
| 54 | size_t getMaxTextureSize() const override; |
| 55 | size_t getMaxViewportDims() const override; |
| 56 | |
| 57 | private: |
| 58 | static EGLConfig chooseEglConfig(EGLDisplay display, int format, bool logConfig); |
| 59 | static EGLContext createEglContext(EGLDisplay display, EGLConfig config, |
| 60 | EGLContext shareContext, bool useContextPriority, |
| 61 | Protection protection); |
| 62 | static EGLSurface createPlaceholderEglPbufferSurface(EGLDisplay display, EGLConfig config, |
| 63 | int hwcFormat, Protection protection); |
Lucas Dupin | 3f11e92 | 2020-09-22 17:31:04 -0700 | [diff] [blame] | 64 | inline SkRect getSkRect(const FloatRect& layer); |
| 65 | inline SkRect getSkRect(const Rect& layer); |
Lucas Dupin | 21f348e | 2020-09-16 17:31:26 -0700 | [diff] [blame] | 66 | inline SkRRect getRoundedRect(const LayerSettings* layer); |
Lucas Dupin | 3f11e92 | 2020-09-22 17:31:04 -0700 | [diff] [blame] | 67 | inline SkColor getSkColor(const vec4& color); |
Lucas Dupin | bb1a1d4 | 2020-09-18 15:17:02 -0700 | [diff] [blame] | 68 | inline SkM44 getSkM44(const mat4& matrix); |
Lucas Dupin | 3f11e92 | 2020-09-22 17:31:04 -0700 | [diff] [blame] | 69 | inline SkPoint3 getSkPoint3(const vec3& vector); |
John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 70 | |
| 71 | base::unique_fd flush(); |
| 72 | bool waitFence(base::unique_fd fenceFd); |
Lucas Dupin | 3f11e92 | 2020-09-22 17:31:04 -0700 | [diff] [blame] | 73 | void drawShadow(SkCanvas* canvas, const SkRect& casterRect, float casterCornerRadius, |
| 74 | const ShadowSettings& shadowSettings); |
John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 75 | |
| 76 | EGLDisplay mEGLDisplay; |
| 77 | EGLConfig mEGLConfig; |
| 78 | EGLContext mEGLContext; |
| 79 | EGLSurface mPlaceholderSurface; |
| 80 | EGLContext mProtectedEGLContext; |
| 81 | EGLSurface mProtectedPlaceholderSurface; |
Lucas Dupin | f4cb4a0 | 2020-09-22 14:19:26 -0700 | [diff] [blame^] | 82 | BlurFilter* mBlurFilter = nullptr; |
John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 83 | |
| 84 | // Cache of GL images that we'll store per GraphicBuffer ID |
| 85 | std::unordered_map<uint64_t, sk_sp<SkImage>> mImageCache GUARDED_BY(mRenderingMutex); |
| 86 | // Mutex guarding rendering operations, so that: |
| 87 | // 1. GL operations aren't interleaved, and |
| 88 | // 2. Internal state related to rendering that is potentially modified by |
| 89 | // multiple threads is guaranteed thread-safe. |
| 90 | std::mutex mRenderingMutex; |
| 91 | |
| 92 | sp<Fence> mLastDrawFence; |
| 93 | |
| 94 | sk_sp<GrDirectContext> mGrContext; |
| 95 | |
| 96 | std::unordered_map<uint64_t, sk_sp<SkSurface>> mSurfaceCache; |
| 97 | }; |
| 98 | |
| 99 | } // namespace skia |
| 100 | } // namespace renderengine |
| 101 | } // namespace android |
| 102 | |
| 103 | #endif /* SF_GLESRENDERENGINE_H_ */ |