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 Direct3D feature level, such as 10_0 or 11_1.
40  int major;
41  int minor;
42 };
43 
44 // A manager for initializing ANGLE correctly and using it to create and
45 // destroy surfaces
46 class Manager {
47  public:
48  // Creates a manager.
49  //
50  // If |allow_inverted_surface| is true and ANGLE supports
51  // EGL_ANGLE_surface_orientation, window surfaces are created with an
52  // inverted Y axis so that the default framebuffer shares Impeller's
53  // top-left origin. See |surface_origin_is_top_left|.
54  //
55  // If |max_feature_level| is set, ANGLE's D3D11 renderer is limited to at
56  // most that feature level, even if the hardware supports a higher one. This
57  // is intended for testing: capping to 10_0 makes ANGLE expose only an
58  // OpenGL ES 2.0 context, emulating older GPUs on any machine.
59  static std::unique_ptr<Manager> Create(
60  GpuPreference gpu_preference,
61  bool allow_inverted_surface,
62  std::optional<D3DFeatureLevel> max_feature_level = std::nullopt);
63 
64  virtual ~Manager();
65 
66  // Whether the manager is currently valid.
67  bool IsValid() const;
68 
69  // Creates an EGL surface that can be used to render a Flutter view into a
70  // win32 HWND.
71  //
72  // After the surface is created, |WindowSurface::SetVSyncEnabled| should be
73  // called on a thread that can make the surface current.
74  //
75  // HWND is the window backing the surface. Width and height are the surface's
76  // physical pixel dimensions.
77  //
78  // Returns nullptr on failure.
79  virtual std::unique_ptr<WindowSurface> CreateWindowSurface(HWND hwnd,
80  size_t width,
81  size_t height);
82 
83  // Check if the current thread has a context bound.
84  bool HasContextCurrent();
85 
86  // Creates a |EGLSurface| from the provided handle.
87  EGLSurface CreateSurfaceFromHandle(EGLenum handle_type,
88  EGLClientBuffer handle,
89  const EGLint* attributes) const;
90 
91  // Gets the |EGLDisplay|.
92  EGLDisplay egl_display() const { return display_; };
93 
94  // Gets the |EGLConfig|.
95  EGLConfig egl_config() const { return config_; };
96 
97  // Whether the origin of the default framebuffer (framebuffer 0) is the
98  // top-left of the window rather than OpenGL's usual bottom-left.
99  //
100  // This is true when window surfaces were created with
101  // EGL_SURFACE_ORIENTATION_INVERT_Y_ANGLE. Content blitted or drawn into the
102  // default framebuffer must be stored top-down in that case.
103  virtual bool surface_origin_is_top_left() const;
104 
105  // Gets the |ID3D11Device| chosen by ANGLE.
106  bool GetDevice(ID3D11Device** device);
107 
108  // Get the EGL context used to render Flutter views.
109  virtual Context* render_context() const;
110 
111  // Get the EGL context used for async texture uploads.
112  virtual Context* resource_context() const;
113 
114  static std::optional<LUID> GetLowPowerGpuLuid();
115 
116  static std::optional<LUID> GetHighPerformanceGpuLuid();
117 
118  protected:
119  // Creates a new surface manager retaining reference to the passed-in target
120  // for the lifetime of the manager.
121  Manager(GpuPreference gpu_preference,
122  bool allow_inverted_surface,
123  std::optional<D3DFeatureLevel> max_feature_level = std::nullopt);
124 
125  private:
126  // Number of active instances of Manager
127  static int instance_count_;
128 
129  // Helper function to get GPU LUID by preference.
130  static std::optional<LUID> GetGpuLuidByPreference(
131  DXGI_GPU_PREFERENCE preference);
132 
133  // Initialize the EGL display.
134  bool InitializeDisplay(GpuPreference gpu_preference,
135  bool allow_inverted_surface,
136  std::optional<D3DFeatureLevel> max_feature_level);
137 
138  // Initialize the EGL configs.
139  bool InitializeConfig();
140 
141  // Initialize the EGL render and resource contexts.
142  bool InitializeContexts();
143 
144  // Initialize the D3D11 device.
145  bool InitializeDevice();
146 
147  void CleanUp();
148 
149  // Whether the manager was initialized successfully.
150  bool is_valid_ = false;
151 
152  // The EGL_SURFACE_ORIENTATION_ANGLE value to create window surfaces with.
153  //
154  // Either 0 or EGL_SURFACE_ORIENTATION_INVERT_Y_ANGLE. Non-zero only when the
155  // caller opted in and ANGLE advertised EGL_ANGLE_surface_orientation.
156  EGLint surface_orientation_ = 0;
157 
158  // EGL representation of native display.
159  EGLDisplay display_ = EGL_NO_DISPLAY;
160 
161  // EGL framebuffer configuration.
162  EGLConfig config_ = nullptr;
163 
164  // The EGL context used to render Flutter views.
165  std::unique_ptr<Context> render_context_;
166 
167  // The EGL context used for async texture uploads.
168  std::unique_ptr<Context> resource_context_;
169 
170  // The current D3D device.
171  Microsoft::WRL::ComPtr<ID3D11Device> resolved_device_ = nullptr;
172 
173  FML_DISALLOW_COPY_AND_ASSIGN(Manager);
174 };
175 
176 } // namespace egl
177 } // namespace flutter
178 
179 #endif // FLUTTER_SHELL_PLATFORM_WINDOWS_EGL_MANAGER_H_
virtual Context * resource_context() const
Definition: manager.cc:445
EGLConfig egl_config() const
Definition: manager.h:95
static std::unique_ptr< Manager > Create(GpuPreference gpu_preference, bool allow_inverted_surface, std::optional< D3DFeatureLevel > max_feature_level=std::nullopt)
Definition: manager.cc:61
virtual ~Manager()
Definition: manager.cc:95
virtual bool surface_origin_is_top_left() const
Definition: manager.cc:381
bool HasContextCurrent()
Definition: manager.cc:419
EGLSurface CreateSurfaceFromHandle(EGLenum handle_type, EGLClientBuffer handle, const EGLint *attributes) const
Definition: manager.cc:423
static std::optional< LUID > GetLowPowerGpuLuid()
Definition: manager.cc:482
virtual Context * render_context() const
Definition: manager.cc:441
static std::optional< LUID > GetHighPerformanceGpuLuid()
Definition: manager.cc:486
virtual std::unique_ptr< WindowSurface > CreateWindowSurface(HWND hwnd, size_t width, size_t height)
Definition: manager.cc:385
bool GetDevice(ID3D11Device **device)
Definition: manager.cc:430
Manager(GpuPreference gpu_preference, bool allow_inverted_surface, std::optional< D3DFeatureLevel > max_feature_level=std::nullopt)
Definition: manager.cc:74
EGLDisplay egl_display() const
Definition: manager.h:92
bool IsValid() const
Definition: manager.cc:377
std::optional< egl::D3DFeatureLevel > max_feature_level