10 #include "flutter/fml/logging.h"
20 bool HasExtension(std::string_view extensions, std::string_view
name) {
21 for (
size_t start = 0; start <= extensions.size();) {
22 const size_t space = extensions.find(
' ', start);
24 space == std::string_view::npos ? extensions.size() : space;
25 if (extensions.substr(start, end - start) ==
name) {
28 if (space == std::string_view::npos) {
39 std::vector<EGLint>& attributes) {
43 attributes.push_back(EGL_PLATFORM_ANGLE_MAX_VERSION_MAJOR_ANGLE);
45 attributes.push_back(EGL_PLATFORM_ANGLE_MAX_VERSION_MINOR_ANGLE);
52 attributes.push_back(EGL_PLATFORM_ANGLE_DISPLAY_KEY_ANGLE);
59 int Manager::instance_count_ = 0;
63 bool allow_inverted_surface,
65 std::unique_ptr<Manager> manager;
68 if (!manager->IsValid()) {
71 return std::move(manager);
75 bool allow_inverted_surface,
79 if (!InitializeDisplay(gpu_preference, allow_inverted_surface,
84 if (!InitializeConfig()) {
88 if (!InitializeContexts()) {
100 bool Manager::InitializeDisplay(
102 bool allow_inverted_surface,
107 std::optional<LUID> luid = std::nullopt;
108 switch (gpu_preference) {
132 const bool request_present_path_fast = !allow_inverted_surface;
137 auto build_d3d11_attributes = [&](
bool with_luid) {
138 std::vector<EGLint> attributes = {
139 EGL_PLATFORM_ANGLE_TYPE_ANGLE,
140 EGL_PLATFORM_ANGLE_TYPE_D3D11_ANGLE,
145 EGL_PLATFORM_ANGLE_ENABLE_AUTOMATIC_TRIM_ANGLE,
149 if (request_present_path_fast) {
152 attributes.push_back(EGL_EXPERIMENTAL_PRESENT_PATH_ANGLE);
153 attributes.push_back(EGL_EXPERIMENTAL_PRESENT_PATH_FAST_ANGLE);
158 attributes.push_back(EGL_PLATFORM_ANGLE_D3D_LUID_HIGH_ANGLE);
159 attributes.push_back(
static_cast<EGLint
>(luid->HighPart));
160 attributes.push_back(EGL_PLATFORM_ANGLE_D3D_LUID_LOW_ANGLE);
161 attributes.push_back(
static_cast<EGLint
>(luid->LowPart));
166 attributes.push_back(EGL_NONE);
172 const std::vector<EGLint> d3d11_display_attributes_with_luid =
173 luid.has_value() ? build_d3d11_attributes(
true)
174 : std::vector<EGLint>{};
176 const std::vector<EGLint> d3d11_display_attributes =
177 build_d3d11_attributes(
false);
181 const EGLint d3d11_fl_9_3_display_attributes[] = {
182 EGL_PLATFORM_ANGLE_TYPE_ANGLE,
183 EGL_PLATFORM_ANGLE_TYPE_D3D11_ANGLE,
184 EGL_PLATFORM_ANGLE_MAX_VERSION_MAJOR_ANGLE,
186 EGL_PLATFORM_ANGLE_MAX_VERSION_MINOR_ANGLE,
188 EGL_PLATFORM_ANGLE_ENABLE_AUTOMATIC_TRIM_ANGLE,
195 std::vector<EGLint> d3d11_warp_display_attributes = {
196 EGL_PLATFORM_ANGLE_TYPE_ANGLE,
197 EGL_PLATFORM_ANGLE_TYPE_D3D11_ANGLE,
198 EGL_PLATFORM_ANGLE_ENABLE_AUTOMATIC_TRIM_ANGLE,
202 d3d11_warp_display_attributes.push_back(EGL_NONE);
204 std::vector<const EGLint*> display_attributes_configs;
208 display_attributes_configs.push_back(
209 d3d11_display_attributes_with_luid.data());
211 display_attributes_configs.push_back(d3d11_display_attributes.data());
212 display_attributes_configs.push_back(d3d11_fl_9_3_display_attributes);
213 display_attributes_configs.push_back(d3d11_warp_display_attributes.data());
215 PFNEGLGETPLATFORMDISPLAYEXTPROC egl_get_platform_display_EXT =
216 reinterpret_cast<PFNEGLGETPLATFORMDISPLAYEXTPROC
>(
217 ::eglGetProcAddress(
"eglGetPlatformDisplayEXT"));
218 if (!egl_get_platform_display_EXT) {
219 LogEGLError(
"eglGetPlatformDisplayEXT not available");
225 for (
auto config : display_attributes_configs) {
226 bool is_last = (config == display_attributes_configs.back());
228 display_ = egl_get_platform_display_EXT(EGL_PLATFORM_ANGLE_ANGLE,
229 EGL_DEFAULT_DISPLAY, config);
231 if (display_ == EGL_NO_DISPLAY) {
233 LogEGLError(
"Failed to get a compatible EGLdisplay");
241 if (::eglInitialize(display_,
nullptr,
nullptr) == EGL_FALSE) {
251 if (allow_inverted_surface) {
252 const char* extensions = ::eglQueryString(display_, EGL_EXTENSIONS);
253 if (extensions !=
nullptr &&
254 HasExtension(extensions,
"EGL_ANGLE_surface_orientation")) {
255 surface_orientation_ = EGL_SURFACE_ORIENTATION_INVERT_Y_ANGLE;
260 FML_LOG(WARNING) <<
"EGL_ANGLE_surface_orientation is unavailable. "
261 "Falling back to a bottom-left surface origin.";
271 bool Manager::InitializeConfig() {
272 const EGLint config_attributes[] = {EGL_RED_SIZE, 8, EGL_GREEN_SIZE, 8,
273 EGL_BLUE_SIZE, 8, EGL_ALPHA_SIZE, 8,
274 EGL_DEPTH_SIZE, 8, EGL_STENCIL_SIZE, 8,
277 EGLint num_config = 0;
280 ::eglChooseConfig(display_, config_attributes, &config_, 1, &num_config);
282 if (result == EGL_TRUE && num_config > 0) {
290 bool Manager::InitializeContexts() {
291 const EGLint gles3_context_attributes[] = {EGL_CONTEXT_CLIENT_VERSION, 3,
293 const EGLint gles2_context_attributes[] = {EGL_CONTEXT_CLIENT_VERSION, 2,
298 const EGLint* context_attributes = gles3_context_attributes;
300 ::eglCreateContext(display_, config_, EGL_NO_CONTEXT, context_attributes);
304 context_attributes = gles2_context_attributes;
305 render_context = ::eglCreateContext(display_, config_, EGL_NO_CONTEXT,
310 LogEGLError(
"Failed to create EGL render context");
315 ::eglCreateContext(display_, config_,
render_context, context_attributes);
317 LogEGLError(
"Failed to create EGL resource context");
321 render_context_ = std::make_unique<Context>(display_,
render_context);
326 bool Manager::InitializeDevice() {
327 const auto query_display_attrib_EXT =
328 reinterpret_cast<PFNEGLQUERYDISPLAYATTRIBEXTPROC
>(
329 ::eglGetProcAddress(
"eglQueryDisplayAttribEXT"));
330 const auto query_device_attrib_EXT =
331 reinterpret_cast<PFNEGLQUERYDEVICEATTRIBEXTPROC
>(
332 ::eglGetProcAddress(
"eglQueryDeviceAttribEXT"));
334 if (query_display_attrib_EXT ==
nullptr ||
335 query_device_attrib_EXT ==
nullptr) {
339 EGLAttrib egl_device = 0;
340 EGLAttrib angle_device = 0;
342 auto result = query_display_attrib_EXT(display_, EGL_DEVICE_EXT, &egl_device);
343 if (result != EGL_TRUE) {
347 result = query_device_attrib_EXT(
reinterpret_cast<EGLDeviceEXT
>(egl_device),
348 EGL_D3D11_DEVICE_ANGLE, &angle_device);
349 if (result != EGL_TRUE) {
353 resolved_device_ =
reinterpret_cast<ID3D11Device*
>(angle_device);
357 void Manager::CleanUp() {
358 EGLBoolean result = EGL_FALSE;
361 resolved_device_.Reset();
364 render_context_.reset();
365 resource_context_.reset();
367 if (display_ != EGL_NO_DISPLAY) {
370 if (instance_count_ == 1) {
371 ::eglTerminate(display_);
373 display_ = EGL_NO_DISPLAY;
382 return surface_orientation_ == EGL_SURFACE_ORIENTATION_INVERT_Y_ANGLE;
388 if (!hwnd || !is_valid_) {
395 std::vector<EGLint> surface_attributes = {
396 EGL_FIXED_SIZE_ANGLE, EGL_TRUE, EGL_WIDTH,
397 static_cast<EGLint
>(width), EGL_HEIGHT,
static_cast<EGLint
>(height),
400 if (surface_orientation_ != 0) {
401 surface_attributes.push_back(EGL_SURFACE_ORIENTATION_ANGLE);
402 surface_attributes.push_back(surface_orientation_);
405 surface_attributes.push_back(EGL_NONE);
407 auto const surface = ::eglCreateWindowSurface(
408 display_, config_,
static_cast<EGLNativeWindowType
>(hwnd),
409 surface_attributes.data());
410 if (surface == EGL_NO_SURFACE) {
415 return std::make_unique<WindowSurface>(display_, render_context_->GetHandle(),
416 surface, width, height);
420 return ::eglGetCurrentContext() != EGL_NO_CONTEXT;
424 EGLClientBuffer handle,
425 const EGLint* attributes)
const {
426 return ::eglCreatePbufferFromClientBuffer(display_, handle_type, handle,
427 config_, attributes);
431 if (!resolved_device_) {
432 if (!InitializeDevice()) {
437 resolved_device_.CopyTo(device);
438 return (resolved_device_ !=
nullptr);
442 return render_context_.get();
446 return resource_context_.get();
449 std::optional<LUID> Manager::GetGpuLuidByPreference(
450 DXGI_GPU_PREFERENCE preference) {
451 Microsoft::WRL::ComPtr<IDXGIFactory1> factory1;
452 HRESULT hr = ::CreateDXGIFactory1(IID_PPV_ARGS(&factory1));
457 Microsoft::WRL::ComPtr<IDXGIFactory6> factory6;
458 hr = factory1->QueryInterface(IID_PPV_ARGS(&factory6));
465 Microsoft::WRL::ComPtr<IDXGIAdapter1> adapter;
466 hr = factory6->EnumAdapterByGpuPreference(0, preference,
467 IID_PPV_ARGS(&adapter));
468 if (FAILED(hr) || !adapter) {
473 DXGI_ADAPTER_DESC desc;
474 hr = adapter->GetDesc(&desc);
479 return desc.AdapterLuid;
483 return GetGpuLuidByPreference(DXGI_GPU_PREFERENCE_MINIMUM_POWER);
487 return GetGpuLuidByPreference(DXGI_GPU_PREFERENCE_HIGH_PERFORMANCE);
virtual Context * resource_context() const
static std::unique_ptr< Manager > Create(GpuPreference gpu_preference, bool allow_inverted_surface, std::optional< D3DFeatureLevel > max_feature_level=std::nullopt)
virtual bool surface_origin_is_top_left() const
EGLSurface CreateSurfaceFromHandle(EGLenum handle_type, EGLClientBuffer handle, const EGLint *attributes) const
static std::optional< LUID > GetLowPowerGpuLuid()
virtual Context * render_context() const
static std::optional< LUID > GetHighPerformanceGpuLuid()
virtual std::unique_ptr< WindowSurface > CreateWindowSurface(HWND hwnd, size_t width, size_t height)
bool GetDevice(ID3D11Device **device)
Manager(GpuPreference gpu_preference, bool allow_inverted_surface, std::optional< D3DFeatureLevel > max_feature_level=std::nullopt)
std::optional< egl::D3DFeatureLevel > max_feature_level
void LogEGLError(std::string_view message)
Log the last EGL error with an error message.
@ HighPerformancePreference