Skip to content

Add an API for providing external images. #561

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 1 commit into from
Nov 16, 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
4 changes: 2 additions & 2 deletions sample/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use gleam::gl;
use std::path::PathBuf;
use std::ffi::CStr;
use webrender_traits::{AuxiliaryListsBuilder, ColorF, Epoch, GlyphInstance};
use webrender_traits::{ImageFormat, PipelineId, RendererKind};
use webrender_traits::{ImageData, ImageFormat, PipelineId, RendererKind};
use std::fs::File;
use std::io::Read;
use std::env;
Expand Down Expand Up @@ -135,7 +135,7 @@ fn main() {

let clip_region = {
let mask = webrender_traits::ImageMask {
image: api.add_image(2, 2, None, ImageFormat::A8, vec![0,80, 180, 255]),
image: api.add_image(2, 2, None, ImageFormat::A8, ImageData::Raw(vec![0,80, 180, 255])),
rect: Rect::new(Point2D::new(75.0, 75.0), Size2D::new(100.0, 100.0)),
repeat: false,
};
Expand Down
7 changes: 4 additions & 3 deletions webrender/res/cs_text_run.vs.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,18 @@ void main(void) {
TextRun text = fetch_text_run(cpi.specific_prim_index);
Glyph glyph = fetch_glyph(cpi.sub_index);
PrimitiveGeometry pg = fetch_prim_geometry(cpi.global_prim_index);
ResourceRect res = fetch_resource_rect(cpi.user_data.x);

// Glyphs size is already in device-pixels.
// The render task origin is in device-pixels. Offset that by
// the glyph offset, relative to its primitive bounding rect.
vec2 size = glyph.uv_rect.zw - glyph.uv_rect.xy;
vec2 size = res.uv_rect.zw - res.uv_rect.xy;
vec2 origin = task.data0.xy + uDevicePixelRatio * (glyph.offset.xy - pg.local_rect.xy);
vec4 local_rect = vec4(origin, size);

vec2 texture_size = vec2(textureSize(sColor0, 0));
vec2 st0 = glyph.uv_rect.xy / texture_size;
vec2 st1 = glyph.uv_rect.zw / texture_size;
vec2 st0 = res.uv_rect.xy / texture_size;
vec2 st1 = res.uv_rect.zw / texture_size;

vec2 pos = mix(local_rect.xy,
local_rect.xy + local_rect.zw,
Expand Down
32 changes: 23 additions & 9 deletions webrender/res/prim_shared.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ uniform sampler2D sData16;
uniform sampler2D sData32;
uniform sampler2D sData64;
uniform sampler2D sData128;
uniform sampler2D sResourceRects;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why does it have to be a separate texture instead of re-using sData16?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the future, I expect this will be an array managed by the texture cache on a thread that is running async to the backend thread - so I've made it a separate array because of that.


ivec2 get_fetch_uv(int index, int vecs_per_item) {
int items_per_row = WR_MAX_VERTEX_TEXTURE_WIDTH / vecs_per_item;
Expand Down Expand Up @@ -208,16 +209,14 @@ GradientStop fetch_gradient_stop(int index) {

struct Glyph {
vec4 offset;
vec4 uv_rect;
};

Glyph fetch_glyph(int index) {
Glyph glyph;

ivec2 uv = get_fetch_uv_2(index);
ivec2 uv = get_fetch_uv_1(index);

glyph.offset = texelFetchOffset(sData32, uv, 0, ivec2(0, 0));
glyph.uv_rect = texelFetchOffset(sData32, uv, 0, ivec2(1, 0));
glyph.offset = texelFetchOffset(sData16, uv, 0, ivec2(0, 0));

return glyph;
}
Expand Down Expand Up @@ -324,19 +323,22 @@ struct CachePrimitiveInstance {
int specific_prim_index;
int render_task_index;
int sub_index;
ivec4 user_data;
};

CachePrimitiveInstance fetch_cache_instance(int index) {
CachePrimitiveInstance cpi;

int offset = index * 1;
int offset = index * 2;

ivec4 data0 = int_data[offset + 0];
ivec4 data1 = int_data[offset + 1];

cpi.global_prim_index = data0.x;
cpi.specific_prim_index = data0.y;
cpi.render_task_index = data0.z;
cpi.sub_index = data0.w;
cpi.user_data = data1;

return cpi;
}
Expand Down Expand Up @@ -536,6 +538,20 @@ TransformVertexInfo write_transform_vertex(vec4 instance_rect,

#endif //WR_FEATURE_TRANSFORM

struct ResourceRect {
vec4 uv_rect;
};

ResourceRect fetch_resource_rect(int index) {
ResourceRect rect;

ivec2 uv = get_fetch_uv_1(index);

rect.uv_rect = texelFetchOffset(sResourceRects, uv, 0, ivec2(0, 0));

return rect;
}

struct Rectangle {
vec4 color;
};
Expand Down Expand Up @@ -565,18 +581,16 @@ TextRun fetch_text_run(int index) {
}

struct Image {
vec4 st_rect; // Location of the image texture in the texture atlas.
vec4 stretch_size_and_tile_spacing; // Size of the actual image and amount of space between
// tiled instances of this image.
};

Image fetch_image(int index) {
Image image;

ivec2 uv = get_fetch_uv_2(index);
ivec2 uv = get_fetch_uv_1(index);

image.st_rect = texelFetchOffset(sData32, uv, 0, ivec2(0, 0));
image.stretch_size_and_tile_spacing = texelFetchOffset(sData32, uv, 0, ivec2(1, 0));
image.stretch_size_and_tile_spacing = texelFetchOffset(sData16, uv, 0, ivec2(0, 0));

return image;
}
Expand Down
5 changes: 3 additions & 2 deletions webrender/res/ps_image.vs.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
void main(void) {
Primitive prim = load_primitive(gl_InstanceID);
Image image = fetch_image(prim.prim_index);
ResourceRect res = fetch_resource_rect(prim.user_data.x);

#ifdef WR_FEATURE_TRANSFORM
TransformVertexInfo vi = write_transform_vertex(prim.local_rect,
Expand All @@ -26,8 +27,8 @@ void main(void) {

// vUv will contain how many times this image has wrapped around the image size.
vec2 texture_size = vec2(textureSize(sColor0, 0));
vec2 st0 = image.st_rect.xy / texture_size;
vec2 st1 = image.st_rect.zw / texture_size;
vec2 st0 = res.uv_rect.xy / texture_size;
vec2 st1 = res.uv_rect.zw / texture_size;

vTextureSize = st1 - st0;
vTextureOffset = st0;
Expand Down
8 changes: 5 additions & 3 deletions webrender/res/ps_text_run.vs.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ void main(void) {
Primitive prim = load_primitive(gl_InstanceID);
TextRun text = fetch_text_run(prim.prim_index);
Glyph glyph = fetch_glyph(prim.sub_index);
vec4 local_rect = vec4(glyph.offset.xy, (glyph.uv_rect.zw - glyph.uv_rect.xy) / uDevicePixelRatio);
ResourceRect res = fetch_resource_rect(prim.user_data.x);

vec4 local_rect = vec4(glyph.offset.xy, (res.uv_rect.zw - res.uv_rect.xy) / uDevicePixelRatio);

#ifdef WR_FEATURE_TRANSFORM
TransformVertexInfo vi = write_transform_vertex(local_rect,
Expand All @@ -28,8 +30,8 @@ void main(void) {
write_clip(vi.global_clamped_pos, prim.clip_area);

vec2 texture_size = vec2(textureSize(sColor0, 0));
vec2 st0 = glyph.uv_rect.xy / texture_size;
vec2 st1 = glyph.uv_rect.zw / texture_size;
vec2 st0 = res.uv_rect.xy / texture_size;
vec2 st1 = res.uv_rect.zw / texture_size;

vColor = text.color;
vUv = mix(st0, st1, f);
Expand Down
5 changes: 5 additions & 0 deletions webrender/src/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1366,6 +1366,11 @@ impl Device {
if u_data128 != -1 {
gl::uniform_1i(u_data128, TextureSampler::Data128 as i32);
}

let u_resource_rects = gl::get_uniform_location(program.id, "sResourceRects");
if u_resource_rects != -1 {
gl::uniform_1i(u_resource_rects, TextureSampler::ResourceRects as i32);
}
}
}
}
Expand Down
7 changes: 3 additions & 4 deletions webrender/src/internal_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use std::sync::Arc;
use tiling;
use webrender_traits::{Epoch, ColorF, PipelineId};
use webrender_traits::{ImageFormat, MixBlendMode, NativeFontHandle};
use webrender_traits::{ScrollLayerId, WebGLCommand};
use webrender_traits::{ExternalImageId, ScrollLayerId, WebGLCommand};

// An ID for a texture that is owned by the
// texture cache module. This can include atlases
Expand All @@ -45,9 +45,7 @@ pub enum SourceTexture {
Invalid,
TextureCache(CacheTextureId),
WebGL(u32), // Is actually a gl::GLuint

// TODO(gw): Implement external image support via callback
//External(i32),
External(ExternalImageId),
}

pub enum GLContextHandleWrapper {
Expand Down Expand Up @@ -203,6 +201,7 @@ pub enum TextureSampler {
Layers,
RenderTasks,
Geometry,
ResourceRects,
}

impl TextureSampler {
Expand Down
1 change: 1 addition & 0 deletions webrender/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,4 +121,5 @@ extern crate offscreen_gl_context;
extern crate byteorder;
extern crate rayon;

pub use renderer::{ExternalImage, ExternalImageSource, ExternalImageHandler};
pub use renderer::{Renderer, RendererOptions};
Loading