pathfinder_renderer 0.5.0

A GPU-accelerated vector graphics and font renderer
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
// pathfinder/renderer/src/paint.rs
//
// Copyright © 2020 The Pathfinder Project Developers.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use crate::allocator::{AllocationMode, TextureAllocator};
use crate::gpu_data::{RenderCommand, TextureLocation, TextureMetadataEntry, TexturePageDescriptor};
use crate::gpu_data::{TexturePageId, TileBatchTexture};
use crate::scene::{RenderTarget, SceneId};
use hashbrown::HashMap;
use pathfinder_color::ColorU;
use pathfinder_content::effects::{Filter, PatternFilter};
use pathfinder_content::gradient::Gradient;
use pathfinder_content::pattern::{Pattern, PatternSource};
use pathfinder_content::render_target::RenderTargetId;
use pathfinder_geometry::line_segment::LineSegment2F;
use pathfinder_geometry::rect::{RectF, RectI};
use pathfinder_geometry::transform2d::{Matrix2x2F, Transform2F};
use pathfinder_geometry::vector::{Vector2F, Vector2I, vec2f, vec2i};
use pathfinder_gpu::TextureSamplingFlags;
use pathfinder_simd::default::F32x2;
use std::fmt::{self, Debug, Formatter};
use std::sync::Arc;

// The size of a gradient tile.
//
// TODO(pcwalton): Choose this size dynamically!
const GRADIENT_TILE_LENGTH: u32 = 256;

#[derive(Clone)]
pub struct Palette {
    pub paints: Vec<Paint>,
    render_targets: Vec<RenderTargetData>,
    cache: HashMap<Paint, PaintId>,
    allocator: TextureAllocator,
    scene_id: SceneId,
}

#[derive(Clone)]
struct RenderTargetData {
    render_target: RenderTarget,
    metadata: RenderTargetMetadata,
}

#[derive(Clone, PartialEq, Eq, Hash, Debug)]
pub struct Paint {
    base_color: ColorU,
    overlay: Option<PaintOverlay>,
}

#[derive(Clone, PartialEq, Eq, Hash, Debug)]
pub struct PaintOverlay {
    composite_op: PaintCompositeOp,
    contents: PaintContents,
}

#[derive(Clone, PartialEq, Eq, Hash)]
pub enum PaintContents {
    Gradient(Gradient),
    Pattern(Pattern),
}

#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
pub struct PaintId(pub u16);

#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
pub struct GradientId(pub u32);

/// How a paint is to be composited over a base color, or vice versa.
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
pub enum PaintCompositeOp {
    SrcIn,
    DestIn,
}

impl Debug for PaintContents {
    fn fmt(&self, formatter: &mut Formatter) -> fmt::Result {
        match *self {
            PaintContents::Gradient(_) => {
                // TODO(pcwalton)
                write!(formatter, "(gradient)")
            }
            PaintContents::Pattern(ref pattern) => pattern.fmt(formatter),
        }
    }
}

impl Palette {
    #[inline]
    pub fn new(scene_id: SceneId) -> Palette {
        Palette {
            paints: vec![],
            render_targets: vec![],
            cache: HashMap::new(),
            allocator: TextureAllocator::new(),
            scene_id,
        }
    }
}

impl Paint {
    #[inline]
    pub fn from_color(color: ColorU) -> Paint {
        Paint { base_color: color, overlay: None }
    }

    #[inline]
    pub fn from_gradient(gradient: Gradient) -> Paint {
        Paint {
            base_color: ColorU::white(),
            overlay: Some(PaintOverlay {
                composite_op: PaintCompositeOp::SrcIn,
                contents: PaintContents::Gradient(gradient),
            }),
        }
    }

    #[inline]
    pub fn from_pattern(pattern: Pattern) -> Paint {
        Paint {
            base_color: ColorU::white(),
            overlay: Some(PaintOverlay {
                composite_op: PaintCompositeOp::SrcIn,
                contents: PaintContents::Pattern(pattern),
            }),
        }
    }

