From deb3bac35d003a64a3e641a6b5fcf829732555ae Mon Sep 17 00:00:00 2001 From: Nick Fisher Date: Thu, 7 Mar 2024 18:27:09 +1100 Subject: [PATCH 1/2] create new WGL context when WGL_USE_BACKING_WINDOW is false --- windows/wgl_context.cpp | 87 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 87 insertions(+) diff --git a/windows/wgl_context.cpp b/windows/wgl_context.cpp index 44e7209e..b9d88737 100644 --- a/windows/wgl_context.cpp +++ b/windows/wgl_context.cpp @@ -11,6 +11,93 @@ namespace flutter_filament { WGLContext::WGLContext(flutter::PluginRegistrarWindows *pluginRegistrar, flutter::TextureRegistrar *textureRegistrar) : FlutterRenderContext(pluginRegistrar, textureRegistrar) { + + #if WGL_USE_BACKING_WINDOW + return; + #endif + + auto hwnd = pluginRegistrar->GetView()->GetNativeWindow(); + + HDC whdc = GetDC(hwnd); + if (whdc == NULL) { + std::cout << "No device context for temporary window" << std::endl; + return; + } + + std::cout << "No GL context exists, creating" << std::endl; + + PIXELFORMATDESCRIPTOR pfd = { + sizeof(PIXELFORMATDESCRIPTOR), + 1, + PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER, // Flags + PFD_TYPE_RGBA, // The kind of framebuffer. RGBA or palette. + 24, // Colordepth of the framebuffer. + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 16, // Number of bits for the depthbuffer + 0, // Number of bits for the stencilbuffer + 0, // Number of Aux buffers in the framebuffer. + PFD_MAIN_PLANE, + 0, + 0, + 0, + 0}; + + int pixelFormat = ChoosePixelFormat(whdc, &pfd); + SetPixelFormat(whdc, pixelFormat, &pfd); + + // We need a tmp context to retrieve and call wglCreateContextAttribsARB. + HGLRC tempContext = wglCreateContext(whdc); + if (!wglMakeCurrent(whdc, tempContext)) { + std::cout << "Failed to acquire temporary context" << std::endl; + return; + } + + GLenum err = glGetError(); + + if (err != GL_NO_ERROR) { + std::cout << "GL Error @ 455 %d" << std::endl; + return; + } + + PFNWGLCREATECONTEXTATTRIBSARBPROC wglCreateContextAttribs = nullptr; + + wglCreateContextAttribs = + (PFNWGLCREATECONTEXTATTRIBSARBPROC)wglGetProcAddress( + "wglCreateContextAttribsARB"); + + if (!wglCreateContextAttribs) { + std::cout << "Failed to resolve wglCreateContextAttribsARB" << std::endl; + return; + } + + for (int minor = 5; minor >= 1; minor--) { + std::vector mAttribs = {WGL_CONTEXT_MAJOR_VERSION_ARB, 4, + WGL_CONTEXT_MINOR_VERSION_ARB, minor, 0}; + _context = wglCreateContextAttribs(whdc, nullptr, mAttribs.data()); + if (_context) { + break; + } + } + + wglMakeCurrent(NULL, NULL); + wglDeleteContext(tempContext); + + if (!_context || !wglMakeCurrent(whdc, _context)) { + std::cout << "Failed to create OpenGL context." << std::endl; + return; + } } void WGLContext::ResizeRenderingSurface(uint32_t width, uint32_t height, uint32_t left, uint32_t top) { From 684d758bd939c37b162fd69d81a96db71127d4e4 Mon Sep 17 00:00:00 2001 From: Nick Fisher Date: Thu, 7 Mar 2024 18:30:09 +1100 Subject: [PATCH 2/2] set pixel ratio before creating rect --- lib/filament_controller_ffi.dart | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/filament_controller_ffi.dart b/lib/filament_controller_ffi.dart index 51f633d8..a5b140ed 100644 --- a/lib/filament_controller_ffi.dart +++ b/lib/filament_controller_ffi.dart @@ -146,12 +146,13 @@ class FilamentControllerFFI extends FilamentController { @override Future setDimensions(Rect rect, double pixelRatio) async { + _pixelRatio = pixelRatio; this._rect.value = Rect.fromLTWH( (rect.left * _pixelRatio).floor().toDouble(), rect.top * _pixelRatio.floor().toDouble(), (rect.width * _pixelRatio).ceil().toDouble(), (rect.height * _pixelRatio).ceil().toDouble()); - _pixelRatio = pixelRatio; + if (!_rectCompleter.isCompleted) { _rectCompleter.complete(this._rect.value); }