Skip to content

Commit

Permalink
Add windows plugin texture support (flutter#19405)
Browse files Browse the repository at this point in the history
  • Loading branch information
jnschulze authored Jan 19, 2021
1 parent 3f0cf18 commit eb031d3
Show file tree
Hide file tree
Showing 31 changed files with 1,138 additions and 21 deletions.
8 changes: 8 additions & 0 deletions ci/licenses_golden/licenses_flutter
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -894,6 +894,7 @@ FILE: ../../../flutter/shell/platform/common/cpp/client_wrapper/include/flutter/
FILE: ../../../flutter/shell/platform/common/cpp/client_wrapper/include/flutter/standard_codec_serializer.h
FILE: ../../../flutter/shell/platform/common/cpp/client_wrapper/include/flutter/standard_message_codec.h
FILE: ../../../flutter/shell/platform/common/cpp/client_wrapper/include/flutter/standard_method_codec.h
FILE: ../../../flutter/shell/platform/common/cpp/client_wrapper/include/flutter/texture_registrar.h
FILE: ../../../flutter/shell/platform/common/cpp/client_wrapper/method_call_unittests.cc
FILE: ../../../flutter/shell/platform/common/cpp/client_wrapper/method_channel_unittests.cc
FILE: ../../../flutter/shell/platform/common/cpp/client_wrapper/method_result_functions_unittests.cc
Expand All @@ -902,6 +903,8 @@ FILE: ../../../flutter/shell/platform/common/cpp/client_wrapper/plugin_registrar
FILE: ../../../flutter/shell/platform/common/cpp/client_wrapper/standard_codec.cc
FILE: ../../../flutter/shell/platform/common/cpp/client_wrapper/standard_message_codec_unittests.cc
FILE: ../../../flutter/shell/platform/common/cpp/client_wrapper/standard_method_codec_unittests.cc
FILE: ../../../flutter/shell/platform/common/cpp/client_wrapper/texture_registrar_impl.h
FILE: ../../../flutter/shell/platform/common/cpp/client_wrapper/texture_registrar_unittests.cc
FILE: ../../../flutter/shell/platform/common/cpp/engine_switches.cc
FILE: ../../../flutter/shell/platform/common/cpp/engine_switches.h
FILE: ../../../flutter/shell/platform/common/cpp/engine_switches_unittests.cc
Expand Down Expand Up @@ -1441,13 +1444,18 @@ FILE: ../../../flutter/shell/platform/windows/client_wrapper/include/flutter/plu
FILE: ../../../flutter/shell/platform/windows/client_wrapper/plugin_registrar_windows_unittests.cc
FILE: ../../../flutter/shell/platform/windows/cursor_handler.cc
FILE: ../../../flutter/shell/platform/windows/cursor_handler.h
FILE: ../../../flutter/shell/platform/windows/external_texture_gl.cc
FILE: ../../../flutter/shell/platform/windows/external_texture_gl.h
FILE: ../../../flutter/shell/platform/windows/flutter_project_bundle.cc
FILE: ../../../flutter/shell/platform/windows/flutter_project_bundle.h
FILE: ../../../flutter/shell/platform/windows/flutter_project_bundle_unittests.cc
FILE: ../../../flutter/shell/platform/windows/flutter_windows.cc
FILE: ../../../flutter/shell/platform/windows/flutter_windows_engine.cc
FILE: ../../../flutter/shell/platform/windows/flutter_windows_engine.h
FILE: ../../../flutter/shell/platform/windows/flutter_windows_engine_unittests.cc
FILE: ../../../flutter/shell/platform/windows/flutter_windows_texture_registrar.cc
FILE: ../../../flutter/shell/platform/windows/flutter_windows_texture_registrar.h
FILE: ../../../flutter/shell/platform/windows/flutter_windows_texture_registrar_unittests.cc
FILE: ../../../flutter/shell/platform/windows/flutter_windows_view.cc
FILE: ../../../flutter/shell/platform/windows/flutter_windows_view.h
FILE: ../../../flutter/shell/platform/windows/flutter_windows_win32.cc
Expand Down
1 change: 1 addition & 0 deletions shell/platform/common/cpp/client_wrapper/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ executable("client_wrapper_unittests") {
"standard_method_codec_unittests.cc",
"testing/test_codec_extensions.cc",
"testing/test_codec_extensions.h",
"texture_registrar_unittests.cc",
]

deps = [
Expand Down
42 changes: 42 additions & 0 deletions shell/platform/common/cpp/client_wrapper/core_implementations.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@

#include <cassert>
#include <iostream>
#include <variant>

#include "binary_messenger_impl.h"
#include "include/flutter/engine_method_result.h"
#include "texture_registrar_impl.h"

namespace flutter {

Expand Down Expand Up @@ -146,4 +148,44 @@ void ReplyManager::SendResponseData(const std::vector<uint8_t>* data) {

} // namespace internal

// ========== texture_registrar_impl.h ==========

TextureRegistrarImpl::TextureRegistrarImpl(
FlutterDesktopTextureRegistrarRef texture_registrar_ref)
: texture_registrar_ref_(texture_registrar_ref) {}

TextureRegistrarImpl::~TextureRegistrarImpl() = default;

int64_t TextureRegistrarImpl::RegisterTexture(TextureVariant* texture) {
if (auto pixel_buffer_texture = std::get_if<PixelBufferTexture>(texture)) {
FlutterDesktopTextureInfo info = {};
info.type = kFlutterDesktopPixelBufferTexture;
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* {
auto texture = static_cast<PixelBufferTexture*>(user_data);
auto buffer = texture->CopyPixelBuffer(width, height);
return buffer;
};

int64_t texture_id = FlutterDesktopTextureRegistrarRegisterExternalTexture(
texture_registrar_ref_, &info);
return texture_id;
}

std::cerr << "Attempting to register unknown texture variant." << std::endl;
return -1;
} // namespace flutter

bool TextureRegistrarImpl::MarkTextureFrameAvailable(int64_t texture_id) {
return FlutterDesktopTextureRegistrarMarkExternalTextureFrameAvailable(
texture_registrar_ref_, texture_id);
}

bool TextureRegistrarImpl::UnregisterTexture(int64_t texture_id) {
return FlutterDesktopTextureRegistrarUnregisterExternalTexture(
texture_registrar_ref_, texture_id);
}

} // namespace flutter
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ core_cpp_client_wrapper_includes =
"include/flutter/standard_codec_serializer.h",
"include/flutter/standard_message_codec.h",
"include/flutter/standard_method_codec.h",
"include/flutter/texture_registrar.h",
],
"abspath")

