cleanup EGL/D3D texture rendering

This commit is contained in:
Nick Fisher
2024-03-07 12:19:01 +11:00
parent 8ef166d9b8
commit f0c48d7f0a
11 changed files with 81 additions and 50 deletions

View File

@@ -297,6 +297,8 @@ class FilamentControllerFFI extends FilamentController {
throw Exception("Failed to get resource loader"); throw Exception("Failed to get resource loader");
} }
var renderingSurface = await _createRenderingSurface();
if (Platform.isWindows && requiresTextureWidget) { if (Platform.isWindows && requiresTextureWidget) {
_driver = Pointer<Void>.fromAddress( _driver = Pointer<Void>.fromAddress(
await _channel.invokeMethod("getDriverPlatform")); await _channel.invokeMethod("getDriverPlatform"));
@@ -309,7 +311,6 @@ class FilamentControllerFFI extends FilamentController {
var renderCallbackOwner = var renderCallbackOwner =
Pointer<Void>.fromAddress(renderCallbackResult[1]); Pointer<Void>.fromAddress(renderCallbackResult[1]);
var renderingSurface = await _createRenderingSurface();
print("Got rendering surface"); print("Got rendering surface");

View File

@@ -5,7 +5,6 @@
#include <chrono> #include <chrono>
#include <thread> #include <thread>
#include <Commctrl.h>
#include <Windows.h> #include <Windows.h>
#include <dwmapi.h> #include <dwmapi.h>
#include <ShObjIdl.h> #include <ShObjIdl.h>

View File

@@ -1,8 +1,17 @@
#include "egl_context.h" #include "egl_context.h"
#define FILAMENT_USE_EXTERNAL_GLES3
#include <gl_headers.h>
#pragma comment(lib, "dwmapi.lib")
#pragma comment(lib, "comctl32.lib")
namespace flutter_filament { 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(); _platform = new filament::backend::PlatformEGL();
@@ -34,7 +43,7 @@ EGLContext::EGLContext(flutter::PluginRegistrarWindows* pluginRegistrar, flutter
dxgi->Release(); dxgi->Release();
if (!adapter_) { if (!adapter_) {
std::cout << "Failed to locate default D3D adapter" << std::endl; std::cout << "Failed to locate default D3D adapter" << std::endl;
return false; return;
} }
DXGI_ADAPTER_DESC adapter_desc_; DXGI_ADAPTER_DESC adapter_desc_;
@@ -49,7 +58,7 @@ EGLContext::EGLContext(flutter::PluginRegistrarWindows* pluginRegistrar, flutter
if (FAILED(hr)) { if (FAILED(hr)) {
std::cout << "Failed to create D3D device" << std::endl; std::cout << "Failed to create D3D device" << std::endl;
return false; return;
} }
Microsoft::WRL::ComPtr<IDXGIDevice> dxgi_device = nullptr; Microsoft::WRL::ComPtr<IDXGIDevice> dxgi_device = nullptr;
@@ -74,7 +83,7 @@ EGLContext::EGLContext(flutter::PluginRegistrarWindows* pluginRegistrar, flutter
EGLBoolean bindAPI = eglBindAPI(EGL_OPENGL_ES_API); EGLBoolean bindAPI = eglBindAPI(EGL_OPENGL_ES_API);
if (UTILS_UNLIKELY(!bindAPI)) { if (UTILS_UNLIKELY(!bindAPI)) {
std::cout << "eglBindAPI EGL_OPENGL_ES_API failed" << std::endl; std::cout << "eglBindAPI EGL_OPENGL_ES_API failed" << std::endl;
return false; return;
} }
_eglDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY); _eglDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
@@ -106,10 +115,10 @@ EGLContext::EGLContext(flutter::PluginRegistrarWindows* pluginRegistrar, flutter
if (UTILS_UNLIKELY(!initialized)) { if (UTILS_UNLIKELY(!initialized)) {
std::cout << "eglInitialize failed" << std::endl; std::cout << "eglInitialize failed" << std::endl;
return false; return;
} }
importGLESExtensionsEntryPoints(); glext::importGLESExtensionsEntryPoints();
EGLint configsCount; EGLint configsCount;
@@ -126,53 +135,65 @@ EGLContext::EGLContext(flutter::PluginRegistrarWindows* pluginRegistrar, flutter
// find an opaque config // find an opaque config
if (!eglChooseConfig(_eglDisplay, configAttribs, &_eglConfig, 1, if (!eglChooseConfig(_eglDisplay, configAttribs, &_eglConfig, 1,
&configsCount)) { &configsCount)) {
return false; return;
} }
_context = (void *)eglCreateContext(_eglDisplay, _eglConfig, EGL_NO_CONTEXT, auto ctx = eglCreateContext(_eglDisplay, _eglConfig, EGL_NO_CONTEXT,contextAttribs);
contextAttribs); _context = (void*)ctx;
if (UTILS_UNLIKELY(_context == EGL_NO_CONTEXT)) { if (UTILS_UNLIKELY(_context == EGL_NO_CONTEXT)) {
return false; return;
} }
} }
EGLContext::CreateRenderingSurface( void FlutterEGLContext::CreateRenderingSurface(
uint32_t width, uint32_t height, uint32_t width, uint32_t height,
std::unique_ptr<flutter::MethodResult<flutter::EncodableValue>> result, std::unique_ptr<flutter::MethodResult<flutter::EncodableValue>> result,
uint32_t left, uint32_t top uint32_t left, uint32_t top
) { ) {
importGLESExtensionsEntryPoints();
glext::importGLESExtensionsEntryPoints();
if(left != 0 || top != 0) { if(left != 0 || top != 0) {
result->Error("ERROR", result->Error("ERROR",
"Rendering with EGL uses a Texture render target/Flutter widget and does not need a window offset."); "Rendering with EGL uses a Texture render target/Flutter widget and does not need a window offset.");
return false; return;
} }
if (_active.get()) { if (_active.get()) {
result->Error("ERROR", result->Error("ERROR",
"Texture already exists. You must call destroyTexture before " "Texture already exists. You must call destroyTexture before "
"attempting to create a new one."); "attempting to create a new one.");
return false; return;
} }
_active = std::make_unique<FlutterAngleTexture>( std::unique_ptr<FlutterTextureBuffer> active = std::make_unique<FlutterAngleTexture>(
_pluginRegistrar, _textureRegistrar, std::move(result), width, height, _pluginRegistrar, _textureRegistrar, std::move(result), width, height,
_D3D11Device, _D3D11DeviceContext, _eglConfig, _eglDisplay, _context, _D3D11Device, _D3D11DeviceContext, _eglConfig, _eglDisplay, _context,
[=](size_t width, size_t height) { [=](size_t width, size_t height) {
std::cout << "RESIZE" << std::endl;
std::vector<int64_t> list; std::vector<int64_t> list;
list.push_back((int64_t)width); list.push_back((int64_t)width);
list.push_back((int64_t)height); list.push_back((int64_t)height);
auto val = std::make_unique<flutter::EncodableValue>(list); // auto val = std::make_unique<flutter::EncodableValue>(list);
this->_channel->InvokeMethod("resize", std::move(val), nullptr); // this->_channel->InvokeMethod("resize", std::move(val), nullptr);
}); });
_active = std::move(active);
return _active->flutterTextureId != -1;
} }
EGLContext::GetSharedContext() { void FlutterEGLContext::RenderCallback() {
return (void*)_eglContext; if(_active.get()) {
((FlutterAngleTexture*)_active.get())->RenderCallback();
}
}
void* FlutterEGLContext::GetSharedContext() {
return (void*)_context;
}
void* FlutterEGLContext::GetPlatform() {
return (void*)_platform;
} }
} }

View File

@@ -1,24 +1,35 @@
#ifndef _EGL_CONTEXT_H #ifndef _EGL_CONTEXT_H
#define _EGL_CONTEXT_H #define _EGL_CONTEXT_H
#include <Windows.h>
#include <flutter/method_channel.h>
#include <flutter/plugin_registrar_windows.h>
#include <flutter/standard_method_codec.h>
#include <flutter/texture_registrar.h>
#include "flutter_angle_texture.h" #include "flutter_angle_texture.h"
#include "backend/platforms/PlatformEGL.h" #include "backend/platforms/PlatformEGL.h"
#include "flutter_render_context.h"
namespace flutter_filament { namespace flutter_filament {
class EGLContext : public FlutterRenderingContext { class FlutterEGLContext : public FlutterRenderContext {
public: 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<flutter::MethodResult<flutter::EncodableValue>> result, uint32_t left, uint32_t top );
private: private:
EGLContext _context = NULL; void* _context = nullptr;
EGLConfig _eglConfig = NULL; EGLConfig _eglConfig = NULL;
EGLDisplay _eglDisplay = NULL; EGLDisplay _eglDisplay = NULL;
std::unique_ptr<FlutterAngleTexture> _active = nullptr;
std::unique_ptr<FlutterAngleTexture> _inactive = nullptr;
ID3D11Device* _D3D11Device = nullptr; ID3D11Device* _D3D11Device = nullptr;
ID3D11DeviceContext* _D3D11DeviceContext = nullptr; ID3D11DeviceContext* _D3D11DeviceContext = nullptr;
filament::backend::Platform* _platform = nullptr; filament::backend::Platform* _platform = nullptr;
} };
} }

View File

@@ -236,6 +236,7 @@ FlutterAngleTexture::FlutterAngleTexture(
resultList.push_back(flutter::EncodableValue(flutterTextureId)); resultList.push_back(flutter::EncodableValue(flutterTextureId));
resultList.push_back(flutter::EncodableValue((int64_t) nullptr)); resultList.push_back(flutter::EncodableValue((int64_t) nullptr));
resultList.push_back(flutter::EncodableValue(glTextureId)); resultList.push_back(flutter::EncodableValue(glTextureId));
resultList.push_back(flutter::EncodableValue((int64_t) eglContext));
result->Success(resultList); result->Success(resultList);
} }

View File

@@ -28,7 +28,7 @@ typedef uint32_t GLuint;
namespace flutter_filament { namespace flutter_filament {
class FlutterAngleTexture : FlutterTextureBuffer { class FlutterAngleTexture : public FlutterTextureBuffer {
public: public:
FlutterAngleTexture( FlutterAngleTexture(
flutter::PluginRegistrarWindows* pluginRegistrar, flutter::PluginRegistrarWindows* pluginRegistrar,
@@ -49,6 +49,7 @@ class FlutterAngleTexture : FlutterTextureBuffer {
GLuint glTextureId = 0; GLuint glTextureId = 0;
std::unique_ptr<flutter::TextureVariant> texture; std::unique_ptr<flutter::TextureVariant> texture;
int64_t flutterTextureId = 0;
private: private:
flutter::PluginRegistrarWindows* _pluginRegistrar; flutter::PluginRegistrarWindows* _pluginRegistrar;

View File

@@ -3,11 +3,7 @@
#include "flutter_filament_plugin.h" #include "flutter_filament_plugin.h"
// This must be included before many other Windows headers. #include <Windows.h>
#include <windows.h>
// For getPlatformVersion; remove unless needed for your plugin implementation.
#include <VersionHelpers.h>
#include <flutter/method_channel.h> #include <flutter/method_channel.h>
#include <flutter/plugin_registrar_windows.h> #include <flutter/plugin_registrar_windows.h>
@@ -18,7 +14,7 @@
#include <cstring> #include <cstring>
#include <filesystem> #include <filesystem>
#include <fstream> #include <fstream>
#include <future>
#include <iostream> #include <iostream>
#include <locale> #include <locale>
#include <map> #include <map>
@@ -31,11 +27,6 @@
#include "FlutterFilamentApi.h" #include "FlutterFilamentApi.h"
#include <Commctrl.h>
#include <Windows.h>
#include <dwmapi.h>
#include <wrl.h>
#include "flutter_render_context.h" #include "flutter_render_context.h"
#if USE_ANGLE #if USE_ANGLE
@@ -154,8 +145,12 @@ void render_callback(void *owner) {
void FlutterFilamentPlugin::RenderCallback() { void FlutterFilamentPlugin::RenderCallback() {
if (_context) { if (_context) {
auto flutterTextureId = _context->GetFlutterTextureId(); auto flutterTextureId = _context->GetFlutterTextureId();
if(flutterTextureId == -1) {
std::cout << "Bad texture" << std::endl;
return;
}
#ifdef USE_ANGLE #ifdef USE_ANGLE
_active->RenderCallback(); _context->RenderCallback();
#endif #endif
#if !WGL_USE_BACKING_WINDOW #if !WGL_USE_BACKING_WINDOW
_textureRegistrar->MarkTextureFrameAvailable(flutterTextureId); _textureRegistrar->MarkTextureFrameAvailable(flutterTextureId);
@@ -185,7 +180,7 @@ void FlutterFilamentPlugin::CreateTexture(
// this will be used to create a backing texture and passed to Filament // this will be used to create a backing texture and passed to Filament
if (!_context) { if (!_context) {
#ifdef USE_ANGLE #ifdef USE_ANGLE
_context = std::make_unique<EGLContext>(_pluginRegistrar); _context = std::make_unique<FlutterEGLContext>(_pluginRegistrar, _textureRegistrar);
#else #else
_context = std::make_unique<WGLContext>(_pluginRegistrar, _textureRegistrar); _context = std::make_unique<WGLContext>(_pluginRegistrar, _textureRegistrar);
#endif #endif
@@ -197,7 +192,6 @@ void FlutterFilamentPlugin::DestroyTexture(
const flutter::MethodCall<flutter::EncodableValue> &methodCall, const flutter::MethodCall<flutter::EncodableValue> &methodCall,
std::unique_ptr<flutter::MethodResult<flutter::EncodableValue>> result) { std::unique_ptr<flutter::MethodResult<flutter::EncodableValue>> result) {
const auto *flutterTextureId = std::get_if<int64_t>(methodCall.arguments()); const auto *flutterTextureId = std::get_if<int64_t>(methodCall.arguments());
if (!flutterTextureId) { if (!flutterTextureId) {
@@ -263,7 +257,7 @@ void FlutterFilamentPlugin::HandleMethodCall(
result->Success(resultList); result->Success(resultList);
} else if (methodCall.method_name() == "getDriverPlatform") { } else if (methodCall.method_name() == "getDriverPlatform") {
#ifdef USE_ANGLE #ifdef USE_ANGLE
result->Success(flutter::EncodableValue((int64_t)_platform)); result->Success(flutter::EncodableValue((int64_t)_context->GetPlatform()));
#else #else
result->Success(flutter::EncodableValue((int64_t) nullptr)); result->Success(flutter::EncodableValue((int64_t) nullptr));
#endif #endif

View File

@@ -16,7 +16,7 @@
#include "FlutterFilamentApi.h" #include "FlutterFilamentApi.h"
#if ANGLE #if USE_ANGLE
#include "egl_context.h" #include "egl_context.h"
#else #else
#include "wgl_context.h" #include "wgl_context.h"
@@ -60,7 +60,7 @@ public:
private: private:
#ifdef USE_ANGLE #ifdef USE_ANGLE
std::unique_ptr<EGLContext> _context = nullptr; std::unique_ptr<FlutterEGLContext> _context = nullptr;
#else #else
std::unique_ptr<WGLContext> _context = nullptr; std::unique_ptr<WGLContext> _context = nullptr;
#endif #endif

View File

@@ -12,6 +12,7 @@ namespace flutter_filament {
class FlutterRenderContext { class FlutterRenderContext {
public: public:
void CreateRenderingSurface(uint32_t width, uint32_t height, std::unique_ptr<flutter::MethodResult<flutter::EncodableValue>> result, uint32_t left, uint32_t top ); void CreateRenderingSurface(uint32_t width, uint32_t height, std::unique_ptr<flutter::MethodResult<flutter::EncodableValue>> result, uint32_t left, uint32_t top );
void DestroyTexture(std::unique_ptr<flutter::MethodResult<flutter::EncodableValue>> result) { void DestroyTexture(std::unique_ptr<flutter::MethodResult<flutter::EncodableValue>> result) {
@@ -33,11 +34,16 @@ namespace flutter_filament {
}); });
} }
int64_t GetFlutterTextureId() { int64_t GetFlutterTextureId() {
if(!_active) {
return -1;
}
return _active->flutterTextureId; return _active->flutterTextureId;
} }
void* sharedContext = nullptr; void* sharedContext = nullptr;
protected: protected:
FlutterRenderContext( flutter::PluginRegistrarWindows* pluginRegistrar, flutter::TextureRegistrar* textureRegistrar) : _pluginRegistrar(pluginRegistrar), _textureRegistrar(textureRegistrar) {};
flutter::PluginRegistrarWindows* _pluginRegistrar; flutter::PluginRegistrarWindows* _pluginRegistrar;
flutter::TextureRegistrar* _textureRegistrar; flutter::TextureRegistrar* _textureRegistrar;
std::unique_ptr<FlutterTextureBuffer> _active = nullptr; std::unique_ptr<FlutterTextureBuffer> _active = nullptr;

View File

@@ -10,7 +10,7 @@ namespace flutter_filament {
WGLContext::WGLContext(flutter::PluginRegistrarWindows *pluginRegistrar, WGLContext::WGLContext(flutter::PluginRegistrarWindows *pluginRegistrar,
flutter::TextureRegistrar *textureRegistrar) flutter::TextureRegistrar *textureRegistrar)
: _pluginRegistrar(pluginRegistrar), _textureRegistrar(textureRegistrar) { : FlutterRenderContext(pluginRegistrar, textureRegistrar) {
auto hwnd = pluginRegistrar->GetView()->GetNativeWindow(); auto hwnd = pluginRegistrar->GetView()->GetNativeWindow();

View File

@@ -20,9 +20,6 @@ namespace flutter_filament {
uint32_t width, uint32_t height, uint32_t left, uint32_t top uint32_t width, uint32_t height, uint32_t left, uint32_t top
); );
private: private:
flutter::PluginRegistrarWindows* _pluginRegistrar = nullptr;
flutter::TextureRegistrar* _textureRegistrar = nullptr;
HGLRC _context = NULL; HGLRC _context = NULL;
#if WGL_USE_BACKING_WINDOW #if WGL_USE_BACKING_WINDOW
std::unique_ptr<BackingWindow> _backingWindow = nullptr; std::unique_ptr<BackingWindow> _backingWindow = nullptr;