    #[inline]
    pub fn black() -> Paint {
        Paint::from_color(ColorU::black())
    }

    #[inline]
    pub fn transparent_black() -> Paint {
        Paint::from_color(ColorU::transparent_black())
    }

    pub fn is_opaque(&self) -> bool {
        if !self.base_color.is_opaque() {
            return false;
        }

        match self.overlay {
            None => true,
            Some(ref overlay) => {
                match overlay.contents {
                    PaintContents::Gradient(ref gradient) => gradient.is_opaque(),
                    PaintContents::Pattern(ref pattern) => pattern.is_opaque(),
                }
            }
        }
    }

    pub fn is_fully_transparent(&self) -> bool {
        if !self.base_color.is_fully_transparent() {
            return false;
        }

        match self.overlay {
            None => true,
            Some(ref overlay) => {
                match overlay.contents {
                    PaintContents::Gradient(ref gradient) => gradient.is_fully_transparent(),
                    PaintContents::Pattern(_) => false,
                }
            }
        }
    }

    #[inline]
    pub fn is_color(&self) -> bool {
        self.overlay.is_none()
    }

    pub fn apply_transform(&mut self, transform: &Transform2F) {
        if transform.is_identity() {
            return;
        }

        if let Some(ref mut overlay) = self.overlay {
            match overlay.contents {
                PaintContents::Gradient(ref mut gradient) => {
                    gradient.set_line(*transform * gradient.line());
                    if let Some(radii) = gradient.radii() {
                        gradient.set_radii(Some(radii * transform.extract_scale().0));
                    }
                }
                PaintContents::Pattern(ref mut pattern) => pattern.apply_transform(*transform),
            }
        }
    }

    #[inline]
    pub fn base_color(&self) -> ColorU {
        self.base_color
    }

    #[inline]
    pub fn set_base_color(&mut self, new_base_color: ColorU) {
        self.base_color = new_base_color;
    }

    #[inline]
    pub fn overlay(&self) -> &Option<PaintOverlay> {
        &self.overlay
    }

    #[inline]
    pub fn overlay_mut(&mut self) -> &mut Option<PaintOverlay> {
        &mut self.overlay
    }

    #[inline]
    pub fn pattern(&self) -> Option<&Pattern> {
        match self.overlay {
            None => None,
            Some(ref overlay) => {
                match overlay.contents {
                    PaintContents::Pattern(ref pattern) => Some(pattern),
                    _ => None,
                }
            }
        }
    }

    #[inline]
    pub fn pattern_mut(&mut self) -> Option<&mut Pattern> {
        match self.overlay {
            None => None,
            Some(ref mut overlay) => {
                match overlay.contents {
                    PaintContents::Pattern(ref mut pattern) => Some(pattern),
                    _ => None,
                }
            }
        }
    }

    #[inline]
    pub fn gradient(&self) -> Option<&Gradient> {
        match self.overlay {
            None => None,
            Some(ref overlay) => {
                match overlay.contents {
                    PaintContents::Gradient(ref gradient) => Some(gradient),
                    _ => None,
                }
            }
        }
    }
}

impl PaintOverlay {
    #[inline]
    pub fn contents(&self) -> &PaintContents {
        &self.contents
    }

    #[inline]
    pub fn composite_op(&self) -> PaintCompositeOp {
        self.composite_op
    }

    #[inline]
    pub fn set_composite_op(&mut self, new_composite_op: PaintCompositeOp) {
        self.composite_op = new_composite_op
    }
}

pub struct PaintInfo {
    /// The render commands needed to prepare the textures.
    pub render_commands: Vec<RenderCommand>,
    /// The metadata for each paint.
    ///
    /// The indices of this vector are paint IDs.
    pub paint_metadata: Vec<PaintMetadata>,
    /// The metadata for each render target.
    ///
    /// The indices of this vector are render target IDs.
    pub render_target_metadata: Vec<RenderTargetMetadata>,
}

