| 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 // HWND is the window backing the surface. Width and height represent
37 // dimensions surface is created at.
38 //
39 // After the surface is created, |SetVSyncEnabled| should be called on a
40 // thread that can bind the |egl_context_|.
41 virtual bool CreateSurface(HWND hwnd, EGLint width, EGLint height);
42
43 // Resizes backing surface from current size to newly requested size
44 // based on width and height for the specific case when width and height do
45 // not match current surface dimensions. Target represents the visual entity
46 // to bind to.
47 //
48 // This binds |egl_context_| to the current thread.
49 virtual void ResizeSurface(HWND hwnd,
50 EGLint width,
51 EGLint height,
52 bool enable_vsync);
53
54 // queries EGL for the dimensions of surface in physical
55 // pixels returning width and height as out params.
56 void GetSurfaceDimensions(EGLint* width, EGLint* height);
57
58 // Releases the pass-in EGLSurface wrapping and backing resources if not null.
59 virtual void DestroySurface();
60
61 // Check if the current thread has a context bound.
62 bool HasContextCurrent();
63
64 // Binds |egl_context_| to the current rendering thread and to the draw and
65 // read surfaces returning a boolean result reflecting success.
66 virtual bool MakeCurrent();
67
68 // Unbinds the current EGL context from the current thread.
69 virtual bool ClearCurrent();
70
71 // Clears the |egl_context_| draw and read surfaces.
72 bool ClearContext();
73
74 // Binds egl_resource_context_ to the current rendering thread and to the draw
75 // and read surfaces returning a boolean result reflecting success.
76 bool MakeResourceCurrent();
77
78 // Swaps the front and back buffers of the DX11 swapchain backing surface if
79 // not null.
80 EGLBoolean SwapBuffers();
81
82 // Creates a |EGLSurface| from the provided handle.
83 EGLSurface CreateSurfaceFromHandle(EGLenum handle_type,
84 EGLClientBuffer handle,
85 const EGLint* attributes) const;
86
87 // Gets the |EGLDisplay|.
88 EGLDisplay egl_display() const { return egl_display_; };
89
90 // If enabled, makes the current surface's buffer swaps block until the
91 // v-blank.
92 //
93 // If disabled, allows one thread to swap multiple buffers per v-blank
94 // but can result in screen tearing if the system compositor is disabled.
95 //
96 // This binds |egl_context_| to the current thread and makes the render
97 // surface current.
98 virtual void SetVSyncEnabled(bool enabled);
99
100 // Gets the |ID3D11Device| chosen by ANGLE.
101 bool GetDevice(ID3D11Device** device);
102
103 protected:
104 // Creates a new surface manager retaining reference to the passed-in target
105 // for the lifetime of the manager.
106 explicit AngleSurfaceManager(bool enable_impeller);
107
108 private:
109 bool Initialize(bool enable_impeller);
110 void CleanUp();
111
112 // Attempts to initialize EGL using ANGLE.
113 bool InitializeEGL(
114 PFNEGLGETPLATFORMDISPLAYEXTPROC egl_get_platform_display_EXT,
115 const EGLint* config,
116 bool should_log);
117
118 // EGL representation of native display.
119 EGLDisplay egl_display_;
120
121 // EGL representation of current rendering context.
122 EGLContext egl_context_;
123
124 // EGL representation of current rendering context used for async texture
125 // uploads.
126 EGLContext egl_resource_context_;
127
128 // current frame buffer configuration.
129 EGLConfig egl_config_;
130
131 // State representing success or failure of display initialization used when
132 // creating surfaces.
133 bool initialize_succeeded_;
134
135 // Current render_surface that engine will draw into.
136 EGLSurface render_surface_ = EGL_NO_SURFACE;
137
138 // Requested dimensions for current surface
139 EGLint surface_width_ = 0;
140 EGLint surface_height_ = 0;
141
142 // The current D3D device.
143 Microsoft::WRL::ComPtr<ID3D11Device> resolved_device_;
144
145 // Number of active instances of AngleSurfaceManager
146 static int instance_count_;
147
148 FML_DISALLOW_COPY_AND_ASSIGN(AngleSurfaceManager);
149 };
150
151 } // namespace flutter
152
153 #endif // FLUTTER_SHELL_PLATFORM_WINDOWS_ANGLE_SURFACE_MANAGER_H_
flutter::AngleSurfaceManager::egl_display
EGLDisplay egl_display() const
Definition: angle_surface_manager.h:88
flutter::AngleSurfaceManager::ClearContext
bool ClearContext()
Definition: angle_surface_manager.cc:322
flutter::AngleSurfaceManager::CreateSurface
virtual bool CreateSurface(HWND hwnd, EGLint width, EGLint height)
Definition: angle_surface_manager.cc:232
flutter::AngleSurfaceManager::HasContextCurrent
bool HasContextCurrent()
Definition: angle_surface_manager.cc:308
flutter::AngleSurfaceManager::ClearCurrent
virtual 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
virtual 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:361
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::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:286
flutter::AngleSurfaceManager::ResizeSurface
virtual void ResizeSurface(HWND hwnd, EGLint width, EGLint height, bool enable_vsync)
Definition: angle_surface_manager.cc:262