Expand All @@ -34,6 +35,7 @@ core_cpp_client_wrapper_internal_headers =
get_path_info([
"binary_messenger_impl.h",
"byte_buffer_streams.h",
"texture_registrar_impl.h",
],
"abspath")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <string>

#include "binary_messenger.h"
#include "texture_registrar.h"

namespace flutter {

Expand Down Expand Up @@ -41,6 +42,10 @@ class PluginRegistrar {
// This pointer will remain valid for the lifetime of this instance.
BinaryMessenger* messenger() { return messenger_.get(); }

// Returns the texture registrar to use for the plugin to render a pixel
// buffer.
TextureRegistrar* texture_registrar() { return texture_registrar_.get(); }

// Takes ownership of |plugin|.
//
// Plugins are not required to call this method if they have other lifetime
Expand All @@ -62,6 +67,8 @@ class PluginRegistrar {

std::unique_ptr<BinaryMessenger> messenger_;

std::unique_ptr<TextureRegistrar> texture_registrar_;

// Plugins registered for ownership.
std::set<std::unique_ptr<Plugin>> plugins_;
};
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// 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_COMMON_CPP_CLIENT_WRAPPER_INCLUDE_FLUTTER_TEXTURE_REGISTRAR_H_
#define FLUTTER_SHELL_PLATFORM_COMMON_CPP_CLIENT_WRAPPER_INCLUDE_FLUTTER_TEXTURE_REGISTRAR_H_

#include <flutter_texture_registrar.h>

#include <cstdint>
#include <functional>
#include <memory>
#include <variant>

namespace flutter {

// A pixel buffer texture.
class PixelBufferTexture {
public:
// A callback used for retrieving pixel buffers.
typedef std::function<const FlutterDesktopPixelBuffer*(size_t width,
size_t height)>
CopyBufferCallback;

// Creates a pixel buffer texture that uses the provided |copy_buffer_cb| to
// retrieve the buffer.
// As the callback is usually invoked from the render thread, the callee must
// take care of proper synchronization. It also needs to be ensured that the
// returned buffer isn't released prior to unregistering this texture.
PixelBufferTexture(CopyBufferCallback copy_buffer_callback)
: copy_buffer_callback_(copy_buffer_callback) {}

// Returns the callback-provided FlutterDesktopPixelBuffer that contains the
// actual pixel data. The intended surface size is specified by |width| and
// |height|.
const FlutterDesktopPixelBuffer* CopyPixelBuffer(size_t width,
size_t height) const {
return copy_buffer_callback_(width, height);
}

private:
const CopyBufferCallback copy_buffer_callback_;
};

// The available texture variants.
// Only PixelBufferTexture is currently implemented.
// Other variants are expected to be added in the future.
typedef std::variant<PixelBufferTexture> TextureVariant;

// An object keeping track of external textures.
//
// Thread safety:
// It's safe to call the member methods from any thread.
class TextureRegistrar {
public:
virtual ~TextureRegistrar() = default;

// Registers a |texture| object and returns the ID for that texture.
virtual int64_t RegisterTexture(TextureVariant* texture) = 0;

// Notifies the flutter engine that the texture object corresponding
// to |texure_id| needs to render a new frame.
//
// For PixelBufferTextures, this will effectively make the engine invoke
// the callback that was provided upon creating the texture.
virtual bool MarkTextureFrameAvailable(int64_t texture_id) = 0;

// Unregisters an existing Texture object.
// Textures must not be unregistered while they're in use.
virtual bool UnregisterTexture(int64_t texture_id) = 0;
};

} // namespace flutter

#endif // FLUTTER_SHELL_PLATFORM_COMMON_CPP_CLIENT_WRAPPER_INCLUDE_FLUTTER_TEXTURE_REGISTRAR_H_
6 changes: 6 additions & 0 deletions shell/platform/common/cpp/client_wrapper/plugin_registrar.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "binary_messenger_impl.h"
#include "include/flutter/engine_method_result.h"
#include "include/flutter/method_channel.h"
#include "texture_registrar_impl.h"

namespace flutter {

Expand All @@ -19,6 +20,11 @@ PluginRegistrar::PluginRegistrar(FlutterDesktopPluginRegistrarRef registrar)
: registrar_(registrar) {
auto core_messenger = FlutterDesktopPluginRegistrarGetMessenger(registrar_);
messenger_ = std::make_unique<BinaryMessengerImpl>(core_messenger);

auto texture_registrar =
FlutterDesktopRegistrarGetTextureRegistrar(registrar_);
texture_registrar_ =
std::make_unique<TextureRegistrarImpl>(texture_registrar);
}

PluginRegistrar::~PluginRegistrar() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,4 +214,15 @@ TEST(PluginRegistrarTest, ManagerRemovesOnDestruction) {
nullptr);
}

// Tests that the texture registrar getter returns a non-null TextureRegistrar
TEST(PluginRegistrarTest, TextureRegistrarNotNull) {
auto dummy_registrar_handle =
reinterpret_cast<FlutterDesktopPluginRegistrarRef>(1);
PluginRegistrar registrar(dummy_registrar_handle);

TextureRegistrar* texture_registrar = registrar.texture_registrar();

ASSERT_NE(texture_registrar, nullptr);
}

} // namespace flutter
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,41 @@ void FlutterDesktopMessengerSetCallback(FlutterDesktopMessengerRef messenger,
s_stub_implementation->MessengerSetCallback(channel, callback, user_data);
}
}

