-
Notifications
You must be signed in to change notification settings - Fork 13
/
flutter_windows_angle_d3d_texture_plugin.cpp
425 lines (407 loc) · 18.4 KB
/
flutter_windows_angle_d3d_texture_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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
#include "flutter_windows_angle_d3d_texture_plugin.h"
// Flutter
#include <flutter/method_channel.h>
#include <flutter/plugin_registrar_windows.h>
#include <flutter/standard_method_codec.h>
#include <flutter/texture_registrar.h>
// Standard C++ STL
#include <fstream>
#include <iostream>
#include <thread>
#include <vector>
#define STRING(s) #s
namespace flutter_windows_angle_d3d_texture {
constexpr auto kD3D11Texture2DWidth = 1280;
constexpr auto kD3D11Texture2DHeight = 720;
GLuint CompileShader(GLenum type, const std::string& source) {
GLuint shader = glCreateShader(type);
const char* source_c_str[1] = {source.c_str()};
glShaderSource(shader, 1, source_c_str, NULL);
glCompileShader(shader);
GLint result;
glGetShaderiv(shader, GL_COMPILE_STATUS, &result);
if (result == 0) {
GLint size;
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &size);
std::vector<GLchar> text(size);
glGetShaderInfoLog(shader, (GLsizei)text.size(), NULL, text.data());
auto error = std::string("Shader Compilation Failed: ");
error += text.data();
std::cerr << text.data() << std::endl;
}
return shader;
}
GLuint CompileProgram(const std::string& vsSource,
const std::string& fsSource) {
GLuint program = glCreateProgram();
if (program == 0) {
std::cerr << "Program Creation Failed." << std::endl;
}
GLuint vs = CompileShader(GL_VERTEX_SHADER, vsSource);
GLuint fs = CompileShader(GL_FRAGMENT_SHADER, fsSource);
if (vs == 0 || fs == 0) {
glDeleteShader(fs);
glDeleteShader(vs);
glDeleteProgram(program);
return 0;
}
glAttachShader(program, vs);
glDeleteShader(vs);
glAttachShader(program, fs);
glDeleteShader(fs);
glLinkProgram(program);
GLint status;
glGetProgramiv(program, GL_LINK_STATUS, &status);
if (status == 0) {
GLint size;
glGetProgramiv(program, GL_INFO_LOG_LENGTH, &size);
std::vector<GLchar> text(size);
glGetProgramInfoLog(program, (GLsizei)text.size(), NULL, text.data());
auto error = std::string("Program Link Failed: ");
error += text.data();
std::cerr << text.data() << std::endl;
}
return program;
}
static void* GetProcAddress(void*, const char* name) {
auto rv = reinterpret_cast<void*>(eglGetProcAddress(name));
std::cout << name << ": " << rv << std::endl;
return rv;
}
static void OnMPVEvents(void* ctx) {
auto* plugin = static_cast<FlutterWindowsAngleD3dTexturePlugin*>(ctx);
mpv_render_context_update(plugin->render_context());
}
static void RenderContextUpdate(void* ctx) {
auto* plugin = static_cast<FlutterWindowsAngleD3dTexturePlugin*>(ctx);
if (eglMakeCurrent(plugin->display(), plugin->surface(), plugin->surface(),
plugin->context()) == 0) {
std::cerr << "eglMakeCurrent FAILED." << std::endl;
}
// if ((mpv_render_context_update(plugin->render_context()) &
// MPV_RENDER_UPDATE_FRAME)) {
// int fbo = -1;
// glGetIntegerv(GL_FRAMEBUFFER_BINDING, &fbo);
// mpv_opengl_fbo opengl_fbo = {fbo, kD3D11Texture2DWidth,
// kD3D11Texture2DHeight, 0};
// int flip_y{1};
// mpv_render_param params[] = {{MPV_RENDER_PARAM_OPENGL_FBO, &opengl_fbo},
// {MPV_RENDER_PARAM_FLIP_Y, &flip_y},
// {MPV_RENDER_PARAM_INVALID, NULL}};
// mpv_render_context_render(plugin->render_context(), params);
// eglSwapBuffers(plugin->display(), plugin->surface());
// std::cout << "eglSwapBuffers" << std::endl;
// glFinish();
// std::cout << "glFinish" << std::endl;
// }
mpv_opengl_fbo fbo{0, kD3D11Texture2DWidth, kD3D11Texture2DHeight, 0};
int flip_y{1};
mpv_render_param params[] = {{MPV_RENDER_PARAM_OPENGL_FBO, &fbo},
{MPV_RENDER_PARAM_FLIP_Y, &flip_y},
{MPV_RENDER_PARAM_INVALID, nullptr}};
mpv_render_context_render(plugin->render_context(), params);
std::cout << "mpv_render_context_render" << std::endl;
eglSwapBuffers(plugin->display(), plugin->surface());
std::cout << "eglSwapBuffers" << std::endl;
glFlush();
std::cout << "glFlush" << std::endl;
plugin->texture_registrar()->MarkTextureFrameAvailable(plugin->texture_id());
if (eglMakeCurrent(plugin->display(), 0, 0, plugin->context()) == 0) {
std::cerr << "eglMakeCurrent FAILED." << std::endl;
}
}
void FlutterWindowsAngleD3dTexturePlugin::RegisterWithRegistrar(
flutter::PluginRegistrarWindows* registrar) {
auto plugin = std::make_unique<FlutterWindowsAngleD3dTexturePlugin>(
registrar,
std::make_unique<flutter::MethodChannel<flutter::EncodableValue>>(
registrar->messenger(),
"com.alexmercerind/flutter_windows_angle_d3d_texture",
&flutter::StandardMethodCodec::GetInstance()),
registrar->texture_registrar());
plugin->channel()->SetMethodCallHandler(
[plugin_pointer = plugin.get()](const auto& call, auto result) {
plugin_pointer->HandleMethodCall(call, std::move(result));
});
registrar->AddPlugin(std::move(plugin));
}
FlutterWindowsAngleD3dTexturePlugin::FlutterWindowsAngleD3dTexturePlugin(
flutter::PluginRegistrarWindows* registrar,
std::unique_ptr<flutter::MethodChannel<flutter::EncodableValue>> channel,
flutter::TextureRegistrar* texture_registrar)
: registrar_(registrar),
channel_(std::move(channel)),
texture_registrar_(texture_registrar) {}
FlutterWindowsAngleD3dTexturePlugin::~FlutterWindowsAngleD3dTexturePlugin() {}
void FlutterWindowsAngleD3dTexturePlugin::HandleMethodCall(
const flutter::MethodCall<flutter::EncodableValue>& method_call,
std::unique_ptr<flutter::MethodResult<flutter::EncodableValue>> result) {
if (method_call.method_name().compare("CreateID3D11Device") == 0) {
HRESULT hr = S_OK;
if (FAILED(hr = ::D3D11CreateDevice(NULL, D3D_DRIVER_TYPE_HARDWARE, NULL,
D3D11_CREATE_DEVICE_VIDEO_SUPPORT, NULL,
0, D3D11_SDK_VERSION, &d3d11_device_,
NULL, &d3d11_device_context_))) {
std::cerr << "D3D11CreateDevice + D3D_DRIVER_TYPE_HARDWARE FAILED."
<< std::endl;
if (FAILED(hr = ::D3D11CreateDevice(NULL, D3D_DRIVER_TYPE_SOFTWARE, NULL,
D3D11_CREATE_DEVICE_VIDEO_SUPPORT,
NULL, 0, D3D11_SDK_VERSION,
&d3d11_device_, NULL,
&d3d11_device_context_))) {
std::cerr << "D3D11CreateDevice + D3D_DRIVER_TYPE_SOFTWARE FAILED."
<< std::endl;
if (FAILED(hr = ::D3D11CreateDevice(NULL, D3D_DRIVER_TYPE_UNKNOWN, NULL,
D3D11_CREATE_DEVICE_VIDEO_SUPPORT,
NULL, 0, D3D11_SDK_VERSION,
&d3d11_device_, NULL,
&d3d11_device_context_))) {
std::cerr << "D3D11CreateDevice + D3D_DRIVER_TYPE_UNKNOWN FAILED."
<< std::endl;
}
}
}
result->Success(flutter::EncodableValue(nullptr));
} else if (method_call.method_name().compare(
"ID3D11Device::CreateTexture2D") == 0) {
D3D11_TEXTURE2D_DESC d3d11_texture2D_desc = {0};
d3d11_texture2D_desc.Width = kD3D11Texture2DWidth;
d3d11_texture2D_desc.Height = kD3D11Texture2DHeight;
d3d11_texture2D_desc.Format = DXGI_FORMAT_B8G8R8A8_UNORM;
d3d11_texture2D_desc.MipLevels = 1;
d3d11_texture2D_desc.ArraySize = 1;
d3d11_texture2D_desc.SampleDesc.Count = 1;
d3d11_texture2D_desc.SampleDesc.Quality = 0;
d3d11_texture2D_desc.Usage = D3D11_USAGE_DEFAULT;
d3d11_texture2D_desc.BindFlags =
D3D11_BIND_RENDER_TARGET | D3D11_BIND_SHADER_RESOURCE;
d3d11_texture2D_desc.CPUAccessFlags = 0;
d3d11_texture2D_desc.MiscFlags = D3D11_RESOURCE_MISC_SHARED;
HRESULT hr = d3d11_device_->CreateTexture2D(&d3d11_texture2D_desc, nullptr,
&d3d11_texture_2D_);
if FAILED (hr) {
std::cerr << "CreateTexture2D FAILED." << std::endl;
}
result->Success(flutter::EncodableValue(nullptr));
} else if (method_call.method_name().compare(
"eglCreatePbufferFromClientBuffer") == 0) {
ComPtr<IDXGIResource> dxgi_resource;
HRESULT hr = d3d11_texture_2D_.As(&dxgi_resource);
if FAILED (hr) {
std::cerr << "Microsoft::WRL::ComPtr<ID3D11Texture2D>::As<IDXGIResource>"
" FAILED."
<< std::endl;
}
hr = dxgi_resource->GetSharedHandle(&shared_handle_);
if FAILED (hr) {
std::cerr
<< "Microsoft::WRL::ComPtr<IDXGIResource>::GetSharedHandle FAILED."
<< std::endl;
}
EGLint buffer_attributes[] = {EGL_WIDTH,
kD3D11Texture2DWidth,
EGL_HEIGHT,
kD3D11Texture2DHeight,
EGL_TEXTURE_TARGET,
EGL_TEXTURE_2D,
EGL_TEXTURE_FORMAT,
EGL_TEXTURE_RGBA,
EGL_NONE};
const EGLint config_attributes[] = {EGL_RED_SIZE, 8, EGL_GREEN_SIZE, 8,
EGL_BLUE_SIZE, 8, EGL_ALPHA_SIZE, 8,
EGL_DEPTH_SIZE, 8, EGL_STENCIL_SIZE, 8,
EGL_NONE};
const EGLint context_attributes[] = {EGL_CONTEXT_CLIENT_VERSION, 2,
EGL_NONE};
const EGLint default_display_attributes[] = {
EGL_PLATFORM_ANGLE_TYPE_ANGLE,
EGL_PLATFORM_ANGLE_TYPE_D3D11_ANGLE,
EGL_PLATFORM_ANGLE_ENABLE_AUTOMATIC_TRIM_ANGLE,
EGL_TRUE,
EGL_NONE,
};
const EGLint fl9_3_display_attributes[] = {
EGL_PLATFORM_ANGLE_TYPE_ANGLE,
EGL_PLATFORM_ANGLE_TYPE_D3D11_ANGLE,
EGL_PLATFORM_ANGLE_MAX_VERSION_MAJOR_ANGLE,
9,
EGL_PLATFORM_ANGLE_MAX_VERSION_MINOR_ANGLE,
3,
EGL_PLATFORM_ANGLE_ENABLE_AUTOMATIC_TRIM_ANGLE,
EGL_TRUE,
EGL_NONE,
};
const EGLint warp_display_attributes[] = {
EGL_PLATFORM_ANGLE_TYPE_ANGLE,
EGL_PLATFORM_ANGLE_TYPE_D3D11_ANGLE,
EGL_PLATFORM_ANGLE_ENABLE_AUTOMATIC_TRIM_ANGLE,
EGL_TRUE,
EGL_NONE,
};
PFNEGLGETPLATFORMDISPLAYEXTPROC eglGetPlatformDisplayEXT =
reinterpret_cast<PFNEGLGETPLATFORMDISPLAYEXTPROC>(
eglGetProcAddress("eglGetPlatformDisplayEXT"));
display_ =
eglGetPlatformDisplayEXT(EGL_PLATFORM_ANGLE_ANGLE, EGL_DEFAULT_DISPLAY,
default_display_attributes);
if (display_ == EGL_NO_DISPLAY) {
std::cerr << "eglGetPlatformDisplayEXT FAILED." << std::endl;
}
if (eglInitialize(display_, NULL, NULL) == EGL_FALSE) {
std::cerr << "eglInitialize FAILED." << std::endl;
display_ = eglGetPlatformDisplayEXT(EGL_PLATFORM_ANGLE_ANGLE,
EGL_DEFAULT_DISPLAY,
fl9_3_display_attributes);
if (display_ == EGL_NO_DISPLAY) {
std::cerr << "eglGetPlatformDisplayEXT FAILED." << std::endl;
}
if (eglInitialize(display_, NULL, NULL) == EGL_FALSE) {
std::cerr << "eglInitialize FAILED." << std::endl;
display_ = eglGetPlatformDisplayEXT(EGL_PLATFORM_ANGLE_ANGLE,
EGL_DEFAULT_DISPLAY,
warp_display_attributes);
if (display_ == EGL_NO_DISPLAY) {
std::cerr << "eglGetPlatformDisplayEXT FAILED." << std::endl;
}
if (eglInitialize(display_, NULL, NULL) == EGL_FALSE) {
std::cerr << "eglInitialize FAILED." << std::endl;
}
}
}
EGLint count = 0;
if ((eglChooseConfig(display_, config_attributes, &config_, 1, &count) ==
EGL_FALSE) ||
(count == 0)) {
std::cerr << "eglChooseConfig FAILED." << std::endl;
}
context_ =
eglCreateContext(display_, config_, EGL_NO_CONTEXT, context_attributes);
if (context_ == EGL_NO_CONTEXT) {
std::cerr << "eglCreateContext FAILED." << std::endl;
}
surface_ = eglCreatePbufferFromClientBuffer(
display_, EGL_D3D_TEXTURE_2D_SHARE_HANDLE_ANGLE, shared_handle_,
config_, buffer_attributes);
if (surface_ == EGL_NO_SURFACE) {
std::cerr << "eglCreatePbufferFromClientBuffer FAILED." << std::endl;
}
result->Success(flutter::EncodableValue(nullptr));
} else if (method_call.method_name().compare("eglBindTexImage") == 0) {
GLuint texture;
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
eglBindTexImage(display_, surface_, EGL_BACK_BUFFER);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
result->Success(flutter::EncodableValue(nullptr));
} else if (method_call.method_name().compare(
"flutter::TextureRegistrar::RegisterTexture") == 0) {
gpu_surface_descriptor_ =
std::make_unique<FlutterDesktopGpuSurfaceDescriptor>();
gpu_surface_descriptor_->struct_size =
sizeof(FlutterDesktopGpuSurfaceDescriptor);
gpu_surface_descriptor_->handle = shared_handle_;
gpu_surface_descriptor_->width = gpu_surface_descriptor_->visible_width =
kD3D11Texture2DWidth;
gpu_surface_descriptor_->height = gpu_surface_descriptor_->visible_height =
kD3D11Texture2DHeight;
gpu_surface_descriptor_->release_context = nullptr;
gpu_surface_descriptor_->release_callback = [](void* release_context) {};
gpu_surface_descriptor_->format = kFlutterDesktopPixelFormatBGRA8888;
gpu_surface_texture_ =
std::make_unique<flutter::TextureVariant>(flutter::GpuSurfaceTexture(
kFlutterDesktopGpuSurfaceTypeDxgiSharedHandle,
[&](size_t width,
size_t height) -> const FlutterDesktopGpuSurfaceDescriptor* {
d3d11_texture_2D_->AddRef();
return gpu_surface_descriptor_.get();
}));
texture_id_ =
texture_registrar_->RegisterTexture(gpu_surface_texture_.get());
result->Success(flutter::EncodableValue(texture_id_));
} else if (method_call.method_name().compare("eglMakeCurrent") == 0) {
if (eglMakeCurrent(display_, surface_, surface_, context_) == 0) {
std::cerr << "eglMakeCurrent FAILED." << std::endl;
}
// constexpr char kVertexShader[] = R"(attribute vec4 vPosition;
// void main()
// {
// gl_Position = vPosition;
// })";
// constexpr char kFragmentShader[] = R"(precision mediump float;
// void main()
// {
// gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);
// })";
// GLuint program = CompileProgram(kVertexShader, kFragmentShader);
// std::cout << program << std::endl;
// glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
// GLfloat vertices[] = {
// 0.0f, -0.5f, 0.0f, -0.5f, 0.5f, 0.0f, 0.5f, 0.5f, 0.0f,
// };
// glClear(GL_COLOR_BUFFER_BIT);
// glUseProgram(program);
// glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, vertices);
// glEnableVertexAttribArray(0);
// glDrawArrays(GL_TRIANGLES, 0, 3);
// |glFinish| must to be present.
// Maybe last line written at
// https://github.com/Microsoft/angle/wiki/Interop-with-other-DirectX-code#using-opengl-es-content-in-other-directx-rendering
// gives a bit of sense to this.
// glViewport(0, 0, kD3D11Texture2DWidth, kD3D11Texture2DHeight);
// glFinish();
result->Success(flutter::EncodableValue(nullptr));
} else if (method_call.method_name().compare("mpv_render_context_create") ==
0) {
mpv_handle_ = mpv_create();
mpv_initialize(mpv_handle_);
mpv_set_option_string(mpv_handle_, "input-default-bindings", "yes");
mpv_set_option_string(mpv_handle_, "input-vo-keyboard", "yes");
int val = 0;
mpv_set_option(mpv_handle_, "osc", MPV_FORMAT_FLAG, &val);
mpv_set_option_string(mpv_handle_, "terminal", "yes");
mpv_set_option_string(mpv_handle_, "msg-level", "all=v");
mpv_set_option_string(mpv_handle_, "log-file", "log.txt");
mpv_set_option_string(mpv_handle_, "gpu-context", "angle");
mpv_set_option_string(mpv_handle_, "gpu-api", "opengl");
mpv_set_option_string(mpv_handle_, "hwdec", "d3d11va");
mpv_set_option_string(mpv_handle_, "angle-d3d11-feature-level", "11_0");
mpv_set_option_string(mpv_handle_, "angle-renderer", "d3d11");
mpv_set_option_string(mpv_handle_, "gpu-dumb-mode", "yes");
mpv_set_option_string(mpv_handle_, "opengl-hwdec-interop", "d3d11-egl");
mpv_set_option_string(mpv_handle_, "gpu-hwdec-interop", "d3d11-egl");
mpv_set_option_string(mpv_handle_, "vo", "libmpv");
mpv_opengl_init_params gl_init_params{GetProcAddress, nullptr};
// int32_t advanced_control = 1;
mpv_render_param params[]{
{MPV_RENDER_PARAM_API_TYPE,
const_cast<char*>(MPV_RENDER_API_TYPE_OPENGL)},
{MPV_RENDER_PARAM_OPENGL_INIT_PARAMS, &gl_init_params},
// {MPV_RENDER_PARAM_ADVANCED_CONTROL, &advanced_control},
{MPV_RENDER_PARAM_INVALID, nullptr}};
if (mpv_render_context_create(&mpv_render_context_, mpv_handle_, params) <
0) {
std::cerr << "mpv_render_context_create FAILED." << std::endl;
}
mpv_set_wakeup_callback(mpv_handle_, OnMPVEvents,
reinterpret_cast<void*>(this));
mpv_render_context_set_update_callback(mpv_render_context_,
RenderContextUpdate,
reinterpret_cast<void*>(this));
mpv_observe_property(mpv_handle_, 0, "duration", MPV_FORMAT_DOUBLE);
mpv_observe_property(mpv_handle_, 0, "time-pos", MPV_FORMAT_DOUBLE);
auto volume = 100.0;
mpv_set_property(mpv_handle_, "volume", MPV_FORMAT_DOUBLE, &volume);
result->Success(flutter::EncodableValue(nullptr));
} else if (method_call.method_name().compare("mpv_command") == 0) {
auto file = std::get<std::string>(*method_call.arguments());
const char* command[] = {"loadfile", file.c_str(), "replace", NULL};
mpv_command(mpv_handle_, command);
auto pause = false;
mpv_set_property(mpv_handle_, "pause", MPV_FORMAT_FLAG, &pause);
result->Success(flutter::EncodableValue(nullptr));
} else {
result->NotImplemented();
}
}
} // namespace flutter_windows_angle_d3d_texture