#[derive(Debug)]
pub struct PaintMetadata {
    /// Metadata associated with the color texture, if applicable.
    pub color_texture_metadata: Option<PaintColorTextureMetadata>,
    /// The base color that the color texture gets mixed into.
    pub base_color: ColorU,
    /// True if this paint is fully opaque.
    pub is_opaque: bool,
}

#[derive(Debug)]
pub struct PaintColorTextureMetadata {
    /// The location of the paint.
    pub location: TextureLocation,
    /// The transform to apply to screen coordinates to translate them into UVs.
    pub transform: Transform2F,
    /// The sampling mode for the texture.
    pub sampling_flags: TextureSamplingFlags,
    /// The filter to be applied to this paint.
    pub filter: PaintFilter,
    /// How the color texture is to be composited over the base color.
    pub composite_op: PaintCompositeOp,
}

#[derive(Clone, Copy, Debug)]
pub struct RadialGradientMetadata {
    /// The line segment that connects the two circles.
    pub line: LineSegment2F,
    /// The radii of the two circles.
    pub radii: F32x2,
}

#[derive(Clone, Copy, Debug)]
pub struct RenderTargetMetadata {
    /// The location of the render target.
    pub location: TextureLocation,
}

#[derive(Debug)]
pub enum PaintFilter {
    None,
    RadialGradient {
        /// The line segment that connects the two circles.
        line: LineSegment2F,
        /// The radii of the two circles.
        radii: F32x2,
    },
    PatternFilter(PatternFilter),
}

impl Palette {
    #[allow(clippy::trivially_copy_pass_by_ref)]
    pub fn push_paint(&mut self, paint: &Paint) -> PaintId {
        if let Some(paint_id) = self.cache.get(paint) {
            return *paint_id;
        }

        let paint_id = PaintId(self.paints.len() as u16);
        self.cache.insert((*paint).clone(), paint_id);
        self.paints.push((*paint).clone());
        paint_id
    }

    pub fn push_render_target(&mut self, render_target: RenderTarget) -> RenderTargetId {
        let id = self.render_targets.len() as u32;

        let metadata = RenderTargetMetadata {
            location: self.allocator.allocate_image(render_target.size()),
        };

        self.render_targets.push(RenderTargetData { render_target, metadata });
        RenderTargetId { scene: self.scene_id.0, render_target: id }
    }