FlutterDesktopTextureRegistrarRef FlutterDesktopRegistrarGetTextureRegistrar(
FlutterDesktopPluginRegistrarRef registrar) {
return reinterpret_cast<FlutterDesktopTextureRegistrarRef>(1);
}

int64_t FlutterDesktopTextureRegistrarRegisterExternalTexture(
FlutterDesktopTextureRegistrarRef texture_registrar,
const FlutterDesktopTextureInfo* info) {
uint64_t result = -1;
if (s_stub_implementation) {
result =
s_stub_implementation->TextureRegistrarRegisterExternalTexture(info);
}
return result;
}

bool FlutterDesktopTextureRegistrarUnregisterExternalTexture(
FlutterDesktopTextureRegistrarRef texture_registrar,
int64_t texture_id) {
bool result = false;
if (s_stub_implementation) {
result = s_stub_implementation->TextureRegistrarUnregisterExternalTexture(
texture_id);
}
return result;
}

bool FlutterDesktopTextureRegistrarMarkExternalTextureFrameAvailable(
FlutterDesktopTextureRegistrarRef texture_registrar,
int64_t texture_id) {
bool result = false;
if (s_stub_implementation) {
result = s_stub_implementation->TextureRegistrarMarkTextureFrameAvailable(
texture_id);
}
return result;
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,22 @@ class StubFlutterApi {
virtual void MessengerSetCallback(const char* channel,
FlutterDesktopMessageCallback callback,
void* user_data) {}

// Called for FlutterDesktopRegisterExternalTexture.
virtual int64_t TextureRegistrarRegisterExternalTexture(
const FlutterDesktopTextureInfo* info) {
return -1;
}

// Called for FlutterDesktopUnregisterExternalTexture.
virtual bool TextureRegistrarUnregisterExternalTexture(int64_t texture_id) {
return false;
}

// Called for FlutterDesktopMarkExternalTextureFrameAvailable.
virtual bool TextureRegistrarMarkTextureFrameAvailable(int64_t texture_id) {
return false;
}
};

// A test helper that owns a stub implementation, making it the test stub for
Expand Down
40 changes: 40 additions & 0 deletions shell/platform/common/cpp/client_wrapper/texture_registrar_impl.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// 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_COMMON_CPP_CLIENT_WRAPPER_TEXTURE_REGISTRAR_IMPL_H_
#define FLUTTER_SHELL_PLATFORM_COMMON_CPP_CLIENT_WRAPPER_TEXTURE_REGISTRAR_IMPL_H_

#include "include/flutter/texture_registrar.h"

namespace flutter {

// Wrapper around a FlutterDesktopTextureRegistrarRef that implements the
// TextureRegistrar API.
class TextureRegistrarImpl : public TextureRegistrar {
public:
explicit TextureRegistrarImpl(
FlutterDesktopTextureRegistrarRef texture_registrar_ref);
virtual ~TextureRegistrarImpl();

// Prevent copying.
TextureRegistrarImpl(TextureRegistrarImpl const&) = delete;
TextureRegistrarImpl& operator=(TextureRegistrarImpl const&) = delete;

// |flutter::TextureRegistrar|
int64_t RegisterTexture(TextureVariant* texture) override;

// |flutter::TextureRegistrar|
bool MarkTextureFrameAvailable(int64_t texture_id) override;

// |flutter::TextureRegistrar|
bool UnregisterTexture(int64_t texture_id) override;

private:
// Handle for interacting with the C API.
FlutterDesktopTextureRegistrarRef texture_registrar_ref_;
};

} // namespace flutter

#endif // FLUTTER_SHELL_PLATFORM_COMMON_CPP_CLIENT_WRAPPER_TEXTURE_REGISTRAR_IMPL_H_
Loading

0 comments on commit eb031d3

Please sign in to comment.