From fec4d526e38254d1e787b7e08ae5580d1722aff6 Mon Sep 17 00:00:00 2001 From: Niklas Schulze Date: Thu, 16 Sep 2021 22:07:02 +0200 Subject: [PATCH] Windows: FlutterDesktopPixelBuffer: Add optional release callback (flutter/engine#28298) --- .../common/public/flutter_texture_registrar.h | 4 + .../flutter/shell/platform/windows/BUILD.gn | 1 + .../platform/windows/external_texture_gl.cc | 91 ++++--------------- .../platform/windows/external_texture_gl.h | 31 ++++++- .../windows/flutter_windows_engine.cc | 5 +- .../platform/windows/flutter_windows_engine.h | 3 + .../flutter_windows_texture_registrar.cc | 28 +++++- .../flutter_windows_texture_registrar.h | 7 +- ...ter_windows_texture_registrar_unittests.cc | 64 +++++++++++-- .../windows/testing/mock_gl_functions.h | 54 +++++++++++ 10 files changed, 203 insertions(+), 85 deletions(-) create mode 100644 engine/src/flutter/shell/platform/windows/testing/mock_gl_functions.h diff --git a/engine/src/flutter/shell/platform/common/public/flutter_texture_registrar.h b/engine/src/flutter/shell/platform/common/public/flutter_texture_registrar.h index b115234e27..711619e353 100644 --- a/engine/src/flutter/shell/platform/common/public/flutter_texture_registrar.h +++ b/engine/src/flutter/shell/platform/common/public/flutter_texture_registrar.h @@ -34,6 +34,10 @@ typedef struct { size_t width; // Height of the pixel buffer. size_t height; + // An optional callback that gets invoked when the |buffer| can be released. + void (*release_callback)(void* release_context); + // Opaque data passed to |release_callback|. + void* release_context; } FlutterDesktopPixelBuffer; // The pixel buffer copy callback definition provided to diff --git a/engine/src/flutter/shell/platform/windows/BUILD.gn b/engine/src/flutter/shell/platform/windows/BUILD.gn index 818c35dbea..8c058cc101 100644 --- a/engine/src/flutter/shell/platform/windows/BUILD.gn +++ b/engine/src/flutter/shell/platform/windows/BUILD.gn @@ -227,6 +227,7 @@ executable("flutter_windows_unittests") { "string_conversion_unittests.cc", "system_utils_unittests.cc", "testing/engine_modifier.h", + "testing/mock_gl_functions.h", ] # Target-specific sources. diff --git a/engine/src/flutter/shell/platform/windows/external_texture_gl.cc b/engine/src/flutter/shell/platform/windows/external_texture_gl.cc index 50b9979c7b..f4292ac757 100644 --- a/engine/src/flutter/shell/platform/windows/external_texture_gl.cc +++ b/engine/src/flutter/shell/platform/windows/external_texture_gl.cc @@ -4,61 +4,6 @@ #include "flutter/shell/platform/windows/external_texture_gl.h" -#include -#include -#include - -namespace { - -typedef void (*glGenTexturesProc)(GLsizei n, GLuint* textures); -typedef void (*glDeleteTexturesProc)(GLsizei n, const GLuint* textures); -typedef void (*glBindTextureProc)(GLenum target, GLuint texture); -typedef void (*glTexParameteriProc)(GLenum target, GLenum pname, GLint param); -typedef void (*glTexImage2DProc)(GLenum target, - GLint level, - GLint internalformat, - GLsizei width, - GLsizei height, - GLint border, - GLenum format, - GLenum type, - const void* data); - -// A struct containing pointers to resolved gl* functions. -struct GlProcs { - glGenTexturesProc glGenTextures; - glDeleteTexturesProc glDeleteTextures; - glBindTextureProc glBindTexture; - glTexParameteriProc glTexParameteri; - glTexImage2DProc glTexImage2D; - bool valid; -}; - -static const GlProcs& GlProcs() { - static struct GlProcs procs = {}; - static bool initialized = false; - if (!initialized) { - procs.glGenTextures = - reinterpret_cast(eglGetProcAddress("glGenTextures")); - procs.glDeleteTextures = reinterpret_cast( - eglGetProcAddress("glDeleteTextures")); - procs.glBindTexture = - reinterpret_cast(eglGetProcAddress("glBindTexture")); - procs.glTexParameteri = reinterpret_cast( - eglGetProcAddress("glTexParameteri")); - procs.glTexImage2D = - reinterpret_cast(eglGetProcAddress("glTexImage2D")); - - procs.valid = procs.glGenTextures && procs.glDeleteTextures && - procs.glBindTexture && procs.glTexParameteri && - procs.glTexImage2D; - initialized = true; - } - return procs; -} - -} // namespace - namespace flutter { struct ExternalTextureGLState { @@ -67,15 +12,17 @@ struct ExternalTextureGLState { ExternalTextureGL::ExternalTextureGL( FlutterDesktopPixelBufferTextureCallback texture_callback, - void* user_data) + void* user_data, + const GlProcs& gl_procs) : state_(std::make_unique()), texture_callback_(texture_callback), - user_data_(user_data) {} + user_data_(user_data), + gl_(gl_procs) {} ExternalTextureGL::~ExternalTextureGL() { const auto& gl = GlProcs(); - if (gl.valid && state_->gl_texture != 0) { - gl.glDeleteTextures(1, &state_->gl_texture); + if (state_->gl_texture != 0) { + gl_.glDeleteTextures(1, &state_->gl_texture); } } @@ -101,30 +48,32 @@ bool ExternalTextureGL::PopulateTexture(size_t width, bool ExternalTextureGL::CopyPixelBuffer(size_t& width, size_t& height) { const FlutterDesktopPixelBuffer* pixel_buffer = texture_callback_(width, height, user_data_); - const auto& gl = GlProcs(); - if (!gl.valid || !pixel_buffer || !pixel_buffer->buffer) { + if (!pixel_buffer || !pixel_buffer->buffer) { return false; } width = pixel_buffer->width; height = pixel_buffer->height; if (state_->gl_texture == 0) { - gl.glGenTextures(1, &state_->gl_texture); + gl_.glGenTextures(1, &state_->gl_texture); - gl.glBindTexture(GL_TEXTURE_2D, state_->gl_texture); + gl_.glBindTexture(GL_TEXTURE_2D, state_->gl_texture); - gl.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER); - gl.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER); + gl_.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER); + gl_.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER); - gl.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); - gl.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + gl_.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + gl_.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); } else { - gl.glBindTexture(GL_TEXTURE_2D, state_->gl_texture); + gl_.glBindTexture(GL_TEXTURE_2D, state_->gl_texture); + } + gl_.glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, pixel_buffer->width, + pixel_buffer->height, 0, GL_RGBA, GL_UNSIGNED_BYTE, + pixel_buffer->buffer); + if (pixel_buffer->release_callback) { + pixel_buffer->release_callback(pixel_buffer->release_context); } - gl.glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, pixel_buffer->width, - pixel_buffer->height, 0, GL_RGBA, GL_UNSIGNED_BYTE, - pixel_buffer->buffer); return true; } diff --git a/engine/src/flutter/shell/platform/windows/external_texture_gl.h b/engine/src/flutter/shell/platform/windows/external_texture_gl.h index 731f709d50..56d9021c85 100644 --- a/engine/src/flutter/shell/platform/windows/external_texture_gl.h +++ b/engine/src/flutter/shell/platform/windows/external_texture_gl.h @@ -12,15 +12,43 @@ #include "flutter/shell/platform/common/public/flutter_texture_registrar.h" #include "flutter/shell/platform/embedder/embedder.h" +#include +#include + namespace flutter { typedef struct ExternalTextureGLState ExternalTextureGLState; +typedef void (*glGenTexturesProc)(GLsizei n, GLuint* textures); +typedef void (*glDeleteTexturesProc)(GLsizei n, const GLuint* textures); +typedef void (*glBindTextureProc)(GLenum target, GLuint texture); +typedef void (*glTexParameteriProc)(GLenum target, GLenum pname, GLint param); +typedef void (*glTexImage2DProc)(GLenum target, + GLint level, + GLint internalformat, + GLsizei width, + GLsizei height, + GLint border, + GLenum format, + GLenum type, + const void* data); + +// A struct containing pointers to resolved gl* functions. +struct GlProcs { + glGenTexturesProc glGenTextures; + glDeleteTexturesProc glDeleteTextures; + glBindTextureProc glBindTexture; + glTexParameteriProc glTexParameteri; + glTexImage2DProc glTexImage2D; + bool valid; +}; + // An abstraction of an OpenGL texture. class ExternalTextureGL { public: ExternalTextureGL(FlutterDesktopPixelBufferTextureCallback texture_callback, - void* user_data); + void* user_data, + const GlProcs& gl_procs); virtual ~ExternalTextureGL(); @@ -49,6 +77,7 @@ class ExternalTextureGL { std::unique_ptr state_; FlutterDesktopPixelBufferTextureCallback texture_callback_ = nullptr; void* user_data_ = nullptr; + const GlProcs& gl_; }; } // namespace flutter diff --git a/engine/src/flutter/shell/platform/windows/flutter_windows_engine.cc b/engine/src/flutter/shell/platform/windows/flutter_windows_engine.cc index 6eee033835..9e2d287b2b 100644 --- a/engine/src/flutter/shell/platform/windows/flutter_windows_engine.cc +++ b/engine/src/flutter/shell/platform/windows/flutter_windows_engine.cc @@ -164,7 +164,10 @@ FlutterWindowsEngine::FlutterWindowsEngine(const FlutterProjectBundle& project) messenger_wrapper_ = std::make_unique(messenger_.get()); message_dispatcher_ = std::make_unique(messenger_.get()); - texture_registrar_ = std::make_unique(this); + + FlutterWindowsTextureRegistrar::ResolveGlFunctions(gl_procs_); + texture_registrar_ = + std::make_unique(this, gl_procs_); surface_manager_ = AngleSurfaceManager::Create(); #ifndef WINUWP window_proc_delegate_manager_ = diff --git a/engine/src/flutter/shell/platform/windows/flutter_windows_engine.h b/engine/src/flutter/shell/platform/windows/flutter_windows_engine.h index 3dc729b253..cca0ff47fa 100644 --- a/engine/src/flutter/shell/platform/windows/flutter_windows_engine.h +++ b/engine/src/flutter/shell/platform/windows/flutter_windows_engine.h @@ -182,6 +182,9 @@ class FlutterWindowsEngine { // The texture registrar. std::unique_ptr texture_registrar_; + // Resolved OpenGL functions used by external texture implementations. + GlProcs gl_procs_ = {}; + // An object used for intializing Angle and creating / destroying render // surfaces. Surface creation functionality requires a valid render_target. // May be nullptr if ANGLE failed to initialize. diff --git a/engine/src/flutter/shell/platform/windows/flutter_windows_texture_registrar.cc b/engine/src/flutter/shell/platform/windows/flutter_windows_texture_registrar.cc index f6c10db14b..c698bd29fd 100644 --- a/engine/src/flutter/shell/platform/windows/flutter_windows_texture_registrar.cc +++ b/engine/src/flutter/shell/platform/windows/flutter_windows_texture_registrar.cc @@ -12,11 +12,16 @@ namespace flutter { FlutterWindowsTextureRegistrar::FlutterWindowsTextureRegistrar( - FlutterWindowsEngine* engine) - : engine_(engine) {} + FlutterWindowsEngine* engine, + const GlProcs& gl_procs) + : engine_(engine), gl_procs_(gl_procs) {} int64_t FlutterWindowsTextureRegistrar::RegisterTexture( const FlutterDesktopTextureInfo* texture_info) { + if (!gl_procs_.valid) { + return -1; + } + if (texture_info->type != kFlutterDesktopPixelBufferTexture) { std::cerr << "Attempted to register texture of unsupport type." << std::endl; @@ -30,7 +35,7 @@ int64_t FlutterWindowsTextureRegistrar::RegisterTexture( auto texture_gl = std::make_unique( texture_info->pixel_buffer_config.callback, - texture_info->pixel_buffer_config.user_data); + texture_info->pixel_buffer_config.user_data, gl_procs_); int64_t texture_id = texture_gl->texture_id(); { @@ -86,4 +91,21 @@ bool FlutterWindowsTextureRegistrar::PopulateTexture( return texture->PopulateTexture(width, height, opengl_texture); } +void FlutterWindowsTextureRegistrar::ResolveGlFunctions(GlProcs& procs) { + procs.glGenTextures = + reinterpret_cast(eglGetProcAddress("glGenTextures")); + procs.glDeleteTextures = reinterpret_cast( + eglGetProcAddress("glDeleteTextures")); + procs.glBindTexture = + reinterpret_cast(eglGetProcAddress("glBindTexture")); + procs.glTexParameteri = reinterpret_cast( + eglGetProcAddress("glTexParameteri")); + procs.glTexImage2D = + reinterpret_cast(eglGetProcAddress("glTexImage2D")); + + procs.valid = procs.glGenTextures && procs.glDeleteTextures && + procs.glBindTexture && procs.glTexParameteri && + procs.glTexImage2D; +} + }; // namespace flutter diff --git a/engine/src/flutter/shell/platform/windows/flutter_windows_texture_registrar.h b/engine/src/flutter/shell/platform/windows/flutter_windows_texture_registrar.h index 75d724f799..a684136bfd 100644 --- a/engine/src/flutter/shell/platform/windows/flutter_windows_texture_registrar.h +++ b/engine/src/flutter/shell/platform/windows/flutter_windows_texture_registrar.h @@ -19,7 +19,8 @@ class FlutterWindowsEngine; // Thread safety: All member methods are thread safe. class FlutterWindowsTextureRegistrar { public: - explicit FlutterWindowsTextureRegistrar(FlutterWindowsEngine* engine); + explicit FlutterWindowsTextureRegistrar(FlutterWindowsEngine* engine, + const GlProcs& gl_procs); // Registers a texture described by the given |texture_info| object. // Returns the non-zero, positive texture id or -1 on error. @@ -41,8 +42,12 @@ class FlutterWindowsTextureRegistrar { size_t height, FlutterOpenGLTexture* texture); + // Populates the OpenGL function pointers in |gl_procs|. + static void ResolveGlFunctions(GlProcs& gl_procs); + private: FlutterWindowsEngine* engine_ = nullptr; + const GlProcs& gl_procs_; // All registered textures, keyed by their IDs. std::unordered_map> diff --git a/engine/src/flutter/shell/platform/windows/flutter_windows_texture_registrar_unittests.cc b/engine/src/flutter/shell/platform/windows/flutter_windows_texture_registrar_unittests.cc index f085b5c891..33439ac2c0 100644 --- a/engine/src/flutter/shell/platform/windows/flutter_windows_texture_registrar_unittests.cc +++ b/engine/src/flutter/shell/platform/windows/flutter_windows_texture_registrar_unittests.cc @@ -8,6 +8,7 @@ #include "flutter/shell/platform/windows/flutter_windows_engine.h" #include "flutter/shell/platform/windows/flutter_windows_texture_registrar.h" #include "flutter/shell/platform/windows/testing/engine_modifier.h" +#include "flutter/shell/platform/windows/testing/mock_gl_functions.h" #include "gtest/gtest.h" namespace flutter { @@ -27,8 +28,9 @@ std::unique_ptr GetTestEngine() { TEST(FlutterWindowsTextureRegistrarTest, CreateDestroy) { std::unique_ptr engine = GetTestEngine(); + std::unique_ptr gl = std::make_unique(); - FlutterWindowsTextureRegistrar registrar(engine.get()); + FlutterWindowsTextureRegistrar registrar(engine.get(), gl->gl_procs()); EXPECT_TRUE(true); } @@ -36,8 +38,9 @@ TEST(FlutterWindowsTextureRegistrarTest, CreateDestroy) { TEST(FlutterWindowsTextureRegistrarTest, RegisterUnregisterTexture) { std::unique_ptr engine = GetTestEngine(); EngineModifier modifier(engine.get()); + std::unique_ptr gl = std::make_unique(); - FlutterWindowsTextureRegistrar registrar(engine.get()); + FlutterWindowsTextureRegistrar registrar(engine.get(), gl->gl_procs()); FlutterDesktopTextureInfo texture_info = {}; texture_info.type = kFlutterDesktopPixelBufferTexture; @@ -90,9 +93,9 @@ TEST(FlutterWindowsTextureRegistrarTest, RegisterUnregisterTexture) { TEST(FlutterWindowsTextureRegistrarTest, RegisterUnknownTextureType) { std::unique_ptr engine = GetTestEngine(); - EngineModifier modifier(engine.get()); + std::unique_ptr gl = std::make_unique(); - FlutterWindowsTextureRegistrar registrar(engine.get()); + FlutterWindowsTextureRegistrar registrar(engine.get(), gl->gl_procs()); FlutterDesktopTextureInfo texture_info = {}; texture_info.type = static_cast(1234); @@ -102,17 +105,62 @@ TEST(FlutterWindowsTextureRegistrarTest, RegisterUnknownTextureType) { EXPECT_EQ(texture_id, -1); } +TEST(FlutterWindowsTextureRegistrarTest, PopulatePixelBufferTexture) { + std::unique_ptr engine = GetTestEngine(); + EngineModifier modifier(engine.get()); + std::unique_ptr gl = std::make_unique(); + + FlutterWindowsTextureRegistrar registrar(engine.get(), gl->gl_procs()); + + bool release_callback_called = false; + size_t width = 100; + size_t height = 100; + std::unique_ptr pixels(new uint8_t[width * height * 4]); + FlutterDesktopPixelBuffer pixel_buffer = {}; + pixel_buffer.width = width; + pixel_buffer.height = height; + pixel_buffer.buffer = pixels.get(); + pixel_buffer.release_context = &release_callback_called; + pixel_buffer.release_callback = [](void* release_context) { + bool* called = reinterpret_cast(release_context); + *called = true; + }; + + FlutterDesktopTextureInfo texture_info = {}; + texture_info.type = kFlutterDesktopPixelBufferTexture; + texture_info.pixel_buffer_config.user_data = &pixel_buffer; + texture_info.pixel_buffer_config.callback = + [](size_t width, size_t height, + void* user_data) -> const FlutterDesktopPixelBuffer* { + return reinterpret_cast(user_data); + }; + + modifier.embedder_api().RegisterExternalTexture = + MOCK_ENGINE_PROC(RegisterExternalTexture, + ([](auto engine, auto texture_id) { return kSuccess; })); + + FlutterOpenGLTexture flutter_texture = {}; + auto texture_id = registrar.RegisterTexture(&texture_info); + EXPECT_NE(texture_id, -1); + + auto result = + registrar.PopulateTexture(texture_id, 640, 480, &flutter_texture); + EXPECT_TRUE(result); + EXPECT_EQ(flutter_texture.width, width); + EXPECT_EQ(flutter_texture.height, height); + EXPECT_EQ(flutter_texture.target, GL_TEXTURE_2D); + EXPECT_TRUE(release_callback_called); +} + TEST(FlutterWindowsTextureRegistrarTest, PopulateInvalidTexture) { std::unique_ptr engine = GetTestEngine(); + std::unique_ptr gl = std::make_unique(); - FlutterWindowsTextureRegistrar registrar(engine.get()); + FlutterWindowsTextureRegistrar registrar(engine.get(), gl->gl_procs()); auto result = registrar.PopulateTexture(1, 640, 480, nullptr); EXPECT_FALSE(result); } -// TODO Add additional tests for PopulateTexture() once we've mocked gl* -// functions used by ExternalTextureGL - } // namespace testing } // namespace flutter diff --git a/engine/src/flutter/shell/platform/windows/testing/mock_gl_functions.h b/engine/src/flutter/shell/platform/windows/testing/mock_gl_functions.h new file mode 100644 index 0000000000..ec0871b886 --- /dev/null +++ b/engine/src/flutter/shell/platform/windows/testing/mock_gl_functions.h @@ -0,0 +1,54 @@ +// Copyright 2013 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef FLUTTER_SHELL_PLATFORM_WINDOWS_TESTING_MOCK_GL_FUNCTIONS_H_ +#define FLUTTER_SHELL_PLATFORM_WINDOWS_TESTING_MOCK_GL_FUNCTIONS_H_ + +#include "flutter/shell/platform/windows/external_texture_gl.h" + +namespace flutter { +namespace testing { + +// A class providing a mocked subset of OpenGL API functions. +class MockGlFunctions { + public: + MockGlFunctions() { + gl_procs_.glGenTextures = &glGenTextures; + gl_procs_.glDeleteTextures = &glDeleteTextures; + gl_procs_.glBindTexture = &glBindTexture; + gl_procs_.glTexParameteri = &glTexParameteri; + gl_procs_.glTexImage2D = &glTexImage2D; + gl_procs_.valid = true; + } + + const GlProcs& gl_procs() { return gl_procs_; } + + static void glGenTextures(GLsizei n, GLuint* textures) { + // The minimum valid texture ID is 1 + for (auto i = 0; i < n; i++) { + textures[i] = i + 1; + } + } + + static void glDeleteTextures(GLsizei n, const GLuint* textures) {} + static void glBindTexture(GLenum target, GLuint texture) {} + static void glTexParameteri(GLenum target, GLenum pname, GLint param) {} + static void glTexImage2D(GLenum target, + GLint level, + GLint internalformat, + GLsizei width, + GLsizei height, + GLint border, + GLenum format, + GLenum type, + const void* data) {} + + private: + GlProcs gl_procs_; +}; + +} // namespace testing +} // namespace flutter + +#endif // FLUTTER_SHELL_PLATFORM_WINDOWS_TESTING_MOCK_GL_FUNCTIONS_H_