Skip to content

Commit

Permalink
WIP glow image rendering support
Browse files Browse the repository at this point in the history
iced-rs#674

This works, but duplicates code from `iced_wgpu` that should ideally be
shared, and the cache never evicts.

The next step here is to work on an API design to move some of the image
loading and caching logic from the backend to `iced_graphics` (or
elsewhere?).
  • Loading branch information
ids1024 committed Oct 25, 2022
1 parent 8221794 commit be155fd
Show file tree
Hide file tree
Showing 7 changed files with 437 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ resolver = "2"
[features]
default = ["wgpu"]
# Enables the `Image` widget
image = ["iced_wgpu/image", "image_rs"]
image = ["iced_wgpu/image", "iced_glow/image", "image_rs"]
# Enables the `Svg` widget
svg = ["iced_wgpu/svg"]
# Enables the `Canvas` widget
Expand Down
21 changes: 20 additions & 1 deletion glow/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,22 @@ license = "MIT AND OFL-1.1"
repository = "https://github.com/iced-rs/iced"

[features]
image = ["png", "jpeg", "jpeg_rayon", "gif", "webp", "bmp"]
png = ["image_rs/png"]
jpeg = ["image_rs/jpeg"]
jpeg_rayon = ["image_rs/jpeg_rayon"]
gif = ["image_rs/gif"]
webp = ["image_rs/webp"]
pnm = ["image_rs/pnm"]
ico = ["image_rs/ico"]
bmp = ["image_rs/bmp"]
hdr = ["image_rs/hdr"]
dds = ["image_rs/dds"]
farbfeld = ["image_rs/farbfeld"]
canvas = ["iced_graphics/canvas"]
qr_code = ["iced_graphics/qr_code"]
default_system_font = ["iced_graphics/font-source"]
# Not supported yet!
image = []
svg = []

[dependencies]
Expand All @@ -22,6 +33,8 @@ glyph_brush = "0.7"
euclid = "0.22"
bytemuck = "1.4"
log = "0.4"
kamadak-exif = "0.5"
bitflags = "1.2"

[dependencies.iced_native]
version = "0.5"
Expand All @@ -32,6 +45,12 @@ version = "0.3"
path = "../graphics"
features = ["font-fallback", "font-icons", "opengl"]

[dependencies.image_rs]
version = "0.23"
package = "image"
default-features = false
optional = true

[package.metadata.docs.rs]
rustdoc-args = ["--cfg", "docsrs"]
all-features = true
22 changes: 20 additions & 2 deletions glow/src/backend.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::image;
use crate::program;
use crate::quad;
use crate::text;
Expand All @@ -16,6 +17,7 @@ use iced_native::{Font, Size};
/// [`iced`]: https://github.com/iced-rs/iced
#[derive(Debug)]
pub struct Backend {
image_pipeline: image::Pipeline,
quad_pipeline: quad::Pipeline,
text_pipeline: text::Pipeline,
triangle_pipeline: triangle::Pipeline,
Expand All @@ -33,10 +35,12 @@ impl Backend {

let shader_version = program::Version::new(gl);

let image_pipeline = image::Pipeline::new(gl, &shader_version);
let quad_pipeline = quad::Pipeline::new(gl, &shader_version);
let triangle_pipeline = triangle::Pipeline::new(gl, &shader_version);

Self {
image_pipeline,
quad_pipeline,
text_pipeline,
triangle_pipeline,
Expand Down Expand Up @@ -113,6 +117,20 @@ impl Backend {
);
}

#[cfg(feature = "image")]
if !layer.images.is_empty() {
let scaled = transformation
* Transformation::scale(scale_factor, scale_factor);

self.image_pipeline.draw(
gl,
target_height,
scaled,
scale_factor,
&layer.images,
);
}

if !layer.text.is_empty() {
for text in layer.text.iter() {
// Target physical coordinates directly to avoid blurry text
Expand Down Expand Up @@ -239,8 +257,8 @@ impl backend::Text for Backend {

#[cfg(feature = "image")]
impl backend::Image for Backend {
fn dimensions(&self, _handle: &iced_native::image::Handle) -> (u32, u32) {
(50, 50)
fn dimensions(&self, handle: &iced_native::image::Handle) -> (u32, u32) {
self.image_pipeline.dimensions(handle)
}
}

Expand Down
Loading

0 comments on commit be155fd

Please sign in to comment.