    pub fn build_paint_info(&mut self, render_transform: Transform2F) -> PaintInfo {
        let mut paint_metadata = vec![];

        // Assign paint locations.
        let mut gradient_tile_builder = GradientTileBuilder::new();
        let mut image_texel_info = vec![];
        for paint in &self.paints {
            let allocator = &mut self.allocator;
            let render_targets = &self.render_targets;
            let color_texture_metadata = paint.overlay.as_ref().map(|overlay| {
                match overlay.contents {
                    PaintContents::Gradient(ref gradient) => {
                        // FIXME(pcwalton): The gradient size might not be big enough. Detect this.
                        PaintColorTextureMetadata {
                            location: gradient_tile_builder.allocate(allocator, gradient),
                            sampling_flags: TextureSamplingFlags::empty(),
                            filter: match gradient.radii() {
                                None => PaintFilter::None,
                                Some(radii) => {
                                    PaintFilter::RadialGradient { line: gradient.line(), radii }
                                }
                            },
                            transform: Transform2F::default(),
                            composite_op: overlay.composite_op(),
                        }
                    }
                    PaintContents::Pattern(ref pattern) => {
                        let location;
                        match *pattern.source() {
                            PatternSource::RenderTarget { id: render_target_id, .. } => {
                                let index = render_target_id.render_target as usize;
                                location = render_targets[index].metadata.location;
                            }
                            PatternSource::Image(ref image) => {
                                // TODO(pcwalton): We should be able to use tile cleverness to
                                // repeat inside the atlas in some cases.
                                let allocation_mode = AllocationMode::OwnPage;
                                location = allocator.allocate(image.size(), allocation_mode);
                                image_texel_info.push(ImageTexelInfo {
                                    location,
                                    texels: (*image.pixels()).clone(),
                                });
                            }
                        }

                        let mut sampling_flags = TextureSamplingFlags::empty();
                        if pattern.repeat_x() {
                            sampling_flags.insert(TextureSamplingFlags::REPEAT_U);
                        }
                        if pattern.repeat_y() {
                            sampling_flags.insert(TextureSamplingFlags::REPEAT_V);
                        }
                        if !pattern.smoothing_enabled() {
                            sampling_flags.insert(TextureSamplingFlags::NEAREST_MIN |
                                                  TextureSamplingFlags::NEAREST_MAG);
                        }

                        let filter = match pattern.filter() {
                            None => PaintFilter::None,
                            Some(pattern_filter) => PaintFilter::PatternFilter(pattern_filter),
                        };

                        PaintColorTextureMetadata {
                            location,
                            sampling_flags,
                            filter,
                            transform: Transform2F::default(),
                            composite_op: overlay.composite_op(),
                        }
                    }
                }
            });

            paint_metadata.push(PaintMetadata {
                color_texture_metadata,
                is_opaque: paint.is_opaque(),
                base_color: paint.base_color(),
            });
        }

        // Calculate texture transforms.
        for (paint, metadata) in self.paints.iter().zip(paint_metadata.iter_mut()) {
            let mut color_texture_metadata = match metadata.color_texture_metadata {
                None => continue,
                Some(ref mut color_texture_metadata) => color_texture_metadata,
            };

            let texture_scale = self.allocator.page_scale(color_texture_metadata.location.page);
            let texture_rect = color_texture_metadata.location.rect;
            color_texture_metadata.transform = match paint.overlay    
                                                          .as_ref()
                                                          .expect("Why do we have color texture \
                                                                   metadata but no overlay?")
                                                          .contents {
                PaintContents::Gradient(Gradient {
                    line: gradient_line,
                    radii: None,
                    ..
                }) => {
                    let v0 = texture_rect.to_f32().center().y() * texture_scale.y();
                    let length_inv = 1.0 / gradient_line.square_length();
                    let (p0, d) = (gradient_line.from(), gradient_line.vector());
                    Transform2F {
                        matrix: Matrix2x2F::row_major(
                            d.x(), d.y(), 0.0, 0.0).scale(length_inv),
                        vector: Vector2F::new(-p0.dot(d) * length_inv, v0),
                    } * render_transform
                }
                PaintContents::Gradient(Gradient { radii: Some(_), .. }) => {
                    let texture_origin_uv = rect_to_inset_uv(texture_rect, texture_scale).origin();
                    let gradient_tile_scale = texture_scale * (GRADIENT_TILE_LENGTH - 1) as
                        f32;
                    Transform2F {
                        matrix: Matrix2x2F::from_scale(gradient_tile_scale),
                        vector: texture_origin_uv,
                    } * render_transform
                }
                PaintContents::Pattern(ref pattern) => {
                    match pattern.source() {
                        PatternSource::Image(_) => {
                            let texture_origin_uv =
                                rect_to_uv(texture_rect, texture_scale).origin();
                            Transform2F::from_translation(texture_origin_uv) *
                                Transform2F::from_scale(texture_scale) *
                                pattern.transform().inverse() * render_transform
                        }
                        PatternSource::RenderTarget { .. } => {
                            // FIXME(pcwalton): Only do this in GL, not Metal!
                            let texture_origin_uv =
                                rect_to_uv(texture_rect, texture_scale).lower_left();
                            Transform2F::from_translation(texture_origin_uv) *
                                Transform2F::from_scale(texture_scale * vec2f(1.0, -1.0)) *
                                pattern.transform().inverse() * render_transform
                        }
                    }
                }
            }
        }

        // Create texture metadata.
        let texture_metadata = paint_metadata.iter().map(|paint_metadata| {
            TextureMetadataEntry {
                color_0_transform: match paint_metadata.color_texture_metadata {
                    None => Transform2F::default(),
                    Some(ref color_texture_metadata) => color_texture_metadata.transform,
                },
                base_color: paint_metadata.base_color,
            }
        }).collect();
        let mut render_commands = vec![RenderCommand::UploadTextureMetadata(texture_metadata)];

        // Allocate textures.
        let mut texture_page_descriptors = vec![];
        for page_index in 0..self.allocator.page_count() {
            let page_id = TexturePageId(page_index);
            let page_size = self.allocator.page_size(page_id);
            let descriptor = TexturePageDescriptor { size: page_size };
            texture_page_descriptors.push(descriptor);

            if self.allocator.page_is_new(page_id) {
                render_commands.push(RenderCommand::AllocateTexturePage { page_id, descriptor });
                self.allocator.mark_page_as_allocated(page_id);
            }
        }

        // Gather up render target metadata.
        let render_target_metadata: Vec<_> = self.render_targets.iter().map(|render_target_data| {
            render_target_data.metadata
        }).collect();

        // Create render commands.
        for (index, metadata) in render_target_metadata.iter().enumerate() {
            let id = RenderTargetId { scene: self.scene_id.0, render_target: index as u32 };
            render_commands.push(RenderCommand::DeclareRenderTarget {
                id,
                location: metadata.location,
            });
        }
        gradient_tile_builder.create_render_commands(&mut render_commands);
        for image_texel_info in image_texel_info {
            render_commands.push(RenderCommand::UploadTexelData {
                texels: image_texel_info.texels,
                location: image_texel_info.location,
            });
        }

        PaintInfo { render_commands, paint_metadata, render_target_metadata }
    }

