-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathHTMLTextureRenderer.cs
562 lines (522 loc) · 18.2 KB
/
HTMLTextureRenderer.cs
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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using CefGlue;
using SlimDX.Direct3D9;
using VVVV.Core;
using VVVV.Core.Logging;
using VVVV.PluginInterfaces.V2.EX9;
using VVVV.Utils.VMath;
using System.Text;
using System.Globalization;
using VVVV.Utils.IO;
using System.Xml.Linq;
using EX9 = SlimDX.Direct3D9;
using VVVV.PluginInterfaces.V2;
using System.Threading;
using System.Diagnostics;
using System.Windows.Forms;
namespace VVVV.Nodes.Texture.HTML
{
public class HTMLTextureRenderer : IDisposable
{
public const string DEFAULT_URL = "http://vvvv.org";
public const int DEFAULT_WIDTH = 640;
public const int DEFAULT_HEIGHT = 480;
private readonly DXResource<EX9.Texture, CefBrowser> FTextureResource;
private readonly List<EX9.Texture> FTextures = new List<EX9.Texture>();
private volatile bool FEnabled;
private CefBrowser FBrowser;
private string FUrl;
private string FHtml = string.Empty;
private readonly object FLock = new object();
private XDocument FCurrentDom;
private string FCurrentUrl;
private string FErrorText;
private ILogger Logger;
private readonly AutoResetEvent FBrowserAttachedEvent = new AutoResetEvent(false);
private readonly AutoResetEvent FBrowserDetachedEvent = new AutoResetEvent(false);
private bool FDisablePopups = false;
public HTMLTextureRenderer(ILogger logger)
{
Logger = logger;
var settings = new CefBrowserSettings();
settings.DeveloperToolsDisabled = true;
// TODO: Needs to be disabled or WebGL won't work at all.
settings.AcceleratedCompositingEnabled = false;
settings.FileAccessFromFileUrlsAllowed = true;
settings.UniversalAccessFromFileUrlsAllowed = true;
//settings.PageCacheDisabled = true;
//settings.PluginsDisabled = true;
using (var windowInfo = new CefWindowInfo())
{
windowInfo.TransparentPainting = true;
windowInfo.SetAsOffScreen(IntPtr.Zero);
var webClient = new WebClient(this);
CefBrowser.Create(windowInfo, webClient, DEFAULT_URL, settings);
// Block till the browser is attached
FBrowserAttachedEvent.WaitOne();
}
FTextureResource = new DXResource<EX9.Texture, CefBrowser>(
FBrowser,
CreateTexture,
UpdateTexture,
DestroyTexture
);
}
public void Dispose()
{
if (FBrowser != null)
{
FBrowser.Close();
FBrowserDetachedEvent.WaitOne();
lock (FLock)
{
FBrowser.Dispose();
FBrowser = null;
FTextureResource.Dispose();
}
FBrowserAttachedEvent.Dispose();
FBrowserDetachedEvent.Dispose();
}
}
public void LoadURL(string url)
{
if (FUrl != url)
{
FUrl = string.IsNullOrEmpty(url) ? "about:blank" : url;
DoLoadURL(FUrl);
}
}
internal void DoLoadURL(string url)
{
using (var mainFrame = FBrowser.GetMainFrame())
{
mainFrame.LoadURL(url);
}
}
public void LoadString(string html, string baseUrl)
{
if (FUrl != baseUrl || FHtml != html)
{
FUrl = baseUrl;
FHtml = html ?? string.Empty;
using (var mainFrame = FBrowser.GetMainFrame())
{
mainFrame.LoadString(html, baseUrl);
}
}
}
public void ExecuteJavaScript(string javaScript)
{
using (var mainFrame = FBrowser.GetMainFrame())
{
mainFrame.ExecuteJavaScript(javaScript, string.Empty, 0);
}
}
public void UpdateDom()
{
lock (FLock)
{
using (var frame = FBrowser.GetMainFrame())
{
UpdateDom(frame);
}
}
}
private void UpdateDom(CefFrame frame)
{
var visitor = new DomVisitor(this);
// Note: Call is async. Will at some point lead to OnVisitDom a few lines down.
frame.VisitDom(visitor);
}
public void Reload()
{
FBrowser.Reload();
}
public void ReloadIC()
{
FBrowser.ReloadIgnoreCache();
}
public DXResource<EX9.Texture, CefBrowser> TextureResource
{
get { return FTextureResource; }
}
public XDocument CurrentDom
{
get
{
lock (FLock)
{
return FCurrentDom;
}
}
}
public string CurrentUrl
{
get
{
lock (FLock)
{
return FCurrentUrl;
}
}
}
public void SetPopUpBehaviour(bool status)
{
FDisablePopups = status;
}
public bool GetPopUpBehaviour()
{
return FDisablePopups;
}
public string CurrentError
{
get
{
lock (FLock)
{
return FErrorText;
}
}
}
private Size FSize = new Size(DEFAULT_WIDTH, DEFAULT_HEIGHT);
public Size Size
{
get { return FSize; }
set
{
// Normalize inputs
var size = new Size(Math.Max(1, value.Width), Math.Max(1, value.Height));
if (size != FSize)
{
lock (FTextures)
{
FTextureResource.Dispose();
FSize = size;
FBrowser.SetSize(CefPaintElementType.View, size.Width, size.Height);
}
}
}
}
private double FZoomLevel;
public double ZoomLevel
{
get { return FZoomLevel; }
set
{
if (FZoomLevel != value)
{
FBrowser.ZoomLevel = value;
FZoomLevel = value;
}
}
}
private Vector2D FScrollTo;
public Vector2D ScrollTo
{
get { return FScrollTo; }
set
{
if (FScrollTo != value)
{
using (var mainFrame = FBrowser.GetMainFrame())
{
var x = VMath.Map(value.x, 0, 1, 0, 1, TMapMode.Clamp);
var y = VMath.Map(value.y, 0, 1, 0, 1, TMapMode.Clamp);
mainFrame.ExecuteJavaScript(
string.Format(CultureInfo.InvariantCulture,
@"
var body = document.body,
html = document.documentElement;
var width = Math.max(body.scrollWidth, body.offsetWidth, html.clientWidth, html.scrollWidth, html.offsetWidth);
var height = Math.max(body.scrollHeight, body.offsetHeight, html.clientHeight, html.scrollHeight, html.offsetHeight);
window.scrollTo({0} * width, {1} * height);
",
x,
y
),
string.Empty,
0);
}
FScrollTo = value;
}
}
}
private MouseState FMouse;
public MouseState Mouse
{
get { return FMouse; }
set
{
if (FMouse != value)
{
var x = (int)VMath.Map(value.X, -1, 1, 0, FSize.Width, TMapMode.Clamp);
var y = (int)VMath.Map(value.Y, 1, -1, 0, FSize.Height, TMapMode.Clamp);
var mouseDown = FMouse.Buttons == MouseButtons.None && value.Buttons != MouseButtons.None;
var mouseUp = FMouse.Buttons != MouseButtons.None && value.Buttons == MouseButtons.None;
var button = value.Buttons;
if (mouseUp) button = FMouse.Buttons;
if (mouseDown || mouseUp)
{
switch (button)
{
case MouseButtons.Left:
FBrowser.SendMouseClickEvent(x, y, CefMouseButtonType.Left, mouseUp, 1);
break;
case MouseButtons.Middle:
FBrowser.SendMouseClickEvent(x, y, CefMouseButtonType.Middle, mouseUp, 1);
break;
case MouseButtons.Right:
FBrowser.SendMouseClickEvent(x, y, CefMouseButtonType.Right, mouseUp, 1);
break;
default:
throw new Exception(string.Format("Unknown mouse button {0}.", button));
}
}
else
{
FBrowser.SendMouseMoveEvent(x, y, false);
}
var mouseWheelDelta = value.MouseWheel - FMouse.MouseWheel;
if (mouseWheelDelta != 0)
{
FBrowser.SendMouseWheelEvent(x, y, mouseWheelDelta, 0);
}
FMouse = value;
}
}
}
private KeyboardState FKeyboard = KeyboardState.Empty;
public KeyboardState Keyboard
{
get { return FKeyboard; }
set
{
value = value ?? KeyboardState.Empty;
if (FKeyboard != value)
{
var isKeyUp = FKeyboard.KeyCodes.Count > value.KeyCodes.Count;
if (isKeyUp)
{
var releasedKeys = FKeyboard.KeyCodes.Except(value.KeyCodes);
var modifiers = (CefHandlerKeyEventModifiers)((int)(FKeyboard.Modifiers) >> 16);
foreach (var key in releasedKeys)
{
var cefKey = new CefKeyInfo((int)key, false, false);
FBrowser.SendKeyEvent(CefKeyType.KeyUp, cefKey, modifiers);
}
}
var isKeyDown = value.KeyCodes.Count > FKeyboard.KeyCodes.Count;
if (isKeyDown)
{
var pressedKeys = value.KeyCodes.Except(FKeyboard.KeyCodes);
var modifiers = (CefHandlerKeyEventModifiers)((int)(value.Modifiers) >> 16);
foreach (var key in pressedKeys)
{
var cefKey = new CefKeyInfo((int)key, false, false);
FBrowser.SendKeyEvent(CefKeyType.KeyDown, cefKey, modifiers);
}
}
if (!isKeyUp)
{
var keyChar = value.KeyChars.LastOrDefault();
if (keyChar != 0 && !(keyChar == '\t' || keyChar == '\b'))
{
var cefKey = new CefKeyInfo((int)keyChar, false, false);
var modifiers = (CefHandlerKeyEventModifiers)((int)(value.Modifiers) >> 16);
FBrowser.SendKeyEvent(CefKeyType.Char, cefKey, modifiers);
}
}
FKeyboard = value;
}
}
}
private int FLoadCount;
public bool IsLoading
{
get
{
lock (FLock)
{
return FLoadCount > 0;
}
}
}
public bool Enabled
{
get { return FEnabled; }
set
{
if (FEnabled != value)
{
lock (FLock)
{
if (FEnabled)
{
FBrowser.StopLoad();
}
FEnabled = value;
if (FEnabled)
{
FBrowser.Invalidate(new CefRect(0, 0, FSize.Width, FSize.Height));
}
}
}
}
}
internal void Attach(CefBrowser browser)
{
FBrowser = browser;
FBrowser.SetSize(CefPaintElementType.View, FSize.Width, FSize.Height);
FBrowser.SendFocusEvent(true);
FErrorText = string.Empty;
FBrowserAttachedEvent.Set();
}
internal void Detach()
{
FBrowserDetachedEvent.Set();
}
internal void Paint(CefRect[] cefRects, IntPtr buffer, int stride)
{
if (!FEnabled) return;
lock (FTextures)
{
try
{
for (int i = 0; i < cefRects.Length; i++)
{
var rect = new Rectangle(cefRects[i].X, cefRects[i].Y, cefRects[i].Width, cefRects[i].Height);
foreach (var texture in FTextures)
{
WriteToTexture(rect, buffer, stride, texture);
}
}
}
catch (Exception e)
{
FErrorText = e.ToString();
}
}
}
private void WriteToTexture(Rectangle rect, IntPtr buffer, int stride, EX9.Texture texture)
{
// Rect needs to be inside of Width/Height
rect = Rectangle.Intersect(new Rectangle(0, 0, FSize.Width, FSize.Height), rect);
if (rect == Rectangle.Empty) return;
var dataRect = texture.LockRectangle(0, rect, LockFlags.None);
try
{
var dataStream = dataRect.Data;
if (rect.Width == FSize.Width && rect.Height == FSize.Height && dataRect.Pitch == stride)
{
dataStream.WriteRange(buffer, FSize.Height * dataRect.Pitch);
}
else
{
var offset = stride * rect.Y + 4 * rect.X;
var source = buffer + offset;
for (int y = 0; y < rect.Height; y++)
{
dataStream.Position = y * dataRect.Pitch;
dataStream.WriteRange(source + y * stride, rect.Width * 4);
}
}
}
finally
{
texture.UnlockRectangle(0);
}
}
private EX9.Texture CreateTexture(CefBrowser browser, Device device)
{
// TODO: Fix exceptions on start up.
lock (FTextures)
{
var usage = Usage.None & ~Usage.AutoGenerateMipMap;
var pool = Pool.Managed;
if (device is DeviceEx)
{
usage = Usage.Dynamic & ~Usage.AutoGenerateMipMap;
pool = Pool.Default;
}
var texture = new EX9.Texture(device, FSize.Width, FSize.Height, 1, usage, Format.A8R8G8B8, pool);
var rect = new CefRect(0, 0, FSize.Width, FSize.Height);
FBrowser.Invalidate(rect);
FTextures.Add(texture);
return texture;
}
}
private void UpdateTexture(CefBrowser browser, EX9.Texture texture)
{
}
private void DestroyTexture(CefBrowser browser, EX9.Texture texture, DestroyReason reason)
{
lock (FTextures)
{
FTextures.Remove(texture);
texture.Dispose();
}
}
internal void OnLoadStart(CefFrame frame)
{
lock (FLock)
{
FLoadCount++;
if (frame.IsMain)
{
FCurrentDom = null;
FErrorText = string.Empty;
FCurrentUrl = frame.GetURL();
}
}
}
internal void OnLoadError(CefFrame frame, CefHandlerErrorCode errorCode, string failedUrl, string errorText)
{
lock (FLock)
{
FLoadCount--;
if (frame.IsMain)
{
FCurrentDom = null;
FErrorText = errorText;
}
}
}
internal void OnLoadEnd(CefFrame frame, int httpStatusCode)
{
lock (FLock)
{
FLoadCount--;
if (frame.IsMain)
{
FCurrentUrl = frame.GetURL();
UpdateDom(frame);
}
}
}
// Called on UI thread
internal void OnVisitDom(CefDomDocument document)
{
try
{
using (var xmlReader = new CefXmlReader(document))
{
var dom = XDocument.Load(xmlReader);
lock (FLock)
{
FCurrentDom = dom;
}
}
}
catch (Exception e)
{
lock (FLock)
{
FErrorText = e.ToString();
}
}
}
}
}