Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Texture support for glfw. #9822

Closed
wants to merge 33 commits into from
Closed
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
5302cf8
Merge pull request #1 from flutter/master
cloudwebrtc Mar 28, 2019
2953e13
Merge remote-tracking branch 'upstream/master'
cloudwebrtc Apr 9, 2019
42b7502
Merge remote-tracking branch 'upstream/master'
cloudwebrtc Apr 18, 2019
ee2a336
Merge remote-tracking branch 'upstream/master'
cloudwebrtc Jun 23, 2019
bfd31aa
Merge remote-tracking branch 'upstream/master'
cloudwebrtc Jul 11, 2019
08e9d07
Add Texture support for GLFW.
cloudwebrtc Jul 11, 2019
0af9192
Fixed EventChannel for embedder.
cloudwebrtc Jul 12, 2019
bfb06c2
Fixed compiler error for stub_flutter_api.cc.
cloudwebrtc Jul 12, 2019
d6529fb
Update texture registrar for glfw.
cloudwebrtc Jul 12, 2019
689f6e6
clang format.
cloudwebrtc Jul 13, 2019
e93c57c
Update.
cloudwebrtc Jul 14, 2019
eaec810
Update.
cloudwebrtc Jul 14, 2019
63ad181
Add comments and code modify.
cloudwebrtc Jul 16, 2019
f07279e
Update DEPS
cloudwebrtc Jul 16, 2019
d1266a8
Update external_texture_gl.h
cloudwebrtc Jul 16, 2019
7391e42
Fix compilation errors for linux and clang/gn format.
cloudwebrtc Jul 16, 2019
4c5f065
Fix license check error.
cloudwebrtc Jul 16, 2019
93fc359
Fixed the order of glad.h contains a run error.
cloudwebrtc Jul 21, 2019
184a13a
Update.
cloudwebrtc Sep 13, 2019
1c70e99
Merge remote-tracking branch 'upstream/master' into texture_glfw
cloudwebrtc Sep 14, 2019
63e1469
Fixed engine type definition error.
cloudwebrtc Sep 14, 2019
0669a9c
Merge remote-tracking branch 'upstream/master' into texture_glfw
cloudwebrtc Sep 24, 2019
2ccfeef
Use the new `glad' dependency.
cloudwebrtc Sep 24, 2019
dd1bffb
Update.
cloudwebrtc Sep 24, 2019
c400937
fix typo.
cloudwebrtc Sep 24, 2019
6456082
clang-format.
cloudwebrtc Sep 24, 2019
a46b562
update.
cloudwebrtc Oct 7, 2019
3ecb5fd
Add comment.
cloudwebrtc Oct 7, 2019
e1b7a8b
Add unit tests for texture.
cloudwebrtc Oct 8, 2019
4903666
update.
cloudwebrtc Oct 10, 2019
eed131a
update.
cloudwebrtc Nov 7, 2019
aba2fa2
update unit test.
cloudwebrtc Nov 7, 2019
d8efcdb
Fixed typo.
cloudwebrtc Dec 9, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions shell/platform/common/cpp/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ _public_headers = [
"public/flutter_export.h",
"public/flutter_messenger.h",
"public/flutter_plugin_registrar.h",
"public/flutter_texture_registrar.h",
]

# Any files that are built by clients (client_wrapper code, library headers for
Expand Down
2 changes: 2 additions & 0 deletions shell/platform/common/cpp/client_wrapper/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ _wrapper_includes = [
"include/flutter/binary_messenger.h",
"include/flutter/encodable_value.h",
"include/flutter/engine_method_result.h",
"include/flutter/event_channel.h",
"include/flutter/json_message_codec.h",
"include/flutter/json_method_codec.h",
"include/flutter/json_type.h",
Expand All @@ -19,6 +20,7 @@ _wrapper_includes = [
"include/flutter/method_codec.h",
"include/flutter/method_result.h",
"include/flutter/plugin_registrar.h",
"include/flutter/texture_registrar.h",
"include/flutter/standard_message_codec.h",
"include/flutter/standard_method_codec.h",
]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
// Copyright 2013 The Flutter Authors. All rights reserved.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please split event channel support out into a separate precursor PR to keep the scope smaller.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I will submit event_channel.h in another PR.

// 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_EVENT_CHANNEL_H_
#define FLUTTER_SHELL_PLATFORM_COMMON_CPP_CLIENT_WRAPPER_INCLUDE_FLUTTER_EVENT_CHANNEL_H_

#include <iostream>
#include <string>

#include "binary_messenger.h"
#include "engine_method_result.h"
#include "message_codec.h"
#include "method_call.h"
#include "method_codec.h"
#include "method_result.h"

namespace flutter {

template <typename T>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Most of the things in this file need declaration comments, per Google's C++ style guide.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This applies to all of the class declarations, method declarations, and typedefs in this PR, not just this file.

using SuccessHandler = std::function<void(const T *event)>;

template <typename T>
using ErrorHandler =
std::function<void(const std::string &errorCode,
const std::string &errorMessage, const T *errorDetails)>;

template <typename T>
struct EventSink {
SuccessHandler<T> Success;
ErrorHandler<T> Error;
};

template <typename T>
struct StreamHandler {
std::function<MethodResult<T> *(const T *arguments,
const EventSink<T> *event_sink)>
onListen;
std::function<MethodResult<T> *(const T *arguments)> onCancel;
};

template <typename T>
class EventChannel {
public:
EventChannel(BinaryMessenger *messenger, const std::string &name,
const MethodCodec<T> *codec)
: messenger_(messenger), name_(name), codec_(codec) {}
~EventChannel() {}

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

void SetStreamHandler(StreamHandler<T> stream_handler) {
stream_handler_ = stream_handler;
const auto messenger = messenger_;
std::string channel_name = name_;
const auto *codec = codec_;

EventSink<T> event_sink = {};

event_sink.Success = [messenger, channel_name, codec](const T *events) {
std::unique_ptr<std::vector<uint8_t>> message =
codec->EncodeSuccessEnvelope(events);
messenger->Send(channel_name, message->data(), message->size());
};

event_sink.Error = [messenger, channel_name, codec](
const std::string &errorCode,
const std::string &errorMessage,
const T *errorDetails) {
std::unique_ptr<std::vector<uint8_t>> message =
codec->EncodeErrorEnvelope(errorCode, errorMessage, errorDetails);
messenger->Send(channel_name, message->data(), message->size());
};

BinaryMessageHandler binary_handler = [&, event_sink, codec, channel_name](
const uint8_t *message,
const size_t message_size,
BinaryReply reply) {
auto result =
std::make_unique<EngineMethodResult<T>>(std::move(reply), codec);
std::unique_ptr<MethodCall<T>> method_call =
codec->DecodeMethodCall(message, message_size);
if (!method_call) {
std::cerr << "Unable to construct method call from message on channel "
<< channel_name << std::endl;
result->NotImplemented();
return;
}
if (method_call->method_name().compare("listen") == 0) {
stream_handler_.onListen(method_call->arguments(), &event_sink);
result->Success(nullptr);
return;
} else if (method_call->method_name().compare("cancel") == 0) {
stream_handler_.onCancel(method_call->arguments());
result->Success(nullptr);
return;
} else {
result->NotImplemented();
}
};

messenger_->SetMessageHandler(name_, std::move(binary_handler));
}

private:
BinaryMessenger *messenger_;
std::string name_;
const MethodCodec<T> *codec_;
StreamHandler<T> stream_handler_;
};

} // namespace flutter

#endif // FLUTTER_SHELL_PLATFORM_COMMON_CPP_CLIENT_WRAPPER_INCLUDE_FLUTTER_EVENT_CHANNEL_H_
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@
#include <string>

#include <flutter_plugin_registrar.h>
#include <flutter_texture_registrar.h>
cloudwebrtc marked this conversation as resolved.
Show resolved Hide resolved

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

namespace flutter {

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

TextureRegistrar* textures() { return textures_.get(); }

// Takes ownership of |plugin|.
//
Expand All @@ -58,6 +62,8 @@ class PluginRegistrar {
FlutterDesktopPluginRegistrarRef registrar_;

std::unique_ptr<BinaryMessenger> messenger_;

std::unique_ptr<TextureRegistrar> textures_;

// 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,45 @@
// 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 <stdint.h>
#include <memory>

namespace flutter {

class Texture {
public:
virtual ~Texture() {}

virtual std::shared_ptr<GLFWPixelBuffer> CopyTextureBuffer(size_t width,
size_t height) = 0;
};

class TextureRegistrar {
public:
virtual ~TextureRegistrar() {}

/**
* Register a |texture| object and return textureId.
cloudwebrtc marked this conversation as resolved.
Show resolved Hide resolved
*/
virtual int64_t RegisterTexture(Texture *texture) = 0;

/**
* Mark a texture buffer is ready.
cloudwebrtc marked this conversation as resolved.
Show resolved Hide resolved
*/
virtual void MarkTextureFrameAvailable(int64_t texture_id) = 0;

/**
* Unregister an existing Texture object.
*/
virtual void UnregisterTexture(int64_t texture_id) = 0;
};

} // namespace flutter

#endif // FLUTTER_SHELL_PLATFORM_COMMON_CPP_CLIENT_WRAPPER_INCLUDE_FLUTTER_TEXTURE_REGISTRAR_H_
41 changes: 41 additions & 0 deletions shell/platform/common/cpp/client_wrapper/plugin_registrar.cc
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,53 @@ void BinaryMessengerImpl::SetMessageHandler(const std::string& channel,
ForwardToHandler, message_handler);
}

// Wrapper around a FlutterDesktopTextureRegistrarRef that implements the
// TextureRegistrar API.
class TextureRegistrarImpl : public TextureRegistrar {
public:
explicit TextureRegistrarImpl(
FlutterDesktopTextureRegistrarRef texture_registrar)
: texture_registrar_(texture_registrar) {}

virtual ~TextureRegistrarImpl() = default;

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

virtual int64_t RegisterTexture(Texture* texture) override {
FlutterTexutreCallback callback =
[](size_t width, size_t height,
void* user_data) -> std::shared_ptr<GLFWPixelBuffer> {
return ((Texture*)user_data)->CopyTextureBuffer(width, height);
};
int64_t texture_id = FlutterDesktopRegisterExternalTexture(
cloudwebrtc marked this conversation as resolved.
Show resolved Hide resolved
texture_registrar_, callback, texture);
return texture_id;
}

virtual void MarkTextureFrameAvailable(int64_t texture_id) override {
FlutterDesktopMarkExternalTextureFrameAvailable(texture_registrar_,
texture_id);
}

virtual void UnregisterTexture(int64_t texture_id) override {
FlutterDesktopUnregisterExternalTexture(texture_registrar_, texture_id);
}

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

// PluginRegistrar:

PluginRegistrar::PluginRegistrar(FlutterDesktopPluginRegistrarRef registrar)
: registrar_(registrar) {
auto core_messenger = FlutterDesktopRegistrarGetMessenger(registrar_);
messenger_ = std::make_unique<BinaryMessengerImpl>(core_messenger);
auto texture_registrar = FlutterDesktopGetTextureRegistrar(registrar_);
textures_ = std::make_unique<TextureRegistrarImpl>(texture_registrar);
}

PluginRegistrar::~PluginRegistrar() {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,30 @@ FlutterDesktopMessengerRef FlutterDesktopRegistrarGetMessenger(
return reinterpret_cast<FlutterDesktopMessengerRef>(1);
}

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

int64_t FlutterDesktopRegisterExternalTexture(
FlutterDesktopTextureRegistrarRef texture_registrar,
FlutterTexutreCallback texture_callback,
void* user_data) {
return -1;
}

bool FlutterDesktopUnregisterExternalTexture(
FlutterDesktopTextureRegistrarRef texture_registrar,
int64_t texture_id) {
return false;
}

bool FlutterDesktopMarkExternalTextureFrameAvailable(
FlutterDesktopTextureRegistrarRef texture_registrar,
int64_t texture_id) {
return false;
}

void FlutterDesktopRegistrarEnableInputBlocking(
FlutterDesktopPluginRegistrarRef registrar,
const char* channel) {
Expand Down
5 changes: 5 additions & 0 deletions shell/platform/common/cpp/public/flutter_plugin_registrar.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

#include "flutter_export.h"
#include "flutter_messenger.h"
#include "flutter_texture_registrar.h"

#if defined(__cplusplus)
extern "C" {
Expand All @@ -22,6 +23,10 @@ typedef struct FlutterDesktopPluginRegistrar* FlutterDesktopPluginRegistrarRef;
FLUTTER_EXPORT FlutterDesktopMessengerRef
FlutterDesktopRegistrarGetMessenger(FlutterDesktopPluginRegistrarRef registrar);

// Returns the texture registrar associated with this registrar.
FLUTTER_EXPORT FlutterDesktopTextureRegistrarRef
FlutterDesktopGetTextureRegistrar(FlutterDesktopPluginRegistrarRef registrar);

// Enables input blocking on the given channel.
//
// If set, then the Flutter window will disable input callbacks
Expand Down
46 changes: 46 additions & 0 deletions shell/platform/common/cpp/public/flutter_texture_registrar.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// 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_PUBLIC_FLUTTER_TEXTURE_REGISTRAR_H_
#define FLUTTER_SHELL_PLATFORM_COMMON_CPP_PUBLIC_FLUTTER_TEXTURE_REGISTRAR_H_

#include <stddef.h>
#include <stdint.h>
#include <memory>

#include "flutter_export.h"

#if defined(__cplusplus)
extern "C" {
#endif

// Opaque reference to a texture registrar.
typedef struct FlutterDesktopTextureRegistrar*
FlutterDesktopTextureRegistrarRef;

struct GLFWPixelBuffer {
cloudwebrtc marked this conversation as resolved.
Show resolved Hide resolved
std::shared_ptr<uint8_t> buffer;
size_t width;
size_t height;
};

typedef std::shared_ptr<GLFWPixelBuffer> (*FlutterTexutreCallback)(
size_t width, size_t height, void* user_data);

FLUTTER_EXPORT int64_t FlutterDesktopRegisterExternalTexture(
FlutterDesktopTextureRegistrarRef texture_registrar,
FlutterTexutreCallback texture_callback, void* user_data);

FLUTTER_EXPORT bool FlutterDesktopUnregisterExternalTexture(
cloudwebrtc marked this conversation as resolved.
Show resolved Hide resolved
FlutterDesktopTextureRegistrarRef texture_registrar, int64_t texture_id);

// Mark that a new texture frame is available for a given texture identifier.
FLUTTER_EXPORT bool FlutterDesktopMarkExternalTextureFrameAvailable(
cloudwebrtc marked this conversation as resolved.
Show resolved Hide resolved
FlutterDesktopTextureRegistrarRef texture_registrar, int64_t texture_id);

#if defined(__cplusplus)
} // extern "C"
#endif

#endif // FLUTTER_SHELL_PLATFORM_COMMON_CPP_PUBLIC_FLUTTER_TEXTURE_REGISTRAR_H_
2 changes: 2 additions & 0 deletions shell/platform/glfw/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ source_set("flutter_glfw_headers") {

source_set("flutter_glfw") {
sources = [
"external_texture_gl.h",
"external_texture_gl.cc",
"flutter_glfw.cc",
"glfw_event_loop.cc",
"glfw_event_loop.h",
Expand Down
Loading