Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix stroke widths and hairline strokes in wgpu and webgl #9981

Draft
wants to merge 17 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 9 additions & 12 deletions core/src/avm1/globals/movie_clip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@ use crate::prelude::*;
use crate::string::AvmString;
use crate::vminterface::Instantiator;
use gc_arena::MutationContext;
use ruffle_render::shape_utils::DrawCommand;
use ruffle_render::bitmap::BitmapSize;
use ruffle_render::shape_utils::{DrawCommand, FillStyle, LineStyle};
use std::str::FromStr;
use swf::{
BlendMode, FillStyle, Fixed8, Gradient, GradientInterpolation, GradientRecord, GradientSpread,
LineCapStyle, LineJoinStyle, LineStyle, Rectangle, Twips,
BlendMode, Fixed8, Gradient, GradientInterpolation, GradientRecord, GradientSpread,
LineCapStyle, LineJoinStyle, Rectangle, Twips,
};

macro_rules! mc_method {
Expand Down Expand Up @@ -406,14 +407,6 @@ fn begin_bitmap_fill<'gc>(
} else {
return Ok(Value::Undefined);
};
let bitmap = ruffle_render::bitmap::BitmapInfo {
handle,
width: bitmap_data.width() as u16,
height: bitmap_data.height() as u16,
};
let id = movie_clip
.drawing(activation.context.gc_context)
.add_bitmap(bitmap);

