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
27RENDER_BEGIN_NAMESPACE()
28class SwapchainGLES;
29struct DeviceCreateInfo;
30struct DevicePlatformData;
31
32namespace GlesImplementation {
33struct SurfaceInfo;
34} // namespace GlesImplementation
35
36namespace EGLHelpers {
37class EGLState {
38public:
39    EGLState() = default;
40    ~EGLState() = default;
41    bool CreateContext(DeviceCreateInfo const& createInfo);
42    bool IsValid();
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) 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
64protected:
65    bool VerifyVersion();
66    bool hasColorSpaceExt_ { false };
67    bool hasConfiglessExt_ { false };
68    bool hasSurfacelessExt_ { false };
69    void HandleExtensions();
70    BASE_NS::string cextensions_;                          // list of client extensions (null terminated strings)
71    BASE_NS::vector<BASE_NS::string_view> cextensionList_; // pointers to cextensions_
72    BASE_NS::string dextensions_;                          // list of display extensions (null terminated strings)
73    BASE_NS::vector<BASE_NS::string_view> dextensionList_; // pointers to dextensions_
74
75    DevicePlatformDataGLES plat_;
76    struct ContextState {
77        EGLContext context = EGL_NO_CONTEXT;
78        EGLDisplay display = EGL_NO_DISPLAY;
79        EGLSurface readSurface = EGL_NO_SURFACE;
80        EGLSurface drawSurface = EGL_NO_SURFACE;
81    };
82    ContextState oldContext_;
83
84    EGLSurface dummySurface_ = EGL_NO_SURFACE;
85    ContextState dummyContext_;
86    bool vSync_ = true;
87    bool oldIsSet_ = false;
88
89    void GetContext(ContextState& oldState);
90    void SetContext(const ContextState& newState, bool force = false);
91    void ChooseConfiguration(const BackendExtraGLES*);
92    void CreateContext(const BackendExtraGLES*);
93    bool IsVersionGreaterOrEqual(uint32_t major, uint32_t minor) const;
94};
95} // namespace EGLHelpers
96RENDER_END_NAMESPACE()
97#endif
98#endif // GLES_EGL_STATE_H
99