refactor resizing to address race condition on Windows

This commit is contained in:
Nick Fisher
2023-10-16 17:02:24 +11:00
parent 025bdf662e
commit 6cf8e58bed
10 changed files with 920 additions and 739 deletions

View File

@@ -12,6 +12,7 @@ set(PLUGIN_NAME "polyvox_filament_plugin")
list(APPEND PLUGIN_SOURCES
"polyvox_filament_plugin.cpp"
"polyvox_filament_plugin.h"
"opengl_texture_buffer.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/../ios/src/AssetManager.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/../ios/src/FilamentViewer.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/../ios/src/PolyvoxFilamentApi.cpp"
@@ -69,8 +70,8 @@ if(USE_ANGLE)
else()
list(APPEND GL_LIBS
bluegl
bluevk
vkshaders
# bluevk
# vkshaders
opengl32
)
set(ANGLE_OR_OPENGL_DIR opengl)

View File

@@ -0,0 +1,129 @@
#include "opengl_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>
#include <thread>
namespace polyvox_filament {
void _release_callback(void *releaseContext) {
// ((OpenGLTextureBuffer*)releaseContext)->unlock();
}
OpenGLTextureBuffer::OpenGLTextureBuffer(
flutter::PluginRegistrarWindows *pluginRegistrar,
flutter::TextureRegistrar *textureRegistrar,
std::unique_ptr<flutter::MethodResult<flutter::EncodableValue>> result,
uint32_t width, uint32_t height, HGLRC context,
std::shared_ptr<std::mutex> renderMutex)
: _pluginRegistrar(pluginRegistrar), _textureRegistrar(textureRegistrar),
_width(width), _height(height), _context(context),
_renderMutex(renderMutex) {
HWND hwnd = _pluginRegistrar->GetView()->GetNativeWindow();
HDC whdc = GetDC(hwnd);
if (!_context || !wglMakeCurrent(whdc, _context)) {
result->Error("ERROR", "Failed to switch OpenGL context in constructor.");
return;
}
glGenTextures(1, &glTextureId);
if (glTextureId == 0) {
result->Error("ERROR", "Failed to generate texture, OpenGL err was %d",
glGetError());
return;
}
glBindTexture(GL_TEXTURE_2D, glTextureId);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, _width, _height, 0, GL_RGBA,
GL_UNSIGNED_BYTE, 0);
GLenum err = glGetError();
if (err != GL_NO_ERROR) {
result->Error("ERROR", "Failed to generate texture, GL error was %d", err);
return;
}
wglMakeCurrent(NULL, NULL);
pixelBuffer = std::make_unique<FlutterDesktopPixelBuffer>();
pixelData.reset(new uint8_t[_width * _height * 4]);
pixelBuffer->buffer = pixelData.get();
pixelBuffer->width = size_t(_width);
pixelBuffer->height = size_t(_height);
pixelBuffer->release_callback = _release_callback;
pixelBuffer->release_context = this;
std::cout << "Created initial pixel data/buffer of size " << _width << "x"
<< _height << std::endl;
texture =
std::make_unique<flutter::TextureVariant>(flutter::PixelBufferTexture(
[=](size_t width,
size_t height) -> const FlutterDesktopPixelBuffer * {
if (width != this->_width || height != this->_height) {
std::cout << "Front-end widget has been resized, you need to "
"teardown/rebuild the swapchain. This pixel buffer "
"will be discarded."
<< std::endl;
return nullptr;
}
uint8_t *data = (uint8_t *)pixelData.get();
if (!_context || !wglMakeCurrent(whdc, _context)) {
std::cout << "Failed to switch OpenGL context in callback."
<< std::endl;
} else {
// It seems there's at least 1 frame delay between resizing a
// front-end widget and the layout operation being performed on
// Windows. I haven't found a way to guarantee that we can resize
// the OpenGL texture before the pixel buffer callback here. (If
// you can find/suggest a way, please let me know). This means we
// need to manually check that the requested size matches the
// current size of our GL texture, and return an empty pixel
// buffer if not.
glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
GLenum err = glGetError();
if (err != GL_NO_ERROR) {
if (err == GL_INVALID_OPERATION) {
std::cout << "Invalid op" << std::endl;
} else if (err == GL_INVALID_VALUE) {
std::cout << "Invalid value" << std::endl;
} else if (err == GL_OUT_OF_MEMORY) {
std::cout << "Out of mem" << std::endl;
} else if (err == GL_INVALID_ENUM) {
std::cout << "Invalid enum" << std::endl;
} else {
std::cout << "Unknown error" << std::endl;
}
}
wglMakeCurrent(NULL, NULL);
}
pixelBuffer->buffer = pixelData.get();
return pixelBuffer.get();
}));
flutterTextureId = textureRegistrar->RegisterTexture(texture.get());
std::cout << "Registered Flutter texture ID " << flutterTextureId
<< std::endl;
std::vector<flutter::EncodableValue> resultList;
resultList.push_back(flutter::EncodableValue(flutterTextureId));
resultList.push_back(flutter::EncodableValue((int64_t) nullptr));
resultList.push_back(flutter::EncodableValue(glTextureId));
result->Success(resultList);
}
OpenGLTextureBuffer::~OpenGLTextureBuffer() {}
} // namespace polyvox_filament

View File

@@ -0,0 +1,53 @@
#pragma once
#ifndef _OPENGL_TEXTURE_BUFFER_H
#define _OPENGL_TEXTURE_BUFFER_H
#include <mutex>
#include <flutter/texture_registrar.h>
#include <flutter/method_channel.h>
#include <flutter/plugin_registrar_windows.h>
#include "GL/GL.h"
#include "GL/GLu.h"
#include "GL/wglext.h"
#include <Windows.h>
#include <wrl.h>
typedef uint32_t GLuint;
namespace polyvox_filament {
class OpenGLTextureBuffer {
public:
OpenGLTextureBuffer(
flutter::PluginRegistrarWindows* pluginRegistrar,
flutter::TextureRegistrar* textureRegistrar,
std::unique_ptr<flutter::MethodResult<flutter::EncodableValue>> result,
uint32_t width,
uint32_t height,
HGLRC context,
std::shared_ptr<std::mutex> renderMutex);
~OpenGLTextureBuffer();
GLuint glTextureId = 0;
int64_t flutterTextureId = 0;
std::unique_ptr<FlutterDesktopPixelBuffer> pixelBuffer;
std::unique_ptr<uint8_t> pixelData;
std::unique_ptr<flutter::TextureVariant> texture;
private:
flutter::PluginRegistrarWindows* _pluginRegistrar;
flutter::TextureRegistrar* _textureRegistrar;
uint32_t _width = 0;
uint32_t _height = 0;
HGLRC _context = NULL;
bool logged = false;
std::shared_ptr<std::mutex> _renderMutex;
};
}
#endif // _OPENGL_TEXTURE_BUFFER_H

View File

