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 <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/ExternalTexture.h>
27 #include <renderengine/RenderEngine.h>
28 #include <sys/types.h>
29 
30 #include <mutex>
31 #include <unordered_map>
32 
33 #include "AutoBackendTexture.h"
34 #include "EGL/egl.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 
48 namespace android {
49 namespace renderengine {
50 namespace skia {
51 
52 class SkiaGLRenderEngine : public skia::SkiaRenderEngine {
53 public:
54     static std::unique_ptr<SkiaGLRenderEngine> create(const RenderEngineCreationArgs& args);
55     ~SkiaGLRenderEngine() override;
56 
57     int getContextPriority() override;
58 
59 protected:
60     // Implementations of abstract SkiaRenderEngine functions specific to
61     // rendering backend
62     virtual SkiaRenderEngine::Contexts createContexts();
63     bool supportsProtectedContentImpl() const override;
64     bool useProtectedContextImpl(GrProtected isProtected) override;
65     void waitFence(SkiaGpuContext* context, base::borrowed_fd fenceFd) override;
66     base::unique_fd flushAndSubmit(SkiaGpuContext* context, sk_sp<SkSurface> dstSurface) override;
67     void appendBackendSpecificInfoToDump(std::string& result) override;
68 
69 private:
70     SkiaGLRenderEngine(const RenderEngineCreationArgs& args, EGLDisplay display, EGLContext ctxt,
71                        EGLSurface placeholder, EGLContext protectedContext,
72                        EGLSurface protectedPlaceholder);
73     bool waitGpuFence(base::borrowed_fd fenceFd);
74     base::unique_fd flushGL();
75     static EGLConfig chooseEglConfig(EGLDisplay display, int format, bool logConfig);
76     static EGLContext createEglContext(EGLDisplay display, EGLConfig config,
77                                        EGLContext shareContext,
78                                        std::optional<ContextPriority> contextPriority,
79                                        Protection protection);
80     static std::optional<RenderEngine::ContextPriority> createContextPriority(
81             const RenderEngineCreationArgs& args);
82     static EGLSurface createPlaceholderEglPbufferSurface(
83             EGLDisplay display, EGLConfig config, int hwcFormat, Protection protection);
84 
85     EGLDisplay mEGLDisplay;
86     EGLContext mEGLContext;
87     EGLSurface mPlaceholderSurface;
88     EGLContext mProtectedEGLContext;
89     EGLSurface mProtectedPlaceholderSurface;
90 };
91 
92 } // namespace skia
93 } // namespace renderengine
94 } // namespace android
95 
96 #endif /* SF_GLESRENDERENGINE_H_ */
97