diff --git a/windows/polyvox_filament_plugin.cpp b/windows/polyvox_filament_plugin.cpp index 0cf59cb8..e5dbf032 100644 --- a/windows/polyvox_filament_plugin.cpp +++ b/windows/polyvox_filament_plugin.cpp @@ -404,7 +404,7 @@ bool PolyvoxFilamentPlugin::MakeOpenGLTexture(uint32_t width, uint32_t height,st if(!_context || !wglMakeCurrent(whdc, _context)) { std::cout << "Failed to switch OpenGL context." << std::endl; } else { - uint8_t* data = new uint8_t[width*height*4]; + uint8_t* data = (uint8_t*)_pixelData.get(); glBindTexture(GL_TEXTURE_2D, _glTextureId); glGetTexImage(GL_TEXTURE_2D,0,GL_RGBA,GL_UNSIGNED_BYTE,data); @@ -449,7 +449,7 @@ void PolyvoxFilamentPlugin::CreateTexture( const flutter::MethodCall &methodCall, std::unique_ptr> result) { - const auto *args = + const auto *args = std::get_if(methodCall.arguments()); const auto width = (uint32_t)round(*(std::get_if(&(args->at(0))))); @@ -462,6 +462,33 @@ void PolyvoxFilamentPlugin::CreateTexture( #endif } +void PolyvoxFilamentPlugin::DestroyTexture( + const flutter::MethodCall &methodCall, + std::unique_ptr> result) { + + #ifdef USE_ANGLE + // TODO + result->Error("NOT_IMPLEMENTED", "Method is not implemented %s", methodCall.method_name()); + #else + HWND hwnd = _pluginRegistrar->GetView() + ->GetNativeWindow(); + + HDC whdc = GetDC(hwnd); + + if(!_context || !wglMakeCurrent(whdc, _context)) { + result->Error("CONTEXT", "Failed to switch OpenGL context.", nullptr); + 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); + result->Success(flutter::EncodableValue(true)); + #endif +} + void PolyvoxFilamentPlugin::HandleMethodCall( const flutter::MethodCall &methodCall, std::unique_ptr> result) { @@ -480,7 +507,7 @@ void PolyvoxFilamentPlugin::HandleMethodCall( } else if (methodCall.method_name() == "createTexture") { CreateTexture(methodCall, std::move(result)); } else if (methodCall.method_name() == "destroyTexture") { - result->Error("NOT_IMPLEMENTED", "Method is not implemented %s", methodCall.method_name()); + DestroyTexture(methodCall, std::move(result)); } else if(methodCall.method_name() == "getRenderCallback") { flutter::EncodableList resultList; resultList.push_back(flutter::EncodableValue((int64_t)&render_callback)); diff --git a/windows/polyvox_filament_plugin.h b/windows/polyvox_filament_plugin.h index 0efcc2b4..12027af2 100644 --- a/windows/polyvox_filament_plugin.h +++ b/windows/polyvox_filament_plugin.h @@ -61,7 +61,7 @@ public: std::unique_ptr _textureDescriptor = nullptr; - int64_t _flutterTextureId; + int64_t _flutterTextureId = -1; #ifdef USE_ANGLE // Device @@ -87,6 +87,9 @@ public: void CreateTexture( const flutter::MethodCall &methodCall, std::unique_ptr> result); + void DestroyTexture( + const flutter::MethodCall &methodCall, + std::unique_ptr> result); void RenderCallback();