diff --git a/res/box_shadow.fs.glsl b/res/box_shadow.fs.glsl index 33895249bc..2d7576d331 100644 --- a/res/box_shadow.fs.glsl +++ b/res/box_shadow.fs.glsl @@ -1,3 +1,26 @@ +// See http://asciimath.org to render the equations here. + +// The Gaussian function used for blurring: +// +// G_sigma(x) = 1/sqrt(2 pi sigma^2) e^(-x^2/(2 sigma^2)) +float gauss(float x, float sigma) { + float sigmaPow2 = sigma * sigma; + return 1.0 / sqrt(6.283185307179586 * sigmaPow2) * exp(-(x * x) / (2.0 * sigmaPow2)); +} + +// An approximation of the error function, which is related to the integral of the Gaussian +// function: +// +// "erf"(x) = 2/sqrt(pi) int_0^x e^(-t^2) dt +// ~~ 1 - 1 / (1 + a_1 x + a_2 x^2 + a_3 x^3 + a_4 x^4)^4 +// +// where: +// +// a_1 = 0.278393, a_2 = 0.230389, a_3 = 0.000972, a_4 = 0.078108 +// +// This approximation is accurate to `5 xx 10^-4`, more than accurate enough for our purposes. +// +// See: https://en.wikipedia.org/wiki/Error_function#Approximation_with_elementary_functions float erf(float x) { bool negative = x < 0.0; if (negative) @@ -10,33 +33,110 @@ float erf(float x) { return negative ? -result : result; } -void main(void) -{ - float range = floor(vBlurRadius) * 3.0; - float sigma = vBlurRadius / 2.0; - float sigmaSqrt2 = sigma * 1.41421356237; - - float length; - float value; - vec2 position = vPosition - vBorderPosition.zw; - if (vBorderRadii.z == 0.0) { - length = range; - value = position.x; - } else { - length = range; - vec2 center = vec2(max(position.x - range, length), - max(position.y - range, length)); - vec2 referencePoint = position - range; - value = distance(referencePoint, center); - } +// A useful helper for calculating integrals of the Gaussian function via the error function: +// +// "erf"_sigma(x) = 2 int 1/sqrt(2 pi sigma^2) e^(-x^2/(2 sigma^2)) dx +// = "erf"(x/(sigma sqrt(2))) +float erfSigma(float x, float sigma) { + return erf(x / (sigma * 1.4142135623730951)); +} - float minValue = min(value - range, length) - value; - float maxValue = min(value + range, length) - value; - if (minValue < maxValue) { - value = 1.0 - (erf(maxValue / sigmaSqrt2) - erf(minValue / sigmaSqrt2)); - } else { - value = 1.0; +// Returns the blurred color value from the box itself (not counting any rounded corners). `p_0` is +// the vector distance to the top left corner of the box; `p_1` is the vector distance to its +// bottom right corner. +// +// "colorFromRect"_sigma(p_0, p_1) +// = int_{p_{0_y}}^{p_{1_y}} int_{p_{1_x}}^{p_{0_x}} G_sigma(y) G_sigma(x) dx dy +// = 1/4 ("erf"_sigma(p_{1_x}) - "erf"_sigma(p_{0_x})) +// ("erf"_sigma(p_{1_y}) - "erf"_sigma(p_{0_y})) +float colorFromRect(vec2 p0, vec2 p1, float sigma) { + return (erfSigma(p1.x, sigma) - erfSigma(p0.x, sigma)) * + (erfSigma(p1.y, sigma) - erfSigma(p0.y, sigma)) / 4.0; +} + +// Returns the `x` coordinate on the ellipse with the given radii for the given `y` coordinate: +// +// "ellipsePoint"(y, y_0, a, b) = a sqrt(1 - ((y - y_0) / b)^2) +float ellipsePoint(float y, float y0, vec2 radii) { + float bStep = (y - y0) / radii.y; + return radii.x * sqrt(1.0 - bStep * bStep); +} + +// A helper function to compute the value that needs to be subtracted to accommodate the border +// corners. +// +// "colorCutout"_sigma(x_{0_l}, x_{0_r}, y_0, y_{min}, y_{max}, a, b) +// = int_{y_{min}}^{y_{max}} +// int_{x_{0_r} + "ellipsePoint"(y, y_0, a, b)}^{x_{0_r} + a} G_sigma(y) G_sigma(x) dx +// + int_{x_{0_l} - a}^{x_{0_l} - "ellipsePoint"(y, y_0, a, b)} G_sigma(y) G_sigma(x) +// dx dy +// = int_{y_{min}}^{y_{max}} 1/2 G_sigma(y) +// ("erf"_sigma(x_{0_r} + a) - "erf"_sigma(x_{0_r} + "ellipsePoint"(y, y_0, a, b)) + +// "erf"_sigma(x_{0_l} - "ellipsePoint"(y, y_0, a, b)) - "erf"_sigma(x_{0_l} - a)) +// +// with the outer integral evaluated numerically. +float colorCutoutGeneral(float x0l, + float x0r, + float y0, + float yMin, + float yMax, + vec2 radii, + float sigma) { + float sum = 0.0; + for (float y = yMin; y <= yMax; y += 1.0) { + float xEllipsePoint = ellipsePoint(y, y0, radii); + sum += gauss(y, sigma) * + (erfSigma(x0r + radii.x, sigma) - erfSigma(x0r + xEllipsePoint, sigma) + + erfSigma(x0l - xEllipsePoint, sigma) - erfSigma(x0l - radii.x, sigma)); } - SetFragColor(vec4(vColor.rgb, vColor.a == 0.0 ? value : 1.0 - value)); + return sum / 2.0; +} + +// The value that needs to be subtracted to accommodate the top border corners. +float colorCutoutTop(float x0l, float x0r, float y0, vec2 radii, float sigma) { + return colorCutoutGeneral(x0l, x0r, y0, y0, y0 + radii.y, radii, sigma); +} + +// The value that needs to be subtracted to accommodate the bottom border corners. +float colorCutoutBottom(float x0l, float x0r, float y0, vec2 radii, float sigma) { + return colorCutoutGeneral(x0l, x0r, y0, y0 - radii.y, y0, radii, sigma); +} + +// The blurred color value for the point at `pos` with the top left corner of the box at +// `p_{0_"rect"}` and the bottom right corner of the box at `p_{1_"rect"}`. +float color(vec2 pos, vec2 p0Rect, vec2 p1Rect, vec2 radii, float sigma) { + // Compute the vector distances `p_0` and `p_1`. + vec2 p0 = p0Rect - pos, p1 = p1Rect - pos; + + // Compute the basic color `"colorFromRect"_sigma(p_0, p_1)`. This is all we have to do if + // the box is unrounded. + float cRect = colorFromRect(p0, p1, sigma); + if (radii.x == 0.0 || radii.y == 0.0) + return cRect; + + // Compute the inner corners of the box, taking border radii into account: `x_{0_l}`, + // `y_{0_t}`, `x_{0_r}`, and `y_{0_b}`. + float x0l = p0.x + radii.x; + float y0t = p1.y - radii.y; + float x0r = p1.x - radii.x; + float y0b = p0.y + radii.y; + + // Compute the final color: + // + // "colorFromRect"_sigma(p_0, p_1) - + // ("colorCutoutTop"_sigma(x_{0_l}, x_{0_r}, y_{0_t}, a, b) + + // "colorCutoutBottom"_sigma(x_{0_l}, x_{0_r}, y_{0_b}, a, b)) + float cCutoutTop = colorCutoutTop(x0l, x0r, y0t, radii, sigma); + float cCutoutBottom = colorCutoutBottom(x0l, x0r, y0b, radii, sigma); + return cRect - (cCutoutTop + cCutoutBottom); +} + +void main(void) { + vec2 pos = vPosition.xy; + vec2 p0Rect = vBorderPosition.xy, p1Rect = vBorderPosition.zw; + vec2 radii = vBorderRadii.xy; + float sigma = vBlurRadius / 2.0; + float value = color(pos, p0Rect, p1Rect, radii, sigma); + SetFragColor(vec4(vColor.rgb, max(value, 0.0))); } diff --git a/res/box_shadow.vs.glsl b/res/box_shadow.vs.glsl index 94e3231239..61f26664df 100644 --- a/res/box_shadow.vs.glsl +++ b/res/box_shadow.vs.glsl @@ -3,7 +3,6 @@ void main(void) vPosition = aPosition.xy; vColor = aColor; vBorderPosition = aBorderPosition; - vDestTextureSize = aDestTextureSize; vBorderRadii = aBorderRadii; vBlurRadius = aBlurRadius; gl_Position = uTransform * vec4(aPosition, 1.0); diff --git a/src/batch_builder.rs b/src/batch_builder.rs index 3bee801486..05150635ba 100644 --- a/src/batch_builder.rs +++ b/src/batch_builder.rs @@ -621,9 +621,11 @@ impl<'a> BatchBuilder<'a> { self.add_box_shadow_corner(matrix_index, &metrics.tl_outer, - &metrics.tl_inner, + &Point2D::new(metrics.tl_outer.x + metrics.edge_size, + metrics.tl_outer.y + metrics.edge_size), &metrics.tl_outer, ¢er, + &rect, &color, blur_radius, border_radius, @@ -633,10 +635,13 @@ impl<'a> BatchBuilder<'a> { clip_buffers, BasicRotationAngle::Upright); self.add_box_shadow_corner(matrix_index, - &Point2D::new(metrics.tr_inner.x, metrics.tr_outer.y), - &Point2D::new(metrics.tr_outer.x, metrics.tr_inner.y), + &Point2D::new(metrics.tr_outer.x - metrics.edge_size, + metrics.tr_outer.y), + &Point2D::new(metrics.tr_outer.x, + metrics.tr_outer.y + metrics.edge_size), &Point2D::new(center.x, metrics.tr_outer.y), &Point2D::new(metrics.tr_outer.x, center.y), + &rect, &color, blur_radius, border_radius, @@ -646,10 +651,12 @@ impl<'a> BatchBuilder<'a> { clip_buffers, BasicRotationAngle::Clockwise90); self.add_box_shadow_corner(matrix_index, - &metrics.br_inner, - &metrics.br_outer, + &Point2D::new(metrics.br_outer.x - metrics.edge_size, + metrics.br_outer.y - metrics.edge_size), + &Point2D::new(metrics.br_outer.x, metrics.br_outer.y), ¢er, &metrics.br_outer, + &rect, &color, blur_radius, border_radius, @@ -659,10 +666,13 @@ impl<'a> BatchBuilder<'a> { clip_buffers, BasicRotationAngle::Clockwise180); self.add_box_shadow_corner(matrix_index, - &Point2D::new(metrics.bl_outer.x, metrics.bl_inner.y), - &Point2D::new(metrics.bl_inner.x, metrics.bl_outer.y), + &Point2D::new(metrics.bl_outer.x, + metrics.bl_outer.y - metrics.edge_size), + &Point2D::new(metrics.bl_outer.x + metrics.edge_size, + metrics.bl_outer.y), &Point2D::new(metrics.bl_outer.x, center.y), &Point2D::new(center.x, metrics.bl_outer.y), + &rect, &color, blur_radius, border_radius, @@ -723,6 +733,7 @@ impl<'a> BatchBuilder<'a> { self.add_box_shadow_edge(matrix_index, &top_rect.origin, &top_rect.bottom_right(), + &rect, color, blur_radius, border_radius, @@ -730,10 +741,11 @@ impl<'a> BatchBuilder<'a> { &clip, resource_cache, clip_buffers, - BasicRotationAngle::Clockwise270); + BasicRotationAngle::Clockwise90); self.add_box_shadow_edge(matrix_index, &right_rect.origin, &right_rect.bottom_right(), + &rect, color, blur_radius, border_radius, @@ -741,10 +753,11 @@ impl<'a> BatchBuilder<'a> { &clip, resource_cache, clip_buffers, - BasicRotationAngle::Upright); + BasicRotationAngle::Clockwise180); self.add_box_shadow_edge(matrix_index, &bottom_rect.origin, &bottom_rect.bottom_right(), + &rect, color, blur_radius, border_radius, @@ -752,10 +765,11 @@ impl<'a> BatchBuilder<'a> { &clip, resource_cache, clip_buffers, - BasicRotationAngle::Clockwise90); + BasicRotationAngle::Clockwise270); self.add_box_shadow_edge(matrix_index, &left_rect.origin, &left_rect.bottom_right(), + &rect, color, blur_radius, border_radius, @@ -763,7 +777,7 @@ impl<'a> BatchBuilder<'a> { &clip, resource_cache, clip_buffers, - BasicRotationAngle::Clockwise180); + BasicRotationAngle::Upright); } fn fill_outside_area_of_inset_box_shadow(&mut self, @@ -1339,6 +1353,7 @@ impl<'a> BatchBuilder<'a> { bottom_right: &Point2D, corner_area_top_left: &Point2D, corner_area_bottom_right: &Point2D, + box_rect: &Rect, color: &ColorF, blur_radius: f32, border_radius: f32, @@ -1361,6 +1376,7 @@ impl<'a> BatchBuilder<'a> { let color_image = match BoxShadowRasterOp::create_corner(blur_radius, border_radius, + box_rect, inverted) { Some(raster_item) => { let raster_item = RasterItem::BoxShadow(raster_item); @@ -1387,6 +1403,7 @@ impl<'a> BatchBuilder<'a> { matrix_index: MatrixIndex, top_left: &Point2D, bottom_right: &Point2D, + box_rect: &Rect, color: &ColorF, blur_radius: f32, border_radius: f32, @@ -1406,6 +1423,7 @@ impl<'a> BatchBuilder<'a> { let color_image = match BoxShadowRasterOp::create_edge(blur_radius, border_radius, + box_rect, inverted) { Some(raster_item) => { let raster_item = RasterItem::BoxShadow(raster_item); @@ -1533,10 +1551,10 @@ impl BoxShadowMetrics { } } -fn compute_box_shadow_rect(box_bounds: &Rect, - box_offset: &Point2D, - spread_radius: f32) - -> Rect { +pub fn compute_box_shadow_rect(box_bounds: &Rect, + box_offset: &Point2D, + spread_radius: f32) + -> Rect { let mut rect = (*box_bounds).clone(); rect.origin.x += box_offset.x; rect.origin.y += box_offset.y; diff --git a/src/internal_types.rs b/src/internal_types.rs index 0b82aed917..d30b88f796 100644 --- a/src/internal_types.rs +++ b/src/internal_types.rs @@ -248,19 +248,8 @@ pub enum TextureUpdateDetails { Blur(Vec, Size2D, Au, TextureImage, TextureImage), /// All four corners, the tessellation index, and whether inverted, respectively. BorderRadius(Au, Au, Au, Au, u32, bool), - /// Blur radius, box shadow part, and whether inverted, respectively. - BoxShadow(Au, BoxShadowPart, bool), -} - -#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)] -pub enum BoxShadowPart { - /// The edge. - Edge, - - /// A corner with a border radius. - /// - /// TODO(pcwalton): Elliptical radii. - Corner(Au), + /// Blur radius, border radius, box rect, raster origin, and whether inverted, respectively. + BoxShadow(Au, Au, Rect, Point2D, bool), } #[derive(Clone, Copy, Debug)] @@ -854,24 +843,61 @@ impl BorderRadiusRasterOp { #[derive(Clone, Debug, Hash, Eq, PartialEq)] pub struct BoxShadowRasterOp { pub blur_radius: Au, + pub border_radius: Au, + // This is a tuple to work around the lack of `Eq` on `Rect`. + pub box_rect_origin: (Au, Au), + pub box_rect_size: (Au, Au), + pub raster_origin: (Au, Au), + pub raster_size: (Au, Au), pub part: BoxShadowPart, - pub raster_size: Au, pub inverted: bool, } impl BoxShadowRasterOp { - pub fn raster_size(blur_radius: f32, border_radius: f32) -> f32 { - (3.0 * blur_radius).max(border_radius) + 3.0 * blur_radius + pub fn raster_rect(blur_radius: f32, + border_radius: f32, + part: BoxShadowPart, + box_rect: &Rect) + -> Rect { + let outer_extent = 3.0 * blur_radius; + let inner_extent = outer_extent.max(border_radius); + let extent = outer_extent + inner_extent; + match part { + BoxShadowPart::Corner => { + Rect::new(Point2D::new(box_rect.origin.x - outer_extent, + box_rect.origin.y - outer_extent), + Size2D::new(extent, extent)) + } + BoxShadowPart::Edge => { + Rect::new(Point2D::new(box_rect.origin.x - outer_extent, + box_rect.origin.y + box_rect.size.height / 2.0), + Size2D::new(extent, 1.0)) + } + } } - pub fn create_corner(blur_radius: f32, border_radius: f32, inverted: bool) + pub fn create_corner(blur_radius: f32, + border_radius: f32, + box_rect: &Rect, + inverted: bool) -> Option { if blur_radius > 0.0 || border_radius > 0.0 { + let raster_rect = BoxShadowRasterOp::raster_rect(blur_radius, + border_radius, + BoxShadowPart::Corner, + box_rect); Some(BoxShadowRasterOp { blur_radius: Au::from_f32_px(blur_radius), - part: BoxShadowPart::Corner(Au::from_f32_px(border_radius)), - raster_size: Au::from_f32_px(BoxShadowRasterOp::raster_size(blur_radius, - border_radius)), + border_radius: Au::from_f32_px(border_radius), + box_rect_origin: (Au::from_f32_px(box_rect.origin.x), + Au::from_f32_px(box_rect.origin.y)), + box_rect_size: (Au::from_f32_px(box_rect.size.width), + Au::from_f32_px(box_rect.size.height)), + raster_origin: (Au::from_f32_px(raster_rect.origin.x), + Au::from_f32_px(raster_rect.origin.y)), + raster_size: (Au::from_f32_px(raster_rect.size.width), + Au::from_f32_px(raster_rect.size.height)), + part: BoxShadowPart::Corner, inverted: inverted, }) } else { @@ -879,14 +905,25 @@ impl BoxShadowRasterOp { } } - pub fn create_edge(blur_radius: f32, border_radius: f32, inverted: bool) + pub fn create_edge(blur_radius: f32, border_radius: f32, box_rect: &Rect, inverted: bool) -> Option { + let raster_rect = BoxShadowRasterOp::raster_rect(blur_radius, + border_radius, + BoxShadowPart::Edge, + box_rect); if blur_radius > 0.0 { Some(BoxShadowRasterOp { blur_radius: Au::from_f32_px(blur_radius), + border_radius: Au::from_f32_px(border_radius), + box_rect_origin: (Au::from_f32_px(box_rect.origin.x), + Au::from_f32_px(box_rect.origin.y)), + box_rect_size: (Au::from_f32_px(box_rect.size.width), + Au::from_f32_px(box_rect.size.height)), + raster_origin: (Au::from_f32_px(raster_rect.origin.x), + Au::from_f32_px(raster_rect.origin.y)), + raster_size: (Au::from_f32_px(raster_rect.size.width), + Au::from_f32_px(raster_rect.size.height)), part: BoxShadowPart::Edge, - raster_size: Au::from_f32_px(BoxShadowRasterOp::raster_size(blur_radius, - border_radius)), inverted: inverted, }) } else { @@ -895,6 +932,12 @@ impl BoxShadowRasterOp { } } +#[derive(Clone, Hash, PartialEq, Eq, Debug)] +pub enum BoxShadowPart { + Corner, + Edge, +} + #[derive(Clone, Hash, PartialEq, Eq, Debug)] pub struct GlyphKey { pub font_key: FontKey, diff --git a/src/renderer.rs b/src/renderer.rs index 3a5f018853..ab29467944 100644 --- a/src/renderer.rs +++ b/src/renderer.rs @@ -8,7 +8,7 @@ use fnv::FnvHasher; use gleam::gl; use internal_types::{RendererFrame, ResultMsg, TextureUpdateOp, BatchUpdateOp, BatchUpdateList}; use internal_types::{TextureUpdateDetails, TextureUpdateList, PackedVertex, RenderTargetMode}; -use internal_types::{ORTHO_NEAR_PLANE, ORTHO_FAR_PLANE, BoxShadowPart, BasicRotationAngle}; +use internal_types::{ORTHO_NEAR_PLANE, ORTHO_FAR_PLANE, BasicRotationAngle}; use internal_types::{PackedVertexForTextureCacheUpdate, CompositionOp}; use internal_types::{AxisDirection, LowLevelFilterOp, DrawCommand, ANGLE_FLOAT_TO_FIXED}; use ipc_channel::ipc; @@ -610,13 +610,27 @@ impl Renderer { None); batch.add_draw_item(update.id, TextureId(0), &vertices); } - TextureUpdateDetails::BoxShadow(blur_radius, part, inverted) => { + TextureUpdateDetails::BoxShadow(blur_radius, + border_radius, + box_rect, + raster_origin, + inverted) => { + let texture_origin = Point2D::new(x as f32, y as f32); + let device_pixel_ratio = self.device_pixel_ratio; self.update_texture_cache_for_box_shadow( update.id, - &Rect::new(Point2D::new(x as f32, y as f32), + &Rect::new(texture_origin, Size2D::new(width as f32, height as f32)), - blur_radius, - part, + &Rect::new( + texture_origin + Point2D::new( + (box_rect.origin.x - raster_origin.x) * + device_pixel_ratio, + (box_rect.origin.y - raster_origin.y) * + device_pixel_ratio), + Size2D::new(box_rect.size.width * device_pixel_ratio, + box_rect.size.height * device_pixel_ratio)), + blur_radius.to_f32_px() * device_pixel_ratio, + border_radius.to_f32_px() * device_pixel_ratio, inverted) } } @@ -630,14 +644,13 @@ impl Renderer { fn update_texture_cache_for_box_shadow(&mut self, update_id: TextureId, - rect: &Rect, - blur_radius: Au, - box_shadow_part: BoxShadowPart, + texture_rect: &Rect, + box_rect: &Rect, + blur_radius: f32, + border_radius: f32, inverted: bool) { let box_shadow_program_id = self.box_shadow_program_id; - let blur_radius = blur_radius.to_f32_px(); - let color = if inverted { ColorF::new(1.0, 1.0, 1.0, 0.0) } else { @@ -647,57 +660,51 @@ impl Renderer { let zero_point = Point2D::new(0.0, 0.0); let zero_size = Size2D::new(0.0, 0.0); - // `arc_radius_inner` here is just a flag to specify to the shader whether we're an edge - // (zero) or a corner (nonzero). - let (arc_radius_outer, arc_radius_inner) = match box_shadow_part { - BoxShadowPart::Edge => { - (Point2D::new(rect.size.width, 0.0), Point2D::new(0.0, 0.0)) - } - BoxShadowPart::Corner(border_radius) => { - (Point2D::new(border_radius.to_f32_px(), border_radius.to_f32_px()), - Point2D::new(1.0, 1.0)) - } - }; + let box_rect_top_left = Point2D::new(box_rect.origin.x, box_rect.origin.y); + let box_rect_bottom_right = + Point2D::new(box_rect_top_left.x + box_rect.size.width, + box_rect_top_left.y + box_rect.size.height); + let border_radii = Point2D::new(border_radius, border_radius); let vertices: [PackedVertexForTextureCacheUpdate; 4] = [ - PackedVertexForTextureCacheUpdate::new(&rect.origin, + PackedVertexForTextureCacheUpdate::new(&texture_rect.origin, &color, &zero_point, - &arc_radius_outer, - &arc_radius_inner, + &border_radii, &zero_point, - &rect.origin, - &rect.size, + &box_rect.origin, + &box_rect_bottom_right, + &zero_size, &zero_size, blur_radius), - PackedVertexForTextureCacheUpdate::new(&rect.top_right(), + PackedVertexForTextureCacheUpdate::new(&texture_rect.top_right(), &color, &zero_point, - &arc_radius_outer, - &arc_radius_inner, + &border_radii, &zero_point, - &rect.origin, - &rect.size, + &box_rect.origin, + &box_rect_bottom_right, + &zero_size, &zero_size, blur_radius), - PackedVertexForTextureCacheUpdate::new(&rect.bottom_left(), + PackedVertexForTextureCacheUpdate::new(&texture_rect.bottom_left(), &color, &zero_point, - &arc_radius_outer, - &arc_radius_inner, + &border_radii, &zero_point, - &rect.origin, - &rect.size, + &box_rect.origin, + &box_rect_bottom_right, + &zero_size, &zero_size, blur_radius), - PackedVertexForTextureCacheUpdate::new(&rect.bottom_right(), + PackedVertexForTextureCacheUpdate::new(&texture_rect.bottom_right(), &color, &zero_point, - &arc_radius_outer, - &arc_radius_inner, + &border_radii, &zero_point, - &rect.origin, - &rect.size, + &box_rect.origin, + &box_rect_bottom_right, + &zero_size, &zero_size, blur_radius), ]; diff --git a/src/resource_cache.rs b/src/resource_cache.rs index ffc443d7d7..d67dd32e12 100644 --- a/src/resource_cache.rs +++ b/src/resource_cache.rs @@ -161,7 +161,9 @@ impl ResourceCache { resource_list.for_each_raster(|raster_item| { if !self.cached_rasters.contains_key(raster_item) { let image_id = self.texture_cache.new_item_id(); - self.texture_cache.insert_raster_op(image_id, raster_item); + self.texture_cache.insert_raster_op(image_id, + raster_item, + self.device_pixel_ratio); self.cached_rasters.insert(raster_item.clone(), image_id); } }); diff --git a/src/resource_list.rs b/src/resource_list.rs index 1221b9295f..ce81fc4d58 100644 --- a/src/resource_list.rs +++ b/src/resource_list.rs @@ -1,6 +1,7 @@ use aabbtree::AABBTreeNode; use app_units::Au; -use euclid::Size2D; +use batch_builder; +use euclid::{Rect, Size2D}; use fnv::FnvHasher; use internal_types::{BorderRadiusRasterOp, BoxShadowRasterOp, DrawListItemIndex}; use internal_types::{Glyph, GlyphKey, RasterItem}; @@ -73,17 +74,27 @@ impl ResourceList { self.add_radius_raster(&radii.bottom_right, &zero_size, false, 0, ImageFormat::A8); } - pub fn add_box_shadow_corner(&mut self, blur_radius: f32, border_radius: f32, inverted: bool) { + pub fn add_box_shadow_corner(&mut self, + blur_radius: f32, + border_radius: f32, + box_rect: &Rect, + inverted: bool) { if let Some(raster_item) = BoxShadowRasterOp::create_corner(blur_radius, border_radius, + box_rect, inverted) { self.required_rasters.insert(RasterItem::BoxShadow(raster_item)); } } - pub fn add_box_shadow_edge(&mut self, blur_radius: f32, border_radius: f32, inverted: bool) { + pub fn add_box_shadow_edge(&mut self, + blur_radius: f32, + border_radius: f32, + box_rect: &Rect, + inverted: bool) { if let Some(raster_item) = BoxShadowRasterOp::create_edge(blur_radius, border_radius, + box_rect, inverted) { self.required_rasters.insert(RasterItem::BoxShadow(raster_item)); } @@ -153,16 +164,26 @@ impl BuildRequiredResources for AABBTreeNode { SpecificDisplayItem::BoxShadow(ref info) => { resource_list.add_radius_raster_for_border_radii( &BorderRadius::uniform(info.border_radius)); + + let box_rect = batch_builder::compute_box_shadow_rect(&info.box_bounds, + &info.offset, + info.spread_radius); resource_list.add_box_shadow_corner(info.blur_radius, info.border_radius, + &box_rect, false); - resource_list.add_box_shadow_edge(info.blur_radius, info.border_radius, false); + resource_list.add_box_shadow_edge(info.blur_radius, + info.border_radius, + &box_rect, + false); if info.clip_mode == BoxShadowClipMode::Inset { resource_list.add_box_shadow_corner(info.blur_radius, info.border_radius, + &box_rect, true); resource_list.add_box_shadow_edge(info.blur_radius, info.border_radius, + &box_rect, true); } } diff --git a/src/texture_cache.rs b/src/texture_cache.rs index 2d6b22a2d6..90db258ebb 100644 --- a/src/texture_cache.rs +++ b/src/texture_cache.rs @@ -740,7 +740,10 @@ impl TextureCache { } } - pub fn insert_raster_op(&mut self, image_id: TextureCacheItemId, item: &RasterItem) { + pub fn insert_raster_op(&mut self, + image_id: TextureCacheItemId, + item: &RasterItem, + device_pixel_ratio: f32) { let update_op = match item { &RasterItem::BorderRadius(ref op) => { let rect = Rect::new(Point2D::new(0.0, 0.0), @@ -782,11 +785,16 @@ impl TextureCache { } } &RasterItem::BoxShadow(ref op) => { + let device_raster_size = + Size2D::new((op.raster_size.0.to_nearest_px() as f32 * + device_pixel_ratio) as u32, + (op.raster_size.1.to_nearest_px() as f32 * + device_pixel_ratio) as u32); let allocation = self.allocate(image_id, 0, 0, - op.raster_size.to_nearest_px() as u32, - op.raster_size.to_nearest_px() as u32, + device_raster_size.width, + device_raster_size.height, ImageFormat::RGBA8, TextureCacheItemKind::Standard, BorderType::NoBorder, @@ -800,9 +808,18 @@ impl TextureCache { op: TextureUpdateOp::Update( allocation.item.requested_rect.origin.x, allocation.item.requested_rect.origin.y, - op.raster_size.to_nearest_px() as u32, - op.raster_size.to_nearest_px() as u32, - TextureUpdateDetails::BoxShadow(op.blur_radius, op.part, op.inverted)), + device_raster_size.width, + device_raster_size.height, + TextureUpdateDetails::BoxShadow( + op.blur_radius, + op.border_radius, + Rect::new(Point2D::new(op.box_rect_origin.0.to_f32_px(), + op.box_rect_origin.1.to_f32_px()), + Size2D::new(op.box_rect_size.0.to_f32_px(), + op.box_rect_size.1.to_f32_px())), + Point2D::new(op.raster_origin.0.to_f32_px(), + op.raster_origin.1.to_f32_px()), + op.inverted)), } } };