@@ -33,10 +33,6 @@
#include "PlatformANGLE.h"
#endif
#include "GL/GL.h"
#include "GL/GLu.h"
#include "GL/wglext.h"
#include <Windows.h>
#include <wrl.h>
@@ -83,7 +79,7 @@ ResourceBuffer PolyvoxFilamentPlugin::loadResource(const char *name) {
name_str = name_str.substr(8);
}
TCHAR pBuf[256];
TCHAR pBuf[512];
size_t len = sizeof(pBuf);
int bytes = GetModuleFileName(NULL, pBuf, len);
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter;
@@ -145,7 +141,10 @@ void PolyvoxFilamentPlugin::RenderCallback() {
_internalD3DTexture2D.Get());
_D3D11DeviceContext->Flush();
#endif
_textureRegistrar->MarkTextureFrameAvailable(_flutterTextureId);
std::lock_guard<std::mutex> guard(*(_renderMutex.get()));
if (_active) {
_textureRegistrar->MarkTextureFrameAvailable(_active->flutterTextureId);
}
}
#ifdef USE_ANGLE
@@ -293,8 +292,9 @@ bool PolyvoxFilamentPlugin::MakeD3DTexture(uint32_t width, uint32_t height,std::
}
#else
bool PolyvoxFilamentPlugin::MakeOpenGLTexture(uint32_t width, uint32_t height,std::unique_ptr<flutter::MethodResult<flutter::EncodableValue>> result) {
HWND hwnd = _pluginRegistrar->GetView()
->GetNativeWindow();;
->GetNativeWindow();
HDC whdc = GetDC(hwnd);
if (whdc == NULL) {
@@ -302,146 +302,99 @@ bool PolyvoxFilamentPlugin::MakeOpenGLTexture(uint32_t width, uint32_t height,st
return false;
}
PIXELFORMATDESCRIPTOR pfd = {
sizeof(PIXELFORMATDESCRIPTOR),
1,
PFD_DRAW_TO_BITMAP | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER, // Flags
PFD_TYPE_RGBA, // The kind of framebuffer. RGBA or palette.
32, // Colordepth of the framebuffer.
0, 0, 0, 0, 0, 0,
0,
0,
0,
0, 0, 0, 0,
32, // 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
};
if(!_renderMutex.get()) {
_renderMutex = std::make_shared<std::mutex>();
}
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)) {
result->Error("ERROR", "Failed to acquire temporary context", nullptr);
// we need a single context (since this will be passed to the renderer)
// if this is the first time we are attempting to create a texture, let's create the context
if(_context == NULL) {
std::cout << "No GL context exists, creating" << std::endl;
PIXELFORMATDESCRIPTOR pfd = {
sizeof(PIXELFORMATDESCRIPTOR),
1,
PFD_DRAW_TO_BITMAP | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER, // Flags
PFD_TYPE_RGBA, // The kind of framebuffer. RGBA or palette.
32, // Colordepth of the framebuffer.
0, 0, 0, 0, 0, 0,
0,
0,
0,
0, 0, 0, 0,
32, // 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)) {
result->Error("ERROR", "Failed to acquire temporary context", nullptr);
}
GLenum err = glGetError();
if(err != GL_NO_ERROR) {
result->Error("ERROR", "GL Error @ 455 %d", err);
return false;
}
PFNWGLCREATECONTEXTATTRIBSARBPROC wglCreateContextAttribs = nullptr;
wglCreateContextAttribs =
(PFNWGLCREATECONTEXTATTRIBSARBPROC)wglGetProcAddress(
"wglCreateContextAttribsARB");
if (!wglCreateContextAttribs) {
result->Error("ERROR", "Failed to resolve wglCreateContextAttribsARB",
nullptr);
return false;
}
for (int minor = 5; minor >= 1; minor--) {
std::vector<int> 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)) {
result->Error("ERROR", "Failed to create OpenGL context.");
return false;
}
}
if(_active.get()) {
result->Error("ERROR", "Texture already exists. You must call destroyTexture before attempting to create a new one.");
return false;
}
}
GLenum err = glGetError();
if(err != GL_NO_ERROR) {
result->Error("ERROR", "GL Error @ 455 %d", err);
return false;
}
PFNWGLCREATECONTEXTATTRIBSARBPROC wglCreateContextAttribs = nullptr;
wglCreateContextAttribs =
(PFNWGLCREATECONTEXTATTRIBSARBPROC)wglGetProcAddress(
"wglCreateContextAttribsARB");
if (!wglCreateContextAttribs) {
result->Error("ERROR", "Failed to resolve wglCreateContextAttribsARB",
nullptr);
return false;
}
for (int minor = 5; minor >= 1; minor--) {
std::vector<int> 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)) {
result->Error("ERROR", "Failed to create OpenGL context.");
return false;
}
glGenTextures(1, &_glTextureId);
if(_glTextureId == 0) {
result->Error("ERROR", "Failed to generate texture, OpenGL err was %d", glGetError());
return false;
}
glBindTexture(GL_TEXTURE_2D, _glTextureId);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, GL_RGBA,
GL_UNSIGNED_BYTE, 0);
err = glGetError();
if (err != GL_NO_ERROR) {
result->Error("ERROR", "Failed to generate texture, GL error was %d", err);
return false;
}
wglMakeCurrent(NULL, NULL);
_pixelData.reset(new uint8_t[width * height * 4]);
_pixelBuffer = std::make_unique<FlutterDesktopPixelBuffer>();
_pixelBuffer->buffer = _pixelData.get();
_pixelBuffer->width = size_t(width);
_pixelBuffer->height = size_t(height);
_texture = std::make_unique<flutter::TextureVariant>(flutter::PixelBufferTexture(
[=](size_t width,
size_t height) -> const FlutterDesktopPixelBuffer * {
std::lock_guard<std::mutex> guard(_renderMutex);
if(!_context || !wglMakeCurrent(whdc, _context)) {
std::cout << "Failed to switch OpenGL context." << std::endl;
} else if (_glTextureId != 0) {
// uint8_t* data = (uint8_t*)_pixelData.get();
uint8_t* data = new uint8_t[width*height*4];
glBindTexture(GL_TEXTURE_2D, _glTextureId);
glGetTexImage(GL_TEXTURE_2D,0,GL_RGBA,GL_UNSIGNED_BYTE,data);
GLenum err = glGetError();
if(err != GL_NO_ERROR) {
if(err == GL_INVALID_OPERATION) {
std::cout << "Invalid op" << std::endl;
} else if(err == GL_INVALID_VALUE) {
std::cout << "Invalid value" << std::endl;
} else if(err == GL_OUT_OF_MEMORY) {
std::cout << "Out of mem" << std::endl;
} else if(err == GL_INVALID_ENUM ) {
std::cout << "Invalid enum" << std::endl;
} else {
std::cout << "Unknown error" << std::endl;
}
}
glFinish();
_pixelData.reset(data);
wglMakeCurrent(NULL, NULL);
}
_pixelBuffer->buffer = _pixelData.get();
return _pixelBuffer.get();
}));
_flutterTextureId = _textureRegistrar->RegisterTexture(_texture.get());
std::cout << "Registered Flutter texture ID " << _flutterTextureId << std::endl;
std::vector<flutter::EncodableValue> resultList;
resultList.push_back(flutter::EncodableValue(_flutterTextureId));
resultList.push_back(flutter::EncodableValue((int64_t)nullptr));
resultList.push_back(flutter::EncodableValue(_glTextureId));
result->Success(resultList);
return true;
_active = std::make_unique<OpenGLTextureBuffer>(
_pluginRegistrar,
_textureRegistrar,
std::move(result),
width,
height,
_context,
_renderMutex
);
return _active->flutterTextureId != -1;
}
#endif
@@ -467,30 +420,53 @@ void PolyvoxFilamentPlugin::DestroyTexture(
const flutter::MethodCall<flutter::EncodableValue> &methodCall,
std::unique_ptr<flutter::MethodResult<flutter::EncodableValue>> result) {
const auto *flutterTextureId =
std::get_if<int64_t>(methodCall.arguments());
if(!flutterTextureId) {
result->Error("NOT_IMPLEMENTED", "Flutter texture ID must be provided");
return;
}
#ifdef USE_ANGLE
// TODO
result->Error("NOT_IMPLEMENTED", "Method is not implemented %s", methodCall.method_name());
#else
std::lock_guard<std::mutex> guard(_renderMutex);
auto sh = std::make_shared<std::unique_ptr<flutter::MethodResult<flutter::EncodableValue>>>(std::move(result));
HWND hwnd = _pluginRegistrar->GetView()
->GetNativeWindow();
HDC whdc = GetDC(hwnd);
if(!_context || !wglMakeCurrent(whdc, _context)) {
result->Error("CONTEXT", "Failed to switch OpenGL context.", nullptr);
if(!_active) {
result->Success("Texture has already been detroyed, ignoring");
return;
}
// for now we will just unregister the Flutter texture and delete the GL texture
// we will leave the pixel data/PixelBufferTexture intact - these will be replaced on the next call to createTexture
// if we wanted to be militant about cleaning up unused memory we could delete here first
// _textureRegistrar->UnregisterTexture(_flutterTextureId);
// _flutterTextureId = -1;
// glDeleteTextures(1, &_glTextureId);
// _glTextureId = 0;
// wglMakeCurrent(NULL, NULL);
result->Success(flutter::EncodableValue(true));
if(_active->flutterTextureId != *flutterTextureId) {
result->Error("TEXTURE_MISMATCH", "Specified texture ID is not active");
return;
}
_textureRegistrar->UnregisterTexture(_active->flutterTextureId, [=,
sharedResult=std::move(sh)
]() {
if(this->_inactive) {
HWND hwnd = _pluginRegistrar->GetView()->GetNativeWindow();
HDC whdc = GetDC(hwnd);
if (!wglMakeCurrent(whdc, _context)) {
std::cout << "Failed to switch OpenGL context in destructor." << std::endl;
// result->Error("CONTEXT", "Failed to switch OpenGL context.", nullptr);
return;
}
glDeleteTextures(1, &this->_inactive->glTextureId);
wglMakeCurrent(NULL, NULL);
}
this->_inactive = std::move(this->_active);
auto unique = std::move(*(sharedResult.get()));
unique->Success(flutter::EncodableValue(true));
std::cout << "Destroyed OpenGLTextureBuffer." << std::endl;
});
#endif
}
@@ -524,8 +500,7 @@ void PolyvoxFilamentPlugin::HandleMethodCall(
#else
result->Success(flutter::EncodableValue((int64_t)nullptr));
#endif
}
else {
} else {
result->Error("NOT_IMPLEMENTED", "Method is not implemented %s", methodCall.method_name());
}
}

