mirror of
https://github.com/servo/servo.git
synced 2025-08-07 14:35:33 +01:00
Render images in a very, very hacky way (i.e. lots of one-pixel-wide rectangles)
This commit is contained in:
parent
a6fe64d761
commit
ea53b5e800
4 changed files with 114 additions and 37 deletions
|
@ -1,6 +1,7 @@
|
||||||
import platform::osmain;
|
import platform::osmain;
|
||||||
import geom::*;
|
import geom::*;
|
||||||
import comm::*;
|
import comm::*;
|
||||||
|
import image::base::image;
|
||||||
import dl = layout::display_list;
|
import dl = layout::display_list;
|
||||||
import azure::*;
|
import azure::*;
|
||||||
import azure::bindgen::*;
|
import azure::bindgen::*;
|
||||||
|
@ -45,27 +46,20 @@ fn renderer<S: sink send copy>(sink: S) -> chan<msg> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn draw_display_list(
|
impl to_float for u8 {
|
||||||
draw_target: AzDrawTargetRef,
|
fn to_float() -> float {
|
||||||
display_list: dl::display_list
|
(self as float) / 255f
|
||||||
) {
|
}
|
||||||
clear(draw_target);
|
}
|
||||||
|
|
||||||
for display_list.each {|item|
|
fn draw_solid_color(draw_target: AzDrawTargetRef, item: dl::display_item,
|
||||||
#debug["drawing %?", item];
|
r: u8, g: u8, b: u8) {
|
||||||
let (r, g, b) = alt check item.item_type {
|
|
||||||
dl::display_item_solid_color(r, g, b) { (r, g, b) }
|
|
||||||
};
|
|
||||||
let bounds = (*item).bounds;
|
let bounds = (*item).bounds;
|
||||||
|
|
||||||
let to_float = fn@(u: u8) -> float {
|
|
||||||
(u as float) / 255f
|
|
||||||
};
|
|
||||||
|
|
||||||
let red_color = {
|
let red_color = {
|
||||||
r: to_float(r) as AzFloat,
|
r: r.to_float() as AzFloat,
|
||||||
g: to_float(g) as AzFloat,
|
g: g.to_float() as AzFloat,
|
||||||
b: to_float(b) as AzFloat,
|
b: b.to_float() as AzFloat,
|
||||||
a: 1f as AzFloat
|
a: 1f as AzFloat
|
||||||
};
|
};
|
||||||
let red_pattern = AzCreateColorPattern(ptr::addr_of(red_color));
|
let red_pattern = AzCreateColorPattern(ptr::addr_of(red_color));
|
||||||
|
@ -84,6 +78,72 @@ fn draw_display_list(
|
||||||
|
|
||||||
AzReleaseColorPattern(red_pattern);
|
AzReleaseColorPattern(red_pattern);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn draw_image(draw_target: AzDrawTargetRef, item: dl::display_item,
|
||||||
|
-image: ~image) {
|
||||||
|
// FIXME: This is hideously inefficient.
|
||||||
|
|
||||||
|
let bounds = (*item).bounds;
|
||||||
|
|
||||||
|
if (image.depth < 3u) {
|
||||||
|
#debug("TODO: can't draw images with depth less than 3 yet");
|
||||||
|
ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
let stride = image.width * image.depth;
|
||||||
|
uint::range(0u, image.height) {
|
||||||
|
|y|
|
||||||
|
uint::range(0u, image.width) {
|
||||||
|
|x|
|
||||||
|
let color = {
|
||||||
|
r: image.data[y * stride + x * image.depth].to_float()
|
||||||
|
as AzFloat,
|
||||||
|
g: image.data[y * stride + x * image.depth + 1u].to_float()
|
||||||
|
as AzFloat,
|
||||||
|
b: image.data[y * stride + x * image.depth + 2u].to_float()
|
||||||
|
as AzFloat,
|
||||||
|
a: 1f as AzFloat
|
||||||
|
};
|
||||||
|
let pattern = AzCreateColorPattern(ptr::addr_of(color));
|
||||||
|
|
||||||
|
let pixel_rect = {
|
||||||
|
x: (au_to_px(bounds.origin.x) + (x as int)) as AzFloat,
|
||||||
|
y: (au_to_px(bounds.origin.y) + (y as int)) as AzFloat,
|
||||||
|
width: 1f as AzFloat,
|
||||||
|
height: 1f as AzFloat
|
||||||
|
};
|
||||||
|
AzDrawTargetFillRect(
|
||||||
|
draw_target,
|
||||||
|
ptr::addr_of(pixel_rect),
|
||||||
|
unsafe { unsafe::reinterpret_cast(pattern) }
|
||||||
|
);
|
||||||
|
|
||||||
|
AzReleaseColorPattern(pattern);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn draw_display_list(
|
||||||
|
draw_target: AzDrawTargetRef,
|
||||||
|
display_list: dl::display_list
|
||||||
|
) {
|
||||||
|
clear(draw_target);
|
||||||
|
|
||||||
|
for display_list.each {|item|
|
||||||
|
#debug["drawing %?", item];
|
||||||
|
|
||||||
|
alt item.item_type {
|
||||||
|
dl::display_item_solid_color(r, g, b) {
|
||||||
|
draw_solid_color(draw_target, item, r, g, b);
|
||||||
|
}
|
||||||
|
dl::display_item_image(image) {
|
||||||
|
draw_image(draw_target, item, image);
|
||||||
|
}
|
||||||
|
dl::padding(*) {
|
||||||
|
fail "should never see padding";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn clear(draw_target: AzDrawTargetRef) {
|
fn clear(draw_target: AzDrawTargetRef) {
|
||||||
|
|
|
@ -2,3 +2,6 @@ export image;
|
||||||
export load;
|
export load;
|
||||||
import stb_image::image::{image, load};
|
import stb_image::image::{image, load};
|
||||||
|
|
||||||
|
// FIXME: Images must not be copied every frame. Instead we should atomically
|
||||||
|
// reference count them.
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,9 @@
|
||||||
import gfx::geom::*;
|
import gfx::geom::*;
|
||||||
|
import image::base::image;
|
||||||
|
|
||||||
enum item_type {
|
enum item_type {
|
||||||
display_item_solid_color(u8, u8, u8),
|
display_item_solid_color(u8, u8, u8),
|
||||||
|
display_item_image(~image),
|
||||||
// FIXME: Shape code does not understand the alignment without this
|
// FIXME: Shape code does not understand the alignment without this
|
||||||
padding(u8, u8, u8, u8)
|
padding(u8, u8, u8, u8)
|
||||||
}
|
}
|
||||||
|
|
|
@ -61,13 +61,25 @@ fn build_display_list(box: @base::box) -> display_list::display_list {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn box_to_display_item(box: @base::box) -> dl::display_item {
|
fn box_to_display_item(box: @base::box) -> dl::display_item {
|
||||||
|
let mut item;
|
||||||
|
alt box.appearance.background_image {
|
||||||
|
some(image) {
|
||||||
|
item = dl::display_item({
|
||||||
|
item_type: dl::display_item_image(~copy *image),
|
||||||
|
bounds: box.bounds
|
||||||
|
});
|
||||||
|
}
|
||||||
|
none {
|
||||||
let r = rand::rng();
|
let r = rand::rng();
|
||||||
let item = dl::display_item({
|
item = dl::display_item({
|
||||||
item_type: dl::display_item_solid_color(r.next() as u8,
|
item_type: dl::display_item_solid_color(r.next() as u8,
|
||||||
r.next() as u8,
|
r.next() as u8,
|
||||||
r.next() as u8),
|
r.next() as u8),
|
||||||
bounds: box.bounds
|
bounds: box.bounds
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#debug("layout: display item: %?", item);
|
#debug("layout: display item: %?", item);
|
||||||
ret item;
|
ret item;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue