Skip to content

Commit

Permalink
Enable image widget by default (without any image load/codec support)
Browse files Browse the repository at this point in the history
This is a non-breaking change that allows using images loaded with a different library in your program, or raw pixel buffers without including all of the bloat that image-rs is.

It's another attempt at iced-rs#2244 that is backwards compatible with existing applications because they will already use the image feature if they want the widget.
  • Loading branch information
dtzxporter committed Feb 12, 2024
1 parent 7615b22 commit cc474fc
Show file tree
Hide file tree
Showing 19 changed files with 38 additions and 101 deletions.
7 changes: 3 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ maintenance = { status = "actively-developed" }
default = ["wgpu"]
# Enable the `wgpu` GPU-accelerated renderer backend
wgpu = ["iced_renderer/wgpu", "iced_widget/wgpu"]
# Enables the `Image` widget
image = ["iced_widget/image", "dep:image"]
# Enables the `Svg` widget
svg = ["iced_widget/svg"]
# Enables the `Canvas` widget
Expand All @@ -31,6 +29,8 @@ canvas = ["iced_widget/canvas"]
qr_code = ["iced_widget/qr_code"]
# Enables lazy widgets
lazy = ["iced_widget/lazy"]
# Enables image load support
image = ["iced_core/image", "image/default"]
# Enables a debug view in native platforms (press F12)
debug = ["iced_winit/debug"]
# Enables `tokio` as the `executor::Default` on native platforms
Expand Down Expand Up @@ -68,7 +68,6 @@ iced_highlighter.optional = true
thiserror.workspace = true

image.workspace = true
image.optional = true

[profile.release-opt]
inherits = "release"
Expand Down Expand Up @@ -129,7 +128,7 @@ glam = "0.25"
glyphon = "0.5"
guillotiere = "0.6"
half = "2.2"
image = "0.24"
image = { version = "0.24", default-features = false }
kamadak-exif = "0.5"
kurbo = "0.10"
log = "0.4"
Expand Down
3 changes: 3 additions & 0 deletions core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ homepage.workspace = true
categories.workspace = true
keywords.workspace = true

[features]
image = []

