mirror of
https://github.com/GraphiteEditor/Graphite.git
synced 2026-12-02 00:01:59 -10:00
Embed GPU rasters in SVG output by downloading them first
This commit is contained in:
1 parent
a12cbea9fb
commit
fc488dd91f
6 files changed
+192
-68
No files matched your search
@@ -23,7 +23,7 @@ use core_types::{
|
||||
use dyn_any::DynAny;
|
||||
use glam::{DAffine2, DMat2, DVec2};
|
||||
use graphene_resource::Resource;
|
||||
use graphic_types::raster_types::{CPU, GPU, Raster};
|
||||
use graphic_types::raster_types::{CPU, GPU, Raster, Texture};
|
||||
use graphic_types::vector_types::gradient::{Gradient, GradientForm};
|
||||
use graphic_types::vector_types::vector::click_target::{ClickTarget, FreePoint, TargetPath};
|
||||
use graphic_types::vector_types::vector::misc::dvec2_to_point;
|
||||
@@ -377,6 +377,9 @@ pub struct RenderParams {
|
||||
pub canvas_image_scale: f64,
|
||||
/// The nearest ancestor's appearance, cascading to items that lack their own.
|
||||
pub inherited_appearance: Option<Appearance>,
|
||||
/// Downloaded images of GPU raster textures, which SVG output embeds in place of the textures it can't reference. GPU rasters without one are not drawn.
|
||||
#[cache_hash(skip)]
|
||||
pub gpu_raster_downloads: Arc<HashMap<Texture, Raster<CPU>>>,
|
||||
}
|
||||
|
||||
impl RenderParams {
|
||||
@@ -775,6 +778,9 @@ pub trait Render: BoundingBox {
|
||||
false
|
||||
}
|
||||
|
||||
/// Recursively collects the textures of the GPU rasters this content draws, including those painting its appearances, so SVG output can download them first.
|
||||
fn collect_gpu_textures(&self, _textures: &mut Vec<Texture>) {}
|
||||
|
||||
fn new_ids_from_hash(&mut self, _reference: Option<NodeId>) {}
|
||||
}
|
||||
|
||||
@@ -1042,6 +1048,20 @@ fn add_graphic_item_outline_targets(item: ItemRef<'_, Graphic>, outlines: &mut V
|
||||
outlines.extend(new_outlines);
|
||||
}
|
||||
|
||||
/// Collects the textures of the GPU rasters painting an appearance's coverages.
|
||||
fn collect_appearance_gpu_textures(appearance: Option<&Appearance>, textures: &mut Vec<Texture>) {
|
||||
for paint in appearance.into_iter().flat_map(Appearance::covers_with_paints).filter_map(|(_, paint)| paint) {
|
||||
paint.collect_gpu_textures(textures);
|
||||
}
|
||||
}
|
||||
|
||||
/// Collects the textures of the GPU rasters painting the appearances of a list's items.
|
||||
fn collect_list_appearance_gpu_textures<T>(list: &List<T>, textures: &mut Vec<Texture>) {
|
||||
for appearance in list.iter_attribute_values::<Appearance>(ATTR_APPEARANCE).into_iter().flatten() {
|
||||
collect_appearance_gpu_textures(Some(appearance), textures);
|
||||
}
|
||||
}
|
||||
|
||||
impl Render for Graphic {
|
||||
fn render_svg(&self, render: &mut SvgRender, render_params: &RenderParams) {
|
||||
match self {
|
||||
@@ -1049,14 +1069,14 @@ impl Render for Graphic {
|
||||
Graphic::Graphic(item) => render_graphic_item_svg(ItemRef::Item(item), false, &mut None, render, render_params),
|
||||
Graphic::Vector(item) => render_vector_item_svg(ItemRef::Item(item), false, &mut None, render, render_params),
|
||||
Graphic::RasterCPU(item) => render_raster_cpu_item_svg(ItemRef::Item(item), render, render_params),
|
||||
Graphic::RasterGPU(_) => (),
|
||||
Graphic::RasterGPU(item) => render_raster_gpu_item_svg(ItemRef::Item(item), render, render_params),
|
||||
Graphic::Color(item) => render_color_item_svg(ItemRef::Item(item), render, render_params),
|
||||
Graphic::Gradient(item) => render_gradient_item_svg(ItemRef::Item(item), render, render_params),
|
||||
Graphic::Text(item) => render_text_item_svg(ItemRef::Item(item), render, render_params),
|
||||
Graphic::GraphicList(list) => list.render_svg(render, render_params),
|
||||
Graphic::VectorList(list) => list.render_svg(render, render_params),
|
||||
Graphic::RasterCPUList(list) => list.render_svg(render, render_params),
|
||||
Graphic::RasterGPUList(_) => (),
|
||||
Graphic::RasterGPUList(list) => list.render_svg(render, render_params),
|
||||
Graphic::ColorList(list) => list.render_svg(render, render_params),
|
||||
Graphic::GradientList(list) => list.render_svg(render, render_params),
|
||||
Graphic::TextList(list) => list.render_svg(render, render_params),
|
||||
@@ -1092,6 +1112,31 @@ impl Render for Graphic {
|
||||
}
|
||||
}
|
||||
|
||||
fn collect_gpu_textures(&self, textures: &mut Vec<Texture>) {
|
||||
match self {
|
||||
Graphic::Graphic(item) => {
|
||||
item.element().collect_gpu_textures(textures);
|
||||
collect_appearance_gpu_textures(item.attribute(ATTR_APPEARANCE), textures);
|
||||
}
|
||||
Graphic::Vector(item) => collect_appearance_gpu_textures(item.attribute(ATTR_APPEARANCE), textures),
|
||||
Graphic::RasterGPU(item) => collect_raster_gpu_texture(item.element(), textures),
|
||||
Graphic::GraphicList(list) => list.collect_gpu_textures(textures),
|
||||
Graphic::VectorList(list) => list.collect_gpu_textures(textures),
|
||||
Graphic::RasterGPUList(list) => list.collect_gpu_textures(textures),
|
||||
Graphic::None(_)
|
||||
| Graphic::NoneList(_)
|
||||
| Graphic::RasterCPU(_)
|
||||
| Graphic::Color(_)
|
||||
| Graphic::Gradient(_)
|
||||
| Graphic::Text(_)
|
||||
| Graphic::RasterCPUList(_)
|
||||
| Graphic::ColorList(_)
|
||||
| Graphic::GradientList(_)
|
||||
| Graphic::TextList(_)
|
||||
| Graphic::StrokeList(_) => {}
|
||||
}
|
||||
}
|
||||
|
||||
fn collect_metadata(&self, metadata: &mut RenderMetadata, footprint: Footprint, element_id: Option<NodeId>, inherited_appearance: Option<&Appearance>) {
|
||||
if let Some(element_id) = element_id {
|
||||
// The footprint always lands; the transform (and for vectors the source layer) comes from the first item when one exists
|
||||
@@ -1388,6 +1433,12 @@ impl Render for List<Artboard> {
|
||||
}
|
||||
}
|
||||
|
||||
fn collect_gpu_textures(&self, textures: &mut Vec<Texture>) {
|
||||
for artboard in self.iter_element_values() {
|
||||
artboard.as_graphic_list().collect_gpu_textures(textures);
|
||||
}
|
||||
}
|
||||
|
||||
fn add_upstream_click_targets(&self, click_targets: &mut Vec<ClickTarget>, _inherited_appearance: Option<&Appearance>) {
|
||||
for index in 0..self.len() {
|
||||
let dimensions: DVec2 = self.attribute_cloned_or_default(ATTR_DIMENSIONS, index);
|
||||
@@ -1439,6 +1490,13 @@ impl Render for List<Graphic> {
|
||||
self.iter_element_values().any(|element| element.contains_artboard())
|
||||
}
|
||||
|
||||
fn collect_gpu_textures(&self, textures: &mut Vec<Texture>) {
|
||||
for element in self.iter_element_values() {
|
||||
element.collect_gpu_textures(textures);
|
||||
}
|
||||
collect_list_appearance_gpu_textures(self, textures);
|
||||
}
|
||||
|
||||
fn new_ids_from_hash(&mut self, _reference: Option<NodeId>) {
|
||||
let (elements, layers) = self.element_and_attribute_slices_mut::<NodeIdPath>(ATTR_EDITOR_LAYER_PATH);
|
||||
for (element, layer) in elements.iter_mut().zip(layers.iter()) {
|
||||
@@ -2242,6 +2300,10 @@ impl Render for List<Vector> {
|
||||
}
|
||||
}
|
||||
|
||||
fn collect_gpu_textures(&self, textures: &mut Vec<Texture>) {
|
||||
collect_list_appearance_gpu_textures(self, textures);
|
||||
}
|
||||
|
||||
fn new_ids_from_hash(&mut self, reference: Option<NodeId>) {
|
||||
for index in 0..self.len() {
|
||||
edit_list_row_geometry(self, index, |vector, paths| vector.vector_new_ids_from_hash(paths, reference.map(|id| id.0).unwrap_or_default()));
|
||||
@@ -2357,7 +2419,19 @@ fn canvas_image_id(buffer_id: u64, halvings: u32) -> u64 {
|
||||
/// Emits one item of CPU raster content as SVG, as a canvas placeholder or an embedded base64 image.
|
||||
fn render_raster_cpu_item_svg(item: ItemRef<'_, Raster<CPU>>, render: &mut SvgRender, render_params: &RenderParams) {
|
||||
let Some(image) = item.element() else { return };
|
||||
render_raster_image_svg(item, image, render, render_params);
|
||||
}
|
||||
|
||||
/// Emits one item of GPU raster content as SVG from its texture's downloaded image, exactly as CPU raster content.
|
||||
fn render_raster_gpu_item_svg(item: ItemRef<'_, Raster<GPU>>, render: &mut SvgRender, render_params: &RenderParams) {
|
||||
let Some(image) = item.element().and_then(Raster::texture).and_then(|texture| render_params.gpu_raster_downloads.get(texture)) else {
|
||||
return;
|
||||
};
|
||||
render_raster_image_svg(item, image, render, render_params);
|
||||
}
|
||||
|
||||
/// Emits a raster image as SVG, placed and styled by the attributes of the item it's drawn for.
|
||||
fn render_raster_image_svg<T>(item: ItemRef<'_, T>, image: &Raster<CPU>, render: &mut SvgRender, render_params: &RenderParams) {
|
||||
let transform: DAffine2 = item.attribute_cloned_or_default(ATTR_TRANSFORM);
|
||||
let blend_mode_attr: BlendMode = item.attribute_cloned_or_default(ATTR_BLEND_MODE);
|
||||
let opacity_attr: f64 = item.attribute_cloned_or(ATTR_OPACITY, 1.);
|
||||
@@ -2547,8 +2621,10 @@ impl Render for List<Raster<CPU>> {
|
||||
}
|
||||
|
||||
impl Render for List<Raster<GPU>> {
|
||||
fn render_svg(&self, _render: &mut SvgRender, _render_params: &RenderParams) {
|
||||
log::warn!("tried to render texture as an svg");
|
||||
fn render_svg(&self, render: &mut SvgRender, render_params: &RenderParams) {
|
||||
for index in 0..self.len() {
|
||||
render_raster_gpu_item_svg(ItemRef::ListItem(self, index), render, render_params);
|
||||
}
|
||||
}
|
||||
|
||||
fn render_to_vello(&self, scene: &mut VelloSceneBuilder, transform: DAffine2, render_params: &RenderParams) {
|
||||
@@ -2566,6 +2642,21 @@ impl Render for List<Raster<GPU>> {
|
||||
add_unit_square_click_target(self.attribute_cloned_or_default(ATTR_TRANSFORM, index), click_targets);
|
||||
}
|
||||
}
|
||||
|
||||
fn collect_gpu_textures(&self, textures: &mut Vec<Texture>) {
|
||||
for raster in self.iter_element_values() {
|
||||
collect_raster_gpu_texture(raster, textures);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Collects a GPU raster's texture unless it's empty or already collected.
|
||||
fn collect_raster_gpu_texture(raster: &Raster<GPU>, textures: &mut Vec<Texture>) {
|
||||
if let Some(texture) = raster.texture()
|
||||
&& !textures.contains(texture)
|
||||
{
|
||||
textures.push(texture.clone());
|
||||
}
|
||||
}
|
||||
|
||||
/// Draws one item of GPU raster content into the Vello scene.
|
||||
|
||||
@@ -16,24 +16,6 @@ fn upload_to_texture(executor: &WgpuExecutor, queue: &wgpu::Queue, image: &Raste
|
||||
|
||||
let texture = executor.request_texture_with_format(glam::UVec2::new(image.width, image.height), crate::RASTER_FORMAT);
|
||||
|
||||
if image.width == 0 || image.height == 0 {
|
||||
queue.write_texture(
|
||||
texture.as_image_copy(),
|
||||
bytemuck::bytes_of(&Color::TRANSPARENT),
|
||||
wgpu::TexelCopyBufferLayout {
|
||||
offset: 0,
|
||||
bytes_per_row: Some(16),
|
||||
rows_per_image: Some(1),
|
||||
},
|
||||
Extent3d {
|
||||
width: 1,
|
||||
height: 1,
|
||||
depth_or_array_layers: 1,
|
||||
},
|
||||
);
|
||||
return texture;
|
||||
}
|
||||
|
||||
queue.write_texture(
|
||||
texture.as_image_copy(),
|
||||
bytemuck::cast_slice(image.data.as_slice()),
|
||||
|
||||
Reference in new issue
Block a user