[librsvg: 12/14] shared_surface: use ExclusiveImageSurface for composite_arithmetic
- From: Federico Mena Quintero <federico src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [librsvg: 12/14] shared_surface: use ExclusiveImageSurface for composite_arithmetic
- Date: Wed, 15 Jan 2020 00:33:10 +0000 (UTC)
commit db2241e8c062066052be4d7ef370e21153c2fdbf
Author: Paolo Borelli <pborelli gnome org>
Date: Tue Jan 14 22:09:53 2020 +0100
shared_surface: use ExclusiveImageSurface for composite_arithmetic
rsvg_internals/benches/composite.rs | 10 ++--
rsvg_internals/src/surface_utils/shared_surface.rs | 70 ++++++++++------------
2 files changed, 37 insertions(+), 43 deletions(-)
---
diff --git a/rsvg_internals/benches/composite.rs b/rsvg_internals/benches/composite.rs
index 17bc0416..b76723ca 100644
--- a/rsvg_internals/benches/composite.rs
+++ b/rsvg_internals/benches/composite.rs
@@ -4,7 +4,7 @@ use criterion::{black_box, Criterion};
use rsvg_internals::rect::IRect;
use rsvg_internals::surface_utils::shared_surface::{
- composite_arithmetic, SharedImageSurface, SurfaceType,
+ composite_arithmetic, ExclusiveImageSurface, SharedImageSurface, SurfaceType,
};
const SURFACE_SIDE: i32 = 512;
@@ -18,14 +18,12 @@ const BOUNDS: IRect = IRect {
fn bench_composite(c: &mut Criterion) {
c.bench_function("composite arithmetic", |b| {
let input_surface =
- cairo::ImageSurface::create(cairo::Format::ARgb32, SURFACE_SIDE, SURFACE_SIDE).unwrap();
- let input_surface = SharedImageSurface::wrap(input_surface, SurfaceType::SRgb).unwrap();
+ SharedImageSurface::empty(SURFACE_SIDE, SURFACE_SIDE, SurfaceType::SRgb).unwrap();
let input_2_surface =
- cairo::ImageSurface::create(cairo::Format::ARgb32, SURFACE_SIDE, SURFACE_SIDE).unwrap();
- let input_2_surface = SharedImageSurface::wrap(input_2_surface, SurfaceType::SRgb).unwrap();
+ SharedImageSurface::empty(SURFACE_SIDE, SURFACE_SIDE, SurfaceType::SRgb).unwrap();
let mut output_surface =
- cairo::ImageSurface::create(cairo::Format::ARgb32, SURFACE_SIDE, SURFACE_SIDE).unwrap();
+ ExclusiveImageSurface::new(SURFACE_SIDE, SURFACE_SIDE, SurfaceType::SRgb).unwrap();
let bounds = black_box(BOUNDS);
diff --git a/rsvg_internals/src/surface_utils/shared_surface.rs
b/rsvg_internals/src/surface_utils/shared_surface.rs
index b57fef7f..292c0c76 100644
--- a/rsvg_internals/src/surface_utils/shared_surface.rs
+++ b/rsvg_internals/src/surface_utils/shared_surface.rs
@@ -1135,15 +1135,15 @@ impl ImageSurface<Shared> {
k3: f64,
k4: f64,
) -> Result<SharedImageSurface, cairo::Status> {
- let mut output_surface =
- cairo::ImageSurface::create(cairo::Format::ARgb32, self.width, self.height)?;
+ let mut output_surface = ExclusiveImageSurface::new(
+ self.width,
+ self.height,
+ self.surface_type.combine(other.surface_type),
+ )?;
composite_arithmetic(self, other, &mut output_surface, bounds, k1, k2, k3, k4);
- SharedImageSurface::wrap(
- output_surface,
- self.surface_type.combine(other.surface_type),
- )
+ output_surface.share()
}
}
@@ -1152,46 +1152,42 @@ impl ImageSurface<Shared> {
pub fn composite_arithmetic(
surface1: &SharedImageSurface,
surface2: &SharedImageSurface,
- output_surface: &mut cairo::ImageSurface,
+ output_surface: &mut ExclusiveImageSurface,
bounds: IRect,
k1: f64,
k2: f64,
k3: f64,
k4: f64,
) {
- let output_stride = output_surface.get_stride() as usize;
+ for (x, y, pixel, pixel_2) in
+ Pixels::within(surface1, bounds).map(|(x, y, p)| (x, y, p, surface2.get_pixel(x, y)))
{
- let mut output_data = output_surface.get_data().unwrap();
+ let i1a = f64::from(pixel.a) / 255f64;
+ let i2a = f64::from(pixel_2.a) / 255f64;
+ let oa = k1 * i1a * i2a + k2 * i1a + k3 * i2a + k4;
+ let oa = clamp(oa, 0f64, 1f64);
+
+ // Contents of image surfaces are transparent by default, so if the resulting pixel is
+ // transparent there's no need to do anything.
+ if oa > 0f64 {
+ let compute = |i1, i2| {
+ let i1 = f64::from(i1) / 255f64;
+ let i2 = f64::from(i2) / 255f64;
+
+ let o = k1 * i1 * i2 + k2 * i1 + k3 * i2 + k4;
+ let o = clamp(o, 0f64, oa);
+
+ ((o * 255f64) + 0.5) as u8
+ };
- for (x, y, pixel, pixel_2) in
- Pixels::within(surface1, bounds).map(|(x, y, p)| (x, y, p, surface2.get_pixel(x, y)))
- {
- let i1a = f64::from(pixel.a) / 255f64;
- let i2a = f64::from(pixel_2.a) / 255f64;
- let oa = k1 * i1a * i2a + k2 * i1a + k3 * i2a + k4;
- let oa = clamp(oa, 0f64, 1f64);
-
- // Contents of image surfaces are transparent by default, so if the resulting pixel is
- // transparent there's no need to do anything.
- if oa > 0f64 {
- let compute = |i1, i2| {
- let i1 = f64::from(i1) / 255f64;
- let i2 = f64::from(i2) / 255f64;
-
- let o = k1 * i1 * i2 + k2 * i1 + k3 * i2 + k4;
- let o = clamp(o, 0f64, oa);
-
- ((o * 255f64) + 0.5) as u8
- };
+ let output_pixel = Pixel {
+ r: compute(pixel.r, pixel_2.r),
+ g: compute(pixel.g, pixel_2.g),
+ b: compute(pixel.b, pixel_2.b),
+ a: ((oa * 255f64) + 0.5) as u8,
+ };
- let output_pixel = Pixel {
- r: compute(pixel.r, pixel_2.r),
- g: compute(pixel.g, pixel_2.g),
- b: compute(pixel.b, pixel_2.b),
- a: ((oa * 255f64) + 0.5) as u8,
- };
- output_data.set_pixel(output_stride, output_pixel, x, y);
- }
+ output_surface.set_pixel(output_pixel, x, y);
}
}
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]