    pub(crate) fn append_palette(&mut self, palette: Palette) -> MergedPaletteInfo {
        // Merge render targets.
        let mut render_target_mapping = HashMap::new();
        for (old_render_target_index, render_target) in palette.render_targets
                                                               .into_iter()
                                                               .enumerate() {
            let old_render_target_id = RenderTargetId {
                scene: palette.scene_id.0,
                render_target: old_render_target_index as u32,
            };
            let new_render_target_id = self.push_render_target(render_target.render_target);
            render_target_mapping.insert(old_render_target_id, new_render_target_id);
        }

        // Merge paints.
        let mut paint_mapping = HashMap::new();
        for (old_paint_index, old_paint) in palette.paints.iter().enumerate() {
            let old_paint_id = PaintId(old_paint_index as u16);
            let new_paint_id = match *old_paint.overlay() {
                None => self.push_paint(old_paint),
                Some(ref overlay) => {
                    match *overlay.contents() {
                        PaintContents::Pattern(ref pattern) => {
                            match pattern.source() {
                                PatternSource::RenderTarget { id: old_render_target_id, size } => {
                                    let mut new_pattern =
                                        Pattern::from_render_target(*old_render_target_id, *size);
                                    new_pattern.set_filter(pattern.filter());
                                    new_pattern.apply_transform(pattern.transform());
                                    new_pattern.set_repeat_x(pattern.repeat_x());
                                    new_pattern.set_repeat_y(pattern.repeat_y());
                                    new_pattern.set_smoothing_enabled(pattern.smoothing_enabled());
                                    self.push_paint(&Paint::from_pattern(new_pattern))
                                }
                                _ => self.push_paint(old_paint),
                            }
                        }
                        _ => self.push_paint(old_paint),
                    }
                }
            };
            paint_mapping.insert(old_paint_id, new_paint_id);
        }

        MergedPaletteInfo { render_target_mapping, paint_mapping }
    }
}

pub(crate) struct MergedPaletteInfo {
    pub(crate) render_target_mapping: HashMap<RenderTargetId, RenderTargetId>,
    pub(crate) paint_mapping: HashMap<PaintId, PaintId>,
}

