Skip to content

Commit

Permalink
chore: refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
triniwiz committed Jul 29, 2024
1 parent cdf9826 commit 20b1bc9
Show file tree
Hide file tree
Showing 165 changed files with 13,717 additions and 29,428 deletions.
3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ members = [
"crates/canvas-android",
"crates/canvas-ios",
"crates/playground",
"crates/canvas-cxx",
"crates/canvas-c",
"crates/canvas-svg"]

Expand All @@ -26,4 +25,4 @@ strip = true

[workspace.dependencies.wgt]
package = "wgpu-types"
version = "0.20.0"
version = "22.0.0"
25 changes: 18 additions & 7 deletions crates/canvas-2d/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,30 @@ edition = "2021"
[features]
metal = ["skia-safe/metal"]
gl = ["skia-safe/gl"]
vulkan = ["skia-safe/vulkan"]

[dependencies]
canvas-core = { path = "../canvas-core", features = ["2d"] }
parking_lot = "0.12.0"
regex = "1.10.3"
base64 = "0.22.0"
parking_lot = "0.12.3"
regex = "1.10.5"
base64 = "0.22.1"
encoding_rs = "0.8.34"
gl-bindings = { path = "../gl-bindings" }
lazy_static = "1.4.0"
csscolorparser = { git = "https://github.com/triniwiz/csscolorparser-rs.git", rev = "c8c7e86", features = ["named-colors"] }
#rgb = { version = "0.8.37", features = ["argb"] }
log = "0.4.21"
once_cell = "1.8.0"
skia-safe = { version = "0.75.0", features = ["gl", "textlayout"] }
bytes = "1.5.0"
bytes = "1.6.1"
env_logger = "0.11.2"
ash = { version = "0.38.0" }


[target.'cfg(target_os="ios")'.dependencies]
skia-safe = { version = "0.75.0", features = ["gl", "metal", "textlayout"] }


[target.'cfg(target_os="macos")'.dependencies]
skia-safe = { version = "0.75.0", features = ["gl", "metal", "textlayout"] }

[target.'cfg(target_os="android")'.dependencies]
skia-safe = { version = "0.75.0", features = ["gl", "vulkan", "textlayout"] }
ash = { version = "0.38.0" }
152 changes: 152 additions & 0 deletions crates/canvas-2d/src/android.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
use std::os::raw::c_void;
use std::sync::Arc;

use ash::vk;
use ash::vk::Handle;
use skia_safe::{gpu, Color};

use crate::context::paths::path::Path;
use crate::context::text_styles::text_direction::TextDirection;
use crate::context::{Context, Recorder, State, SurfaceData};

