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
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
//! A glutin window implementation.
use crate::app;
use crate::context::GlContext;
use crate::events_loop::EventsLoop;
use crate::keyutils::keyboard_event_from_winit;
use crate::window_trait::{WindowPortsMethods, LINE_HEIGHT};
use euclid::{Angle, default::Size2D as UntypedSize2D, Point2D, Rotation3D, Scale, Size2D, UnknownUnit, Vector2D};
use gleam::gl;
use glutin::dpi::{LogicalPosition, LogicalSize, PhysicalSize};
#[cfg(target_os = "macos")]
use glutin::os::macos::{ActivationPolicy, WindowBuilderExt};
#[cfg(target_os = "linux")]
use glutin::os::unix::WindowExt;
use glutin::Api;
#[cfg(any(target_os = "linux", target_os = "windows"))]
use glutin::Icon;
use glutin::{ElementState, KeyboardInput, MouseButton, MouseScrollDelta, TouchPhase, VirtualKeyCode};
#[cfg(any(target_os = "linux", target_os = "windows"))]
use image;
use keyboard_types::{Key, KeyState, KeyboardEvent};
use servo::compositing::windowing::{AnimationState, MouseWindowEvent, WindowEvent};
use servo::compositing::windowing::{EmbedderCoordinates, WindowMethods};
use servo::embedder_traits::Cursor;
use servo::script_traits::{TouchEventType, WheelMode, WheelDelta};
use servo::servo_config::{opts, pref};
use servo::servo_geometry::DeviceIndependentPixel;
use servo::style_traits::DevicePixel;
use servo::webrender_api::ScrollLocation;
use servo::webrender_api::units::{DeviceIntPoint, DeviceIntRect, DeviceIntSize};
use servo_media::player::context::{GlApi, GlContext as PlayerGLContext, NativeDisplay};
use std::cell::{Cell, RefCell};
use std::mem;
use std::rc::Rc;
#[cfg(target_os = "windows")]
use winapi;
const MULTISAMPLES: u16 = 16;
#[cfg(target_os = "macos")]
fn builder_with_platform_options(mut builder: glutin::WindowBuilder) -> glutin::WindowBuilder {
if opts::get().output_file.is_some() {
// Prevent the window from showing in Dock.app, stealing focus,
// when generating an output file.
builder = builder.with_activation_policy(ActivationPolicy::Prohibited)
}
builder
}
#[cfg(not(target_os = "macos"))]
fn builder_with_platform_options(builder: glutin::WindowBuilder) -> glutin::WindowBuilder {
builder
}
pub struct Window {
gl_context: RefCell<GlContext>,
events_loop: Rc<RefCell<EventsLoop>>,
screen_size: Size2D<u32, DeviceIndependentPixel>,
inner_size: Cell<Size2D<u32, DeviceIndependentPixel>>,
mouse_down_button: Cell<Option<glutin::MouseButton>>,
mouse_down_point: Cell<Point2D<i32, DevicePixel>>,
primary_monitor: glutin::MonitorId,
event_queue: RefCell<Vec<WindowEvent>>,
mouse_pos: Cell<Point2D<i32, DevicePixel>>,
last_pressed: Cell<Option<KeyboardEvent>>,
animation_state: Cell<AnimationState>,
fullscreen: Cell<bool>,
gl: Rc<dyn gl::Gl>,
xr_rotation: Cell<Rotation3D<f32, UnknownUnit, UnknownUnit>>,
angle: bool,
enable_vsync: bool,
use_msaa: bool,
no_native_titlebar: bool,
device_pixels_per_px: Option<f32>,
}
#[cfg(not(target_os = "windows"))]
fn window_creation_scale_factor() -> Scale<f32, DeviceIndependentPixel, DevicePixel> {
Scale::new(1.0)
}
#[cfg(target_os = "windows")]
fn window_creation_scale_factor() -> Scale<f32, DeviceIndependentPixel, DevicePixel> {
let hdc = unsafe { winapi::um::winuser::GetDC(::std::ptr::null_mut()) };
let ppi = unsafe { winapi::um::wingdi::GetDeviceCaps(hdc, winapi::um::wingdi::LOGPIXELSY) };
Scale::new(ppi as f32 / 96.0)
}
impl Window {
pub fn new(
win_size: Size2D<u32, DeviceIndependentPixel>,
sharing: Option<&Window>,
events_loop: Rc<RefCell<EventsLoop>>,
angle: bool,
enable_vsync: bool,
use_msaa: bool,
no_native_titlebar: bool,
device_pixels_per_px: Option<f32>,
) -> Window {
let opts = opts::get();
// If there's no chrome, start off with the window invisible. It will be set to visible in
// `load_end()`. This avoids an ugly flash of unstyled content (especially important since
// unstyled content is white and chrome often has a transparent background). See issue
// #9996.
let visible = opts.output_file.is_none() && !no_native_titlebar;
let win_size: DeviceIntSize = (win_size.to_f32() * window_creation_scale_factor()).to_i32();
let width = win_size.to_untyped().width;
let height = win_size.to_untyped().height;
let mut window_builder = glutin::WindowBuilder::new()
.with_title("Servo".to_string())
.with_decorations(!no_native_titlebar)
.with_transparency(no_native_titlebar)
.with_dimensions(LogicalSize::new(width as f64, height as f64))
.with_visibility(visible)
.with_multitouch();
window_builder = builder_with_platform_options(window_builder);
let mut context_builder = glutin::ContextBuilder::new()
.with_gl(app::gl_version(angle))
.with_vsync(enable_vsync);
if use_msaa {
context_builder = context_builder.with_multisampling(MULTISAMPLES)
}
let context = match sharing {
Some(sharing) => sharing.gl_context.borrow().new_window(
context_builder,
window_builder,
events_loop.borrow().as_winit()
),
None => context_builder.build_windowed(window_builder, events_loop.borrow().as_winit()),
}.expect("Failed to create window.");
#[cfg(any(target_os = "linux", target_os = "windows"))]
{
let icon_bytes = include_bytes!("../../resources/servo64.png");
context.window().set_window_icon(Some(load_icon(icon_bytes)));
}
if let Some(sharing) = sharing {
debug!("Making window {:?} not current", sharing.gl_context.borrow().window().id());
sharing.gl_context.borrow_mut().make_not_current();
}
let context = unsafe {
debug!("Making window {:?} current", context.window().id());
context.make_current().expect("Couldn't make window current")
};
let primary_monitor = events_loop.borrow().as_winit().get_primary_monitor();
let PhysicalSize {
width: screen_width,
height: screen_height,
} = primary_monitor.get_dimensions();
let screen_size = Size2D::new(screen_width as u32, screen_height as u32);
// TODO(ajeffrey): can this fail?
let LogicalSize { width, height } = context
.window()
.get_inner_size()
.expect("Failed to get window inner size.");
let inner_size = Size2D::new(width as u32, height as u32);
context.window().show();
let gl = if let Some(sharing) = sharing {
sharing.gl.clone()
} else { match context.get_api() {
Api::OpenGl => unsafe {
gl::GlFns::load_with(|s| context.get_proc_address(s) as *const _)
},
Api::OpenGlEs => unsafe {
gl::GlesFns::load_with(|s| context.get_proc_address(s) as *const _)
},
Api::WebGl => unreachable!("webgl is unsupported"),
} };
gl.clear_color(0.6, 0.6, 0.6, 1.0);
gl.clear(gl::COLOR_BUFFER_BIT);
gl.finish();
let context = GlContext::Current(context);
debug!("Created window {:?}", context.window().id());
let window = Window {
gl_context: RefCell::new(context),
events_loop,
event_queue: RefCell::new(vec![]),
mouse_down_button: Cell::new(None),
mouse_down_point: Cell::new(Point2D::new(0, 0)),
mouse_pos: Cell::new(Point2D::new(0, 0)),
last_pressed: Cell::new(None),
gl: gl.clone(),
animation_state: Cell::new(AnimationState::Idle),
fullscreen: Cell::new(false),
inner_size: Cell::new(inner_size),
primary_monitor,
screen_size,
xr_rotation: Cell::new(Rotation3D::identity()),
angle,
enable_vsync,
use_msaa,
no_native_titlebar,
device_pixels_per_px,
};
window.present();
window
}
fn handle_received_character(&self, mut ch: char) {
info!("winit received character: {:?}", ch);
if ch.is_control() {
if ch as u8 >= 32 {
return;
}
// shift ASCII control characters to lowercase
ch = (ch as u8 + 96) as char;
}
let mut event = if let Some(event) = self.last_pressed.replace(None) {
event
} else if ch.is_ascii() {
// Some keys like Backspace emit a control character in winit
// but they are already dealt with in handle_keyboard_input
// so just ignore the character.
return;
} else {
// For combined characters like the letter e with an acute accent
// no keyboard event is emitted. A dummy event is created in this case.
KeyboardEvent::default()
};
event.key = Key::Character(ch.to_string());
self.event_queue
.borrow_mut()
.push(WindowEvent::Keyboard(event));
}
fn handle_keyboard_input(&self, input: KeyboardInput) {
let event = keyboard_event_from_winit(input);
if event.state == KeyState::Down && event.key == Key::Unidentified {
// If pressed and probably printable, we expect a ReceivedCharacter event.
self.last_pressed.set(Some(event));
} else if event.key != Key::Unidentified {
self.last_pressed.set(None);
self.handle_xr_rotation(&input);
self.event_queue
.borrow_mut()
.push(WindowEvent::Keyboard(event));
}
}
fn handle_xr_rotation(&self, input: &KeyboardInput) {
if input.state != glutin::ElementState::Pressed {
return;
}
let mut x = 0.0;
let mut y = 0.0;
match input.virtual_keycode {
Some(VirtualKeyCode::Up) => x = 1.0,
Some(VirtualKeyCode::Down) => x = -1.0,
Some(VirtualKeyCode::Left) => y = 1.0,
Some(VirtualKeyCode::Right) => y = -1.0,
_ => return,
};
if input.modifiers.shift {
x = 10.0 * x;
y = 10.0 * y;
}
let x: Rotation3D<_, UnknownUnit, UnknownUnit> = Rotation3D::around_x(Angle::degrees(x));
let y: Rotation3D<_, UnknownUnit, UnknownUnit> = Rotation3D::around_y(Angle::degrees(y));
let rotation = self.xr_rotation.get().post_rotate(&x).post_rotate(&y);
self.xr_rotation.set(rotation);
}
/// Helper function to handle a click
fn handle_mouse(
&self,
button: glutin::MouseButton,
action: glutin::ElementState,
coords: Point2D<i32, DevicePixel>,
) {
use servo::script_traits::MouseButton;
let max_pixel_dist = 10.0 * self.servo_hidpi_factor().get();
let event = match action {
ElementState::Pressed => {
self.mouse_down_point.set(coords);
self.mouse_down_button.set(Some(button));
MouseWindowEvent::MouseDown(MouseButton::Left, coords.to_f32())
},
ElementState::Released => {
let mouse_up_event = MouseWindowEvent::MouseUp(MouseButton::Left, coords.to_f32());
match self.mouse_down_button.get() {
None => mouse_up_event,
Some(but) if button == but => {
let pixel_dist = self.mouse_down_point.get() - coords;
let pixel_dist =
((pixel_dist.x * pixel_dist.x + pixel_dist.y * pixel_dist.y) as f32)
.sqrt();
if pixel_dist < max_pixel_dist {
self.event_queue
.borrow_mut()
.push(WindowEvent::MouseWindowEventClass(mouse_up_event));
MouseWindowEvent::Click(MouseButton::Left, coords.to_f32())
} else {
mouse_up_event
}
},
Some(_) => mouse_up_event,
}
},
};
self.event_queue
.borrow_mut()
.push(WindowEvent::MouseWindowEventClass(event));
}
fn device_hidpi_factor(&self) -> Scale<f32, DeviceIndependentPixel, DevicePixel> {
Scale::new(self.gl_context.borrow().window().get_hidpi_factor() as f32)
}
fn servo_hidpi_factor(&self) -> Scale<f32, DeviceIndependentPixel, DevicePixel> {
match self.device_pixels_per_px {
Some(device_pixels_per_px) => Scale::new(device_pixels_per_px),
_ => match opts::get().output_file {
Some(_) => Scale::new(1.0),
None => self.device_hidpi_factor(),
},
}
}
}
impl WindowPortsMethods for Window {
fn get_events(&self) -> Vec<WindowEvent> {
mem::replace(&mut *self.event_queue.borrow_mut(), Vec::new())
}
fn has_events(&self) -> bool {
!self.event_queue.borrow().is_empty()
}
fn page_height(&self) -> f32 {
let dpr = self.servo_hidpi_factor();
let size = self
.gl_context
.borrow()
.window()
.get_inner_size()
.expect("Failed to get window inner size.");
size.height as f32 * dpr.get()
}
fn set_title(&self, title: &str) {
self.gl_context.borrow().window().set_title(title);
}
fn set_inner_size(&self, size: DeviceIntSize) {
let size = size.to_f32() / self.device_hidpi_factor();
self.gl_context.borrow_mut().window()
.set_inner_size(LogicalSize::new(size.width.into(), size.height.into()))
}
fn set_position(&self, point: DeviceIntPoint) {
let point = point.to_f32() / self.device_hidpi_factor();
self.gl_context.borrow_mut().window()
.set_position(LogicalPosition::new(point.x.into(), point.y.into()))
}
fn set_fullscreen(&self, state: bool) {
if self.fullscreen.get() != state {
self.gl_context.borrow_mut().window()
.set_fullscreen(if state { Some(self.primary_monitor.clone()) } else { None });
}
self.fullscreen.set(state);
}
fn get_fullscreen(&self) -> bool {
return self.fullscreen.get();
}
fn set_cursor(&self, cursor: Cursor) {
use glutin::MouseCursor;
let winit_cursor = match cursor {
Cursor::Default => MouseCursor::Default,
Cursor::Pointer => MouseCursor::Hand,
Cursor::ContextMenu => MouseCursor::ContextMenu,
Cursor::Help => MouseCursor::Help,
Cursor::Progress => MouseCursor::Progress,
Cursor::Wait => MouseCursor::Wait,
Cursor::Cell => MouseCursor::Cell,
Cursor::Crosshair => MouseCursor::Crosshair,
Cursor::Text => MouseCursor::Text,
Cursor::VerticalText => MouseCursor::VerticalText,
Cursor::Alias => MouseCursor::Alias,
Cursor::Copy => MouseCursor::Copy,
Cursor::Move => MouseCursor::Move,
Cursor::NoDrop => MouseCursor::NoDrop,
Cursor::NotAllowed => MouseCursor::NotAllowed,
Cursor::Grab => MouseCursor::Grab,
Cursor::Grabbing => MouseCursor::Grabbing,
Cursor::EResize => MouseCursor::EResize,
Cursor::NResize => MouseCursor::NResize,
Cursor::NeResize => MouseCursor::NeResize,
Cursor::NwResize => MouseCursor::NwResize,
Cursor::SResize => MouseCursor::SResize,
Cursor::SeResize => MouseCursor::SeResize,
Cursor::SwResize => MouseCursor::SwResize,
Cursor::WResize => MouseCursor::WResize,
Cursor::EwResize => MouseCursor::EwResize,
Cursor::NsResize => MouseCursor::NsResize,
Cursor::NeswResize => MouseCursor::NeswResize,
Cursor::NwseResize => MouseCursor::NwseResize,
Cursor::ColResize => MouseCursor::ColResize,
Cursor::RowResize => MouseCursor::RowResize,
Cursor::AllScroll => MouseCursor::AllScroll,
Cursor::ZoomIn => MouseCursor::ZoomIn,
Cursor::ZoomOut => MouseCursor::ZoomOut,
_ => MouseCursor::Default,
};
self.gl_context.borrow_mut().window().set_cursor(winit_cursor);
}
fn is_animating(&self) -> bool {
self.animation_state.get() == AnimationState::Animating
}
fn id(&self) -> glutin::WindowId {
self.gl_context.borrow().window().id()
}
fn winit_event_to_servo_event(&self, event: glutin::WindowEvent) {
match event {
glutin::WindowEvent::ReceivedCharacter(ch) => self.handle_received_character(ch),
glutin::WindowEvent::KeyboardInput { input, .. } => self.handle_keyboard_input(input),
glutin::WindowEvent::MouseInput { state, button, .. } => {
if button == MouseButton::Left || button == MouseButton::Right {
self.handle_mouse(button, state, self.mouse_pos.get());
}
},
glutin::WindowEvent::CursorMoved { position, .. } => {
let pos = position.to_physical(self.device_hidpi_factor().get() as f64);
let (x, y): (i32, i32) = pos.into();
self.mouse_pos.set(Point2D::new(x, y));
self.event_queue
.borrow_mut()
.push(WindowEvent::MouseWindowMoveEventClass(Point2D::new(
x as f32, y as f32,
)));
},
glutin::WindowEvent::MouseWheel { delta, phase, .. } => {
let (mut dx, mut dy, mode) = match delta {
MouseScrollDelta::LineDelta(dx, dy) => (dx as f64, (dy * LINE_HEIGHT) as f64,
WheelMode::DeltaLine),
MouseScrollDelta::PixelDelta(position) => {
let position =
position.to_physical(self.device_hidpi_factor().get() as f64);
(position.x as f64, position.y as f64, WheelMode::DeltaPixel)
},
};
// Create wheel event before snapping to the major axis of movement
let wheel_delta = WheelDelta { x: dx, y: dy, z: 0.0, mode };
let pos = self.mouse_pos.get();
let position = Point2D::new(pos.x as f32, pos.y as f32);
let wheel_event = WindowEvent::Wheel(wheel_delta, position);
// Scroll events snap to the major axis of movement, with vertical
// preferred over horizontal.
if dy.abs() >= dx.abs() {
dx = 0.0;
} else {
dy = 0.0;
}
let scroll_location = ScrollLocation::Delta(Vector2D::new(dx as f32, dy as f32));
let phase = winit_phase_to_touch_event_type(phase);
let scroll_event = WindowEvent::Scroll(scroll_location, self.mouse_pos.get(), phase);
// Send events
self.event_queue.borrow_mut().push(wheel_event);
self.event_queue.borrow_mut().push(scroll_event);
},
glutin::WindowEvent::Touch(touch) => {
use servo::script_traits::TouchId;
let phase = winit_phase_to_touch_event_type(touch.phase);
let id = TouchId(touch.id as i32);
let position = touch
.location
.to_physical(self.device_hidpi_factor().get() as f64);
let point = Point2D::new(position.x as f32, position.y as f32);
self.event_queue
.borrow_mut()
.push(WindowEvent::Touch(phase, id, point));
},
glutin::WindowEvent::Refresh => {
self.event_queue.borrow_mut().push(WindowEvent::Refresh);
},
glutin::WindowEvent::CloseRequested => {
self.event_queue.borrow_mut().push(WindowEvent::Quit);
},
glutin::WindowEvent::Resized(size) => {
let physical_size = size.to_physical(self.device_hidpi_factor().get() as f64);
self.gl_context.borrow_mut().resize(physical_size);
// window.set_inner_size() takes DeviceIndependentPixel.
let (width, height) = size.into();
let new_size = Size2D::new(width, height);
if self.inner_size.get() != new_size {
self.inner_size.set(new_size);
self.event_queue.borrow_mut().push(WindowEvent::Resize);
}
},
_ => {},
}
}
}
impl webxr::glwindow::GlWindow for Window {
fn make_current(&self) {
debug!("Making window {:?} current", self.gl_context.borrow().window().id());
self.gl_context.borrow_mut().make_current();
}
fn swap_buffers(&self) {
debug!("Swapping buffers on window {:?}", self.gl_context.borrow().window().id());
self.gl_context.borrow().swap_buffers();
self.gl_context.borrow_mut().make_not_current();
}
fn size(&self) -> UntypedSize2D<gl::GLsizei> {
let dpr = self.device_hidpi_factor().get() as f64;
let LogicalSize { width, height } = self
.gl_context
.borrow()
.window()
.get_inner_size()
.expect("Failed to get window inner size.");
Size2D::new(width * dpr, height *dpr).to_i32()
}
fn get_rotation(&self) -> Rotation3D<f32, UnknownUnit, UnknownUnit> {
self.xr_rotation.get().clone()
}
fn new_window(&self) -> Result<Rc<dyn webxr::glwindow::GlWindow>, ()> {
let window = Rc::new(Window::new(
self.inner_size.get(),
Some(self),
self.events_loop.clone(),
self.angle,
self.enable_vsync,
self.use_msaa,
self.no_native_titlebar,
self.device_pixels_per_px,
));
app::register_window(window.clone());
Ok(window)
}
}
impl WindowMethods for Window {
fn gl(&self) -> Rc<dyn gl::Gl> {
self.gl.clone()
}
fn get_coordinates(&self) -> EmbedderCoordinates {
// TODO(ajeffrey): can this fail?
let dpr = self.device_hidpi_factor();
let LogicalSize { width, height } = self
.gl_context
.borrow()
.window()
.get_outer_size()
.expect("Failed to get window outer size.");
let LogicalPosition { x, y } = self
.gl_context
.borrow()
.window()
.get_position()
.unwrap_or(LogicalPosition::new(0., 0.));
let win_size = (Size2D::new(width as f32, height as f32) * dpr).to_i32();
let win_origin = (Point2D::new(x as f32, y as f32) * dpr).to_i32();
let screen = (self.screen_size.to_f32() * dpr).to_i32();
let LogicalSize { width, height } = self
.gl_context
.borrow()
.window()
.get_inner_size()
.expect("Failed to get window inner size.");
let inner_size = (Size2D::new(width as f32, height as f32) * dpr).to_i32();
let viewport = DeviceIntRect::new(Point2D::zero(), inner_size);
let framebuffer = DeviceIntSize::from_untyped(viewport.size.to_untyped());
EmbedderCoordinates {
viewport,
framebuffer,
window: (win_size, win_origin),
screen: screen,
// FIXME: Glutin doesn't have API for available size. Fallback to screen size
screen_avail: screen,
hidpi_factor: self.servo_hidpi_factor(),
}
}
fn present(&self) {
self.gl_context.borrow().swap_buffers();
self.gl_context.borrow_mut().make_not_current();
}
fn set_animation_state(&self, state: AnimationState) {
self.animation_state.set(state);
}
fn make_gl_context_current(&self) {
self.gl_context.borrow_mut().make_current();
}
fn get_gl_context(&self) -> PlayerGLContext {
if pref!(media.glvideo.enabled) {
return self.gl_context.borrow().raw_context();
}
return PlayerGLContext::Unknown;
}
fn get_native_display(&self) -> NativeDisplay {
if !pref!(media.glvideo.enabled) {
return NativeDisplay::Unknown;
}
#[cfg(target_os = "linux")]
{
if let Some(display) = self.gl_context.borrow().window().get_wayland_display() {
return NativeDisplay::Wayland(display as usize);
} else if let Some(display) =
self.gl_context.borrow().window().get_xlib_display()
{
return NativeDisplay::X11(display as usize);
}
}
#[cfg(any(target_os = "linux", target_os = "windows"))]
{
if let Some(display) = self.gl_context.borrow().egl_display() {
return NativeDisplay::Egl(display as usize);
}
}
NativeDisplay::Unknown
}
fn get_gl_api(&self) -> GlApi {
let api = self.gl_context.borrow().get_api();
let version = self.gl.get_string(gl::VERSION);
let version = version.trim_start_matches("OpenGL ES ");
let mut values = version.split(&['.', ' '][..]);
let major = values
.next()
.and_then(|v| v.parse::<u32>().ok())
.unwrap_or(1);
let minor = values
.next()
.and_then(|v| v.parse::<u32>().ok())
.unwrap_or(20);
match api {
glutin::Api::OpenGl if major >= 3 && minor >= 2 => GlApi::OpenGL3,
glutin::Api::OpenGl => GlApi::OpenGL,
glutin::Api::OpenGlEs if major > 1 => GlApi::Gles2,
glutin::Api::OpenGlEs => GlApi::Gles1,
_ => GlApi::None,
}
}
}
fn winit_phase_to_touch_event_type(phase: TouchPhase) -> TouchEventType {
match phase {
TouchPhase::Started => TouchEventType::Down,
TouchPhase::Moved => TouchEventType::Move,
TouchPhase::Ended => TouchEventType::Up,
TouchPhase::Cancelled => TouchEventType::Cancel,
}
}
#[cfg(any(target_os = "linux", target_os = "windows"))]
fn load_icon(icon_bytes: &[u8]) -> Icon {
let (icon_rgba, icon_width, icon_height) = {
use image::{GenericImageView, Pixel};
let image = image::load_from_memory(icon_bytes).expect("Failed to load icon");
let (width, height) = image.dimensions();
let mut rgba = Vec::with_capacity((width * height) as usize * 4);
for (_, _, pixel) in image.pixels() {
rgba.extend_from_slice(&pixel.to_rgba().0);
}
(rgba, width, height)
};
Icon::from_rgba(icon_rgba, icon_width, icon_height).expect("Failed to load icon")
}
|