Skip to content

Commit 0736957

Browse files
authored
Auto merge of #417 - pcwalton:image-border, r=pcwalton
Add 1px of texture border around non-repeating image patterns. Closes #401.
2 parents a05270f + 444cae2 commit 0736957

File tree

1 file changed

+18
-7
lines changed

1 file changed

+18
-7
lines changed

renderer/src/paint.rs

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,10 @@ pub(crate) struct PaintColorTextureMetadata {
344344
pub(crate) filter: PaintFilter,
345345
/// How the color texture is to be composited over the base color.
346346
pub(crate) composite_op: PaintCompositeOp,
347+
/// How much of a border there needs to be around the image.
348+
///
349+
/// The border ensures clamp-to-edge yields the right result.
350+
pub(crate) border: Vector2I,
347351
}
348352

349353
#[derive(Clone, Copy, Debug)]
@@ -488,9 +492,13 @@ impl Palette {
488492
},
489493
transform: Transform2F::default(),
490494
composite_op: overlay.composite_op(),
495+
border: Vector2I::zero(),
491496
})
492497
}
493498
PaintContents::Pattern(ref pattern) => {
499+
let border = vec2i(if pattern.repeat_x() { 0 } else { 1 },
500+
if pattern.repeat_y() { 0 } else { 1 });
501+
494502
let location;
495503
match *pattern.source() {
496504
PatternSource::RenderTarget { id: render_target_id, .. } => {
@@ -501,24 +509,26 @@ impl Palette {
501509
// TODO(pcwalton): We should be able to use tile cleverness to
502510
// repeat inside the atlas in some cases.
503511
let image_hash = image.get_hash();
504-
//println!("image hash: {:?}", image_hash);
505512
match texture_manager.cached_images.get(&image_hash) {
506513
Some(cached_location) => {
507-
//println!("... cache hit: {:?}", cached_location);
508514
location = *cached_location;
509515
used_image_hashes.insert(image_hash);
510516
}
511517
None => {
512-
//println!("... cache MISS");
518+
// Leave a pixel of border on the side.
513519
let allocation_mode = AllocationMode::OwnPage;
514-
location = allocator.allocate(image.size(),
515-
allocation_mode);
520+
location = allocator.allocate(
521+
image.size() + border * 2,
522+
allocation_mode);
516523
texture_manager.cached_images.insert(image_hash,
517524
location);
518525
}
519526
}
520527
image_texel_info.push(ImageTexelInfo {
521-
location,
528+
location: TextureLocation {
529+
page: location.page,
530+
rect: location.rect.contract(border),
531+
},
522532
texels: (*image.pixels()).clone(),
523533
});
524534
}
@@ -546,8 +556,9 @@ impl Palette {
546556
page_scale: allocator.page_scale(location.page),
547557
sampling_flags,
548558
filter,
549-
transform: Transform2F::default(),
559+
transform: Transform2F::from_translation(border.to_f32()),
550560
composite_op: overlay.composite_op(),
561+
border,
551562
})
552563
}
553564
}

0 commit comments

Comments
 (0)