| Flutter Windows Embedder |

angle_surface_manager.h
Go to the documentation of this file.
1 // Copyright 2013 The Flutter Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #ifndef FLUTTER_SHELL_PLATFORM_WINDOWS_ANGLE_SURFACE_MANAGER_H_
6 #define FLUTTER_SHELL_PLATFORM_WINDOWS_ANGLE_SURFACE_MANAGER_H_
7
8 // OpenGL ES and EGL includes
9 #include <EGL/egl.h>
10 #include <EGL/eglext.h>
11 #include <EGL/eglplatform.h>
12 #include <GLES2/gl2.h>
13 #include <GLES2/gl2ext.h>
14
15 // Windows platform specific includes
16 #include <d3d11.h>
17 #include <windows.h>
18 #include <wrl/client.h>
19 #include <memory>
20
21 #include "flutter/fml/macros.h"
22 #include "flutter/shell/platform/windows/window_binding_handler.h"
23
24 namespace flutter {
25
26 // A manager for initializing ANGLE correctly and using it to create and
27 // destroy surfaces
28 class AngleSurfaceManager {
29 public:
30 static std::unique_ptr<AngleSurfaceManager> Create(bool enable_impeller);
31
32 virtual ~AngleSurfaceManager();
33
34 // Creates an EGLSurface wrapper and backing DirectX 11 SwapChain
35 // associated with window, in the appropriate format for display.
36 // Target represents the visual entity to bind to. Width and
37 // height represent dimensions surface is created at.
38 //
39 // This binds |egl_context_| to the current thread.
40 virtual bool CreateSurface(WindowsRenderTarget* render_target,
41 EGLint width,
42 EGLint height,
43 bool enable_vsync);
44
45 // Resizes backing surface from current size to newly requested size
46 // based on width and height for the specific case when width and height do
47 // not match current surface dimensions. Target represents the visual entity
48 // to bind to.
49 //
50 // This binds |egl_context_| to the current thread.
51 virtual void ResizeSurface(WindowsRenderTarget* render_target,
52 EGLint width,
53 EGLint height,
54 bool enable_vsync);
55
56 // queries EGL for the dimensions of surface in physical
57 // pixels returning width and height as out params.
58 void GetSurfaceDimensions(EGLint* width, EGLint* height);
59
60 // Releases the pass-in EGLSurface wrapping and backing resources if not null.
61 virtual void DestroySurface();
62
63 // Check if the current thread has a context bound.
64 bool HasContextCurrent();
65
66 // Binds |egl_context_| to the current rendering thread and to the draw and
67 // read surfaces returning a boolean result reflecting success.
68 bool MakeCurrent();
69
70 // Unbinds the current EGL context from the current thread.
71 bool ClearCurrent();
72
73 // Clears the |egl_context_| draw and read surfaces.
74 bool ClearContext();
75
76 // Binds egl_resource_context_ to the current rendering thread and to the draw
77 // and read surfaces returning a boolean result reflecting success.
78 bool MakeResourceCurrent();
79
80 // Swaps the front and back buffers of the DX11 swapchain backing surface if
81 // not null.
82 EGLBoolean SwapBuffers();
83
84 // Creates a |EGLSurface| from the provided handle.
85 EGLSurface CreateSurfaceFromHandle(EGLenum handle_type,
86 EGLClientBuffer handle,
87 const EGLint* attributes) const;
88
89 // Gets the |EGLDisplay|.
90 EGLDisplay egl_display() const { return egl_display_; };
91
92 // If enabled, makes the current surface's buffer swaps block until the
93 // v-blank.
94 virtual void SetVSyncEnabled(bool enabled);
95
96 // Gets the |ID3D11Device| chosen by ANGLE.
97 bool GetDevice(ID3D11Device** device);
98
99 protected:
100 // Creates a new surface manager retaining reference to the passed-in target
101 // for the lifetime of the manager.
102 explicit AngleSurfaceManager(bool enable_impeller);
103
104 private:
105 bool Initialize(bool enable_impeller);
106 void CleanUp();
107
108 // Attempts to initialize EGL using ANGLE.
109 bool InitializeEGL(
110 PFNEGLGETPLATFORMDISPLAYEXTPROC egl_get_platform_display_EXT,
111 const EGLint* config,
112 bool should_log);
113
114 // EGL representation of native display.
115 EGLDisplay egl_display_;
116
117 // EGL representation of current rendering context.
118 EGLContext egl_context_;
119
120 // EGL representation of current rendering context used for async texture
121 // uploads.
122 EGLContext egl_resource_context_;
123
124 // current frame buffer configuration.
125 EGLConfig egl_config_;
126
127 // State representing success or failure of display initialization used when
128 // creating surfaces.
129 bool initialize_succeeded_;
130
131 // Current render_surface that engine will draw into.
132 EGLSurface render_surface_ = EGL_NO_SURFACE;
133
134 // Requested dimensions for current surface
135 EGLint surface_width_ = 0;
136 EGLint surface_height_ = 0;
137
138 // The current D3D device.
139 Microsoft::WRL::ComPtr<ID3D11Device> resolved_device_;
140
141 // Number of active instances of AngleSurfaceManager
142 static int instance_count_;
143
144 FML_DISALLOW_COPY_AND_ASSIGN(AngleSurfaceManager);
145 };
146
147 } // namespace flutter
148
149 #endif // FLUTTER_SHELL_PLATFORM_WINDOWS_ANGLE_SURFACE_MANAGER_H_
flutter::AngleSurfaceManager::egl_display
EGLDisplay egl_display() const
Definition: angle_surface_manager.h:90
flutter::AngleSurfaceManager::ClearContext
bool ClearContext()
Definition: angle_surface_manager.cc:322
flutter::AngleSurfaceManager::HasContextCurrent
bool HasContextCurrent()
Definition: angle_surface_manager.cc:308
flutter::WindowsRenderTarget
std::variant< HWND > WindowsRenderTarget
Definition: window_binding_handler.h:44
flutter::AngleSurfaceManager::CreateSurface
virtual bool CreateSurface(WindowsRenderTarget *render_target, EGLint width, EGLint height, bool enable_vsync)
Definition: angle_surface_manager.cc:232
flutter::AngleSurfaceManager::ClearCurrent
bool ClearCurrent()
Definition: angle_surface_manager.cc:317
flutter::AngleSurfaceManager::AngleSurfaceManager
AngleSurfaceManager(bool enable_impeller)
Definition: angle_surface_manager.cc:33
flutter::AngleSurfaceManager
Definition: angle_surface_manager.h:28
flutter::AngleSurfaceManager::DestroySurface
virtual void DestroySurface()
Definition: angle_surface_manager.cc:301
window_binding_handler.h
flutter
Definition: accessibility_bridge_windows.cc:11
flutter::AngleSurfaceManager::MakeCurrent
bool MakeCurrent()
Definition: angle_surface_manager.cc:312
flutter::AngleSurfaceManager::~AngleSurfaceManager
virtual ~AngleSurfaceManager()
Definition: angle_surface_manager.cc:41
flutter::AngleSurfaceManager::MakeResourceCurrent
bool MakeResourceCurrent()
Definition: angle_surface_manager.cc:327
flutter::AngleSurfaceManager::GetDevice
bool GetDevice(ID3D11Device **device)
Definition: angle_surface_manager.cc:356
flutter::AngleSurfaceManager::SwapBuffers
EGLBoolean SwapBuffers()
Definition: angle_surface_manager.cc:332
flutter::AngleSurfaceManager::SetVSyncEnabled
virtual void SetVSyncEnabled(bool enabled)
Definition: angle_surface_manager.cc:344
flutter::AngleSurfaceManager::ResizeSurface
virtual void ResizeSurface(WindowsRenderTarget *render_target, EGLint width, EGLint height, bool enable_vsync)
Definition: angle_surface_manager.cc:268
flutter::AngleSurfaceManager::Create
static std::unique_ptr< AngleSurfaceManager > Create(bool enable_impeller)
Definition: angle_surface_manager.cc:23
flutter::AngleSurfaceManager::CreateSurfaceFromHandle
EGLSurface CreateSurfaceFromHandle(EGLenum handle_type, EGLClientBuffer handle, const EGLint *attributes) const
Definition: angle_surface_manager.cc:336
flutter::AngleSurfaceManager::GetSurfaceDimensions
void GetSurfaceDimensions(EGLint *width, EGLint *height)
Definition: angle_surface_manager.cc:287