View File

@@ -26,6 +26,8 @@
#include "GLES2/gl2.h"
#include "GLES2/gl2ext.h"
#include "PlatformAngle.h"
#else
#include "opengl_texture_buffer.h"
#endif
#include "PolyvoxFilamentApi.h"
@@ -54,15 +56,8 @@ public:
std::map<uint32_t, ResourceBuffer> _resources;
std::unique_ptr<flutter::TextureVariant> _texture = nullptr;
std::unique_ptr<FlutterDesktopPixelBuffer> _pixelBuffer = nullptr;
std::unique_ptr<uint8_t> _pixelData = nullptr;
std::unique_ptr<FlutterDesktopGpuSurfaceDescriptor> _textureDescriptor = nullptr;
int64_t _flutterTextureId = -1;
#ifdef USE_ANGLE
// Device
ID3D11Device* _D3D11Device = nullptr;
@@ -75,12 +70,13 @@ public:
filament::backend::PlatformANGLE* _platform = nullptr;
bool MakeD3DTexture(uint32_t width, uint32_t height, std::unique_ptr<flutter::MethodResult<flutter::EncodableValue>> result);
#else
// OpenGL
HGLRC _context = NULL;
GLuint _glTextureId = 0;
std::mutex _renderMutex;
#else
std::shared_ptr<std::mutex> _renderMutex;
std::unique_ptr<OpenGLTextureBuffer> _active = nullptr;
std::unique_ptr<OpenGLTextureBuffer> _inactive = nullptr;
// shared OpenGLContext
HGLRC _context = NULL;
bool MakeOpenGLTexture(uint32_t width, uint32_t height, std::unique_ptr<flutter::MethodResult<flutter::EncodableValue>> result);
#endif
@@ -90,7 +86,6 @@ public:
void DestroyTexture(
const flutter::MethodCall<flutter::EncodableValue> &methodCall,
std::unique_ptr<flutter::MethodResult<flutter::EncodableValue>> result);
void RenderCallback();
ResourceBuffer loadResource(const char *path);