#[cfg(feature = "vulkan")]
impl Context {
#[cfg(target_os = "android")]
pub fn new_vulkan(
width: f32,
height: f32,
window: *mut c_void,
density: f32,
samples: u32,
alpha: bool,
font_color: i32,
ppi: f32,
direction: u8,
) -> Self {
let ash_graphics =
unsafe { crate::context::surface_vulkan::AshGraphics::new("ns-canvas") }.unwrap();

let mut context = {
let get_proc = |of| unsafe {
match ash_graphics.get_proc(of) {
Some(f) => f as _,
None => {
println!("resolve of {} failed", of.name().to_str().unwrap());
str::ptr::null()
}
}
};

let backend_context = unsafe {
gpu::vk::BackendContext::new(
ash_graphics.instance.handle().as_raw() as _,
ash_graphics.physical_device.as_raw() as _,
ash_graphics.device.handle().as_raw() as _,
(
ash_graphics.queue_and_index.0.as_raw() as _,
ash_graphics.queue_and_index.1,
),
&get_proc,
)
};

gpu::direct_contexts::make_vulkan(&backend_context, None)
}
.unwrap();

let surface_fn =
ash::khr::android_surface::Instance::new(&ash_graphics.entry, &ash_graphics.instance);

let mut info = vk::AndroidSurfaceCreateInfoKHR::default();
let _ = info.window(window);

let vk_surface = unsafe { surface_fn.create_android_surface(&info, None).ok() };

let alpha_type = if alpha {
skia_safe::AlphaType::Unpremul
} else {
skia_safe::AlphaType::Premul
};

let info = skia_safe::ImageInfo::new(
skia_safe::ISize::new(width, height),
skia_safe::ColorType::N32,
alpha_type,
Some(skia_safe::ColorSpace::new_srgb()),
);

let surface = gpu::surfaces::render_target(
&mut context,
gpu::Budgeted::Yes,
&info,
Some(samples as usize),
Some(gpu::SurfaceOrigin::TopLeft),
None,
false,
None,
)
.unwrap();

let mut state = State::default();
state.direction = TextDirection::from(direction as u32);

let bounds = skia_safe::Rect::from_wh(width, height);
Context {
direct_context: Some(context),
surface_data: SurfaceData {
bounds,
scale: density,
ppi,
},
surface,
vk_surface,
ash_graphics: None,
recorder: Arc::new(parking_lot::Mutex::new(Recorder::new(bounds))),
path: Path::default(),
state,
state_stack: vec![],
font_color: Color::new(font_color as u32),
}
}

#[cfg(target_os = "android")]
pub fn resize_vulkan(
context: &mut Context,
width: f32,
height: f32,
samples: u32,
alpha: bool,
) {
if let Some(direct_context) = context.direct_context.as_mut() {
let alpha_type = if alpha {
skia_safe::AlphaType::Unpremul
} else {
skia_safe::AlphaType::Premul
};

let info = skia_safe::ImageInfo::new(
skia_safe::ISize::new(width as i32, height as i32),
skia_safe::ColorType::N32,
alpha_type,
Some(skia_safe::ColorSpace::new_srgb()),
);

let surface = gpu::surfaces::render_target(
direct_context,
gpu::Budgeted::Yes,
&info,
Some(samples as usize),
Some(gpu::SurfaceOrigin::TopLeft),
None,
false,
None,
)
.unwrap();

let bounds = skia_safe::Rect::from_wh(width, height);
context.surface_data.bounds = bounds;
context.recorder = Arc::new(parking_lot::Mutex::new(Recorder::new(bounds)));
context.surface = surface;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use skia_safe::BlendMode;

#[repr(C)]
#[derive(Clone, Copy, Debug)]
pub enum CompositeOperationType {
SourceOver = 0,
Expand Down
100 changes: 34 additions & 66 deletions crates/canvas-2d/src/context/drawing_images/mod.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
use log::log;
use skia_safe::{Image, Rect};
use skia_safe::canvas::SrcRectConstraint;
use skia_safe::{Image, Rect};

use canvas_core::image_asset::ImageAsset;

use crate::context::Context;
use crate::prelude::ScaleUtils;

impl Context {
pub fn draw_image_asset_src_xywh_dst_xywh(
Expand Down Expand Up @@ -74,11 +72,7 @@ impl Context {
width as i32,
height as i32,
) {
self.draw_image_dx_dy(
&image,
x,
y,
)
self.draw_image_dx_dy(&image, x, y)
}
}
}
Expand All @@ -100,9 +94,7 @@ impl Context {
if let Some(image) =
crate::utils::image::from_image_slice_no_copy(bytes, w as i32, h as i32)
{
self.draw_image_dx_dy_dw_dh(
&image, x, y, width, height,
)
self.draw_image_dx_dy_dw_dh(&image, x, y, width, height)
}
}
}
Expand Down Expand Up @@ -144,53 +136,30 @@ impl Context {
)
}



pub fn draw_image_dx_dy(
&mut self,
image: &Image,
x: f32,
y: f32
) {

pub fn draw_image_dx_dy(&mut self, image: &Image, x: f32, y: f32) {
self.state
.paint
.image_smoothing_quality_set(self.state.image_filter_quality());

let paint = self.state.paint.image_paint();

self.surface.canvas().draw_image_with_sampling_options(
image,
skia_safe::Point::new(
x,
y
),
self.state.image_smoothing_quality,
Some(paint)
);
self.with_canvas(|canvas| {
canvas.draw_image_with_sampling_options(
image,
skia_safe::Point::new(x, y),
self.state.image_smoothing_quality,
Some(paint),
);
});
}


pub fn draw_image_dx_dy_dw_dh(
&mut self,
image: &Image,
dx: f32,
dy: f32,
dw: f32,
dh: f32,
) {
self.draw_image_with_rect(image, Rect::from_xywh(dx, dy, dw, dh))
pub fn draw_image_dx_dy_dw_dh(&mut self, image: &Image, dx: f32, dy: f32, dw: f32, dh: f32) {
self.draw_image_with_rect(image, Rect::from_xywh(dx, dy, dw, dh))
}

fn draw_image(
&mut self,
image: &Image,
src_rect: impl Into<Rect>,
dst_rect: impl Into<Rect>,
) {
fn draw_image(&mut self, image: &Image, src_rect: impl Into<Rect>, dst_rect: impl Into<Rect>) {
let src_rect = src_rect.into();
let dst_rect = dst_rect.into();


let dimensions = image.dimensions();
let (src, dst) = crate::utils::fit_bounds(
Expand All @@ -206,24 +175,22 @@ impl Context {

let paint = self.state.paint.image_paint();

self.surface.canvas().draw_image_rect_with_sampling_options(
image,
Some((&src, SrcRectConstraint::Strict)),
dst,
self.state.image_smoothing_quality,
paint,
);
self.with_canvas(|canvas| {
canvas.draw_image_rect_with_sampling_options(
image,
Some((&src, SrcRectConstraint::Strict)),
dst,
self.state.image_smoothing_quality,
paint,
);
});
}



fn draw_image_with_rect(&mut self, image: &Image, dst_rect: impl Into<Rect>) {

let dimensions = image.dimensions();

let src_rect = Rect::from_xywh(0., 0., dimensions.width as f32, dimensions.height as f32);
let dst_rect = dst_rect.into();


let (src, dst) = crate::utils::fit_bounds(
dimensions.width as f32,
Expand All @@ -232,20 +199,21 @@ impl Context {
dst_rect,
);


self.state
.paint
.image_smoothing_quality_set(self.state.image_filter_quality());
let paint = self.state.paint.image_paint();
self.surface.canvas().draw_image_rect_with_sampling_options(
image,
Some((&src, SrcRectConstraint::Strict)),
dst,
self.state.image_smoothing_quality,
paint,
);
}

self.with_canvas(|canvas| {
canvas.draw_image_rect_with_sampling_options(
image,
Some((&src, SrcRectConstraint::Strict)),
dst,
self.state.image_smoothing_quality,
paint,
);
});
}

pub(crate) fn draw_image_with_points(&mut self, image: &Image, x: f32, y: f32) {
self.draw_image_with_rect(
Expand Down
1 change: 0 additions & 1 deletion crates/canvas-2d/src/context/drawing_paths/fill_rule.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use skia_safe::path::FillType;

#[repr(C)]
#[derive(Copy, Clone, Debug)]
pub enum FillRule {
NonZero = 0,
Expand Down
Loading

0 comments on commit 20b1bc9

Please sign in to comment.