let mut matrix = avm1::globals::matrix::object_to_matrix_or_default(
args.get(1)
Expand All @@ -437,7 +430,11 @@ fn begin_bitmap_fill<'gc>(
movie_clip
.drawing(activation.context.gc_context)
.set_fill_style(Some(FillStyle::Bitmap {
id,
size: Some(BitmapSize {
width: bitmap_data.width() as u16,
height: bitmap_data.height() as u16,
}),
handle: Some(handle),
matrix: matrix.into(),
is_smoothed,
is_repeating,
Expand Down
4 changes: 2 additions & 2 deletions core/src/avm2/globals/flash/display/graphics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ use crate::avm2_stub_method;
use crate::display_object::TDisplayObject;
use crate::drawing::Drawing;
use crate::string::WStr;
use ruffle_render::shape_utils::DrawCommand;
use ruffle_render::shape_utils::{DrawCommand, FillStyle, LineStyle};
use std::f64::consts::FRAC_1_SQRT_2;
use swf::{Color, FillStyle, Fixed8, LineCapStyle, LineJoinStyle, LineStyle, Twips};
use swf::{Color, Fixed8, LineCapStyle, LineJoinStyle, Twips};

/// Convert an RGB `color` and `alpha` argument pair into a `swf::Color`.
/// `alpha` is normalized from 0.0 - 1.0.
Expand Down
14 changes: 8 additions & 6 deletions core/src/display_object/edit_text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use chrono::Utc;
use core::fmt;
use gc_arena::{Collect, Gc, GcCell, MutationContext};
use ruffle_render::commands::CommandHandler;
use ruffle_render::shape_utils::DrawCommand;
use ruffle_render::shape_utils::{DrawCommand, FillStyle, LineStyle};
use ruffle_render::transform::Transform;
use std::{cell::Ref, cell::RefMut, sync::Arc};
use swf::{Color, Twips};
Expand Down Expand Up @@ -643,7 +643,7 @@ impl<'gc> EditText<'gc> {
.intersects(EditTextFlag::BORDER | EditTextFlag::HAS_BACKGROUND)
{
let line_style = write.flags.contains(EditTextFlag::BORDER).then_some(
swf::LineStyle::new()
LineStyle::new()
.with_width(Twips::new(1))
.with_color(write.border_color.clone()),
);
Expand All @@ -652,7 +652,7 @@ impl<'gc> EditText<'gc> {
let fill_style = write
.flags
.contains(EditTextFlag::HAS_BACKGROUND)
.then_some(swf::FillStyle::Color(write.background_color.clone()));
.then_some(FillStyle::Color(write.background_color.clone()));
write.drawing.set_fill_style(fill_style);

let width = write.bounds.width();
Expand Down Expand Up @@ -904,9 +904,11 @@ impl<'gc> EditText<'gc> {

// Render glyph.
let glyph_shape_handle = glyph.shape_handle(context.renderer);
context
.commands
.render_shape(glyph_shape_handle, context.transform_stack.transform());
context.commands.render_shape(
glyph_shape_handle,
context.transform_stack.transform(),
false,
);
context.transform_stack.pop();

if let Some((caret_pos, length)) = caret {
Expand Down
103 changes: 90 additions & 13 deletions core/src/display_object/graphic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ use core::fmt;
use gc_arena::{Collect, GcCell, MutationContext};
use ruffle_render::backend::ShapeHandle;
use ruffle_render::commands::CommandHandler;
use ruffle_render::shape_utils::{DistilledShape, ShapeStrokes};
use ruffle_render::transform::Transform;
use std::cell::{Ref, RefMut};
use std::sync::Arc;

Expand All @@ -35,6 +37,10 @@ pub struct GraphicData<'gc> {
static_data: gc_arena::Gc<'gc, GraphicStatic>,
avm2_object: Option<Avm2Object<'gc>>,
drawing: Option<Drawing>,
#[collect(require_static)]
strokes_handle: Option<ShapeHandle>,
#[collect(require_static)]
last_scale: (f32, f32),
}

impl<'gc> Graphic<'gc> {
Expand All @@ -45,16 +51,21 @@ impl<'gc> Graphic<'gc> {
movie: Arc<SwfMovie>,
) -> Self {
let library = context.library.library_for_movie(movie.clone()).unwrap();
let bitmap_source = MovieLibrarySource {
library,
gc_context: context.gc_context,
};
let shape = DistilledShape::from_shape(&swf_shape, &bitmap_source, context.renderer);

let static_data = GraphicStatic {
id: swf_shape.id,
bounds: swf_shape.shape_bounds.clone(),
render_handle: Some(context.renderer.register_shape(
(&swf_shape).into(),
&MovieLibrarySource {
library,
gc_context: context.gc_context,
},
)),
fills_handle: Some(
context
.renderer
.register_shape_fills(&shape.fills, shape.id),
),
strokes: Some(shape.strokes),
shape: swf_shape,
movie,
};
Expand All @@ -66,6 +77,8 @@ impl<'gc> Graphic<'gc> {
static_data: gc_arena::Gc::allocate(context.gc_context, static_data),
avm2_object: None,
drawing: None,
strokes_handle: None,
last_scale: (0.0, 0.0),
},
))
}
Expand All @@ -78,7 +91,8 @@ impl<'gc> Graphic<'gc> {
let static_data = GraphicStatic {
id: 0,
bounds: Default::default(),
render_handle: None,
fills_handle: None,
strokes: None,
shape: swf::Shape {
version: 32,
id: 0,
Expand All @@ -102,6 +116,8 @@ impl<'gc> Graphic<'gc> {
static_data: gc_arena::Gc::allocate(context.gc_context, static_data),
avm2_object: Some(avm2_object),
drawing: Some(drawing),
strokes_handle: None,
last_scale: (0.0, 0.0),
},
))
}
Expand Down Expand Up @@ -172,7 +188,9 @@ impl<'gc> TDisplayObject<'gc> for Graphic<'gc> {
.library_for_movie_mut(self.movie())
.get_graphic(id)
{
self.0.write(context.gc_context).static_data = new_graphic.0.read().static_data;
let mut write = self.0.write(context.gc_context);
write.static_data = new_graphic.0.read().static_data;
write.last_scale = (0.0, 0.0); // Force recreation of stroke
} else {
tracing::warn!("PlaceObject: expected Graphic at character ID {}", id);
}
Expand All @@ -182,18 +200,76 @@ impl<'gc> TDisplayObject<'gc> for Graphic<'gc> {
// Noop
}

fn render_self(&self, context: &mut RenderContext) {
fn render_self(&self, context: &mut RenderContext<'_, 'gc>) {
if !context.is_offscreen && !self.world_bounds().intersects(&context.stage.view_bounds()) {
// Off-screen; culled
return;
}

if let Some(drawing) = &self.0.read().drawing {
drawing.render(context);
} else if let Some(render_handle) = self.0.read().static_data.render_handle {
return;
}

if let Some(render_handle) = self.0.read().static_data.fills_handle {
context
.commands
.render_shape(render_handle, context.transform_stack.transform())
.render_shape(render_handle, context.transform_stack.transform(), false)
}

// Update the stroke if we're drawing it at a different scale than last time
let old_scale = self.0.read().last_scale;
let cur_matrix = context.transform_stack.transform().matrix;
let render_stroke_matrix = Matrix {
a: 1.0,
b: 0.0,
c: 0.0,
d: 1.0,
tx: cur_matrix.tx,
ty: cur_matrix.ty,
};
let cur_scale = (
f32::abs(cur_matrix.a + cur_matrix.c),
f32::abs(cur_matrix.b + cur_matrix.d),
);
if old_scale != cur_scale {
let mut write = self.0.write(context.gc_context);
if let Some(strokes) = &write.static_data.strokes {
let build_stroke_matrix = Matrix {
a: cur_matrix.a,
b: cur_matrix.b,
c: cur_matrix.c,
d: cur_matrix.d,
tx: Default::default(),
ty: Default::default(),
};
if let Some(handle) = write.strokes_handle {
context.renderer.replace_shape_strokes(
strokes,
write.static_data.id,
build_stroke_matrix,
handle,
);
} else {
write.strokes_handle = Some(context.renderer.register_shape_strokes(
strokes,
write.static_data.id,
build_stroke_matrix,
));
}
}
write.last_scale = cur_scale;
}

if let Some(render_handle) = self.0.read().strokes_handle {
context.commands.render_shape(
render_handle,
Transform {
matrix: render_stroke_matrix,
color_transform: context.transform_stack.transform().color_transform,
},
true,
);
}
}

Expand Down Expand Up @@ -268,7 +344,8 @@ impl<'gc> TDisplayObject<'gc> for Graphic<'gc> {
struct GraphicStatic {
id: CharacterId,
shape: swf::Shape,
render_handle: Option<ShapeHandle>,
fills_handle: Option<ShapeHandle>,
strokes: Option<ShapeStrokes>,
bounds: Rectangle<Twips>,
movie: Arc<SwfMovie>,
}
Loading