-
Notifications
You must be signed in to change notification settings - Fork 2
/
windows_texture_test_plugin.cpp
184 lines (146 loc) · 5.44 KB
/
windows_texture_test_plugin.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#include "include/windows_texture_test/windows_texture_test_plugin.h"
// This must be included before many other Windows headers.
#include <windows.h>
#include <flutter/method_channel.h>
#include <flutter/plugin_registrar_windows.h>
#include <flutter/standard_method_codec.h>
#include <map>
#include <memory>
#include <sstream>
#include <thread>
namespace
{
void FillColorBars(FlutterDesktopPixelBuffer *buffer, bool reverse)
{
const size_t num_bars = 8;
const uint32_t bars[num_bars] = {0xFFFFFFFF, 0xFF00C0C0, 0xFFC0C000, 0xFF00C000, 0xFFC000C0, 0xFF0000C0, 0xFFC00000, 0xFF000000};
uint32_t *word = (uint32_t *)buffer->buffer;
auto width = buffer->width;
auto height = buffer->height;
auto column_width = width / num_bars;
for (size_t y = 0; y < height; y++)
{
for (size_t x = 0; x < width; x++)
{
auto index = x / column_width;
*(word++) = reverse ? bars[num_bars - index - 1] : bars[index];
}
}
}
void StartTimer(int interval, std::function<void(void)> func)
{
std::thread([=]() {
while (true)
{
func();
std::this_thread::sleep_for(
std::chrono::milliseconds(interval));
}
}).detach();
}
class ColorBarTexture
{
public:
ColorBarTexture(size_t width, size_t height);
virtual ~ColorBarTexture();
const FlutterDesktopPixelBuffer *CopyPixelBuffer(size_t width, size_t height);
private:
std::unique_ptr<FlutterDesktopPixelBuffer> buffer1_;
std::unique_ptr<FlutterDesktopPixelBuffer> buffer2_;
std::unique_ptr<uint8_t> pixels1_;
std::unique_ptr<uint8_t> pixels2_;
size_t request_count_ = 0;
};
ColorBarTexture::ColorBarTexture(size_t width, size_t height)
{
size_t size = width * height * 4;
pixels1_.reset(new uint8_t[size]);
pixels2_.reset(new uint8_t[size]);
buffer1_ = std::make_unique<FlutterDesktopPixelBuffer>();
buffer1_->buffer = pixels1_.get();
buffer1_->width = width;
buffer1_->height = height;
buffer2_ = std::make_unique<FlutterDesktopPixelBuffer>();
buffer2_->buffer = pixels2_.get();
buffer2_->width = width;
buffer2_->height = height;
FillColorBars(buffer1_.get(), false);
FillColorBars(buffer2_.get(), true);
}
const FlutterDesktopPixelBuffer *ColorBarTexture::CopyPixelBuffer(size_t width, size_t height)
{
if (request_count_++ % 2 == 0)
{
return buffer2_.get();
}
return buffer1_.get();
}
ColorBarTexture::~ColorBarTexture() {}
class WindowsTextureTestPlugin : public flutter::Plugin
{
public:
static void RegisterWithRegistrar(flutter::PluginRegistrarWindows *registrar);
WindowsTextureTestPlugin(flutter::TextureRegistrar *textures);
virtual ~WindowsTextureTestPlugin();
private:
// Called when a method is called on this plugin's channel from Dart.
void HandleMethodCall(
const flutter::MethodCall<flutter::EncodableValue> &method_call,
std::unique_ptr<flutter::MethodResult<flutter::EncodableValue>> result);
flutter::TextureRegistrar *textures_;
std::unique_ptr<flutter::TextureVariant> texture_;
std::unique_ptr<ColorBarTexture> color_bar_texture_;
};
// static
void WindowsTextureTestPlugin::RegisterWithRegistrar(
flutter::PluginRegistrarWindows *registrar)
{
auto channel =
std::make_unique<flutter::MethodChannel<flutter::EncodableValue>>(
registrar->messenger(), "windows_texture_test",
&flutter::StandardMethodCodec::GetInstance());
auto plugin = std::make_unique<WindowsTextureTestPlugin>(registrar->texture_registrar());
channel->SetMethodCallHandler(
[plugin_pointer = plugin.get()](const auto &call, auto result) {
plugin_pointer->HandleMethodCall(call, std::move(result));
});
registrar->AddPlugin(std::move(plugin));
}
WindowsTextureTestPlugin::WindowsTextureTestPlugin(flutter::TextureRegistrar *textures) : textures_(textures) {}
WindowsTextureTestPlugin::~WindowsTextureTestPlugin() {}
void WindowsTextureTestPlugin::HandleMethodCall(
const flutter::MethodCall<flutter::EncodableValue> &method_call,
std::unique_ptr<flutter::MethodResult<flutter::EncodableValue>> result)
{
const std::string &method_name = method_call.method_name();
if (method_name.compare("initialize") == 0)
{
color_bar_texture_ = std::make_unique<ColorBarTexture>(1920, 1080);
texture_ = std::make_unique<flutter::TextureVariant>(flutter::PixelBufferTexture([this](size_t width, size_t height) -> const FlutterDesktopPixelBuffer * {
return color_bar_texture_->CopyPixelBuffer(width, height);
}));
int64_t texture_id = textures_->RegisterTexture(texture_.get());
auto response = flutter::EncodableValue(flutter::EncodableMap{
{flutter::EncodableValue("textureId"),
flutter::EncodableValue(texture_id)},
});
result->Success(response);
// Update the texture @ 10 Hz
// Setting this to 60 Hz might cause epileptic shocks :D
StartTimer(1000 / 10, [&, texture_id]() {
textures_->MarkTextureFrameAvailable(texture_id);
});
}
else
{
result->NotImplemented();
}
}
} // namespace
void WindowsTextureTestPluginRegisterWithRegistrar(
FlutterDesktopPluginRegistrarRef registrar)
{
WindowsTextureTestPlugin::RegisterWithRegistrar(
flutter::PluginRegistrarManager::GetInstance()
->GetRegistrar<flutter::PluginRegistrarWindows>(registrar));
}