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 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514
// SPDX-License-Identifier: LGPL-3.0-or-later OR MPL-2.0
// This file is a part of `theo`.
//
// `theo` is free software: you can redistribute it and/or modify it under the terms of
// either:
//
// * GNU Lesser General Public License as published by the Free Software Foundation, either
// version 3 of the License, or (at your option) any later version.
// * Mozilla Public License as published by the Mozilla Foundation, version 2.
//
// `theo` is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
// without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// See the GNU Lesser General Public License or the Mozilla Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License and the Mozilla
// Public License along with `theo`. If not, see <https://www.gnu.org/licenses/>.
//! A generic [`piet`] rendering context for all windowing and graphics backends.
//!
//! Windowing frameworks like [`winit`] do not provide a way to draw into them by default. This decision
//! is intentional; it allows the user to choose which graphics backend that they'd like to use, and also
//! makes maintaining the windowing code much simpler. For games (what [`winit`] was originally designed
//! for), usually a 3D rendering context like [`wgpu`] or [`glow`] would be used in this case. However,
//! GUI applications will need a 2D vector graphics context.
//!
//! [`piet`] is a 2D graphics abstraction that can be used with many different graphics backends. However,
//! [`piet`]'s default implementation, [`piet-common`], is difficult to integrate with windowing systems.
//! [`theo`] aims to bridge this gap by providing a generic [`piet`] rendering context that easily
//! integrates with windowing systems.
//!
//! Rather than going through drawing APIs like [`cairo`] and DirectX, `theo` directly uses GPU APIs in
//! order to render to the window. This allows for better performance and greater flexibility, and also
//! ensures that much of the rendering logic is safe. This also reduces the number of dynamic
//! dependencies that your final program needs to rely on.
//!
//! `theo` prioritizes versatility and performance. By default, `theo` uses an optimized GPU backend for
//! rendering. If the GPU is not available, `theo` will fall back to software rendering.
//!
//! ## Usage Example
//!
//! First, users must create a [`Display`], which represents the root display of the system. From here,
//! users should create [`Surface`]s, which represent drawing areas. Finally, a [`Surface`] can be used
//! to create the [`RenderContext`] type, which is used to draw.
//!
//! ```no_run
//! use piet::{RenderContext as _, kurbo::Circle};
//! use theo::{Display, Surface, RenderContext};
//! # struct MyDisplay;
//! # unsafe impl raw_window_handle::HasRawDisplayHandle for MyDisplay {
//! # fn raw_display_handle(&self) -> raw_window_handle::RawDisplayHandle {
//! # unimplemented!()
//! # }
//! # }
//! # #[derive(Copy, Clone)]
//! # struct Window;
//! # unsafe impl raw_window_handle::HasRawWindowHandle for Window {
//! # fn raw_window_handle(&self) -> raw_window_handle::RawWindowHandle {
//! # unimplemented!()
//! # }
//! # }
//! # impl Window {
//! # fn width(&self) -> u32 { 0 }
//! # fn height(&self) -> u32 { 0 }
//! # fn on_draw<R>(&self, f: impl FnOnce() -> R) { f(); }
//! # }
//! # let my_display = MyDisplay;
//! # let window = Window;
//!
//! # futures_lite::future::block_on(async move {
//! // Create a display using a display handle from your windowing framework.
//! // It must implement `raw_window_handle::HasRawDisplayHandle`.
//! let mut display = unsafe {
//! Display::builder()
//! .build(&my_display)
//! .expect("failed to create display")
//! };
//!
//! // Create a surface using a window handle from your windowing framework.
//! // It must implement `raw_window_handle::HasRawWindowHandle`.
//! let surface_future = unsafe {
//! display.make_surface(
//! &window,
//! window.width(),
//! window.height()
//! )
//! };
//!
//! // make_surface returns a future that needs to be polled.
//! let mut surface = surface_future.await.expect("failed to create surface");
//!
//! // Set up drawing logic.
//! window.on_draw(move || async move {
//! // Create the render context.
//! let mut ctx = RenderContext::new(
//! &mut display,
//! &mut surface,
//! window.width(),
//! window.height()
//! ).expect("failed to create render context");
//!
//! // Clear the screen and draw a circle.
//! ctx.clear(None, piet::Color::WHITE);
//! ctx.fill(
//! &Circle::new((200.0, 200.0), 50.0),
//! &piet::Color::RED
//! );
//!
//! // Finish drawing.
//! ctx.finish().expect("failed to finish drawing");
//!
//! // Present the display.
//! drop(ctx);
//! display.present().await;
//! });
//! # });
//! ```
//!
//! See the documentation for the [`piet`] crate for more information on how to use the drawing API.
//!
//! # Backends
//!
//! As of the time of writing, `theo` supports the following backends:
//!
//! - [`wgpu`] backend (enabled with the `wgpu` feature), which uses the [`piet-wgpu`] crate to render
//! to the window. This backend supports all of the graphics APIs that `wgpu` supports, including
//! Vulkan, Metal, and DirectX 11/12.
//! - [`glow`] backend (enabled with the `gl` feature), which uses the [`piet-glow`] crate to render to
//! the window. [`glutin`] is used on desktop platforms to create the OpenGL context, and [`glow`] is
//! used to interact with the OpenGL API. This backend supports OpenGL 3.2 and above.
//! - A software rasterization backend. [`tiny-skia`] is used to render to a bitmap, and then
//! [`softbuffer`] is used to copy the bitmap to the window. This backend is enabled by default and is
//! used when no other backend is available.
//!
//! # Performance
//!
//! As `theo` implements most of its own rendering logic, this can lead to serious performance
//! degradations if used improperly, especially on the software rasterization backend. In some cases,
//! compiling `theo` on Debug Mode rather than Release Mode can half the frame rate of the application.
//! If you are experiencing low frame rates with `theo`, make sure that you are compiling it on Release
//! Mode.
//!
//! In addition, gradient brushes are optimized in such a way that the actual gradient needs to be
//! computed only once. However, this means that, if you re-instantiate the brush every time, the
//! gradient will be re-computed every time. This can lead to serious performance degradations even on
//! hardware-accelerated backends. The solution is to cache the brushes that you use. For instance,
//! instead of doing this:
//!
//! ```no_compile
//! let gradient = /* ... */;
//! window.on_draw(|| {
//! let mut ctx = /* ... */;
//! ctx.fill(&Circle::new((200.0, 200.0), 50.0), &gradient);
//! })
//! ```
//!
//! Do this, making sure to cache the gradient brush:
//!
//! ```no_compile
//! let gradient = /* ... */;
//! let mut gradient_brush = None;
//! window.on_draw(|| {
//! let mut ctx = /* ... */;
//! let gradient_brush = gradient_brush.get_or_insert_with(|| {
//! ctx.gradient_brush(gradient.clone()).unwrap()
//! });
//! ctx.fill(&Circle::new((200.0, 200.0), 50.0), gradient_brush);
//! })
//! ```
//!
//! `theo` explicitly opts into a thread-unsafe model. Not only is thread-unsafe code more performant,
//! but these API types are usually thread-unsafe anyways.
//!
//! [`cairo`]: https://www.cairographics.org/
//! [`softbuffer`]: https://crates.io/crates/softbuffer
//! [`tiny-skia`]: https://crates.io/crates/tiny-skia
//! [`piet-wgpu`]: https://crates.io/crates/piet-wgpu
//! [`piet-glow`]: https://crates.io/crates/piet-glow
//! [`glutin`]: https://crates.io/crates/glutin
//! [`piet`]: https://crates.io/crates/piet
//! [`piet-common`]: https://crates.io/crates/piet-common
//! [`winit`]: https://crates.io/crates/winit
//! [`wgpu`]: https://crates.io/crates/wgpu
//! [`glow`]: https://crates.io/crates/glow
//! [`theo`]: https://crates.io/crates/theo
#[cfg(feature = "wgpu")]
extern crate wgpu0 as wgpu;
#[cfg(all(feature = "gl", not(target_arch = "wasm32")))]
mod desktop_gl;
mod swrast;
mod text;
#[cfg(all(feature = "gl", target_arch = "wasm32"))]
mod web_gl;
#[cfg(feature = "wgpu")]
#[path = "wgpu.rs"]
mod wgpu_backend;
use piet::kurbo::{Affine, Point, Shape, Size};
use piet::{kurbo::Rect, Error};
use piet::{FixedGradient, ImageFormat, InterpolationMode, IntoBrush, StrokeStyle};
use raw_window_handle::{
HasRawDisplayHandle, HasRawWindowHandle, RawDisplayHandle, RawWindowHandle,
};
use std::borrow::Cow;
use std::cell::Cell;
use std::ffi::c_void;
use std::fmt;
use std::marker::PhantomData;
use std::rc::Rc;
pub use text::{Text, TextLayout, TextLayoutBuilder};
std::thread_local! {
// Make sure that we don't try to multiple contexts per thread.
static HAS_CONTEXT: Cell<bool> = Cell::new(false);
}
/// An error handler for GLX.
pub type XlibErrorHook = Box<dyn Fn(*mut c_void, *mut c_void) -> bool + Send + Sync>;
/// An error handler for GLX.
type XlibErrorHookRegistrar = Box<dyn Fn(XlibErrorHook)>;
/// A builder containing system-specific information to create a [`Display`].
///
/// The [`DisplayBuilder`] is used to create a [`Display`]. It allows the user to submit some
/// parameters to the [`Display`] to customize its behavior. It is also used to provide essential
/// parameters on some platforms; for instance, on X11, an [`XlibErrorHook`] is required to use
/// the GLX backend.
///
/// # Examples
///
/// ```no_run
/// use theo::Display;
/// # struct MyDisplay;
/// # unsafe impl raw_window_handle::HasRawDisplayHandle for MyDisplay {
/// # fn raw_display_handle(&self) -> raw_window_handle::RawDisplayHandle {
/// # unimplemented!()
/// # }
/// # }
/// # struct Window;
/// # unsafe impl raw_window_handle::HasRawWindowHandle for Window {
/// # fn raw_window_handle(&self) -> raw_window_handle::RawWindowHandle {
/// # unimplemented!()
/// # }
/// # }
/// # let my_display = MyDisplay;
/// # let window = Window;
/// # fn register_x11_error_hook(_: theo::XlibErrorHook) {}
///
/// let mut builder = Display::builder();
///
/// // Provide a window handle to bootstrap the context.
/// builder = builder.window(&window);
///
/// // Provide an error hook for GLX.
/// builder = builder.glx_error_hook(register_x11_error_hook);
///
/// // Force using software rendering.
/// builder = builder.force_swrast(true);
///
/// // Disable use of transparency.
/// builder = builder.transparent(false);
///
/// // Create the display.
/// let display = unsafe { builder.build(&my_display) };
/// ```
pub struct DisplayBuilder {
/// The raw window handle to use to bootstrap the context.
///
/// This is only necessary for WGL bootstrapping.
window: Option<RawWindowHandle>,
/// The error handler for GLX.
glx_error_hook: Option<XlibErrorHookRegistrar>,
/// Whether or not we should support transparent backgrounds.
transparent: bool,
/// Force software rendering.
force_swrast: bool,
_thread_unsafe: PhantomData<*mut ()>,
}
impl Default for DisplayBuilder {
fn default() -> Self {
Self {
window: None,
glx_error_hook: None,
transparent: true,
force_swrast: false,
_thread_unsafe: PhantomData,
}
}
}
impl DisplayBuilder {
/// Create a new [`DisplayBuilder`] with default parameters.
///
/// # Examples
///
/// ```
/// use theo::DisplayBuilder;
///
/// let builder = DisplayBuilder::new();
/// ```
pub fn new() -> Self {
Self::default()
}
/// Set the raw window handle to use to bootstrap the context.
///
/// This is only necessary for WGL bootstrapping. A window is necessary for querying for the
/// WGL extensions. If you don't provide a window, the context will be created with fewer
/// available extensions. This is not necessary for any other platforms or backends.
///
/// # Examples
///
/// ```no_run
/// use theo::DisplayBuilder;
/// use winit::window::Window;
///
/// # fn create_window() -> Window {
/// # unimplemented!()
/// # }
/// let window: Window = create_window();
///
/// let mut builder = DisplayBuilder::new();
///
/// // Only needed on Windows.
/// #[cfg(target_os = "windows")]
/// {
/// builder = builder.window(&window);
/// }
/// ```
pub fn window(mut self, window: impl HasRawWindowHandle) -> Self {
self.window = Some(window.raw_window_handle());
self
}
/// Set the error handler for GLX.
///
/// For the GLX platform, an error handler is required in order to properly handle errors.
/// Error handling in Xlib is part of the global state, so there needs to be a single "source of
/// truth" for error handling. This "source of truth" should support taking a closure that
/// handles the error. This closure is then called whenever an error occurs.
///
/// For `theo`, this method allows passing in a closure that allows adding an error handler to
/// the global state. This closure is then called whenever an error occurs.
///
/// For [`winit`], you should pass in [`register_xlib_error_hook`].
///
/// [`winit`]: https://crates.io/crates/winit
/// [`register_xlib_error_hook`]: https://docs.rs/winit/0.28.5/winit/platform/x11/fn.register_xlib_error_hook.html
///
/// # Examples
///
/// ```no_run
/// # #[cfg(not(any(windows, target_arch = "wasm32")))]
/// # {
/// use std::os::raw::c_int;
/// use std::ptr;
/// use std::sync::Mutex;
///
/// use theo::{DisplayBuilder, XlibErrorHook};
/// use x11_dl::xlib::{Display, Xlib, XErrorEvent};
///
/// // A list of error handlers.
/// static ERROR_HANDLERS: Mutex<Vec<XlibErrorHook>> = Mutex::new(Vec::new());
///
/// // Register an error handler function.
/// // Note: This is a not a production-ready error handler. A real error handler should
/// // handle panics and interop with the rest of the application.
/// unsafe {
/// unsafe extern "C" fn error_handler(
/// display: *mut Display,
/// event: *mut XErrorEvent
/// ) -> c_int {
/// let mut handlers = ERROR_HANDLERS.lock().unwrap();
/// for handler in &*handlers {
/// if (handler)(display.cast(), event.cast()) {
/// break;
/// }
/// }
/// 0
/// }
///
/// let xlib = Xlib::open().unwrap();
/// let display = (xlib.XOpenDisplay)(ptr::null());
/// (xlib.XSetErrorHandler)(Some(error_handler));
/// }
///
/// let mut builder = DisplayBuilder::new();
///
/// // Provide an error hook for GLX.
/// builder = builder.glx_error_hook(|hook| {
/// // Add the error hook to the list of error handlers.
/// ERROR_HANDLERS.lock().unwrap().push(hook);
/// });
/// # }
/// ```
pub fn glx_error_hook(mut self, hook: impl Fn(XlibErrorHook) + 'static) -> Self {
self.glx_error_hook = Some(Box::new(hook));
self
}
/// Set whether or not we should support transparent backgrounds.
///
/// Some backends, such as the software rasterizer, do not support transparency. On the other hand,
/// others, such as EGL, do. This method allows you to set whether or not we should support
/// transparent backgrounds.
///
/// # Examples
///
/// ```
/// use theo::DisplayBuilder;
///
/// let mut builder = DisplayBuilder::new();
/// builder = builder.transparent(false);
/// ```
pub fn transparent(mut self, transparent: bool) -> Self {
self.transparent = transparent;
self
}
/// Force software rendering.
///
/// `theo` contains a software rasterization backend which is used as a fallback when no other
/// backend is available. This method allows you to force the software rasterizer to be used
/// even when other backends are available. This can be used, for example, to test the software
/// rasterizer or to avoid using hardware acceleration.
///
/// # Examples
///
/// ```
/// use theo::DisplayBuilder;
///
/// let mut builder = DisplayBuilder::new();
/// builder = builder.force_swrast(true);
/// ```
pub fn force_swrast(mut self, force_swrast: bool) -> Self {
self.force_swrast = force_swrast;
self
}
/// Build a new [`Display`].
///
/// Using the provided parameters, this method will attempt to build a new [`Display`]. If
/// successful, it will return a new [`Display`]. Otherwise, it will return an error.
///
/// # Safety
///
/// - The `display` handle must be a valid `display` that isn't currently suspended.
/// - The `window` handle, if any, must also be valid.
///
/// # Examples
///
/// ```no_run
/// use theo::DisplayBuilder;
///
/// let event_loop = winit::event_loop::EventLoop::new();
/// let mut builder = DisplayBuilder::new();
/// let display = unsafe { builder.build(&event_loop) }.unwrap();
/// ```
pub unsafe fn build(self, display: impl HasRawDisplayHandle) -> Result<Display, Error> {
self.build_from_raw(display.raw_display_handle())
}
}
/// The display used to manage all surfaces.
///
/// This type contains all common types that can be shared among surfaces. It also contains
/// methods for creating new surfaces. Most interactions with `theo` will be done through this
/// type.
///
/// The backend used by this type is determined by the [`DisplayBuilder`] used to create it.
/// By default, it will try to use the following backends in order. If one fails, it will try
/// the next one.
///
/// - [`wgpu`]
/// - OpenGL
/// - Software rasterizer
///
/// This type also has properties that are useful for creating new surfaces. For example, you
/// can use [`Display::supports_transparency`] to check if the display supports transparent
/// backgrounds.
///
/// [`wgpu`]: https://crates.io/crates/wgpu
///
/// # Examples
///
/// ```no_run
/// use theo::Display;
/// # struct MyDisplay;
/// # unsafe impl raw_window_handle::HasRawDisplayHandle for MyDisplay {
/// # fn raw_display_handle(&self) -> raw_window_handle::RawDisplayHandle {
/// # unimplemented!()
/// # }
/// # }
/// # struct Window;
/// # unsafe impl raw_window_handle::HasRawWindowHandle for Window {
/// # fn raw_window_handle(&self) -> raw_window_handle::RawWindowHandle {
/// # unimplemented!()
/// # }
/// # }
/// # impl Window {
/// # fn width(&self) -> u32 { 0 }
/// # fn height(&self) -> u32 { 0 }
/// # }
/// # struct WindowBuilder;
/// # impl WindowBuilder {
/// # fn new() -> Self {
/// # unimplemented!()
/// # }
/// # fn build(&self) -> Window {
/// # unimplemented!()
/// # }
/// # fn with_transparency(self, _transparent: bool) -> Self {
/// # self
/// # }
/// # unsafe fn with_x11_visual(self, _visual: *const ()) -> Self {
/// # self
/// # }
/// # }
/// # let my_display = MyDisplay;
///
/// # futures_lite::future::block_on(async {
/// // Create a new display.
/// let mut display = unsafe { Display::new(&my_display) }.unwrap();
///
/// // Use the display to create a new window.
/// let mut window_builder = WindowBuilder::new()
/// .with_transparency(display.supports_transparency());
///
/// if cfg!(using_x11) {
/// if let Some(visual) = display.x11_visual() {
/// unsafe {
/// window_builder = window_builder.with_x11_visual(visual.as_ptr());
/// }
/// }
/// }
///
/// // Create the window.
/// let window = window_builder.build();
///
/// // Use the window to create a new theo surface.
/// let surface = unsafe {
/// display.make_surface(
/// &window,
/// window.width(),
/// window.height(),
/// ).await.unwrap()
/// };
/// # });
/// ```
pub struct Display {
dispatch: Box<DisplayDispatch>,
_thread_unsafe: PhantomData<*mut ()>,
}
impl fmt::Debug for Display {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Display").finish_non_exhaustive()
}
}
impl From<DisplayDispatch> for Display {
fn from(dispatch: DisplayDispatch) -> Self {
Self {
dispatch: Box::new(dispatch),
_thread_unsafe: PhantomData,
}
}
}
impl Display {
/// Create a new [`DisplayBuilder`].
///
/// This is a shorthand that allows the user to avoid having to import the [`DisplayBuilder`]
/// type.
pub fn builder() -> DisplayBuilder {
DisplayBuilder::new()
}
/// Create a new, default [`Display`].
///
/// This is a shorthand for `DisplayBuilder::new().build()`.
///
/// # Safety
///
/// The `display` handle must be a valid `display` that isn't currently suspended.
/// See the safety requirements of [`DisplayBuilder::build`] for more information.
///
/// # Example
///
/// ```no_run
/// use theo::Display;
///
/// let event_loop = winit::event_loop::EventLoop::new();
/// let display = unsafe { Display::new(&event_loop) }.unwrap();
/// ```
pub unsafe fn new(display: impl HasRawDisplayHandle) -> Result<Self, Error> {
Self::builder().build_from_raw(display.raw_display_handle())
}
/// Create a new [`Surface`] from a window.
///
/// This function creates the state that `theo` associates with a window with the provided
/// width and height. The [`Surface`] can be used with the [`Display`] to draw to the window.
///
/// # Asynchronous
///
/// This function is asynchronous, as it may be necessary to wait for data to become available.
/// This is only used for the [`wgpu`] backend, as it requires the adapter to be created
/// asynchronously. For remaining backends, this future will not return `Pending`.
///
/// # Safety
///
/// The `window` handle must be a valid `window` that isn't currently suspended. The
/// `width` and `height` parameters aren't necessarily required to be correct, but
/// it's recommended that they are in order to avoid visual bugs.
///
/// # Examples
///
/// ```no_run
/// use theo::Display;
/// use winit::event_loop::EventLoop;
/// use winit::window::Window;
///
/// # futures_lite::future::block_on(async {
/// let event_loop = EventLoop::new();
/// let mut display = unsafe { Display::new(&event_loop) }.unwrap();
///
/// // In a real-world use case, parameters from the `display` would be used to create the window.
/// let window = Window::new(&event_loop).unwrap();
/// let size = window.inner_size();
///
/// // Create a new surface from the window.
/// let surface = unsafe {
/// display.make_surface(
/// &window,
/// size.width,
/// size.height,
/// ).await.unwrap()
/// };
/// # });
pub async unsafe fn make_surface(
&mut self,
window: impl HasRawWindowHandle,
width: u32,
height: u32,
) -> Result<Surface, Error> {
self.make_surface_from_raw(window.raw_window_handle(), width, height)
.await
}
}
/// The surface used to draw to.
///
/// The surface represents a rectangle on screen that can be drawn to. It's created from a
/// [`Display`] and a window, and can be used to draw to the window.
///
/// # Example
///
/// ```no_run
/// use theo::{Display, RenderContext};
/// # struct MyDisplay;
/// # unsafe impl raw_window_handle::HasRawDisplayHandle for MyDisplay {
/// # fn raw_display_handle(&self) -> raw_window_handle::RawDisplayHandle {
/// # unimplemented!()
/// # }
/// # }
/// # struct Window;
/// # unsafe impl raw_window_handle::HasRawWindowHandle for Window {
/// # fn raw_window_handle(&self) -> raw_window_handle::RawWindowHandle {
/// # unimplemented!()
/// # }
/// # }
/// # impl Window {
/// # fn width(&self) -> u32 { 0 }
/// # fn height(&self) -> u32 { 0 }
/// # }
/// # struct WindowBuilder;
/// # impl WindowBuilder {
/// # fn new() -> Self {
/// # unimplemented!()
/// # }
/// # fn build(&self) -> Window {
/// # unimplemented!()
/// # }
/// # fn with_transparency(self, _transparent: bool) -> Self {
/// # self
/// # }
/// # unsafe fn with_x11_visual(self, _visual: *const ()) -> Self {
/// # self
/// # }
/// # }
/// # let my_display = MyDisplay;
///
/// # futures_lite::future::block_on(async {
/// // Create a new display.
/// let mut display = unsafe { Display::new(&my_display) }.unwrap();
///
/// // Use the display to create a new window.
/// let mut window_builder = WindowBuilder::new()
/// .with_transparency(display.supports_transparency());
///
/// if cfg!(using_x11) {
/// if let Some(visual) = display.x11_visual() {
/// unsafe {
/// window_builder = window_builder.with_x11_visual(visual.as_ptr());
/// }
/// }
/// }
///
/// // Create the window.
/// let window = window_builder.build();
///
/// // Use the window to create a new theo surface.
/// let mut surface = unsafe {
/// display.make_surface(
/// &window,
/// window.width(),
/// window.height(),
/// ).await.unwrap()
/// };
///
/// // Use the surface to create a new render context.
/// let mut context = RenderContext::new(
/// &mut display,
/// &mut surface,
/// window.width(),
/// window.height(),
/// ).unwrap();
/// # });
/// ```
pub struct Surface {
dispatch: Box<SurfaceDispatch>,
_thread_unsafe: PhantomData<*mut ()>,
}
impl fmt::Debug for Surface {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Surface").finish_non_exhaustive()
}
}
impl From<SurfaceDispatch> for Surface {
fn from(dispatch: SurfaceDispatch) -> Self {
Self {
dispatch: Box::new(dispatch),
_thread_unsafe: PhantomData,
}
}
}
/// The context used to draw to a [`Surface`].
///
/// This is the whole point of this crate, and is the aperture used to actually draw with vector
/// graphics. It's created from a [`Display`] and a [`Surface`], and can be used to draw to the
/// surface.
///
/// See the [`RenderContext`] documentation for more information.
///
/// [`RenderContext`]: https://docs.rs/piet/0.6.2/piet/trait.RenderContext.html
pub struct RenderContext<'dsp, 'surf> {
/// The dispatch used to draw to the surface.
dispatch: Box<ContextDispatch<'dsp, 'surf>>,
/// The mismatch error.
mismatch: Result<(), Error>,
/// Whether we check for an existing context.
check_context: bool,
/// Ensure that the context is not sent to another thread.
_thread_unsafe: PhantomData<*mut ()>,
}
impl<'dsp, 'surf> RenderContext<'dsp, 'surf> {
fn from_dispatch(dispatch: ContextDispatch<'dsp, 'surf>, check_context: bool) -> Self {
Self {
dispatch: Box::new(dispatch),
mismatch: Ok(()),
check_context,
_thread_unsafe: PhantomData,
}
}
}
impl fmt::Debug for RenderContext<'_, '_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("RenderContext").finish_non_exhaustive()
}
}
impl Drop for RenderContext<'_, '_> {
fn drop(&mut self) {
if self.check_context {
// Unlock the thread.
HAS_CONTEXT
.try_with(|has_context| has_context.set(false))
.ok();
}
}
}
/// The brushes used to draw to a [`Surface`].
///
/// See the documentation for [`Brush`] for more information.
///
/// [`Brush`]: https://docs.rs/piet/0.6.2/piet/trait.RenderContext.html#associatedtype.Brush
#[derive(Clone)]
pub struct Brush {
dispatch: Rc<BrushDispatch>,
_thread_unsafe: PhantomData<*mut ()>,
}
impl From<BrushDispatch> for Brush {
fn from(dispatch: BrushDispatch) -> Self {
Self {
dispatch: Rc::new(dispatch),
_thread_unsafe: PhantomData,
}
}
}
impl fmt::Debug for Brush {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Brush").finish_non_exhaustive()
}
}
/// The images used to draw to a [`Surface`].
///
/// See the documentation for [`Image`] for more information.
///
/// [`Image`]: https://docs.rs/piet/0.6.2/piet/trait.RenderContext.html#associatedtype.Image
#[derive(Clone)]
pub struct Image {
dispatch: Rc<ImageDispatch>,
_thread_unsafe: PhantomData<*mut ()>,
}
impl From<ImageDispatch> for Image {
fn from(dispatch: ImageDispatch) -> Self {
Self {
dispatch: Rc::new(dispatch),
_thread_unsafe: PhantomData,
}
}
}
impl fmt::Debug for Image {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Image").finish_non_exhaustive()
}
}
macro_rules! make_dispatch {
($($(#[$meta:meta])* $name:ident (
$display:ty,
$surface:ty,
$ctx:ty,
$brush:ty,
$image:ty
)),* $(,)?) => {
enum DisplayDispatch {
$(
$(#[$meta])*
$name($display),
)*
}
enum SurfaceDispatch {
$(
$(#[$meta])*
$name($surface),
)*
}
enum ContextDispatch<'dsp, 'surf> {
$(
$(#[$meta])*
$name($ctx),
)*
}
enum BrushDispatch {
$(
$(#[$meta])*
$name($brush),
)*
}
enum ImageDispatch {
$(
$(#[$meta])*
$name($image),
)*
}
impl DisplayBuilder {
/// Build the [`Display`] from a raw display handle.
///
/// This is equivalent to [`DisplayBuilder::build`], but takes a raw display handle
/// instead of a type that implements [`HasRawDisplayHandle`]. This is useful for
/// implementing [`HasRawDisplayHandle`] for types that don't own their display.
///
/// # Safety
///
/// The `raw` handle must be a valid `display` that isn't currently suspended.
/// The `raw` handle must be valid for the duration of the [`Display`].
#[allow(unused_assignments, unused_mut)]
pub unsafe fn build_from_raw(
mut self,
raw: RawDisplayHandle
) -> Result<Display, Error> {
let mut last_error;
$(
$(#[$meta])*
{
match <$display>::new(&mut self, raw) {
Ok(display) => {
tracing::trace!("Created `{}` display", stringify!($name));
return Ok(DisplayDispatch::$name(display).into());
},
Err(e) => {
tracing::warn!(
"Failed to create `{}` display: {}",
stringify!($name),
e
);
last_error = e;
}
}
}
)*
Err(last_error)
}
}
impl Display {
/// Whether or not this display supports transparent backgrounds.
///
/// If this is `true`, then the [`Surface`]s created from this display will support
/// transparency if the underlying windowing system supports it. You should use this
/// to decide whether or not to use a transparent background.
///
/// # Example
///
/// ```no_run
/// use theo::Display;
///
/// let event_loop = winit::event_loop::EventLoop::new();
/// let display = unsafe { Display::new(&event_loop) }.unwrap();
///
/// if display.supports_transparency() {
/// create_transparent_window();
/// } else {
/// create_opaque_window();
/// }
/// # fn create_transparent_window() {}
/// # fn create_opaque_window() {}
/// ```
pub fn supports_transparency(&self) -> bool {
match &*self.dispatch {
$(
$(#[$meta])*
DisplayDispatch::$name(display) => display.supports_transparency(),
)*
}
}
/// The X11 visual used by this display, if any.
///
/// This is useful for creating [`Surface`]s with a specific visual. On X11, you can
/// use this to create a surface.
///
/// # Example
///
/// ```no_run
/// use theo::Display;
///
/// let event_loop = winit::event_loop::EventLoop::new();
/// let display = unsafe { Display::new(&event_loop) }.unwrap();
///
/// let visual = display.x11_visual().unwrap();
/// ```
pub fn x11_visual(&self) -> Option<std::ptr::NonNull<()>> {
match &*self.dispatch {
$(
$(#[$meta])*
DisplayDispatch::$name(display) => display.x11_visual(),
)*
}
}
/// Create a new [`Surface`] from a raw window handle.
///
/// This is equivalent to [`Display::make_surface`], except that it takes a raw window
/// handle instead of a window. This is useful if you want to create a surface from a
/// window that doesn't implement [`RawWindowHandle`].
///
/// # Asynchronous
///
/// This function is asynchronous, as it may be necessary to wait for data to become available.
/// This is only used for the [`wgpu`] backend, as it requires the adapter to be created
/// asynchronously. For remaining backends, this future will not return `Pending`.
///
/// # Safety
///
/// The `window` handle must be a valid `window` that isn't currently suspended. The
/// `width` and `height` parameters aren't necessarily required to be correct, but
/// it's recommended that they are in order to avoid visual bugs.
pub async unsafe fn make_surface_from_raw(
&mut self,
window: RawWindowHandle,
width: u32,
height: u32,
) -> Result<Surface, Error> {
match &mut *self.dispatch {
$(
$(#[$meta])*
DisplayDispatch::$name(display) => {
let surface = display.make_surface(window, width, height).await?;
Ok(SurfaceDispatch::$name(surface).into())
},
)*
}
}
/// Push the queue and present to all known surfaces.
///
/// This is necessary to call after all windows have been drawn to. It should be called
/// once all windows that need to be drawn to have already been drawn to.
///
/// # Asynchronous
///
/// For most backends, this is a no-op. For [`wgpu`], it submits the queue and then
/// waits for all of the queues to finish submitting.
pub async fn present(&mut self) {
match &mut *self.dispatch {
$(
$(#[$meta])*
DisplayDispatch::$name(ctx) => ctx.present().await,
)*
}
}
}
impl<'dsp, 'surf> RenderContext<'dsp, 'surf> {
/// Create a new [`RenderContext`] from a [`Surface`] and a [`Display`].
///
/// This creates a new [`RenderContext`] from a [`Surface`] and a [`Display`]. This is
/// the only way to create a [`RenderContext`].
#[allow(unreachable_patterns)]
pub fn new(
display: &'dsp mut Display,
surface: &'surf mut Surface,
width: u32,
height: u32,
) -> Result<Self, Error> {
// Make sure there's only one per thread.
let prev = HAS_CONTEXT
.try_with(|has_context| has_context.replace(true))
.piet_err()?;
if prev {
return Err(Error::BackendError(
"Only one context can be active per thread.".into()
));
}
match (&mut *display.dispatch, &mut *surface.dispatch) {
$(
$(#[$meta])*
(DisplayDispatch::$name(display), SurfaceDispatch::$name(surface)) => {
let ctx = unsafe {
<$ctx>::new(display, surface, width, height)?
};
Ok(RenderContext::from_dispatch(
ContextDispatch::$name(ctx),
true
))
},
)*
_ => Err(Error::InvalidInput)
}
}
/// Create a new [`RenderContext`] without checking for exclusive access.
///
/// # Safety
///
/// The same as `new`, but you need to make sure that there are no other OpenGL contexts
/// active on the current thread.
#[allow(unreachable_patterns)]
pub unsafe fn new_unchecked(
display: &'dsp mut Display,
surface: &'surf mut Surface,
width: u32,
height: u32,
) -> Result<Self, Error> {
match (&mut *display.dispatch, &mut *surface.dispatch) {
$(
$(#[$meta])*
(DisplayDispatch::$name(display), SurfaceDispatch::$name(surface)) => {
let ctx = <$ctx>::new_unchecked(display, surface, width, height)?;
Ok(RenderContext::from_dispatch(
ContextDispatch::$name(ctx),
false
))
},
)*
_ => Err(Error::InvalidInput)
}
}
}
impl piet::RenderContext for RenderContext<'_, '_> {
type Brush = Brush;
type Image = Image;
type Text = Text;
type TextLayout = TextLayout;
fn status(&mut self) -> Result<(), Error> {
let mismatch = std::mem::replace(&mut self.mismatch, Ok(()));
let status = match &mut *self.dispatch {
$(
$(#[$meta])*
ContextDispatch::$name(ctx) => ctx.status(),
)*
};
status.and(mismatch)
}
fn solid_brush(&mut self, color: piet::Color) -> Self::Brush {
match &mut *self.dispatch {
$(
$(#[$meta])*
ContextDispatch::$name(ctx) => {
let brush = ctx.solid_brush(color);
BrushDispatch::$name(brush).into()
},
)*
}
}
fn gradient(
&mut self,
gradient: impl Into<FixedGradient>
) -> Result<Self::Brush, Error> {
match &mut *self.dispatch {
$(
$(#[$meta])*
ContextDispatch::$name(ctx) => {
Ok(BrushDispatch::$name(ctx.gradient(gradient.into())?).into())
},
)*
}
}
fn clear(&mut self, region: impl Into<Option<Rect>>, color: piet::Color) {
match &mut *self.dispatch {
$(
$(#[$meta])*
ContextDispatch::$name(ctx) => ctx.clear(region.into(), color),
)*
}
}
#[allow(unreachable_patterns)]
fn stroke(&mut self, shape: impl Shape, brush: &impl IntoBrush<Self>, width: f64) {
let brush = brush.make_brush(self, || shape.bounding_box());
match (&mut *self.dispatch, &*brush.dispatch) {
$(
$(#[$meta])*
(ContextDispatch::$name(ctx), BrushDispatch::$name(brush)) => {
ctx.stroke(shape, brush, width)
},
)*
_ => unreachable!(),
}
}
#[allow(unreachable_patterns)]
fn stroke_styled(
&mut self,
shape: impl Shape,
brush: &impl IntoBrush<Self>,
width: f64,
style: &StrokeStyle,
) {
let brush = brush.make_brush(self, || shape.bounding_box());
match (&mut *self.dispatch, &*brush.dispatch) {
$(
$(#[$meta])*
(ContextDispatch::$name(ctx), BrushDispatch::$name(brush)) => {
ctx.stroke_styled(shape, brush, width, style)
},
)*
_ => unreachable!(),
}
}
#[allow(unreachable_patterns)]
fn fill(&mut self, shape: impl Shape, brush: &impl IntoBrush<Self>) {
let brush = brush.make_brush(self, || shape.bounding_box());
match (&mut *self.dispatch, &*brush.dispatch) {
$(
$(#[$meta])*
(ContextDispatch::$name(ctx), BrushDispatch::$name(brush)) => {
ctx.fill(shape, brush)
},
)*
_ => unreachable!(),
}
}
#[allow(unreachable_patterns)]
fn fill_even_odd(&mut self, shape: impl Shape, brush: &impl IntoBrush<Self>) {
let brush = brush.make_brush(self, || shape.bounding_box());
match (&mut *self.dispatch, &*brush.dispatch) {
$(
$(#[$meta])*
(ContextDispatch::$name(ctx), BrushDispatch::$name(brush)) => {
ctx.fill_even_odd(shape, brush)
},
)*
_ => unreachable!(),
}
}
fn clip(&mut self, shape: impl Shape) {
match &mut *self.dispatch {
$(
$(#[$meta])*
ContextDispatch::$name(ctx) => ctx.clip(shape),
)*
}
}
fn text(&mut self) -> &mut Self::Text {
match &mut *self.dispatch {
$(
$(#[$meta])*
ContextDispatch::$name(ctx) => {
ctx.text()
},
)*
}
}
fn draw_text(&mut self, layout: &Self::TextLayout, pos: impl Into<Point>) {
match &mut *self.dispatch {
$(
$(#[$meta])*
ContextDispatch::$name(ctx) => ctx.draw_text(layout, pos.into()),
)*
}
}
fn save(&mut self) -> Result<(), Error> {
match &mut *self.dispatch {
$(
$(#[$meta])*
ContextDispatch::$name(ctx) => ctx.save(),
)*
}
}
fn restore(&mut self) -> Result<(), Error> {
match &mut *self.dispatch {
$(
$(#[$meta])*
ContextDispatch::$name(ctx) => ctx.restore(),
)*
}
}
fn finish(&mut self) -> Result<(), Error> {
match &mut *self.dispatch {
$(
$(#[$meta])*
ContextDispatch::$name(ctx) => ctx.finish(),
)*
}
}
fn transform(&mut self, transform: Affine) {
match &mut *self.dispatch {
$(
$(#[$meta])*
ContextDispatch::$name(ctx) => ctx.transform(transform),
)*
}
}
fn make_image(
&mut self,
width: usize,
height: usize,
buf: &[u8],
format: ImageFormat,
) -> Result<Self::Image, Error> {
match &mut *self.dispatch {
$(
$(#[$meta])*
ContextDispatch::$name(ctx) => {
let img = ctx.make_image(width, height, buf, format)?;
Ok(ImageDispatch::$name(img).into())
}
)*
}
}
#[allow(unreachable_patterns)]
fn draw_image(
&mut self,
image: &Self::Image,
dst_rect: impl Into<Rect>,
interp: InterpolationMode,
) {
match (&mut *self.dispatch, &*image.dispatch) {
$(
$(#[$meta])*
(ContextDispatch::$name(ctx), ImageDispatch::$name(img)) => {
ctx.draw_image(img, dst_rect.into(), interp)
}
)*
_ => unreachable!(),
}
}
#[allow(unreachable_patterns)]
fn draw_image_area(
&mut self,
image: &Self::Image,
src_rect: impl Into<Rect>,
dst_rect: impl Into<Rect>,
interp: InterpolationMode,
) {
match (&mut *self.dispatch, &*image.dispatch) {
$(
$(#[$meta])*
(ContextDispatch::$name(ctx), ImageDispatch::$name(img)) => {
ctx.draw_image_area(
img,
src_rect.into(),
dst_rect.into(),
interp
)
}
)*
_ => unreachable!(),
}
}
fn capture_image_area(&mut self, src_rect: impl Into<Rect>) -> Result<Self::Image, Error> {
match &mut *self.dispatch {
$(
$(#[$meta])*
ContextDispatch::$name(ctx) => {
let img = ctx.capture_image_area(src_rect.into())?;
Ok(ImageDispatch::$name(img).into())
}
)*
}
}
#[allow(unreachable_patterns)]
fn blurred_rect(&mut self, rect: Rect, blur_radius: f64, brush: &impl IntoBrush<Self>) {
let brush = brush.make_brush(self, || rect);
match (&mut *self.dispatch, &*brush.dispatch) {
$(
$(#[$meta])*
(ContextDispatch::$name(ctx), BrushDispatch::$name(brush)) => {
ctx.blurred_rect(rect, blur_radius, brush)
},
)*
_ => unreachable!(),
}
}
fn current_transform(&self) -> Affine {
match &*self.dispatch {
$(
$(#[$meta])*
ContextDispatch::$name(ctx) => ctx.current_transform(),
)*
}
}
}
impl piet::IntoBrush<RenderContext<'_, '_>> for Brush {
fn make_brush<'a>(
&'a self,
_piet: &mut RenderContext<'_, '_>,
_bbox: impl FnOnce() -> Rect,
) -> Cow<'a, <RenderContext<'_, '_> as piet::RenderContext>::Brush> {
Cow::Borrowed(self)
}
}
impl piet::Image for Image {
fn size(&self) -> Size {
match &*self.dispatch {
$(
$(#[$meta])*
ImageDispatch::$name(image) => image.size(),
)*
}
}
}
}
}
make_dispatch! {
#[cfg(feature = "wgpu")]
Wgpu(
wgpu_backend::Display,
wgpu_backend::Surface,
wgpu_backend::RenderContext<'dsp, 'surf>,
piet_wgpu::Brush,
piet_wgpu::Image
),
#[cfg(all(feature = "gl", not(target_arch = "wasm32")))]
DesktopGl(
desktop_gl::Display,
desktop_gl::Surface,
desktop_gl::RenderContext<'dsp, 'surf>,
piet_glow::Brush<glow::Context>,
piet_glow::Image<glow::Context>
),
#[cfg(all(feature = "gl", target_arch = "wasm32"))]
WebGl(
web_gl::Display,
web_gl::Surface,
web_gl::RenderContext<'dsp, 'surf>,
piet_glow::Brush<glow::Context>,
piet_glow::Image<glow::Context>
),
SwRast(
swrast::Display,
swrast::Surface,
swrast::RenderContext<'dsp, 'surf>,
swrast::Brush,
swrast::Image
),
}
/// A wrapper around an error that doesn't expose it to public API.
struct LibraryError<E>(E);
impl<E: fmt::Debug> fmt::Debug for LibraryError<E> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Debug::fmt(&self.0, f)
}
}
impl<E: fmt::Display> fmt::Display for LibraryError<E> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Display::fmt(&self.0, f)
}
}
impl<E: fmt::Debug + fmt::Display> std::error::Error for LibraryError<E> {}
trait ResultExt<T, E: std::error::Error + 'static> {
fn piet_err(self) -> Result<T, Error>;
}
impl<T, E: std::error::Error + 'static> ResultExt<T, E> for Result<T, E> {
fn piet_err(self) -> Result<T, Error> {
self.map_err(|e| Error::BackendError(Box::new(LibraryError(e))))
}
}
trait OptionExt<T> {
fn piet_err(self, message: impl Into<String>) -> Result<T, Error>;
}
impl<T> OptionExt<T> for Option<T> {
fn piet_err(self, message: impl Into<String>) -> Result<T, Error> {
self.ok_or_else(|| Error::BackendError(message.into().into()))
}
}
#[derive(Debug)]
struct SwitchToSwrast;
impl fmt::Display for SwitchToSwrast {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"Switching to software rendering, this may cause lower performance"
)
}
}
impl std::error::Error for SwitchToSwrast {}