Flutter Windows Embedder
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_EGL_MANAGER_H_
6 #define FLUTTER_SHELL_PLATFORM_WINDOWS_EGL_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 <dxgi.h>
18 #include <dxgi1_6.h>
19 #include <windows.h>
20 #include <wrl/client.h>
21 #include <memory>
22 #include <optional>
23 
24 #include "flutter/fml/macros.h"
28 
29 namespace flutter {
30 namespace egl {
31 
32 enum class GpuPreference {
36 };
37 
38 // A manager for initializing ANGLE correctly and using it to create and
39 // destroy surfaces
40 class Manager {
41  public:
42  static std::unique_ptr<Manager> Create(GpuPreference gpu_preference);
43 
44  virtual ~Manager();
45 
46  // Whether the manager is currently valid.
47  bool IsValid() const;
48 
49  // Creates an EGL surface that can be used to render a Flutter view into a
50  // win32 HWND.
51  //
52  // After the surface is created, |WindowSurface::SetVSyncEnabled| should be
53  // called on a thread that can make the surface current.
54  //
55  // HWND is the window backing the surface. Width and height are the surface's
56  // physical pixel dimensions.
57  //
58  // Returns nullptr on failure.
59  virtual std::unique_ptr<WindowSurface> CreateWindowSurface(HWND hwnd,
60  size_t width,
61  size_t height);
62 
63  // Check if the current thread has a context bound.
64  bool HasContextCurrent();
65 
66  // Creates a |EGLSurface| from the provided handle.
67  EGLSurface CreateSurfaceFromHandle(EGLenum handle_type,
68  EGLClientBuffer handle,
69  const EGLint* attributes) const;
70 
71  // Gets the |EGLDisplay|.
72  EGLDisplay egl_display() const { return display_; };
73 
74  // Gets the |ID3D11Device| chosen by ANGLE.
75  bool GetDevice(ID3D11Device** device);
76 
77  // Get the EGL context used to render Flutter views.
78  virtual Context* render_context() const;
79 
80  // Get the EGL context used for async texture uploads.
81  virtual Context* resource_context() const;
82 
83  static std::optional<LUID> GetLowPowerGpuLuid();
84 
85  static std::optional<LUID> GetHighPerformanceGpuLuid();
86 
87  protected:
88  // Creates a new surface manager retaining reference to the passed-in target
89  // for the lifetime of the manager.
90  explicit Manager(GpuPreference gpu_preference);
91 
92  private:
93  // Number of active instances of Manager
94  static int instance_count_;
95 
96  // Helper function to get GPU LUID by preference.
97  static std::optional<LUID> GetGpuLuidByPreference(
98  DXGI_GPU_PREFERENCE preference);
99 
100  // Initialize the EGL display.
101  bool InitializeDisplay(GpuPreference gpu_preference);
102 
103  // Initialize the EGL configs.
104  bool InitializeConfig();
105 
106  // Initialize the EGL render and resource contexts.
107  bool InitializeContexts();
108 
109  // Initialize the D3D11 device.
110  bool InitializeDevice();
111 
112  void CleanUp();
113 
114  // Whether the manager was initialized successfully.
115  bool is_valid_ = false;
116 
117  // EGL representation of native display.
118  EGLDisplay display_ = EGL_NO_DISPLAY;
119 
120  // EGL framebuffer configuration.
121  EGLConfig config_ = nullptr;
122 
123  // The EGL context used to render Flutter views.
124  std::unique_ptr<Context> render_context_;
125 
126  // The EGL context used for async texture uploads.
127  std::unique_ptr<Context> resource_context_;
128 
129  // The current D3D device.
130  Microsoft::WRL::ComPtr<ID3D11Device> resolved_device_ = nullptr;
131 
132  FML_DISALLOW_COPY_AND_ASSIGN(Manager);
133 };
134 
135 } // namespace egl
136 } // namespace flutter
137 
138 #endif // FLUTTER_SHELL_PLATFORM_WINDOWS_EGL_MANAGER_H_
virtual Context * resource_context() const
Definition: manager.cc:339
Manager(GpuPreference gpu_preference)
Definition: manager.cc:26
virtual ~Manager()
Definition: manager.cc:44
bool HasContextCurrent()
Definition: manager.cc:313
EGLSurface CreateSurfaceFromHandle(EGLenum handle_type, EGLClientBuffer handle, const EGLint *attributes) const
Definition: manager.cc:317
static std::optional< LUID > GetLowPowerGpuLuid()
Definition: manager.cc:376
virtual Context * render_context() const
Definition: manager.cc:335
static std::optional< LUID > GetHighPerformanceGpuLuid()
Definition: manager.cc:380
static std::unique_ptr< Manager > Create(GpuPreference gpu_preference)
Definition: manager.cc:17
virtual std::unique_ptr< WindowSurface > CreateWindowSurface(HWND hwnd, size_t width, size_t height)
Definition: manager.cc:283
bool GetDevice(ID3D11Device **device)
Definition: manager.cc:324
EGLDisplay egl_display() const
Definition: manager.h:72
bool IsValid() const
Definition: manager.cc:279