Skip to content

Commit

Permalink
Use OnceCell to keep Pipeline private
Browse files Browse the repository at this point in the history
  • Loading branch information
hecrj committed May 8, 2024
1 parent f7e46e0 commit 4112732
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 8 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ etagere = "0.2.10"
cosmic-text = "0.11"
lru = { version = "0.12.1", default-features = false }
rustc-hash = "1.1"
once_cell = "1.19"

[dev-dependencies]
winit = { version = "0.29.10", features = ["rwh_05"] }
Expand Down
7 changes: 3 additions & 4 deletions examples/hello-world.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use glyphon::{
Attrs, Buffer, Color, Family, FontSystem, Metrics, Pipeline, Resolution, Shaping, SwashCache,
TextArea, TextAtlas, TextBounds, TextRenderer,
Attrs, Buffer, Color, Family, FontSystem, Metrics, Resolution, Shaping, SwashCache, TextArea,
TextAtlas, TextBounds, TextRenderer,
};
use wgpu::{
CommandEncoderDescriptor, CompositeAlphaMode, DeviceDescriptor, Features, Instance,
Expand Down Expand Up @@ -72,8 +72,7 @@ async fn run() {
// Set up text renderer
let mut font_system = FontSystem::new();
let mut cache = SwashCache::new();
let pipeline = Pipeline::new(&device);
let mut atlas = TextAtlas::new(&device, &queue, &pipeline, swapchain_format);
let mut atlas = TextAtlas::new(&device, &queue, swapchain_format);
let mut text_renderer =
TextRenderer::new(&mut atlas, &device, MultisampleState::default(), None);
let mut buffer = Buffer::new(&mut font_system, Metrics::new(30.0, 42.0));
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ mod text_atlas;
mod text_render;

pub use error::{PrepareError, RenderError};
pub use pipeline::Pipeline;
pub use text_atlas::{ColorMode, TextAtlas};
pub use text_render::TextRenderer;

use pipeline::Pipeline;
use text_render::ContentType;

// Re-export all top-level types from `cosmic-text` for convenience.
Expand Down
9 changes: 6 additions & 3 deletions src/text_atlas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::{
};
use etagere::{size2, Allocation, BucketedAtlasAllocator};
use lru::LruCache;
use once_cell::sync::OnceCell;
use rustc_hash::FxHasher;
use std::{collections::HashSet, hash::BuildHasherDefault, sync::Arc};
use wgpu::{
Expand Down Expand Up @@ -262,18 +263,20 @@ pub struct TextAtlas {

impl TextAtlas {
/// Creates a new [`TextAtlas`].
pub fn new(device: &Device, queue: &Queue, pipeline: &Pipeline, format: TextureFormat) -> Self {
Self::with_color_mode(device, queue, pipeline, format, ColorMode::Accurate)
pub fn new(device: &Device, queue: &Queue, format: TextureFormat) -> Self {
Self::with_color_mode(device, queue, format, ColorMode::Accurate)
}

/// Creates a new [`TextAtlas`] with the given [`ColorMode`].
pub fn with_color_mode(
device: &Device,
queue: &Queue,
pipeline: &Pipeline,
format: TextureFormat,
color_mode: ColorMode,
) -> Self {
static PIPELINE: OnceCell<Pipeline> = OnceCell::new();
let pipeline = PIPELINE.get_or_init(|| Pipeline::new(device));

let color_atlas = InnerAtlas::new(
device,
queue,
Expand Down

0 comments on commit 4112732

Please sign in to comment.