Skip to content

Commit

Permalink
[wip] Use piet-coregraphics backend
Browse files Browse the repository at this point in the history
This is currently just for testing, won't be able to use this
until we do a release of piet.
  • Loading branch information
cmyr committed May 12, 2020
1 parent f9da61c commit 0dc4eac
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 24 deletions.
109 changes: 107 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/book_examples/src/custom_widgets_md.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ fn sized_swatch() -> impl Widget<Color> {
// ANCHOR: background_label
fn background_label() -> impl Widget<Color> {
Label::dynamic(|color: &Color, _| {
let (r, g, b, _) = color.as_rgba_u8();
let (r, g, b, _) = color.as_rgba8();
format!("#{:X}{:X}{:X}", r, g, b)
})
.background(make_color_swatch())
Expand Down
9 changes: 5 additions & 4 deletions druid-shell/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ x11 = ["xcb", "cairo-sys-rs"]
default-target = "x86_64-pc-windows-msvc"

[dependencies]
piet-common = "0.0.12"
piet-common = "0.0.13"
log = "0.4.8"
lazy_static = "1.0"
time = "0.2.7"
cfg-if = "0.1.10"
# NOTE: if changing, ensure version is compatible with the version in piet
kurbo = "0.5.11"
kurbo = "0.6.0"
# NOTE: This defaults to mimicking std::time on non-wasm targets
instant = { version = "0.1", features = [ "wasm-bindgen" ] }

Expand All @@ -38,7 +38,7 @@ gtk-sys = { version = "0.9.0", optional = true }
xcb = { version = "0.9.0", features = ["thread", "xlib_xcb", "randr"], optional = true }

[dev-dependencies]
piet-common = {version = "0.0.12", features = ["png"]}
piet-common = {version = "0.0.13", features = ["png"]}

[target.'cfg(target_os="windows")'.dependencies]
wio = "0.2"
Expand All @@ -50,7 +50,8 @@ features = ["d2d1_1", "dwrite", "winbase", "libloaderapi", "errhandlingapi", "wi
[target.'cfg(target_os="macos")'.dependencies]
cocoa = "0.20.0"
objc = "0.2.5"
cairo-rs = { version = "0.8.1", default_features = false }
core-graphics = "0.19"
foreign-types = "0.3.2"

# TODO(x11/dependencies): only use feature "xcb" if using XCB
[target.'cfg(target_os="linux")'.dependencies]
Expand Down
2 changes: 1 addition & 1 deletion druid-shell/examples/invalidate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ struct InvalidateTest {
impl InvalidateTest {
fn update_color_and_rect(&mut self) {
let time_since_start = (Instant::now() - self.start_time).as_millis();
let (r, g, b, _) = self.color.as_rgba_u8();
let (r, g, b, _) = self.color.as_rgba8();
self.color = match (time_since_start % 2, time_since_start % 3) {
(0, _) => Color::rgb8(r.wrapping_add(10), g, b),
(_, 0) => Color::rgb8(r, g.wrapping_add(10), b),
Expand Down
26 changes: 10 additions & 16 deletions druid-shell/src/platform/mac/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@ use cocoa::base::{id, nil, BOOL, NO, YES};
use cocoa::foundation::{
NSAutoreleasePool, NSInteger, NSPoint, NSRect, NSSize, NSString, NSUInteger,
};
use core_graphics::context::CGContextRef;
use foreign_types::ForeignTypeRef;
use lazy_static::lazy_static;
use log::{error, info};
use objc::declare::ClassDecl;
use objc::rc::WeakPtr;
use objc::runtime::{Class, Object, Sel};
use objc::{class, msg_send, sel, sel_impl};

use cairo::{Context, QuartzSurface};
use log::{error, info};

use crate::kurbo::{Point, Rect, Size, Vec2};
use crate::piet::{Piet, RenderContext};

Expand Down Expand Up @@ -537,23 +537,17 @@ extern "C" fn mods_changed(this: &mut Object, _: Sel, nsevent: id) {
extern "C" fn draw_rect(this: &mut Object, _: Sel, dirtyRect: NSRect) {
unsafe {
let context: id = msg_send![class![NSGraphicsContext], currentContext];
// TODO: probably should use a better type than void pointer, but it's not obvious what's best.
// cairo_sys::CGContextRef would be better documentation-wise, but it's a type alias.
let cgcontext: *mut c_void = msg_send![context, CGContext];
// TODO: use width and height from view size
let frame = NSView::frame(this as *mut _);
let width = frame.size.width as u32;
let height = frame.size.height as u32;
//FIXME: when core_graphics is at 0.20, we should be able to use
//core_graphics::sys::CGContextRef as our pointer type.
let cgcontext_ptr: *mut <CGContextRef as ForeignTypeRef>::CType =
msg_send![context, CGContext];
let cgcontext_ref = CGContextRef::from_ptr_mut(cgcontext_ptr);

let rect = Rect::from_origin_size(
(dirtyRect.origin.x, dirtyRect.origin.y),
(dirtyRect.size.width, dirtyRect.size.height),
);
let cairo_surface =
QuartzSurface::create_for_cg_context(cgcontext, width, height).expect("cairo surface");
let mut cairo_ctx = Context::new(&cairo_surface);
cairo_ctx.set_source_rgb(0.0, 0.5, 0.0);
cairo_ctx.paint();
let mut piet_ctx = Piet::new(&mut cairo_ctx);
let mut piet_ctx = Piet::new_y_down(cgcontext_ref);
let view_state: *mut c_void = *this.get_ivar("viewState");
let view_state = &mut *(view_state as *mut ViewState);
let anim = (*view_state).handler.paint(&mut piet_ctx, rect);
Expand Down

0 comments on commit 0dc4eac

Please sign in to comment.