split D3D/GLES texture creation

This commit is contained in:
Nick Fisher
2024-11-04 17:17:24 +08:00
parent f3e96fe94a
commit bdcbd90ec6
26 changed files with 897 additions and 646 deletions

View File

@@ -5,7 +5,6 @@ import 'package:thermion_dart/src/viewer/src/shared_types/view.dart' as t;
import 'package:thermion_flutter/src/widgets/src/resize_observer.dart';
import 'package:thermion_flutter/thermion_flutter.dart';
import 'package:thermion_flutter_platform_interface/thermion_flutter_texture.dart';
import 'package:vector_math/vector_math_64.dart' hide Colors;
class ThermionTextureWidget extends StatefulWidget {
///
@@ -52,7 +51,10 @@ class _ThermionTextureWidgetState extends State<ThermionTextureWidget> {
void dispose() {
super.dispose();
_views.remove(widget.view);
_texture?.destroy();
if(_texture != null) {
ThermionFlutterPlatform.instance.destroyTexture(_texture!);
}
_states.remove(this);
}
@@ -79,7 +81,10 @@ class _ThermionTextureWidgetState extends State<ThermionTextureWidget> {
"Target texture dimensions ${width}x${height} (pixel ratio : $dpr)");
_texture = await ThermionFlutterPlatform.instance
.createTexture(widget.view, width, height);
.createTexture(width, height);
await ThermionFlutterPlatform.instance
.bind(widget.view, _texture!);
_logger.info(
"Actual texture dimensions ${_texture!.width}x${_texture!.height} (pixel ratio : $dpr)");
@@ -109,7 +114,10 @@ class _ThermionTextureWidgetState extends State<ThermionTextureWidget> {
if (mounted) {
setState(() {});
}
await texture?.destroy();
if(texture != null) {
ThermionFlutterPlatform.instance.destroyTexture(texture);
}
_views.clear();
});
});
@@ -124,7 +132,7 @@ class _ThermionTextureWidgetState extends State<ThermionTextureWidget> {
///
/// Each instance of ThermionTextureWidget in the widget hierarchy must
/// call[markFrameAvailable] on every frame to notify Flutter that the content
/// call [markFrameAvailable] on every frame to notify Flutter that the content
/// of its backing texture has changed.
///
/// Calling [requestFrame] on [ThermionViewer], however, will render all
@@ -149,7 +157,9 @@ class _ThermionTextureWidgetState extends State<ThermionTextureWidget> {
await widget.viewer.requestFrame();
lastRender = d.inMilliseconds;
}
await _texture?.markFrameAvailable();
if(_texture != null) {
await ThermionFlutterPlatform.instance.markTextureFrameAvailable(_texture!);
}
_rendering = false;
}
_requestFrame();
@@ -190,12 +200,7 @@ class _ThermionTextureWidgetState extends State<ThermionTextureWidget> {
_logger.info(
"Resizing texture to dimensions ${newWidth}x${newHeight} (pixel ratio : $dpr)");
await _texture?.resize(
newWidth,
newHeight,
0,
0,
);
await ThermionFlutterPlatform.instance.resizeTexture(_texture!, newWidth, newHeight);
_logger.info(
"Resized texture to dimensions ${_texture!.width}x${_texture!.height} (pixel ratio : $dpr)");

View File

@@ -3,4 +3,5 @@ library thermion_flutter;
export 'src/thermion_flutter_plugin.dart';
export 'src/widgets/widgets.dart';
export 'package:thermion_flutter_platform_interface/thermion_flutter_platform_interface.dart';
export 'package:thermion_flutter_platform_interface/thermion_flutter_texture.dart';
export 'package:thermion_dart/thermion_dart.dart';

View File

@@ -24,6 +24,11 @@ dependencies:
logging: ^1.2.0
web: ^1.0.0
dependency_overrides:
thermion_flutter_ffi:
path: ../thermion_flutter_ffi
thermion_flutter_platform_interface:
path: ../thermion_flutter_platform_interface
dev_dependencies:
flutter_test:
sdk: flutter

View File

@@ -4,31 +4,16 @@ project(${PROJECT_NAME} LANGUAGES C CXX)
cmake_policy(VERSION 3.14...3.25)
# This value is used when generating builds using this plugin, so it must
# not be changed
set(PLUGIN_NAME "thermion_flutter_plugin")
# Any new source files that you add to the plugin should be added here.
list(APPEND PLUGIN_SOURCES
"thermion_flutter_plugin.cpp"
"thermion_flutter_plugin.h"
"flutter_egl_texture.cpp"
)
set(THERMION_EGL FALSE)
set(WGL_USE_BACKING_WINDOW FALSE)
add_subdirectory("rendering/egl")
if(THERMION_EGL)
add_compile_definitions(THERMION_EGL)
list(APPEND PLUGIN_SOURCES "flutter_angle_texture.cpp" "egl_context.cpp" )
else()
if(WGL_USE_BACKING_WINDOW)
add_compile_definitions(WGL_USE_BACKING_WINDOW)
endif()
list(APPEND PLUGIN_SOURCES "wgl_context.cpp" "opengl_texture_buffer.cpp" "backing_window.cpp")
endif()
# Define the plugin library target. Its name must not be changed (see comment
# on PLUGIN_NAME above).
add_library(${PLUGIN_NAME} SHARED
"include/thermion_flutter/thermion_flutter_plugin_c_api.h"
"thermion_flutter_plugin_c_api.cpp"
@@ -45,37 +30,12 @@ target_compile_features(${PLUGIN_NAME} PUBLIC cxx_std_20)
target_compile_definitions(${PLUGIN_NAME} PRIVATE FLUTTER_PLUGIN_IMPL)
target_include_directories(${PLUGIN_NAME} INTERFACE
"${CMAKE_CURRENT_SOURCE_DIR}/include"
"${CMAKE_CURRENT_SOURCE_DIR}"
)
include_directories(
"${CMAKE_SOURCE_DIR}/../../../../thermion_dart/native/include/filament"
"${CMAKE_SOURCE_DIR}/../../../../thermion_dart/native/include"
)
if(THERMION_EGL)
list(APPEND GL_LIBS
EGL
GLESv2
)
set(ANGLE_OR_OPENGL_DIR angle)
add_library(EGL SHARED IMPORTED)
set_property(TARGET EGL PROPERTY IMPORTED_IMPLIB_DEBUG "${CMAKE_SOURCE_DIR}/../../../../thermion_dart/native/lib/windows/x86_64/mdd/libEGL.dll.lib")
set_property(TARGET EGL PROPERTY IMPORTED_IMPLIB_PROFILE "${CMAKE_SOURCE_DIR}/../../../../thermion_dart/native/lib/windows/x86_64/mt/angle/libEGL.dll.lib")
set_property(TARGET EGL PROPERTY IMPORTED_IMPLIB_RELEASE "${CMAKE_SOURCE_DIR}/../../../../thermion_dart/native/lib/windows/x86_64/mt/angle/libEGL.dll.lib")
add_library(GLESv2 SHARED IMPORTED)
set_property(TARGET GLESv2 PROPERTY IMPORTED_IMPLIB_DEBUG "${CMAKE_SOURCE_DIR}/../../../../thermion_dart/native/lib/windows/x86_64/mdd/libGLESv2.dll.lib")
set_property(TARGET GLESv2 PROPERTY IMPORTED_IMPLIB_PROFILE "${CMAKE_SOURCE_DIR}/../../../../thermion_dart/native/lib/windows/x86_64/mt/angle/libGLESv2.dll.lib")
set_property(TARGET GLESv2 PROPERTY IMPORTED_IMPLIB_RELEASE "${CMAKE_SOURCE_DIR}/../../../../thermion_dart/native/lib/windows/x86_64/mt/angle/libGLESv2.dll.lib")
else()
list(APPEND GL_LIBS
opengl32
dwmapi
comctl32
)
set(ANGLE_OR_OPENGL_DIR opengl)
endif()
include_directories(
${CMAKE_CURRENT_SOURCE_DIR}/include
)
@@ -83,24 +43,19 @@ target_link_libraries(${PLUGIN_NAME} PRIVATE
flutter
flutter_wrapper_plugin
Shlwapi
${GL_LIBS}
thermion_egl
)
# List of absolute paths to libraries that should be bundled with the plugin
if(THERMION_EGL)
set(thermion_flutter_bundled_libraries
${CMAKE_CURRENT_SOURCE_DIR}/lib/Debug/angle/libEGL.dll
${CMAKE_CURRENT_SOURCE_DIR}/lib/Debug/angle/libGLESv2.dll
${CMAKE_CURRENT_SOURCE_DIR}/lib/Debug/libc++.dll
${CMAKE_CURRENT_SOURCE_DIR}/lib/Debug/third_party_abseil-cpp_absl.dll
${CMAKE_CURRENT_SOURCE_DIR}/lib/Debug/third_party_zlib.dll
${CMAKE_CURRENT_SOURCE_DIR}/thermion_dart.dll
PARENT_SCOPE
# Copy thermion_egl library to the Flutter build directory
add_custom_command(TARGET ${PLUGIN_NAME} POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_if_different
$<TARGET_FILE:thermion_egl>
$<TARGET_FILE_DIR:${PLUGIN_NAME}>
)
else()
set(thermion_flutter_bundled_libraries
${runner_BINARY_DIR}/../../../native_assets/windows/thermion_dart.dll
$<TARGET_FILE:thermion_egl>
PARENT_SCOPE
)
endif()

View File

@@ -0,0 +1,53 @@
#pragma once
#include <flutter/method_channel.h>
#include <flutter/plugin_registrar_windows.h>
#include <flutter/standard_method_codec.h>
#include <flutter/texture_registrar.h>
#include <flutter_texture_registrar.h>
#include "flutter_egl_texture.h"
namespace thermion::tflutter::windows
{
FlutterEGLTexture::FlutterEGLTexture(HANDLE d3dTexture2DHandle, uint32_t width, uint32_t height) : _width(width), _height(height)
{
_textureDescriptor = std::make_unique<FlutterDesktopGpuSurfaceDescriptor>();
_textureDescriptor->struct_size = sizeof(FlutterDesktopGpuSurfaceDescriptor);
_textureDescriptor->handle = d3dTexture2DHandle;
_textureDescriptor->width = _textureDescriptor->visible_width = width;
_textureDescriptor->height = _textureDescriptor->visible_height = height;
_textureDescriptor->release_context = nullptr;
_textureDescriptor->release_callback = [](void *release_context) {
};
_textureDescriptor->format = kFlutterDesktopPixelFormatBGRA8888;
_texture =
std::make_unique<::flutter::TextureVariant>(::flutter::GpuSurfaceTexture::GpuSurfaceTexture(
kFlutterDesktopGpuSurfaceTypeDxgiSharedHandle,
[&](size_t width, size_t height)
{
if (width != this->_width || height != this->_height)
{
//this->_onResizeRequested(width, height);
}
return _textureDescriptor.get();
}));
}
::flutter::TextureVariant* FlutterEGLTexture::GetFlutterTexture() {
return _texture.get();
}
void FlutterEGLTexture::SetFlutterTextureId(int64_t textureId) {
_flutterTextureId = textureId;
}
int64_t FlutterEGLTexture::GetFlutterTextureId()
{
return _flutterTextureId;
}
}

View File

@@ -0,0 +1,29 @@
#pragma once
#include <d3d.h>
#include <d3d11.h>
#include <memory>
#include <flutter/method_channel.h>
#include <flutter/plugin_registrar_windows.h>
#include <flutter/standard_method_codec.h>
#include <flutter/texture_registrar.h>
#include <flutter_texture_registrar.h>
namespace thermion::tflutter::windows {
class FlutterEGLTexture {
public:
FlutterEGLTexture(HANDLE d3dTexture2DHandle, uint32_t width, uint32_t height);
::flutter::TextureVariant* GetFlutterTexture();
int64_t GetFlutterTextureId();
void SetFlutterTextureId(int64_t textureId);
private:
uint32_t _width;
uint32_t _height;
std::unique_ptr<FlutterDesktopGpuSurfaceDescriptor> _textureDescriptor = nullptr;
std::unique_ptr<::flutter::TextureVariant> _texture;
int64_t _flutterTextureId = -1;
};
}

View File

@@ -4,8 +4,6 @@ project(${PROJECT_NAME} LANGUAGES C CXX)
cmake_policy(VERSION 3.14...3.25)
add_compile_definitions(THERMION_EGL)
set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON)
set(BUILD_SHARED_LIBS TRUE)
set(CMAKE_ENABLE_EXPORTS TRUE)
@@ -41,22 +39,3 @@ target_link_libraries(${PROJECT_NAME} PRIVATE
d3d11
)
# Test executable
add_executable(${PROJECT_NAME}_test
"main.cpp"
)
# Make sure the test depends on the library
add_dependencies(${PROJECT_NAME}_test ${PROJECT_NAME})
target_link_libraries(${PROJECT_NAME}_test PRIVATE
${PROJECT_NAME}
)
# Copy ANGLE DLLs to the output directory
add_custom_command(TARGET ${PROJECT_NAME}_test POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_if_different
"E:/angle/libEGL.dll"
"E:/angle/libGLESv2.dll"
"$<TARGET_FILE_DIR:${PROJECT_NAME}_test>"
)

View File

@@ -6,7 +6,7 @@
namespace thermion::windows::egl {
FlutterEGLContext::FlutterEGLContext() {
ThermionEGLContext::ThermionEGLContext() {
// D3D starts here
IDXGIAdapter *adapter_ = nullptr;
@@ -144,7 +144,7 @@ FlutterEGLContext::FlutterEGLContext() {
}
}
void FlutterEGLContext::CreateRenderingSurface(
EGLTexture* ThermionEGLContext::CreateRenderingSurface(
uint32_t width, uint32_t height,
uint32_t left, uint32_t top
) {
@@ -152,17 +152,16 @@ void FlutterEGLContext::CreateRenderingSurface(
// 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;
std::cout << "ERROR Rendering with EGL uses a Texture render target/Flutter widget and does not need a window offset." << std::endl;
return nullptr;
}
if (_active.get()) {
// result->Error("ERROR",
// "Texture already exists. You must call destroyTexture before "
// "attempting to create a new one.");
return;
}
//if (_active && _active.get()) {
// // result->Error("ERROR",
// // "Texture already exists. You must call destroyTexture before "
// // "attempting to create a new one.");
// return nullptr;
//}
_active = std::make_unique<EGLTexture>(
width, height,
@@ -176,17 +175,25 @@ void FlutterEGLContext::CreateRenderingSurface(
// this->_channel->InvokeMethod("resize", std::move(val), nullptr);
});
return _active.get();
}
void FlutterEGLContext::RenderCallback() {
if(_active.get()) {
((EGLTexture*)_active.get())->RenderCallback();
}
}
void* FlutterEGLContext::GetSharedContext() {
void* ThermionEGLContext::GetSharedContext() {
return (void*)_context;
}
void ThermionEGLContext::ResizeRenderingSurface(uint32_t width, uint32_t height, uint32_t left, uint32_t top) {
}
void ThermionEGLContext::DestroyRenderingSurface() {
}
EGLTexture *ThermionEGLContext::GetActiveTexture() {
return _active.get();
}
}

View File

@@ -7,12 +7,15 @@
namespace thermion::windows::egl {
class FlutterEGLContext {
class ThermionEGLContext {
public:
FlutterEGLContext();
ThermionEGLContext();
void* GetSharedContext();
void RenderCallback();
void CreateRenderingSurface(uint32_t width, uint32_t height, uint32_t left, uint32_t top);
EGLTexture * CreateRenderingSurface(uint32_t width, uint32_t height, uint32_t left, uint32_t top);
void DestroyRenderingSurface();
void ResizeRenderingSurface(uint32_t width, uint32_t height, uint32_t left, uint32_t top);
EGLTexture * GetActiveTexture();
private:
void* _context = nullptr;
@@ -20,7 +23,7 @@ private:
EGLDisplay _eglDisplay = NULL;
ID3D11Device* _D3D11Device = nullptr;
ID3D11DeviceContext* _D3D11DeviceContext = nullptr;
std::unique_ptr<EGLTexture> _active;
std::unique_ptr<EGLTexture> _active = nullptr;
};
}

View File

@@ -59,21 +59,25 @@ static void logEglError(const char *name) noexcept {
std::cout << name << " failed with " << err << std::endl;
}
void EGLTexture::RenderCallback() {
glFinish();
_D3D11DeviceContext->Flush();
void EGLTexture::Flush() {
// glFlush(); // Ensure GL commands are completed
// _D3D11DeviceContext->Flush();
}
HANDLE EGLTexture::GetTextureHandle() {
return _d3dTexture2DHandle;
}
EGLTexture::~EGLTexture() {
if (_eglDisplay != EGL_NO_DISPLAY && _eglSurface != EGL_NO_SURFACE) {
eglReleaseTexImage(_eglDisplay, _eglSurface, EGL_BACK_BUFFER);
}
auto success = eglDestroySurface(this->_eglDisplay, this->_eglSurface);
if(success != EGL_TRUE) {
std::cout << "Failed to destroy EGL Surface" << std::endl;
}
// if (_eglDisplay != EGL_NO_DISPLAY && _eglSurface != EGL_NO_SURFACE) {
// eglReleaseTexImage(_eglDisplay, _eglSurface, EGL_BACK_BUFFER);
// }
// auto success = eglDestroySurface(this->_eglDisplay, this->_eglSurface);
// if(success != EGL_TRUE) {
// std::cout << "Failed to destroy EGL Surface" << std::endl;
// }
_d3dTexture2D->Release();
glDeleteTextures(1, &this->glTextureId);
// glDeleteTextures(1, &this->glTextureId);
}
EGLTexture::EGLTexture(
@@ -104,7 +108,7 @@ EGLTexture::EGLTexture(
auto hr = _D3D11Device->CreateTexture2D(&d3d11_texture2D_desc, nullptr,
&_d3dTexture2D);
if FAILED (hr) {
// result->Error("ERROR", "Failed to create D3D texture", nullptr);
std::cout << "Failed to create D3D texture" << std::endl;
return;
;
}
@@ -112,21 +116,39 @@ EGLTexture::EGLTexture(
hr = _d3dTexture2D.As(&resource);
if FAILED (hr) {
// result->Error("ERROR", "Failed to create D3D texture", nullptr);
std::cout << "Failed to create D3D texture" << std::endl;
return;
;
}
hr = resource->GetSharedHandle(&_d3dTexture2DHandle);
if FAILED (hr) {
// result->Error("ERROR",
// "Failed to get shared handle to external D3D texture",
// nullptr);
std::cout << "Failed to get shared handle to external D3D texture" << std::endl;
return;
;
}
_d3dTexture2D->AddRef();
std::cout << "Created external D3D texture" << std::endl;
std::cout << "Created external D3D texture " << width << "x" << height << std::endl;
// Create render target view of the texture
ID3D11RenderTargetView* rtv = nullptr;
D3D11_RENDER_TARGET_VIEW_DESC rtvDesc = {};
rtvDesc.Format = DXGI_FORMAT_B8G8R8A8_UNORM;
rtvDesc.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2D;
rtvDesc.Texture2D.MipSlice = 0;
hr = _D3D11Device->CreateRenderTargetView(_d3dTexture2D.Get(), &rtvDesc, &rtv);
if (FAILED(hr)) {
std::cout << "Failed to create render target view" << std::endl;
return;
}
// Clear the texture to blue
float blueColor[4] = { 0.0f, 0.0f, 1.0f, 1.0f }; // RGBA
_D3D11DeviceContext->ClearRenderTargetView(rtv, blueColor);
_D3D11DeviceContext->Flush();
std::cout << "Filled D3D texture blue" << std::endl;
EGLint pbufferAttribs[] = {
EGL_WIDTH, width, EGL_HEIGHT, height,
@@ -144,25 +166,168 @@ EGLTexture::EGLTexture(
return;
}
glGenTextures(1, &glTextureId);
/******************
*
*
* THis is working
*
*
*/
// // Clear to purple
// glClearColor(0.5f, 0.0f, 0.5f, 1.0f);
// glClear(GL_COLOR_BUFFER_BIT);
if (glTextureId == 0) {
std::cout
<< "Failed to generate OpenGL texture for ANGLE, OpenGL err was %d",
glGetError();
// // Present the surface
// eglSwapBuffers(_eglDisplay, _eglSurface);
// // Synchronize
// glFlush();
if (!eglMakeCurrent(_eglDisplay, _eglSurface, _eglSurface, _eglContext)) {
logEglError("eglMakeCurrent");
return;
}
glBindTexture(GL_TEXTURE_2D, glTextureId);
eglBindTexImage(_eglDisplay, _eglSurface, EGL_BACK_BUFFER);
// Create and setup shaders for rendering the texture
const char* vertexShaderSource = R"(#version 300 es
precision mediump float;
in vec2 position;
in vec2 texcoord;
out vec2 v_texcoord;
void main() {
gl_Position = vec4(position, 0.0, 1.0);
v_texcoord = texcoord;
}
)";
const char* fragmentShaderSource = R"(#version 300 es
precision mediump float;
in vec2 v_texcoord;
uniform sampler2D u_texture;
out vec4 fragColor;
void main() {
fragColor = texture(u_texture, v_texcoord);
}
)";
// Create and compile vertex shader
GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertexShader, 1, &vertexShaderSource, nullptr);
glCompileShader(vertexShader);
// Check vertex shader compilation
GLint success;
GLchar infoLog[512];
glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success);
if (!success) {
glGetShaderInfoLog(vertexShader, 512, nullptr, infoLog);
std::cout << "Vertex shader compilation failed:\n" << infoLog << std::endl;
}
// Create and compile fragment shader
GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragmentShader, 1, &fragmentShaderSource, nullptr);
glCompileShader(fragmentShader);
// Check fragment shader compilation
glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &success);
if (!success) {
glGetShaderInfoLog(fragmentShader, 512, nullptr, infoLog);
std::cout << "Fragment shader compilation failed:\n" << infoLog << std::endl;
}
// Create shader program
GLuint shaderProgram = glCreateProgram();
glAttachShader(shaderProgram, vertexShader);
glAttachShader(shaderProgram, fragmentShader);
glLinkProgram(shaderProgram);
// Check program linking
glGetProgramiv(shaderProgram, GL_LINK_STATUS, &success);
if (!success) {
glGetProgramInfoLog(shaderProgram, 512, nullptr, infoLog);
std::cout << "Shader program linking failed:\n" << infoLog << std::endl;
}
// Create the source texture
GLuint sourceTexture;
glGenTextures(1, &sourceTexture);
glBindTexture(GL_TEXTURE_2D, sourceTexture);
// Fill texture with purple color
uint8_t purplePixels[] = {
128, 0, 128, 255 // Single purple pixel (RGBA)
};
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 1, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, purplePixels);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
// clearGlError
GLenum const error = glGetError();
if (error != GL_NO_ERROR) {
std::cout << "Ignoring pending GL error " << error << std::endl;
// Create vertex buffer for fullscreen quad
float vertices[] = {
// Position // Texcoords
-1.0f, -1.0f, 0.0f, 0.0f, // Bottom left
1.0f, -1.0f, 1.0f, 0.0f, // Bottom right
-1.0f, 1.0f, 0.0f, 1.0f, // Top left
1.0f, 1.0f, 1.0f, 1.0f // Top right
};
GLuint vbo;
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
// Setup vertex attributes
glUseProgram(shaderProgram);
GLint posAttrib = glGetAttribLocation(shaderProgram, "position");
GLint texAttrib = glGetAttribLocation(shaderProgram, "texcoord");
if (posAttrib < 0 || texAttrib < 0) {
std::cout << "Failed to get attribute locations. position: " << posAttrib
<< " texcoord: " << texAttrib << std::endl;
}
glEnableVertexAttribArray(posAttrib);
glEnableVertexAttribArray(texAttrib);
glVertexAttribPointer(posAttrib, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(float), 0);
glVertexAttribPointer(texAttrib, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(float), (void*)(2 * sizeof(float)));
// Set texture uniform
GLint texUniform = glGetUniformLocation(shaderProgram, "u_texture");
glUniform1i(texUniform, 0); // Use texture unit 0
// Clear and render
glViewport(0, 0, width, height);
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
// Draw the fullscreen quad
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
// Present the result
eglSwapBuffers(_eglDisplay, _eglSurface);
glFlush();
// // Cleanup
// glDeleteBuffers(1, &vbo);
// glDeleteProgram(shaderProgram);
// glDeleteShader(vertexShader);
// glDeleteShader(fragmentShader);
// glDeleteTextures(1, &sourceTexture);
// Check for errors
GLenum error = glGetError();
if (error != GL_NO_ERROR) {
std::cout << "GL error after rendering: " << error << std::endl;
}
std::cout << "FINISHED TEXTURE CREATION AND RENDERING" << std::endl;
_D3D11DeviceContext->Flush();
char const *version;
version = (char const *)glGetString(GL_VERSION);
@@ -172,27 +337,127 @@ EGLTexture::EGLTexture(
glGetIntegerv(GL_MAJOR_VERSION, &major);
glGetIntegerv(GL_MINOR_VERSION, &minor);
// _textureDescriptor = std::make_unique<FlutterDesktopGpuSurfaceDescriptor>();
// _textureDescriptor->struct_size = sizeof(FlutterDesktopGpuSurfaceDescriptor);
// _textureDescriptor->handle = _d3dTexture2DHandle;
// _textureDescriptor->width = _textureDescriptor->visible_width = width;
// _textureDescriptor->height = _textureDescriptor->visible_height = height;
// _textureDescriptor->release_context = nullptr;
// _textureDescriptor->release_callback = [](void *release_context) {
// };
// _textureDescriptor->format = kFlutterDesktopPixelFormatBGRA8888;
// texture =
// std::make_unique<flutter::TextureVariant>(flutter::GpuSurfaceTexture(
// kFlutterDesktopGpuSurfaceTypeDxgiSharedHandle,
// [&](size_t width, size_t height) {
// if(width != this->_width || height != this->_height) {
// this->_onResizeRequested(width, height);
// }
// return _textureDescriptor.get();
// }));
std::cout << "FINISHED TEXTURE CREATION" << std::endl;
}
void EGLTexture::FillBlueAndSaveToBMP(const char* filename) {
// Create render target view of the texture
ID3D11RenderTargetView* rtv = nullptr;
D3D11_RENDER_TARGET_VIEW_DESC rtvDesc = {};
rtvDesc.Format = DXGI_FORMAT_B8G8R8A8_UNORM;
rtvDesc.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2D;
rtvDesc.Texture2D.MipSlice = 0;
HRESULT hr = _D3D11Device->CreateRenderTargetView(_d3dTexture2D.Get(), &rtvDesc, &rtv);
if (FAILED(hr)) {
std::cout << "Failed to create render target view" << std::endl;
return;
}
// Clear the texture to blue
float blueColor[4] = { 0.0f, 0.0f, 1.0f, 1.0f }; // RGBA
_D3D11DeviceContext->ClearRenderTargetView(rtv, blueColor);
// Create staging texture for CPU read access
D3D11_TEXTURE2D_DESC stagingDesc = {};
_d3dTexture2D->GetDesc(&stagingDesc);
stagingDesc.Usage = D3D11_USAGE_STAGING;
stagingDesc.CPUAccessFlags = D3D11_CPU_ACCESS_READ;
stagingDesc.BindFlags = 0;
stagingDesc.MiscFlags = 0;
ID3D11Texture2D* stagingTexture = nullptr;
hr = _D3D11Device->CreateTexture2D(&stagingDesc, nullptr, &stagingTexture);
if (FAILED(hr)) {
rtv->Release();
std::cout << "Failed to create staging texture" << std::endl;
return;
}
// Copy to staging texture
_D3D11DeviceContext->CopyResource(stagingTexture, _d3dTexture2D.Get());
// Save to BMP
bool success = SaveTextureAsBMP(stagingTexture, filename);
// Cleanup
stagingTexture->Release();
rtv->Release();
if (success) {
std::cout << "Successfully saved texture to " << filename << std::endl;
}
}
bool EGLTexture::SaveTextureAsBMP(ID3D11Texture2D* texture, const char* filename) {
D3D11_TEXTURE2D_DESC desc;
texture->GetDesc(&desc);
// Map texture to get pixel data
D3D11_MAPPED_SUBRESOURCE mappedResource;
HRESULT hr = _D3D11DeviceContext->Map(texture, 0, D3D11_MAP_READ, 0, &mappedResource);
if (FAILED(hr)) {
std::cout << "Failed to map texture" << std::endl;
return false;
}
// BMP file header
#pragma pack(push, 1)
struct BMPHeader {
uint16_t signature;
uint32_t fileSize;
uint32_t reserved;
uint32_t dataOffset;
uint32_t headerSize;
int32_t width;
int32_t height;
uint16_t planes;
uint16_t bitsPerPixel;
uint32_t compression;
uint32_t imageSize;
int32_t xPixelsPerMeter;
int32_t yPixelsPerMeter;
uint32_t totalColors;
uint32_t importantColors;
};
#pragma pack(pop)
// Create and fill header
BMPHeader header = {};
header.signature = 0x4D42; // 'BM'
header.fileSize = sizeof(BMPHeader) + desc.Width * desc.Height * 4;
header.dataOffset = sizeof(BMPHeader);
header.headerSize = 40;
header.width = desc.Width;
header.height = desc.Height;
header.planes = 1;
header.bitsPerPixel = 32;
header.compression = 0;
header.imageSize = desc.Width * desc.Height * 4;
header.xPixelsPerMeter = 2835; // 72 DPI
header.yPixelsPerMeter = 2835; // 72 DPI
// Write to file
FILE* file = nullptr;
fopen_s(&file, filename, "wb");
if (!file) {
_D3D11DeviceContext->Unmap(texture, 0);
return false;
}
fwrite(&header, sizeof(header), 1, file);
// Write pixel data (need to flip rows as BMP is bottom-up)
uint8_t* srcData = reinterpret_cast<uint8_t*>(mappedResource.pData);
for (int y = desc.Height - 1; y >= 0; y--) {
uint8_t* rowData = srcData + y * mappedResource.RowPitch;
fwrite(rowData, desc.Width * 4, 1, file);
}
fclose(file);
_D3D11DeviceContext->Unmap(texture, 0);
return true;
}
} // namespace thermion_flutter

View File

@@ -34,9 +34,14 @@ class EGLTexture {
);
~EGLTexture();
void RenderCallback();
void Flush();
HANDLE GetTextureHandle();
GLuint glTextureId = 0;
void FillBlueAndSaveToBMP(const char* filename);
bool SaveTextureAsBMP(ID3D11Texture2D* texture, const char* filename);
private:
bool _error = false;
@@ -57,8 +62,6 @@ class EGLTexture {
EGLConfig _eglConfig = EGL_NO_CONFIG_KHR;
EGLSurface _eglSurface = EGL_NO_SURFACE;
// std::unique_ptr<FlutterDesktopGpuSurfaceDescriptor> _textureDescriptor = nullptr;
};
}

View File

@@ -6,14 +6,14 @@
int main() {
std::cout << "Initializing EGL Context..." << std::endl;
thermion::windows::egl::FlutterEGLContext context;
thermion::windows::egl::ThermionEGLContext context;
// Create a rendering surface
const uint32_t width = 800;
const uint32_t height = 600;
std::cout << "Creating rendering surface " << width << "x" << height << std::endl;
context.CreateRenderingSurface(width, height, 0, 0);
auto *texture = context.CreateRenderingSurface(width, height, 0, 0);
void* sharedContext = context.GetSharedContext();
if (sharedContext) {
@@ -23,13 +23,10 @@ int main() {
return 1;
}
// Run a simple render loop
std::cout << "Starting render loop..." << std::endl;
for (int i = 0; i < 10; i++) {
context.RenderCallback();
std::cout << "Rendered frame " << i + 1 << std::endl;
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
// Fill with blue and save
texture->FillBlueAndSaveToBMP("output.bmp");
std::cout << "Saved blue texture to output.bmp" << std::endl;
std::cout << "EGL Context demo completed" << std::endl;
return 0;

View File

@@ -1,23 +0,0 @@
#pragma once
#include "flutter_texture_buffer.h"
namespace thermion_flutter {
class FlutterRenderContext {
public:
void CreateRenderingSurface(uint32_t width, uint32_t height, uint32_t left, uint32_t top);
void DestroyRenderingSurface();
void *GetSharedContext();
FlutterTextureBuffer GetActiveTexture() {
return _active->get();
}
protected:
FlutterRenderContext();
std::unique_ptr<FlutterTextureBuffer> _active = nullptr;
std::unique_ptr<FlutterTextureBuffer> _inactive = nullptr;
};
}
#endif

View File

@@ -1,25 +0,0 @@
#ifndef _FLUTTER_TEXTURE_BUFFER_H
#define _FLUTTER_TEXTURE_BUFFER_H
#include <flutter/method_channel.h>
#include <flutter/plugin_registrar_windows.h>
#include <flutter/standard_method_codec.h>
#include <flutter/texture_registrar.h>
namespace thermion_flutter {
class FlutterTextureBuffer {
public:
flutter::TextureVariant GetTexture() {
return texture->get();
}
void RegisterFlutterTextureId(int64_t flutterTextureId);
int64_t GetFlutterTextureId();
private:
int64_t flutterTextureId = -1;
std::unique_ptr<flutter::TextureVariant> texture;
};
}
#endif

View File

@@ -27,15 +27,10 @@
#include <vector>
#include <thread>
#include "flutter_render_context.h"
#include "flutter_egl_texture.h"
#include "rendering/egl/egl_context.h"
#if THERMION_EGL
#include "egl_context.h"
#else
#include "wgl_context.h"
#endif
namespace thermion_flutter {
namespace thermion::tflutter::windows {
using namespace std::chrono_literals;
@@ -133,10 +128,16 @@ static void _freeResource(ResourceBuffer rbf, void *const plugin) {
((ThermionFlutterPlugin *)plugin)->freeResource(rbf);
}
static std::unique_ptr<FlutterEGLTexture> _texture;
void ThermionFlutterPlugin::CreateTexture(
const flutter::MethodCall<flutter::EncodableValue> &methodCall,
std::unique_ptr<flutter::MethodResult<flutter::EncodableValue>> result) {
if (!_context) {
_context = std::make_unique<thermion::windows::egl::ThermionEGLContext>();
}
const auto *args =
std::get_if<flutter::EncodableList>(methodCall.arguments());
@@ -146,14 +147,17 @@ void ThermionFlutterPlugin::CreateTexture(
int dTop = *(std::get_if<int>(&(args->at(3))));
auto width = (uint32_t)round(dWidth );
auto height = (uint32_t)round(dHeight );
auto left = (uint32_t)round(dLeft );
auto top = (uint32_t)round(dTop );
auto left = (uint32_t)round(dLeft);
auto top = (uint32_t)round(dTop);
_context->CreateRenderingSurface(width, height, std::move(result), left, top);
auto texture = _context->GetActiveTexture();
thermion::windows::egl::EGLTexture* eglTexture = _context->CreateRenderingSurface(width, height, left, top);
auto flutterTextureId = _textureRegistrar->RegisterTexture(texture.GetFlutterTextureId());
texture.RegisterFlutterTextureId(flutterTextureId);
_texture = std::make_unique<FlutterEGLTexture>(eglTexture->GetTextureHandle(), width, height);
auto flutterTextureId = _textureRegistrar->RegisterTexture(_texture->GetFlutterTexture());
_texture->SetFlutterTextureId(flutterTextureId);
auto glTextureId = 0; // _texture.GetGLTextureId();
std::cout << "Registered Flutter texture ID " << flutterTextureId
<< std::endl;
@@ -169,7 +173,8 @@ void ThermionFlutterPlugin::DestroyTexture(
std::unique_ptr<flutter::MethodResult<flutter::EncodableValue>> result) {
if (_context) {
_context->DestroyRenderingSurface(std::move(result));
_context->DestroyRenderingSurface();
result->Success(flutter::EncodableValue((int64_t)nullptr));
}
else {
result->Error("NO_CONTEXT", "No rendering context is active");
@@ -192,15 +197,11 @@ void ThermionFlutterPlugin::HandleMethodCall(
result->Success(flutter::EncodableValue((int64_t)wrapper));
} else if(methodCall.method_name() == "getSharedContext") {
if (!_context) {
#ifdef THERMION_EGL
_context = std::make_unique<FlutterEGLContext>(_pluginRegistrar, _textureRegistrar);
#else
_context = std::make_unique<WGLContext>(_pluginRegistrar, _textureRegistrar);
#endif
_context = std::make_unique<thermion::windows::egl::ThermionEGLContext>();
}
result->Success(flutter::EncodableValue((int64_t)_context->GetSharedContext()));
} else if (methodCall.method_name() == "resizeWindow") {
#if WGL_USE_BACKING_WINDOW
const auto *args =
std::get_if<flutter::EncodableList>(methodCall.arguments());
@@ -215,9 +216,6 @@ void ThermionFlutterPlugin::HandleMethodCall(
_context->ResizeRenderingSurface(width, height, left, top);
result->Success();
#else
result->Error("ERROR", "resizeWindow is only available when using a backing window");
#endif
} else if (methodCall.method_name() == "createTexture") {
CreateTexture(methodCall, std::move(result));
} else if (methodCall.method_name() == "createWindow") {
@@ -226,18 +224,17 @@ void ThermionFlutterPlugin::HandleMethodCall(
DestroyTexture(methodCall, std::move(result));
} else if (methodCall.method_name() == "markTextureFrameAvailable") {
if (_context) {
auto texture = context->GetActiveTexture();
auto flutterTextureId = texture.GetFlutterTextureId();
if(flutterTextureId == -1) {
auto texture = _context->GetActiveTexture();
texture->Flush();
const auto* flutterTextureId = std::get_if<int64_t>(methodCall.arguments());
if(!flutterTextureId || *flutterTextureId == -1) {
std::cout << "Bad texture" << std::endl;
return;
}
#ifdef THERMION_EGL
_context->RenderCallback();
#endif
#if !WGL_USE_BACKING_WINDOW
_textureRegistrar->MarkTextureFrameAvailable(flutterTextureId);
#endif
std::cout << "Marking texture" << flutterTextureId << "available" << std::endl;
_textureRegistrar->MarkTextureFrameAvailable(*flutterTextureId);
}
result->Success(flutter::EncodableValue((int64_t)nullptr));
} else if (methodCall.method_name() == "getDriverPlatform") {

View File

@@ -1,5 +1,4 @@
#ifndef FLUTTER_PLUGIN_FLUTTER_FILAMENT_PLUGIN_H_
#define FLUTTER_PLUGIN_FLUTTER_FILAMENT_PLUGIN_H_
#pragma once
#include <flutter/method_channel.h>
#include <flutter/plugin_registrar_windows.h>
@@ -15,18 +14,13 @@
#include "GL/GLu.h"
#include "ResourceBuffer.h"
#include "rendering/egl/egl_context.h"
#if THERMION_EGL
#include "egl_context.h"
#else
#include "wgl_context.h"
#endif
namespace thermion::tflutter::windows {
namespace thermion_flutter {
class ThermionFlutterPlugin : public flutter::Plugin {
class ThermionFlutterPlugin : public ::flutter::Plugin {
public:
static void RegisterWithRegistrar(flutter::PluginRegistrarWindows *registrar);
static void RegisterWithRegistrar(::flutter::PluginRegistrarWindows *registrar);
ThermionFlutterPlugin(flutter::TextureRegistrar *textureRegistrar,
flutter::PluginRegistrarWindows *registrar,
@@ -59,13 +53,9 @@ public:
void freeResource(ResourceBuffer rbuf);
private:
#ifdef THERMION_EGL
std::unique_ptr<FlutterEGLContext> _context = nullptr;
#else
std::unique_ptr<WGLContext> _context = nullptr;
#endif
std::unique_ptr<thermion::windows::egl::ThermionEGLContext> _context = nullptr;
};
} // namespace thermion_flutter
#endif // FLUTTER_PLUGIN_FLUTTER_FILAMENT_PLUGIN_H_

View File

@@ -6,7 +6,7 @@
void ThermionFlutterPluginCApiRegisterWithRegistrar(
FlutterDesktopPluginRegistrarRef registrar) {
thermion_flutter::ThermionFlutterPlugin::RegisterWithRegistrar(
thermion::tflutter::windows::ThermionFlutterPlugin::RegisterWithRegistrar(
flutter::PluginRegistrarManager::GetInstance()
->GetRegistrar<flutter::PluginRegistrarWindows>(registrar));
}