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_WGL_STATE_H
17 #define GLES_WGL_STATE_H
18 
19 #if _WIN32
20 
21 #include <base/containers/string.h>
22 #include <base/containers/string_view.h>
23 #include <base/containers/vector.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 WGLHelpers {
37 class WGLState final {
38 public:
39     WGLState() = default;
40     ~WGLState() = default;
41     void CreateContext(DeviceCreateInfo const& createInfo);
42     bool IsValid();
43     void GlInitialize();
44     void DestroyContext();
45     void SetSwapInterval(uint32_t interval);
46     const DevicePlatformData& GetPlatformData() const;
47 
48     void SaveContext();
49     void SetContext(const SwapchainGLES* swapChain);
50     void RestoreContext();
51     void* ErrorFilter() const;
52 
53     bool HasExtension(BASE_NS::string_view) const;
54     uintptr_t CreateSurface(uintptr_t window, uintptr_t instance) const noexcept;
55     void DestroySurface(uintptr_t surface) const noexcept;
56     bool GetSurfaceInformation(HDC surface, GlesImplementation::SurfaceInfo& res) const;
57     void SwapBuffers(const SwapchainGLES& swapChain);
58 
59 protected:
60     bool GetInformation(HDC surface, int32_t configId, GlesImplementation::SurfaceInfo& res) const;
61     int ChoosePixelFormat(HDC dc, const BASE_NS::vector<int>& inattributes);
62 
63     HMODULE glModule_ = nullptr;
64     DevicePlatformDataGL plat_;
65     struct ContextState {
66         HGLRC context { nullptr };
67         HDC display { nullptr };
68     };
69     ContextState oldContext_;
70     ContextState dummyContext_;
71     BASE_NS::string extensions_;
72     BASE_NS::vector<BASE_NS::string_view> extensionList_;
73     bool vSync_ = true;
74     bool oldIsSet_ = false;
75     bool hasSRGBFB_ = false;
76     bool hasColorSpace_ = false;
77 };
78 } // namespace WGLHelpers
79 RENDER_END_NAMESPACE()
80 
81 #endif
82 #endif // GLES_WGL_STATE_H