[dependencies]
bitflags.workspace = true
glam.workspace = true
Expand Down
3 changes: 3 additions & 0 deletions core/src/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ impl Handle {
/// Creates an image [`Handle`] pointing to the image of the given path.
///
/// Makes an educated guess about the image format by examining the data in the file.
#[cfg(feature = "image")]
pub fn from_path<T: Into<PathBuf>>(path: T) -> Handle {
Self::from_data(Data::Path(path.into()))
}
Expand Down Expand Up @@ -43,6 +44,7 @@ impl Handle {
///
/// This is useful if you already have your image loaded in-memory, maybe
/// because you downloaded or generated it procedurally.
#[cfg(feature = "image")]
pub fn from_memory(
bytes: impl AsRef<[u8]> + Send + Sync + 'static,
) -> Handle {
Expand Down Expand Up @@ -70,6 +72,7 @@ impl Handle {
}
}

#[cfg(feature = "image")]
impl<T> From<T> for Handle
where
T: Into<PathBuf>,
Expand Down
5 changes: 0 additions & 5 deletions graphics/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ all-features = true

[features]
geometry = ["lyon_path"]
image = ["dep:image", "kamadak-exif"]
web-colors = []

[dependencies]
Expand All @@ -34,12 +33,8 @@ rustc-hash.workspace = true
thiserror.workspace = true
unicode-segmentation.workspace = true
xxhash-rust.workspace = true

image.workspace = true
image.optional = true

kamadak-exif.workspace = true
kamadak-exif.optional = true

lyon_path.workspace = true
lyon_path.optional = true
4 changes: 1 addition & 3 deletions graphics/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,14 @@ pub mod color;
pub mod compositor;
pub mod damage;
pub mod gradient;
pub mod image;
pub mod mesh;
pub mod renderer;
pub mod text;

#[cfg(feature = "geometry")]
pub mod geometry;

#[cfg(feature = "image")]
pub mod image;

pub use antialiasing::Antialiasing;
pub use backend::Backend;
pub use compositor::Compositor;
Expand Down
1 change: 0 additions & 1 deletion renderer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ keywords.workspace = true

[features]
wgpu = ["iced_wgpu"]
image = ["iced_tiny_skia/image", "iced_wgpu?/image"]
svg = ["iced_tiny_skia/svg", "iced_wgpu?/svg"]
geometry = ["iced_graphics/geometry", "iced_tiny_skia/geometry", "iced_wgpu?/geometry"]
tracing = ["iced_wgpu?/tracing"]
Expand Down
1 change: 0 additions & 1 deletion renderer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,6 @@ impl text::Renderer for Renderer {
}
}

#[cfg(feature = "image")]
impl crate::core::image::Renderer for Renderer {
type Handle = crate::core::image::Handle;

Expand Down
3 changes: 1 addition & 2 deletions src/window/icon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,10 @@ pub enum Error {
InvalidError(#[from] icon::Error),

/// The underlying OS failed to create the icon.
#[error("The underlying OS failted to create the window icon: {0}")]
#[error("The underlying OS failed to create the window icon: {0}")]
OsError(#[from] io::Error),

/// The `image` crate reported an error.
#[cfg(feature = "image")]
#[error("Unable to create icon from a file: {0}")]
ImageError(#[from] image::error::ImageError),
}
1 change: 0 additions & 1 deletion tiny_skia/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ categories.workspace = true
keywords.workspace = true

[features]
image = ["iced_graphics/image"]
svg = ["resvg"]
geometry = ["iced_graphics/geometry"]

Expand Down
14 changes: 0 additions & 14 deletions tiny_skia/src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ use std::borrow::Cow;

pub struct Backend {
text_pipeline: crate::text::Pipeline,

#[cfg(feature = "image")]
raster_pipeline: crate::raster::Pipeline,

#[cfg(feature = "svg")]
Expand All @@ -22,8 +20,6 @@ impl Backend {
pub fn new() -> Self {
Self {
text_pipeline: crate::text::Pipeline::new(),

#[cfg(feature = "image")]
raster_pipeline: crate::raster::Pipeline::new(),

#[cfg(feature = "svg")]
Expand Down Expand Up @@ -131,8 +127,6 @@ impl Backend {
}

self.text_pipeline.trim_cache();

#[cfg(feature = "image")]
self.raster_pipeline.trim_cache();

#[cfg(feature = "svg")]
Expand Down Expand Up @@ -571,7 +565,6 @@ impl Backend {
transformation,
);
}
#[cfg(feature = "image")]
Primitive::Image {
handle,
filter_method,
Expand All @@ -598,12 +591,6 @@ impl Backend {
clip_mask,
);
}
#[cfg(not(feature = "image"))]
Primitive::Image { .. } => {
log::warn!(
"Unsupported primitive in `iced_tiny_skia`: {primitive:?}",
);
}
#[cfg(feature = "svg")]
Primitive::Svg {
handle,
Expand Down Expand Up @@ -999,7 +986,6 @@ impl backend::Text for Backend {
}
}

#[cfg(feature = "image")]
impl backend::Image for Backend {
fn dimensions(
&self,
Expand Down
4 changes: 1 addition & 3 deletions tiny_skia/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,10 @@ pub mod window;

mod backend;
mod primitive;
mod raster;
mod settings;
mod text;

#[cfg(feature = "image")]
mod raster;

#[cfg(feature = "svg")]
mod vector;

Expand Down
1 change: 0 additions & 1 deletion wgpu/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ all-features = true

[features]
geometry = ["iced_graphics/geometry", "lyon"]
image = ["iced_graphics/image"]
svg = ["resvg/text"]
web-colors = ["iced_graphics/web-colors"]
webgl = ["wgpu/webgl"]
Expand Down
60 changes: 21 additions & 39 deletions wgpu/src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::core::{Color, Size, Transformation};
use crate::graphics::backend;
use crate::graphics::color;
use crate::graphics::Viewport;
use crate::image;
use crate::primitive::pipeline;
use crate::primitive::{self, Primitive};
use crate::quad;
Expand All @@ -12,9 +13,6 @@ use crate::{Layer, Settings};
#[cfg(feature = "tracing")]
use tracing::info_span;

#[cfg(any(feature = "image", feature = "svg"))]
use crate::image;

use std::borrow::Cow;

/// A [`wgpu`] graphics backend for [`iced`].
Expand All @@ -26,10 +24,8 @@ pub struct Backend {
quad_pipeline: quad::Pipeline,
text_pipeline: text::Pipeline,
triangle_pipeline: triangle::Pipeline,
pipeline_storage: pipeline::Storage,

#[cfg(any(feature = "image", feature = "svg"))]
image_pipeline: image::Pipeline,
pipeline_storage: pipeline::Storage,
}

impl Backend {
Expand All @@ -44,18 +40,14 @@ impl Backend {
let quad_pipeline = quad::Pipeline::new(device, format);
let triangle_pipeline =
triangle::Pipeline::new(device, format, settings.antialiasing);

#[cfg(any(feature = "image", feature = "svg"))]
let image_pipeline = image::Pipeline::new(device, format);

Self {
quad_pipeline,
text_pipeline,
triangle_pipeline,
pipeline_storage: pipeline::Storage::default(),

#[cfg(any(feature = "image", feature = "svg"))]
image_pipeline,
pipeline_storage: pipeline::Storage::default(),
}
}

Expand Down Expand Up @@ -113,8 +105,6 @@ impl Backend {
self.quad_pipeline.end_frame();
self.text_pipeline.end_frame();
self.triangle_pipeline.end_frame();

#[cfg(any(feature = "image", feature = "svg"))]
self.image_pipeline.end_frame();
}

Expand Down Expand Up @@ -158,21 +148,18 @@ impl Backend {
);
}

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

self.image_pipeline.prepare(
device,
queue,
_encoder,
&layer.images,
scaled,
scale_factor,
);
}
self.image_pipeline.prepare(
device,
queue,
_encoder,
&layer.images,
scaled,
scale_factor,
);
}

if !layer.text.is_empty() {
Expand Down Expand Up @@ -216,7 +203,6 @@ impl Backend {

let mut quad_layer = 0;
let mut triangle_layer = 0;
#[cfg(any(feature = "image", feature = "svg"))]
let mut image_layer = 0;
let mut text_layer = 0;

Expand Down Expand Up @@ -303,17 +289,14 @@ impl Backend {
));
}

#[cfg(any(feature = "image", feature = "svg"))]
{
if !layer.images.is_empty() {
self.image_pipeline.render(
image_layer,
bounds,
&mut render_pass,
);
if !layer.images.is_empty() {
self.image_pipeline.render(
image_layer,
bounds,
&mut render_pass,
);

image_layer += 1;
}
image_layer += 1;
}

if !layer.text.is_empty() {
Expand Down Expand Up @@ -377,7 +360,6 @@ impl backend::Text for Backend {
}
}

#[cfg(feature = "image")]
impl backend::Image for Backend {
fn dimensions(&self, handle: &crate::core::image::Handle) -> Size<u32> {
self.image_pipeline.dimensions(handle)
Expand Down
Loading

0 comments on commit cc474fc

Please sign in to comment.