Skip to content

Clip stack collapse path #595

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

Merged
merged 5 commits into from
Nov 28, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
169 changes: 95 additions & 74 deletions sample/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
extern crate app_units;
extern crate webrender;
extern crate glutin;
extern crate gleam;
extern crate webrender_traits;
extern crate euclid;

use app_units::Au;
use euclid::{Size2D, Point2D, Rect, Matrix4D};
use gleam::gl;
use std::path::PathBuf;
Expand All @@ -13,7 +15,7 @@ use std::fs::File;
use std::io::Read;
use std::env;

fn _load_file(name: &str) -> Vec<u8> {
fn load_file(name: &str) -> Vec<u8> {
let mut file = File::open(name).unwrap();
let mut buffer = vec![];
file.read_to_end(&mut buffer).unwrap();
Expand Down Expand Up @@ -89,10 +91,6 @@ fn main() {
let (mut renderer, sender) = webrender::renderer::Renderer::new(opts);
let api = sender.create_api();

// let font_path = "/usr/share/fonts/truetype/dejavu/DejaVuSansMono.ttf";
// let font_bytes = load_file(font_path);
// let font_key = api.add_raw_font(font_bytes);

let notifier = Box::new(Notifier::new(window.create_window_proxy()));
renderer.set_render_notifier(notifier);

Expand All @@ -103,97 +101,120 @@ fn main() {
let mut builder = webrender_traits::DisplayListBuilder::new(pipeline_id);

let bounds = Rect::new(Point2D::new(0.0, 0.0), Size2D::new(width as f32, height as f32));
let clip_region = {
let complex = webrender_traits::ComplexClipRegion::new(
Rect::new(Point2D::new(50.0, 50.0), Size2D::new(100.0, 100.0)),
webrender_traits::BorderRadius::uniform(20.0));

builder.new_clip_region(&bounds, vec![complex], None)
};

builder.push_stacking_context(webrender_traits::ScrollPolicy::Scrollable,
bounds,
bounds,
clip_region,
0,
&Matrix4D::identity(),
&Matrix4D::identity(),
webrender_traits::MixBlendMode::Normal,
Vec::new());

let clip_region = {
let sub_clip = {
let mask = webrender_traits::ImageMask {
image: api.add_image(2, 2, None, ImageFormat::A8, ImageData::new(vec![0,80, 180, 255])),
rect: Rect::new(Point2D::new(75.0, 75.0), Size2D::new(100.0, 100.0)),
repeat: false,
};
let radius = webrender_traits::BorderRadius::uniform(20.0);
let complex = webrender_traits::ComplexClipRegion::new(
Rect::new(Point2D::new(50.0, 50.0), Size2D::new(100.0, 100.0)),
radius);
webrender_traits::BorderRadius::uniform(20.0));

builder.new_clip_region(&bounds, vec![complex], Some(mask))
};

builder.push_rect(Rect::new(Point2D::new(100.0, 100.0), Size2D::new(100.0, 100.0)),
clip_region,
sub_clip,
ColorF::new(0.0, 1.0, 0.0, 1.0));
builder.push_rect(Rect::new(Point2D::new(250.0, 100.0), Size2D::new(100.0, 100.0)),
sub_clip,
ColorF::new(0.0, 1.0, 0.0, 1.0));

let _text_bounds = Rect::new(Point2D::new(100.0, 200.0), Size2D::new(700.0, 300.0));

let _glyphs = vec![
GlyphInstance {
index: 48,
x: 100.0,
y: 100.0,
},
GlyphInstance {
index: 68,
x: 150.0,
y: 100.0,
},
GlyphInstance {
index: 80,
x: 200.0,
y: 100.0,
},
GlyphInstance {
index: 82,
x: 250.0,
y: 100.0,
},
GlyphInstance {
index: 81,
x: 300.0,
y: 100.0,
},
GlyphInstance {
index: 3,
x: 350.0,
y: 100.0,
},
GlyphInstance {
index: 86,
x: 400.0,
y: 100.0,
},
GlyphInstance {
index: 79,
x: 450.0,
y: 100.0,
},
GlyphInstance {
index: 72,
x: 500.0,
y: 100.0,
},
GlyphInstance {
index: 83,
x: 550.0,
y: 100.0,
},
GlyphInstance {
index: 87,
x: 600.0,
y: 100.0,
},
GlyphInstance {
index: 17,
x: 650.0,
y: 100.0,
},
];
if false { // draw text?
let font_bytes = load_file("res/FreeSans.ttf");
let font_key = api.add_raw_font(font_bytes);

let text_bounds = Rect::new(Point2D::new(100.0, 200.0), Size2D::new(700.0, 300.0));

let glyphs = vec![
GlyphInstance {
index: 48,
x: 100.0,
y: 100.0,
},
GlyphInstance {
index: 68,
x: 150.0,
y: 100.0,
},
GlyphInstance {
index: 80,
x: 200.0,
y: 100.0,
},
GlyphInstance {
index: 82,
x: 250.0,
y: 100.0,
},
GlyphInstance {
index: 81,
x: 300.0,
y: 100.0,
},
GlyphInstance {
index: 3,
x: 350.0,
y: 100.0,
},
GlyphInstance {
index: 86,
x: 400.0,
y: 100.0,
},
GlyphInstance {
index: 79,
x: 450.0,
y: 100.0,
},
GlyphInstance {
index: 72,
x: 500.0,
y: 100.0,
},
GlyphInstance {
index: 83,
x: 550.0,
y: 100.0,
},
GlyphInstance {
index: 87,
x: 600.0,
y: 100.0,
},
GlyphInstance {
index: 17,
x: 650.0,
y: 100.0,
},
];

builder.push_text(text_bounds,
webrender_traits::ClipRegion::simple(&bounds),
glyphs,
font_key,
ColorF::new(1.0, 1.0, 0.0, 1.0),
Au::from_px(32),
Au::from_px(0));
}

builder.pop_stacking_context();

Expand Down
2 changes: 2 additions & 0 deletions webrender/res/clip_shared.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ struct CacheClipInstance {
int render_task_index;
int layer_index;
int data_index;
int base_task_index;
};

CacheClipInstance fetch_clip_item(int index) {
Expand All @@ -21,6 +22,7 @@ CacheClipInstance fetch_clip_item(int index) {
cci.render_task_index = data0.x;
cci.layer_index = data0.y;
cci.data_index = data0.z;
cci.base_task_index = data0.w;

return cci;
}
Expand Down
8 changes: 8 additions & 0 deletions webrender/res/cs_clip_copy.fs.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

void main(void) {
float alpha = texelFetch(sCache, ivec3(vClipMaskUv), 0).a;
oFragColor = vec4(1.0, 1.0, 1.0, alpha);
}
19 changes: 19 additions & 0 deletions webrender/res/cs_clip_copy.vs.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#line 1
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

void main(void) {
CacheClipInstance cci = fetch_clip_item(gl_InstanceID);
ClipArea area = fetch_clip_area(cci.render_task_index);
ClipArea source = fetch_clip_area(cci.base_task_index);

vec2 final_pos = mix(area.task_bounds.xy, area.task_bounds.zw, aPosition.xy);

gl_Position = uTransform * vec4(final_pos, 0.0, 1.0);

// convert to the source task space via the screen space
vec2 tuv = final_pos - area.task_bounds.xy + area.screen_origin_target_index.xy +
source.task_bounds.xy - source.screen_origin_target_index.xy;
vClipMaskUv = vec3(tuv, source.screen_origin_target_index.z);
}
Loading