diff --git a/lib/filament_controller_ffi.dart b/lib/filament_controller_ffi.dart index 5a5facc8..51f633d8 100644 --- a/lib/filament_controller_ffi.dart +++ b/lib/filament_controller_ffi.dart @@ -297,6 +297,8 @@ class FilamentControllerFFI extends FilamentController { throw Exception("Failed to get resource loader"); } + var renderingSurface = await _createRenderingSurface(); + if (Platform.isWindows && requiresTextureWidget) { _driver = Pointer.fromAddress( await _channel.invokeMethod("getDriverPlatform")); @@ -309,7 +311,6 @@ class FilamentControllerFFI extends FilamentController { var renderCallbackOwner = Pointer.fromAddress(renderCallbackResult[1]); - var renderingSurface = await _createRenderingSurface(); print("Got rendering surface"); diff --git a/windows/backing_window.cpp b/windows/backing_window.cpp index bc314c26..e9668489 100644 --- a/windows/backing_window.cpp +++ b/windows/backing_window.cpp @@ -5,7 +5,6 @@ #include #include -#include #include #include #include diff --git a/windows/egl_context.cpp b/windows/egl_context.cpp index 1e7a87e6..cec41498 100644 --- a/windows/egl_context.cpp +++ b/windows/egl_context.cpp @@ -1,8 +1,17 @@ + #include "egl_context.h" +#define FILAMENT_USE_EXTERNAL_GLES3 +#include + +#pragma comment(lib, "dwmapi.lib") +#pragma comment(lib, "comctl32.lib") + namespace flutter_filament { -EGLContext::EGLContext(flutter::PluginRegistrarWindows* pluginRegistrar, flutter::TextureRegistrar* textureRegistrar); { +FlutterEGLContext::FlutterEGLContext( + flutter::PluginRegistrarWindows* pluginRegistrar, + flutter::TextureRegistrar* textureRegistrar) : FlutterRenderContext(pluginRegistrar, textureRegistrar) { _platform = new filament::backend::PlatformEGL(); @@ -34,7 +43,7 @@ EGLContext::EGLContext(flutter::PluginRegistrarWindows* pluginRegistrar, flutter dxgi->Release(); if (!adapter_) { std::cout << "Failed to locate default D3D adapter" << std::endl; - return false; + return; } DXGI_ADAPTER_DESC adapter_desc_; @@ -49,7 +58,7 @@ EGLContext::EGLContext(flutter::PluginRegistrarWindows* pluginRegistrar, flutter if (FAILED(hr)) { std::cout << "Failed to create D3D device" << std::endl; - return false; + return; } Microsoft::WRL::ComPtr dxgi_device = nullptr; @@ -74,7 +83,7 @@ EGLContext::EGLContext(flutter::PluginRegistrarWindows* pluginRegistrar, flutter EGLBoolean bindAPI = eglBindAPI(EGL_OPENGL_ES_API); if (UTILS_UNLIKELY(!bindAPI)) { std::cout << "eglBindAPI EGL_OPENGL_ES_API failed" << std::endl; - return false; + return; } _eglDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY); @@ -106,10 +115,10 @@ EGLContext::EGLContext(flutter::PluginRegistrarWindows* pluginRegistrar, flutter if (UTILS_UNLIKELY(!initialized)) { std::cout << "eglInitialize failed" << std::endl; - return false; + return; } - importGLESExtensionsEntryPoints(); + glext::importGLESExtensionsEntryPoints(); EGLint configsCount; @@ -126,53 +135,65 @@ EGLContext::EGLContext(flutter::PluginRegistrarWindows* pluginRegistrar, flutter // find an opaque config if (!eglChooseConfig(_eglDisplay, configAttribs, &_eglConfig, 1, &configsCount)) { - return false; + return; } - _context = (void *)eglCreateContext(_eglDisplay, _eglConfig, EGL_NO_CONTEXT, - contextAttribs); + auto ctx = eglCreateContext(_eglDisplay, _eglConfig, EGL_NO_CONTEXT,contextAttribs); + _context = (void*)ctx; if (UTILS_UNLIKELY(_context == EGL_NO_CONTEXT)) { - return false; + return; } } -EGLContext::CreateRenderingSurface( +void FlutterEGLContext::CreateRenderingSurface( uint32_t width, uint32_t height, std::unique_ptr> result, uint32_t left, uint32_t top ) { - importGLESExtensionsEntryPoints(); + + glext::importGLESExtensionsEntryPoints(); if(left != 0 || top != 0) { result->Error("ERROR", "Rendering with EGL uses a Texture render target/Flutter widget and does not need a window offset."); - return false; + return; } if (_active.get()) { result->Error("ERROR", "Texture already exists. You must call destroyTexture before " "attempting to create a new one."); - return false; + return; } - _active = std::make_unique( + std::unique_ptr active = std::make_unique( _pluginRegistrar, _textureRegistrar, std::move(result), width, height, _D3D11Device, _D3D11DeviceContext, _eglConfig, _eglDisplay, _context, [=](size_t width, size_t height) { + std::cout << "RESIZE" << std::endl; std::vector list; list.push_back((int64_t)width); list.push_back((int64_t)height); - auto val = std::make_unique(list); - this->_channel->InvokeMethod("resize", std::move(val), nullptr); + // auto val = std::make_unique(list); + // this->_channel->InvokeMethod("resize", std::move(val), nullptr); }); + _active = std::move(active); - return _active->flutterTextureId != -1; } -EGLContext::GetSharedContext() { - return (void*)_eglContext; +void FlutterEGLContext::RenderCallback() { + if(_active.get()) { + ((FlutterAngleTexture*)_active.get())->RenderCallback(); + } +} + +void* FlutterEGLContext::GetSharedContext() { + return (void*)_context; +} + +void* FlutterEGLContext::GetPlatform() { + return (void*)_platform; } } \ No newline at end of file diff --git a/windows/egl_context.h b/windows/egl_context.h index 454cae0f..c77b7216 100644 --- a/windows/egl_context.h +++ b/windows/egl_context.h @@ -1,24 +1,35 @@ #ifndef _EGL_CONTEXT_H #define _EGL_CONTEXT_H +#include + +#include +#include +#include +#include + #include "flutter_angle_texture.h" #include "backend/platforms/PlatformEGL.h" +#include "flutter_render_context.h" namespace flutter_filament { -class EGLContext : public FlutterRenderingContext { +class FlutterEGLContext : public FlutterRenderContext { public: - EGLContext(flutter::PluginRegistrarWindows* pluginRegistrar, flutter::TextureRegistrar* textureRegistrar); + FlutterEGLContext(flutter::PluginRegistrarWindows* pluginRegistrar, flutter::TextureRegistrar* textureRegistrar); + void* GetSharedContext(); + void RenderCallback(); + void* GetPlatform(); + void CreateRenderingSurface(uint32_t width, uint32_t height, std::unique_ptr> result, uint32_t left, uint32_t top ); + private: - EGLContext _context = NULL; + void* _context = nullptr; EGLConfig _eglConfig = NULL; EGLDisplay _eglDisplay = NULL; - std::unique_ptr _active = nullptr; - std::unique_ptr _inactive = nullptr; ID3D11Device* _D3D11Device = nullptr; ID3D11DeviceContext* _D3D11DeviceContext = nullptr; filament::backend::Platform* _platform = nullptr; -} +}; } diff --git a/windows/flutter_angle_texture.cpp b/windows/flutter_angle_texture.cpp index 96e8d413..832e20e6 100644 --- a/windows/flutter_angle_texture.cpp +++ b/windows/flutter_angle_texture.cpp @@ -236,6 +236,7 @@ FlutterAngleTexture::FlutterAngleTexture( resultList.push_back(flutter::EncodableValue(flutterTextureId)); resultList.push_back(flutter::EncodableValue((int64_t) nullptr)); resultList.push_back(flutter::EncodableValue(glTextureId)); + resultList.push_back(flutter::EncodableValue((int64_t) eglContext)); result->Success(resultList); } diff --git a/windows/flutter_angle_texture.h b/windows/flutter_angle_texture.h index 479d0e1b..fd250887 100644 --- a/windows/flutter_angle_texture.h +++ b/windows/flutter_angle_texture.h @@ -28,7 +28,7 @@ typedef uint32_t GLuint; namespace flutter_filament { -class FlutterAngleTexture : FlutterTextureBuffer { +class FlutterAngleTexture : public FlutterTextureBuffer { public: FlutterAngleTexture( flutter::PluginRegistrarWindows* pluginRegistrar, @@ -49,6 +49,7 @@ class FlutterAngleTexture : FlutterTextureBuffer { GLuint glTextureId = 0; std::unique_ptr texture; + int64_t flutterTextureId = 0; private: flutter::PluginRegistrarWindows* _pluginRegistrar; diff --git a/windows/flutter_filament_plugin.cpp b/windows/flutter_filament_plugin.cpp index e9b0112c..3670da36 100644 --- a/windows/flutter_filament_plugin.cpp +++ b/windows/flutter_filament_plugin.cpp @@ -3,11 +3,7 @@ #include "flutter_filament_plugin.h" -// This must be included before many other Windows headers. -#include - -// For getPlatformVersion; remove unless needed for your plugin implementation. -#include +#include #include #include @@ -18,7 +14,7 @@ #include #include #include -#include + #include #include #include @@ -31,11 +27,6 @@ #include "FlutterFilamentApi.h" -#include -#include -#include -#include - #include "flutter_render_context.h" #if USE_ANGLE @@ -154,8 +145,12 @@ void render_callback(void *owner) { void FlutterFilamentPlugin::RenderCallback() { if (_context) { auto flutterTextureId = _context->GetFlutterTextureId(); + if(flutterTextureId == -1) { + std::cout << "Bad texture" << std::endl; + return; + } #ifdef USE_ANGLE - _active->RenderCallback(); + _context->RenderCallback(); #endif #if !WGL_USE_BACKING_WINDOW _textureRegistrar->MarkTextureFrameAvailable(flutterTextureId); @@ -185,7 +180,7 @@ void FlutterFilamentPlugin::CreateTexture( // this will be used to create a backing texture and passed to Filament if (!_context) { #ifdef USE_ANGLE - _context = std::make_unique(_pluginRegistrar); + _context = std::make_unique(_pluginRegistrar, _textureRegistrar); #else _context = std::make_unique(_pluginRegistrar, _textureRegistrar); #endif @@ -197,7 +192,6 @@ void FlutterFilamentPlugin::DestroyTexture( const flutter::MethodCall &methodCall, std::unique_ptr> result) { - const auto *flutterTextureId = std::get_if(methodCall.arguments()); if (!flutterTextureId) { @@ -263,7 +257,7 @@ void FlutterFilamentPlugin::HandleMethodCall( result->Success(resultList); } else if (methodCall.method_name() == "getDriverPlatform") { #ifdef USE_ANGLE - result->Success(flutter::EncodableValue((int64_t)_platform)); + result->Success(flutter::EncodableValue((int64_t)_context->GetPlatform())); #else result->Success(flutter::EncodableValue((int64_t) nullptr)); #endif diff --git a/windows/flutter_filament_plugin.h b/windows/flutter_filament_plugin.h index 95502014..3b918fc1 100644 --- a/windows/flutter_filament_plugin.h +++ b/windows/flutter_filament_plugin.h @@ -16,7 +16,7 @@ #include "FlutterFilamentApi.h" -#if ANGLE +#if USE_ANGLE #include "egl_context.h" #else #include "wgl_context.h" @@ -60,7 +60,7 @@ public: private: #ifdef USE_ANGLE - std::unique_ptr _context = nullptr; + std::unique_ptr _context = nullptr; #else std::unique_ptr _context = nullptr; #endif diff --git a/windows/flutter_render_context.h b/windows/flutter_render_context.h index f1ebd238..42e2fe70 100644 --- a/windows/flutter_render_context.h +++ b/windows/flutter_render_context.h @@ -12,6 +12,7 @@ namespace flutter_filament { class FlutterRenderContext { public: + void CreateRenderingSurface(uint32_t width, uint32_t height, std::unique_ptr> result, uint32_t left, uint32_t top ); void DestroyTexture(std::unique_ptr> result) { @@ -33,11 +34,16 @@ namespace flutter_filament { }); } int64_t GetFlutterTextureId() { + if(!_active) { + return -1; + } return _active->flutterTextureId; } void* sharedContext = nullptr; protected: + FlutterRenderContext( flutter::PluginRegistrarWindows* pluginRegistrar, flutter::TextureRegistrar* textureRegistrar) : _pluginRegistrar(pluginRegistrar), _textureRegistrar(textureRegistrar) {}; + flutter::PluginRegistrarWindows* _pluginRegistrar; flutter::TextureRegistrar* _textureRegistrar; std::unique_ptr _active = nullptr; diff --git a/windows/wgl_context.cpp b/windows/wgl_context.cpp index 6c404ff5..344f7ecd 100644 --- a/windows/wgl_context.cpp +++ b/windows/wgl_context.cpp @@ -10,7 +10,7 @@ namespace flutter_filament { WGLContext::WGLContext(flutter::PluginRegistrarWindows *pluginRegistrar, flutter::TextureRegistrar *textureRegistrar) - : _pluginRegistrar(pluginRegistrar), _textureRegistrar(textureRegistrar) { + : FlutterRenderContext(pluginRegistrar, textureRegistrar) { auto hwnd = pluginRegistrar->GetView()->GetNativeWindow(); diff --git a/windows/wgl_context.h b/windows/wgl_context.h index e08e5f3d..33406180 100644 --- a/windows/wgl_context.h +++ b/windows/wgl_context.h @@ -20,9 +20,6 @@ namespace flutter_filament { uint32_t width, uint32_t height, uint32_t left, uint32_t top ); private: - - flutter::PluginRegistrarWindows* _pluginRegistrar = nullptr; - flutter::TextureRegistrar* _textureRegistrar = nullptr; HGLRC _context = NULL; #if WGL_USE_BACKING_WINDOW std::unique_ptr _backingWindow = nullptr;