blob: ef06bfa5f58581b3795ca948ac80b64c7642fdd1 [file] [log] [blame]
John Reck67b1e2b2020-08-26 13:17:24 -07001/*
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 Mourib5777452020-09-28 11:32:42 -070020#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 Reck67b1e2b2020-08-26 13:17:24 -070027#include <sys/types.h>
Alec Mourib5777452020-09-28 11:32:42 -070028
John Reck67b1e2b2020-08-26 13:17:24 -070029#include <mutex>
30#include <unordered_map>
31
Alec Mouric7f6c8b2020-11-09 18:35:20 -080032#include "AutoBackendTexture.h"
Alec Mourib5777452020-09-28 11:32:42 -070033#include "EGL/egl.h"
Alec Mouric7f6c8b2020-11-09 18:35:20 -080034#include "SkImageInfo.h"
John Reck67b1e2b2020-08-26 13:17:24 -070035#include "SkiaRenderEngine.h"
Alec Mouric7f6c8b2020-11-09 18:35:20 -080036#include "android-base/macros.h"
Lucas Dupinf4cb4a02020-09-22 14:19:26 -070037#include "filters/BlurFilter.h"
Alec Mouric16a77d2020-11-13 13:20:11 -080038#include "skia/filters/LinearEffect.h"
John Reck67b1e2b2020-08-26 13:17:24 -070039
40namespace android {
41namespace renderengine {
42namespace skia {
43
44class SkiaGLRenderEngine : public skia::SkiaRenderEngine {
45public:
46 static std::unique_ptr<SkiaGLRenderEngine> create(const RenderEngineCreationArgs& args);
Lucas Dupind508e472020-11-04 04:32:06 +000047 SkiaGLRenderEngine(const RenderEngineCreationArgs& args, EGLDisplay display, EGLContext ctxt,
48 EGLSurface placeholder, EGLContext protectedContext,
John Reck67b1e2b2020-08-26 13:17:24 -070049 EGLSurface protectedPlaceholder);
50 ~SkiaGLRenderEngine() override{};
51
52 void unbindExternalTextureBuffer(uint64_t bufferId) override;
53 status_t drawLayers(const DisplaySettings& display,
54 const std::vector<const LayerSettings*>& layers,
55 const sp<GraphicBuffer>& buffer, const bool useFramebufferCache,
56 base::unique_fd&& bufferFence, base::unique_fd* drawFence) override;
57 void cleanFramebufferCache() override;
Lucas Dupind508e472020-11-04 04:32:06 +000058 bool isProtected() const override { return mInProtectedContext; }
59 bool supportsProtectedContent() const override;
60 bool useProtectedContext(bool useProtectedContext) override;
John Reck67b1e2b2020-08-26 13:17:24 -070061
62protected:
63 void dump(std::string& /*result*/) override{};
64 size_t getMaxTextureSize() const override;
65 size_t getMaxViewportDims() const override;
66
67private:
68 static EGLConfig chooseEglConfig(EGLDisplay display, int format, bool logConfig);
69 static EGLContext createEglContext(EGLDisplay display, EGLConfig config,
70 EGLContext shareContext, bool useContextPriority,
71 Protection protection);
72 static EGLSurface createPlaceholderEglPbufferSurface(EGLDisplay display, EGLConfig config,
73 int hwcFormat, Protection protection);
Lucas Dupin3f11e922020-09-22 17:31:04 -070074 inline SkRect getSkRect(const FloatRect& layer);
75 inline SkRect getSkRect(const Rect& layer);
Lucas Dupin21f348e2020-09-16 17:31:26 -070076 inline SkRRect getRoundedRect(const LayerSettings* layer);
Galia Peycheva80116e52020-11-06 11:57:25 +010077 inline BlurRegion getBlurRegion(const LayerSettings* layer);
Lucas Dupin3f11e922020-09-22 17:31:04 -070078 inline SkColor getSkColor(const vec4& color);
Lucas Dupinbb1a1d42020-09-18 15:17:02 -070079 inline SkM44 getSkM44(const mat4& matrix);
Lucas Dupin3f11e922020-09-22 17:31:04 -070080 inline SkPoint3 getSkPoint3(const vec3& vector);
John Reck67b1e2b2020-08-26 13:17:24 -070081
82 base::unique_fd flush();
83 bool waitFence(base::unique_fd fenceFd);
Lucas Dupin3f11e922020-09-22 17:31:04 -070084 void drawShadow(SkCanvas* canvas, const SkRect& casterRect, float casterCornerRadius,
85 const ShadowSettings& shadowSettings);
Galia Peychevaf7889b32020-11-25 22:22:40 +010086 void drawBlurRegion(SkCanvas* canvas, const BlurRegion& blurRegion, const SkRect& layerRect,
87 sk_sp<SkSurface> blurredSurface);
88 SkMatrix getBlurShaderTransform(const SkCanvas* canvas, const SkRect& layerRect);
John Reck67b1e2b2020-08-26 13:17:24 -070089
90 EGLDisplay mEGLDisplay;
John Reck67b1e2b2020-08-26 13:17:24 -070091 EGLContext mEGLContext;
92 EGLSurface mPlaceholderSurface;
93 EGLContext mProtectedEGLContext;
94 EGLSurface mProtectedPlaceholderSurface;
Lucas Dupinf4cb4a02020-09-22 14:19:26 -070095 BlurFilter* mBlurFilter = nullptr;
John Reck67b1e2b2020-08-26 13:17:24 -070096
Alec Mourib5777452020-09-28 11:32:42 -070097 const bool mUseColorManagement;
98
Alec Mouric7f6c8b2020-11-09 18:35:20 -080099 // Cache of GL textures that we'll store per GraphicBuffer ID
100 std::unordered_map<uint64_t, std::shared_ptr<AutoBackendTexture::LocalRef>> mTextureCache
101 GUARDED_BY(mRenderingMutex);
102 std::unordered_map<uint64_t, std::shared_ptr<AutoBackendTexture::LocalRef>>
103 mProtectedTextureCache GUARDED_BY(mRenderingMutex);
Alec Mouric16a77d2020-11-13 13:20:11 -0800104 std::unordered_map<LinearEffect, sk_sp<SkRuntimeEffect>, LinearEffectHasher> mRuntimeEffects;
John Reck67b1e2b2020-08-26 13:17:24 -0700105 // Mutex guarding rendering operations, so that:
106 // 1. GL operations aren't interleaved, and
107 // 2. Internal state related to rendering that is potentially modified by
108 // multiple threads is guaranteed thread-safe.
109 std::mutex mRenderingMutex;
110
111 sp<Fence> mLastDrawFence;
112
Lucas Dupind508e472020-11-04 04:32:06 +0000113 // Graphics context used for creating surfaces and submitting commands
John Reck67b1e2b2020-08-26 13:17:24 -0700114 sk_sp<GrDirectContext> mGrContext;
Lucas Dupind508e472020-11-04 04:32:06 +0000115 // Same as above, but for protected content (eg. DRM)
116 sk_sp<GrDirectContext> mProtectedGrContext;
John Reck67b1e2b2020-08-26 13:17:24 -0700117
Lucas Dupind508e472020-11-04 04:32:06 +0000118 bool mInProtectedContext = false;
John Reck67b1e2b2020-08-26 13:17:24 -0700119};
120
121} // namespace skia
122} // namespace renderengine
123} // namespace android
124
Galia Peycheva6c460652020-11-03 19:42:42 +0100125#endif /* SF_GLESRENDERENGINE_H_ */