Skip to content

Commit 19bdbfa

Browse files
authored
Auto merge of #391 - pcwalton:docs-renderer-2, r=pcwalton
Document the remainder of the `pathfinder_renderer` API
2 parents d4ec175 + 26bf6cc commit 19bdbfa

File tree

7 files changed

+196
-29
lines changed

7 files changed

+196
-29
lines changed

export/src/lib.rs

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
use pathfinder_content::outline::ContourIterFlags;
1212
use pathfinder_content::segment::SegmentKind;
1313
use pathfinder_geometry::vector::{Vector2F, vec2f};
14-
use pathfinder_renderer::scene::{DrawPath, Scene};
14+
use pathfinder_renderer::scene::{DrawPathId, Scene};
1515
use std::fmt;
1616
use std::io::{self, Write};
1717

@@ -53,13 +53,15 @@ fn export_svg<W: Write>(scene: &Scene, writer: &mut W) -> io::Result<()> {
5353
view_box.size().x(),
5454
view_box.size().y()
5555
)?;
56-
for &DrawPath { paint: paint_id, ref outline, ref name, .. } in scene.draw_paths() {
57-
let paint = scene.get_paint(paint_id);
56+
for draw_path_index in 0..scene.draw_path_count() {
57+
let draw_path_id = DrawPathId(draw_path_index);
58+
let draw_path = scene.get_draw_path(draw_path_id);
59+
let paint = scene.get_paint(draw_path.paint);
5860
write!(writer, " <path")?;
59-
if !name.is_empty() {
60-
write!(writer, " id=\"{}\"", name)?;
61+
if !draw_path.name.is_empty() {
62+
write!(writer, " id=\"{}\"", draw_path.name)?;
6163
}
62-
writeln!(writer, " fill=\"{:?}\" d=\"{:?}\" />", paint, outline)?;
64+
writeln!(writer, " fill=\"{:?}\" d=\"{:?}\" />", draw_path.paint, draw_path.outline)?;
6365
}
6466
writeln!(writer, "</svg>")?;
6567
Ok(())
@@ -76,14 +78,17 @@ fn export_pdf<W: Write>(scene: &Scene, writer: &mut W) -> io::Result<()> {
7678
vec2f(r.x(), height - r.y())
7779
};
7880

79-
for &DrawPath { paint: paint_id, ref outline, .. } in scene.draw_paths() {
81+
for draw_path_index in 0..scene.draw_path_count() {
82+
let draw_path_id = DrawPathId(draw_path_index);
83+
let draw_path = scene.get_draw_path(draw_path_id);
84+
8085
// TODO(pcwalton): Gradients and patterns.
81-
let paint = scene.get_paint(paint_id);
86+
let paint = scene.get_paint(draw_path.paint);
8287
if paint.is_color() {
8388
pdf.set_fill_color(paint.base_color());
8489
}
8590

86-
for contour in outline.contours() {
91+
for contour in draw_path.outline.contours() {
8792
for (segment_index, segment) in contour.iter(ContourIterFlags::empty()).enumerate() {
8893
if segment_index == 0 {
8994
pdf.move_to(tr(segment.baseline.from()));
@@ -140,14 +145,16 @@ fn export_ps<W: Write>(scene: &Scene, writer: &mut W) -> io::Result<()> {
140145
writeln!(writer, "0 {} translate", view_box.size().y())?;
141146
writeln!(writer, "1 -1 scale")?;
142147

143-
for &DrawPath { paint: paint_id, ref outline, ref name, .. } in scene.draw_paths() {
144-
if !name.is_empty() {
145-
writeln!(writer, "newpath % {}", name)?;
148+
for draw_path_index in 0..scene.draw_path_count() {
149+
let draw_path_id = DrawPathId(draw_path_index);
150+
let draw_path = scene.get_draw_path(draw_path_id);
151+
if !draw_path.name.is_empty() {
152+
writeln!(writer, "newpath % {}", draw_path.name)?;
146153
} else {
147154
writeln!(writer, "newpath")?;
148155
}
149156

150-
for contour in outline.contours() {
157+
for contour in draw_path.outline.contours() {
151158
for (segment_index, segment) in contour.iter(ContourIterFlags::empty()).enumerate() {
152159
if segment_index == 0 {
153160
writeln!(writer, "{} moveto", P(segment.baseline.from()))?;
@@ -182,7 +189,7 @@ fn export_ps<W: Write>(scene: &Scene, writer: &mut W) -> io::Result<()> {
182189
}
183190

184191
// TODO(pcwalton): Gradients and patterns.
185-
let paint = scene.get_paint(paint_id);
192+
let paint = scene.get_paint(draw_path.paint);
186193
if paint.is_color() {
187194
let color = paint.base_color();
188195
writeln!(writer, "{} {} {} setrgbcolor", color.r, color.g, color.b)?;

renderer/src/gpu/options.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ pub struct RendererOptions<D> where D: Device {
3232
pub show_debug_ui: bool,
3333
}
3434

35+
/// The GPU API level that Pathfinder will use.
36+
///
37+
/// Note that this is a *level*, not a *backend*. Levels describe rough GPU feature requirements
38+
/// instead of specific APIs. "D3D9" doesn't mean "Direct3D 9" specifically: rather, it's a more
39+
/// convenient way to write something like "Direct3D 9/OpenGL 3.0/Metal/WebGL 2.0".
3540
#[derive(Clone, Copy, Debug, PartialEq)]
3641
pub enum RendererLevel {
3742
/// Direct3D 9/OpenGL 3.0/WebGL 2.0 compatibility. Bin on CPU, fill and composite on GPU.
@@ -41,6 +46,7 @@ pub enum RendererLevel {
4146
}
4247

4348
impl RendererMode {
49+
/// Creates a new `RendererMode` with a suitable API level for the given GPU device.
4450
#[inline]
4551
pub fn default_for_device<D>(device: &D) -> RendererMode where D: Device {
4652
RendererMode { level: RendererLevel::default_for_device(device) }

renderer/src/gpu/renderer.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
//! The GPU renderer that processes commands necessary to render a scene.
12+
1113
use crate::gpu::blend::{ToBlendState, ToCompositeCtrl};
1214
use crate::gpu::d3d9::renderer::RendererD3D9;
1315
use crate::gpu::d3d11::renderer::RendererD3D11;
@@ -66,6 +68,7 @@ const COMBINER_CTRL_COLOR_FILTER_SHIFT: i32 = 4;
6668
const COMBINER_CTRL_COLOR_COMBINE_SHIFT: i32 = 6;
6769
const COMBINER_CTRL_COMPOSITE_SHIFT: i32 = 8;
6870

71+
/// The GPU renderer that processes commands necessary to render a scene.
6972
pub struct Renderer<D> where D: Device {
7073
// Basic data
7174
pub(crate) core: RendererCore<D>,

renderer/src/gpu_data.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ pub struct DrawTileBatchD3D11 {
242242
pub struct TileBatchTexture {
243243
pub page: TexturePageId,
244244
pub sampling_flags: TextureSamplingFlags,
245-
pub composite_op: PaintCompositeOp,
245+
pub(crate) composite_op: PaintCompositeOp,
246246
}
247247

248248
#[derive(Clone, Copy, PartialEq, Debug)]

renderer/src/lib.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
//! The CPU portion of Pathfinder's renderer.
11+
//! Pathfinder's renderer and associated objects.
12+
13+
#![warn(missing_docs)]
1214

1315
#[macro_use]
1416
extern crate bitflags;

renderer/src/paint.rs

Lines changed: 49 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
//! Defines how a path is to be filled.
12+
1113
use crate::allocator::{AllocationMode, TextureAllocator};
1214
use crate::gpu_data::{ColorCombineMode, RenderCommand, TextureLocation, TextureMetadataEntry};
1315
use crate::gpu_data::{TexturePageDescriptor, TexturePageId, TileBatchTexture};
@@ -48,34 +50,46 @@ struct RenderTargetData {
4850
metadata: RenderTargetMetadata,
4951
}
5052

53+
/// Defines how a path is to be filled: with a solid color, gradient, or pattern.
5154
#[derive(Clone, PartialEq, Eq, Hash, Debug)]
5255
pub struct Paint {
5356
base_color: ColorU,
5457
overlay: Option<PaintOverlay>,
5558
}
5659

60+
/// What is to be overlaid on top of a base color.
61+
///
62+
/// An overlay is a gradient or a pattern, plus a composite operation which determines how the
63+
/// gradient or pattern is to be combined with the base color.
5764
#[derive(Clone, PartialEq, Eq, Hash, Debug)]
5865
pub struct PaintOverlay {
5966
composite_op: PaintCompositeOp,
6067
contents: PaintContents,
6168
}
6269

70+
/// The contents of an overlay: either a gradient or a pattern.
6371
#[derive(Clone, PartialEq, Eq, Hash)]
64-
pub enum PaintContents {
72+
pub(crate) enum PaintContents {
73+
/// A gradient, either linear or radial.
6574
Gradient(Gradient),
75+
/// A raster image pattern.
6676
Pattern(Pattern),
6777
}
6878

79+
/// The ID of a paint, unique to a scene.
6980
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
7081
pub struct PaintId(pub u16);
7182

83+
/// The ID of a gradient, unique to a scene.
7284
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
7385
pub struct GradientId(pub u32);
7486

75-
/// How a paint is to be composited over a base color, or vice versa.
87+
/// How an overlay is to be composited over a base color.
7688
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
7789
pub enum PaintCompositeOp {
90+
/// The source that overlaps the destination, replaces the destination.
7891
SrcIn,
92+
/// Destination which overlaps the source, replaces the source.
7993
DestIn,
8094
}
8195

@@ -102,11 +116,13 @@ impl Palette {
102116
}
103117

104118
impl Paint {
119+
/// Creates a simple paint from a single base color.
105120
#[inline]
106121
pub fn from_color(color: ColorU) -> Paint {
107122
Paint { base_color: color, overlay: None }
108123
}
109124

125+
/// Creates a paint from a gradient.
110126
#[inline]
111127
pub fn from_gradient(gradient: Gradient) -> Paint {
112128
Paint {
@@ -118,6 +134,7 @@ impl Paint {
118134
}
119135
}
120136

137+
/// Creates a paint from a raster pattern.
121138
#[inline]
122139
pub fn from_pattern(pattern: Pattern) -> Paint {
123140
Paint {
@@ -129,16 +146,21 @@ impl Paint {
129146
}
130147
}
131148

149+
/// A convenience function to create a solid black paint.
132150
#[inline]
133151
pub fn black() -> Paint {
134152
Paint::from_color(ColorU::black())
135153
}
136154

155+
/// A convenience function to create a transparent paint with all channels set to zero.
137156
#[inline]
138157
pub fn transparent_black() -> Paint {
139158
Paint::from_color(ColorU::transparent_black())
140159
}
141160

161+
/// Returns true if this paint is obviously opaque, via a quick check.
162+
///
163+
/// Even if the paint is opaque, this function might return false.
142164
pub fn is_opaque(&self) -> bool {
143165
if !self.base_color.is_opaque() {
144166
return false;
@@ -155,6 +177,9 @@ impl Paint {
155177
}
156178
}
157179

180+
/// Returns true if this paint is fully transparent, via a quick check.
181+
///
182+
/// Even if the paint is fully transparent, this function might return false.
158183
pub fn is_fully_transparent(&self) -> bool {
159184
if !self.base_color.is_fully_transparent() {
160185
return false;
@@ -171,11 +196,15 @@ impl Paint {
171196
}
172197
}
173198

199+
/// Returns true if this paint represents a solid color.
174200
#[inline]
175201
pub fn is_color(&self) -> bool {
176202
self.overlay.is_none()
177203
}
178204

205+
/// Applies an affine transform to this paint.
206+
///
207+
/// This has no effect if this paint is a solid color.
179208
pub fn apply_transform(&mut self, transform: &Transform2F) {
180209
if transform.is_identity() {
181210
return;
@@ -189,26 +218,36 @@ impl Paint {
189218
}
190219
}
191220

221+
/// Returns the *base color* of this paint.
222+
///
223+
/// The base color is the color that goes underneath the gradient or pattern, if there is one.
192224
#[inline]
193225
pub fn base_color(&self) -> ColorU {
194226
self.base_color
195227
}
196228

229+
/// Changes the *base color* of this paint.
230+
///
231+
/// The base color is the color that goes underneath the gradient or pattern, if there is one.
197232
#[inline]
198233
pub fn set_base_color(&mut self, new_base_color: ColorU) {
199234
self.base_color = new_base_color;
200235
}
201236

237+
/// Returns the paint overlay, which is the portion of the paint on top of the base color.
202238
#[inline]
203239
pub fn overlay(&self) -> &Option<PaintOverlay> {
204240
&self.overlay
205241
}
206242

243+
/// Returns a mutable reference to the paint overlay, which is the portion of the paint on top
244+
/// of the base color.
207245
#[inline]
208246
pub fn overlay_mut(&mut self) -> &mut Option<PaintOverlay> {
209247
&mut self.overlay
210248
}
211249

250+
/// Returns the pattern, if this paint represents one.
212251
#[inline]
213252
pub fn pattern(&self) -> Option<&Pattern> {
214253
match self.overlay {
@@ -222,6 +261,7 @@ impl Paint {
222261
}
223262
}
224263

264+
/// Returns a mutable reference to the pattern, if this paint represents one.
225265
#[inline]
226266
pub fn pattern_mut(&mut self) -> Option<&mut Pattern> {
227267
match self.overlay {
@@ -235,6 +275,7 @@ impl Paint {
235275
}
236276
}
237277

278+
/// Returns the gradient, if this paint represents one.
238279
#[inline]
239280
pub fn gradient(&self) -> Option<&Gradient> {
240281
match self.overlay {
@@ -251,18 +292,22 @@ impl Paint {
251292

252293
impl PaintOverlay {
253294
#[inline]
254-
pub fn contents(&self) -> &PaintContents {
295+
pub(crate) fn contents(&self) -> &PaintContents {
255296
&self.contents
256297
}
257298

299+
/// Returns the composite operation, which defines how the overlay is to be composited on top
300+
/// of the base color.
258301
#[inline]
259302
pub fn composite_op(&self) -> PaintCompositeOp {
260303
self.composite_op
261304
}
262305

306+
/// Changes the composite operation, which defines how the overlay is to be composited on top
307+
/// of the base color.
263308
#[inline]
264309
pub fn set_composite_op(&mut self, new_composite_op: PaintCompositeOp) {
265-
self.composite_op = new_composite_op
310+
self.composite_op = new_composite_op;
266311
}
267312
}
268313

0 commit comments

Comments
 (0)