• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef GLES_EGL_STATE_H
17 #define GLES_EGL_STATE_H
18 
19 #if RENDER_HAS_GLES_BACKEND
20 #include <cstdint>
21 
22 #include <base/containers/string.h>
23 #include <base/containers/string_view.h>
24 #include <render/gles/intf_device_gles.h>
25 #include <render/namespace.h>
26 
27 RENDER_BEGIN_NAMESPACE()
28 class SwapchainGLES;
29 struct DeviceCreateInfo;
30 struct DevicePlatformData;
31 
32 namespace GlesImplementation {
33 struct SurfaceInfo;
34 } // namespace GlesImplementation
35 
36 namespace EGLHelpers {
37 class EGLState {
38 public:
39     EGLState() = default;
40     ~EGLState() = default;
41     bool CreateContext(DeviceCreateInfo const& createInfo);
42     bool IsValid() const;
43 
44     void GlInitialize();
45     void DestroyContext();
46     void SetSwapInterval(uint32_t interval);
47     const DevicePlatformData& GetPlatformData() const;
48 
49     void SaveContext();
50     void SetContext(const SwapchainGLES* swapchain);
51     void RestoreContext();
52     void* ErrorFilter() const;
53 
54     uint32_t MajorVersion() const;
55     uint32_t MinorVersion() const;
56 
57     bool HasExtension(BASE_NS::string_view) const;
58     uintptr_t CreateSurface(uintptr_t window, uintptr_t instance, bool isSrgb) const noexcept;
59     void DestroySurface(uintptr_t surface) const noexcept;
60     bool GetSurfaceInformation(
61         const DevicePlatformDataGLES& plat, EGLSurface surface, GlesImplementation::SurfaceInfo& res) const;
62     void SwapBuffers(const SwapchainGLES& swapchain);
63 
64 protected:
65     bool VerifyVersion();
66     bool hasColorSpaceExt_ { false };
67     bool hasConfiglessExt_ { false };
68     bool hasSurfacelessExt_ { false };
69     void HandleExtensions();
70     void FillSurfaceInfo(EGLDisplay display, EGLSurface surface, EGLint configId, EGLConfig config,
71         GlesImplementation::SurfaceInfo& res) const;
72     BASE_NS::string cextensions_;                          // list of client extensions (null terminated strings)
73     BASE_NS::vector<BASE_NS::string_view> cextensionList_; // pointers to cextensions_
74     BASE_NS::string dextensions_;                          // list of display extensions (null terminated strings)
75     BASE_NS::vector<BASE_NS::string_view> dextensionList_; // pointers to dextensions_
76 
77     DevicePlatformDataGLES plat_;
78     struct ContextState {
79         EGLContext context = EGL_NO_CONTEXT;
80         EGLDisplay display = EGL_NO_DISPLAY;
81         EGLSurface readSurface = EGL_NO_SURFACE;
82         EGLSurface drawSurface = EGL_NO_SURFACE;
83     };
84     ContextState oldContext_;
85 
86     EGLSurface dummySurface_ = EGL_NO_SURFACE;
87     ContextState dummyContext_;
88     bool vSync_ = true;
89     bool oldIsSet_ = false;
90 
91     void GetContext(ContextState& oldState);
92     void SetContext(const ContextState& newState, bool force = false);
93     void ChooseConfiguration(const BackendExtraGLES*);
94     void CreateContext(const BackendExtraGLES*);
95     bool IsVersionGreaterOrEqual(uint32_t major, uint32_t minor) const;
96 };
97 } // namespace EGLHelpers
98 RENDER_END_NAMESPACE()
99 #endif
100 #endif // GLES_EGL_STATE_H
101