impl PaintMetadata {
    pub(crate) fn filter(&self) -> Filter {
        match self.color_texture_metadata {
            None => Filter::None,
            Some(ref color_metadata) => {
                match color_metadata.filter {
                    PaintFilter::None => Filter::None,
                    PaintFilter::RadialGradient { line, radii } => {
                        let uv_origin = color_metadata.transform.vector;
                        Filter::RadialGradient { line, radii, uv_origin }
                    }
                    PaintFilter::PatternFilter(pattern_filter) => {
                        Filter::PatternFilter(pattern_filter)
                    }
                }
            }
        }
    }

    pub(crate) fn tile_batch_texture(&self) -> Option<TileBatchTexture> {
        self.color_texture_metadata.as_ref().map(PaintColorTextureMetadata::as_tile_batch_texture)
    }
}

fn rect_to_uv(rect: RectI, texture_scale: Vector2F) -> RectF {
    rect.to_f32() * texture_scale
}

fn rect_to_inset_uv(rect: RectI, texture_scale: Vector2F) -> RectF {
    rect_to_uv(rect, texture_scale).contract(texture_scale * 0.5)
}

// Gradient allocation

struct GradientTileBuilder {
    tiles: Vec<GradientTile>,
}

struct GradientTile {
    texels: Vec<ColorU>,
    page: TexturePageId,
    next_index: u32,
}

impl GradientTileBuilder {
    fn new() -> GradientTileBuilder {
        GradientTileBuilder { tiles: vec![] }
    }

    fn allocate(&mut self, allocator: &mut TextureAllocator, gradient: &Gradient)
                -> TextureLocation {
        if self.tiles.is_empty() ||
                self.tiles.last().unwrap().next_index == GRADIENT_TILE_LENGTH {
            let size = Vector2I::splat(GRADIENT_TILE_LENGTH as i32);
            let area = size.x() as usize * size.y() as usize;
            self.tiles.push(GradientTile {
                texels: vec![ColorU::black(); area],
                page: allocator.allocate(size, AllocationMode::OwnPage).page,
                next_index: 0,
            })
        }

        let mut data = self.tiles.last_mut().unwrap();
        let location = TextureLocation {
            page: data.page,
            rect: RectI::new(vec2i(0, data.next_index as i32),
                             vec2i(GRADIENT_TILE_LENGTH as i32, 1)),
        };
        data.next_index += 1;

        // FIXME(pcwalton): Paint transparent if gradient line has zero size, per spec.
        // TODO(pcwalton): Optimize this:
        // 1. Calculate ∇t up front and use differencing in the inner loop.
        // 2. Go four pixels at a time with SIMD.
        let first_address = location.rect.origin_y() as usize * GRADIENT_TILE_LENGTH as usize;
        for x in 0..(GRADIENT_TILE_LENGTH as i32) {
            let t = (x as f32 + 0.5) / GRADIENT_TILE_LENGTH as f32;
            data.texels[first_address + x as usize] = gradient.sample(t);
        }

        location
    }

    fn create_render_commands(self, render_commands: &mut Vec<RenderCommand>) {
        for tile in self.tiles {
            render_commands.push(RenderCommand::UploadTexelData {
                texels: Arc::new(tile.texels),
                location: TextureLocation {
                    rect: RectI::new(vec2i(0, 0), Vector2I::splat(GRADIENT_TILE_LENGTH as i32)),
                    page: tile.page,
                },
            });
        }
    }
}

struct ImageTexelInfo {
    location: TextureLocation,
    texels: Arc<Vec<ColorU>>,
}

impl PaintColorTextureMetadata {
    pub(crate) fn as_tile_batch_texture(&self) -> TileBatchTexture {
        TileBatchTexture {
            page: self.location.page,
            sampling_flags: self.sampling_flags,
            composite_op: self.composite_op,
        }
    }
}