1#![deny(missing_docs)]
6
7use std::cell::Cell;
8use std::fmt;
9use std::marker::PhantomData;
10use std::path::PathBuf;
11use std::sync::Arc;
12use std::u32;
13use time::precise_time_ns;
14use crate::api::channel::{Sender, single_msg_channel, unbounded_channel};
16use crate::api::{ColorF, BuiltDisplayList, IdNamespace, ExternalScrollId};
17use crate::api::{SharedFontInstanceMap, FontKey, FontInstanceKey, NativeFontHandle};
18use crate::api::{BlobImageData, BlobImageKey, ImageData, ImageDescriptor, ImageKey, Epoch, QualitySettings};
19use crate::api::{BlobImageParams, BlobImageRequest, BlobImageResult, AsyncBlobImageRasterizer, BlobImageHandler};
20use crate::api::{DocumentId, PipelineId, PropertyBindingId, PropertyBindingKey, ExternalEvent};
21use crate::api::{HitTestResult, HitTesterRequest, ApiHitTester, PropertyValue, DynamicProperties};
22use crate::api::{ScrollClamping, TileSize, NotificationRequest, DebugFlags, ScrollNodeState};
23use crate::api::{GlyphDimensionRequest, GlyphIndexRequest, GlyphIndex, GlyphDimensions};
24use crate::api::{FontInstanceOptions, FontInstancePlatformOptions, FontVariation};
25use crate::api::DEFAULT_TILE_SIZE;
26use crate::api::units::*;
27use crate::api_resources::ApiResources;
28use crate::scene_builder_thread::{SceneBuilderRequest, SceneBuilderResult};
29use crate::intern::InterningMemoryReport;
30use crate::profiler::{self, TransactionProfile};
31
32#[repr(C)]
33#[derive(Clone, Copy, Debug)]
34#[cfg_attr(any(feature = "serde"), derive(Deserialize, Serialize))]
35struct ResourceId(pub u32);
36
37#[derive(Clone)]
41#[cfg_attr(any(feature = "serde"), derive(Deserialize, Serialize))]
42pub enum ResourceUpdate {
43 AddImage(AddImage),
45 UpdateImage(UpdateImage),
47 DeleteImage(ImageKey),
53 AddBlobImage(AddBlobImage),
55 UpdateBlobImage(UpdateBlobImage),
57 DeleteBlobImage(BlobImageKey),
59 SetBlobImageVisibleArea(BlobImageKey, DeviceIntRect),
61 AddFont(AddFont),
63 DeleteFont(FontKey),
69 AddFontInstance(AddFontInstance),
71 DeleteFontInstance(FontInstanceKey),
77}
78
79impl fmt::Debug for ResourceUpdate {
80 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
81 match self {
82 ResourceUpdate::AddImage(ref i) => f.write_fmt(format_args!(
83 "ResourceUpdate::AddImage size({:?})",
84 &i.descriptor.size
85 )),
86 ResourceUpdate::UpdateImage(ref i) => f.write_fmt(format_args!(
87 "ResourceUpdate::UpdateImage size({:?})",
88 &i.descriptor.size
89 )),
90 ResourceUpdate::AddBlobImage(ref i) => f.write_fmt(format_args!(
91 "ResourceUFpdate::AddBlobImage size({:?})",
92 &i.descriptor.size
93 )),
94 ResourceUpdate::UpdateBlobImage(i) => f.write_fmt(format_args!(
95 "ResourceUpdate::UpdateBlobImage size({:?})",
96 &i.descriptor.size
97 )),
98 ResourceUpdate::DeleteImage(..) => f.write_str("ResourceUpdate::DeleteImage"),
99 ResourceUpdate::DeleteBlobImage(..) => f.write_str("ResourceUpdate::DeleteBlobImage"),
100 ResourceUpdate::SetBlobImageVisibleArea(..) => f.write_str("ResourceUpdate::SetBlobImageVisibleArea"),
101 ResourceUpdate::AddFont(..) => f.write_str("ResourceUpdate::AddFont"),
102 ResourceUpdate::DeleteFont(..) => f.write_str("ResourceUpdate::DeleteFont"),
103 ResourceUpdate::AddFontInstance(..) => f.write_str("ResourceUpdate::AddFontInstance"),
104 ResourceUpdate::DeleteFontInstance(..) => f.write_str("ResourceUpdate::DeleteFontInstance"),
105 }
106 }
107}
108
109#[derive(Clone, Debug)]
112pub enum GenerateFrame {
113 Yes {
115 id: u64,
118 },
119 No,
121}
122
123impl GenerateFrame {
124 pub fn as_bool(&self) -> bool {
126 match self {
127 GenerateFrame::Yes { .. } => true,
128 GenerateFrame::No => false,
129 }
130 }
131
132 pub fn id(&self) -> Option<u64> {
134 match self {
135 GenerateFrame::Yes { id } => Some(*id),
136 GenerateFrame::No => None,
137 }
138 }
139}
140
141pub struct Transaction {
148 scene_ops: Vec<SceneMsg>,
150 frame_ops: Vec<FrameMsg>,
152
153 notifications: Vec<NotificationRequest>,
154
155 pub resource_updates: Vec<ResourceUpdate>,
157
158 use_scene_builder_thread: bool,
166
167 generate_frame: GenerateFrame,
171
172 pub invalidate_rendered_frame: bool,
175
176 low_priority: bool,
177}
178
179impl Transaction {
180 pub fn new() -> Self {
182 Transaction {
183 scene_ops: Vec::new(),
184 frame_ops: Vec::new(),
185 resource_updates: Vec::new(),
186 notifications: Vec::new(),
187 use_scene_builder_thread: true,
188 generate_frame: GenerateFrame::No,
189 invalidate_rendered_frame: false,
190 low_priority: false,
191 }
192 }
193
194 pub fn skip_scene_builder(&mut self) {
203 self.use_scene_builder_thread = false;
204 }
205
206 pub fn use_scene_builder_thread(&mut self) {
208 self.use_scene_builder_thread = true;
209 }
210
211 pub fn is_empty(&self) -> bool {
213 !self.generate_frame.as_bool() &&
214 !self.invalidate_rendered_frame &&
215 self.scene_ops.is_empty() &&
216 self.frame_ops.is_empty() &&
217 self.resource_updates.is_empty() &&
218 self.notifications.is_empty()
219 }
220
221 pub fn update_epoch(&mut self, pipeline_id: PipelineId, epoch: Epoch) {
223 self.scene_ops.push(SceneMsg::UpdateEpoch(pipeline_id, epoch));
226 self.frame_ops.push(FrameMsg::UpdateEpoch(pipeline_id, epoch));
229 }
233
234 pub fn set_root_pipeline(&mut self, pipeline_id: PipelineId) {
249 self.scene_ops.push(SceneMsg::SetRootPipeline(pipeline_id));
250 }
251
252 pub fn remove_pipeline(&mut self, pipeline_id: PipelineId) {
256 self.scene_ops.push(SceneMsg::RemovePipeline(pipeline_id));
257 }
258
259 pub fn set_display_list(
276 &mut self,
277 epoch: Epoch,
278 background: Option<ColorF>,
279 viewport_size: LayoutSize,
280 (pipeline_id, mut display_list): (PipelineId, BuiltDisplayList),
281 preserve_frame_state: bool,
282 ) {
283 display_list.set_send_time_ns(precise_time_ns());
284 self.scene_ops.push(
285 SceneMsg::SetDisplayList {
286 display_list,
287 epoch,
288 pipeline_id,
289 background,
290 viewport_size,
291 preserve_frame_state,
292 }
293 );
294 }
295
296 pub fn update_resources(&mut self, mut resources: Vec<ResourceUpdate>) {
298 self.resource_updates.append(&mut resources);
299 }
300
301 pub fn notify(&mut self, event: NotificationRequest) {
313 self.notifications.push(event);
314 }
315
316 pub fn set_document_view(
318 &mut self,
319 device_rect: DeviceIntRect,
320 ) {
321 window_size_sanity_check(device_rect.size());
322 self.scene_ops.push(
323 SceneMsg::SetDocumentView {
324 device_rect,
325 },
326 );
327 }
328
329 pub fn scroll_node_with_id(
341 &mut self,
342 origin: LayoutPoint,
343 id: ExternalScrollId,
344 clamp: ScrollClamping,
345 ) {
346 self.frame_ops.push(FrameMsg::ScrollNodeWithId(origin, id, clamp));
347 }
348
349 pub fn set_quality_settings(&mut self, settings: QualitySettings) {
351 self.scene_ops.push(SceneMsg::SetQualitySettings { settings });
352 }
353
354 pub fn set_is_transform_async_zooming(&mut self, is_zooming: bool, animation_id: PropertyBindingId) {
356 self.frame_ops.push(FrameMsg::SetIsTransformAsyncZooming(is_zooming, animation_id));
357 }
358
359 pub fn generate_frame(&mut self, id: u64) {
367 self.generate_frame = GenerateFrame::Yes{ id };
368 }
369
370 pub fn invalidate_rendered_frame(&mut self) {
377 self.invalidate_rendered_frame = true;
378 }
379
380 pub fn update_dynamic_properties(&mut self, properties: DynamicProperties) {
383 self.frame_ops.push(FrameMsg::UpdateDynamicProperties(properties));
384 }
385
386 pub fn append_dynamic_transform_properties(&mut self, transforms: Vec<PropertyValue<LayoutTransform>>) {
391 self.frame_ops.push(FrameMsg::AppendDynamicTransformProperties(transforms));
392 }
393
394 pub fn get_frame_ops(self) -> Vec<FrameMsg> {
396 self.frame_ops
397 }
398
399 fn finalize(self, document_id: DocumentId) -> Box<TransactionMsg> {
400 Box::new(TransactionMsg {
401 document_id,
402 scene_ops: self.scene_ops,
403 frame_ops: self.frame_ops,
404 resource_updates: self.resource_updates,
405 notifications: self.notifications,
406 use_scene_builder_thread: self.use_scene_builder_thread,
407 generate_frame: self.generate_frame,
408 invalidate_rendered_frame: self.invalidate_rendered_frame,
409 low_priority: self.low_priority,
410 blob_rasterizer: None,
411 blob_requests: Vec::new(),
412 rasterized_blobs: Vec::new(),
413 profile: TransactionProfile::new(),
414 })
415 }
416
417 pub fn add_image(
419 &mut self,
420 key: ImageKey,
421 descriptor: ImageDescriptor,
422 data: ImageData,
423 tiling: Option<TileSize>,
424 ) {
425 self.resource_updates.push(ResourceUpdate::AddImage(AddImage {
426 key,
427 descriptor,
428 data,
429 tiling,
430 }));
431 }
432
433 pub fn update_image(
435 &mut self,
436 key: ImageKey,
437 descriptor: ImageDescriptor,
438 data: ImageData,
439 dirty_rect: &ImageDirtyRect,
440 ) {
441 self.resource_updates.push(ResourceUpdate::UpdateImage(UpdateImage {
442 key,
443 descriptor,
444 data,
445 dirty_rect: *dirty_rect,
446 }));
447 }
448
449 pub fn delete_image(&mut self, key: ImageKey) {
451 self.resource_updates.push(ResourceUpdate::DeleteImage(key));
452 }
453
454 pub fn add_blob_image(
456 &mut self,
457 key: BlobImageKey,
458 descriptor: ImageDescriptor,
459 data: Arc<BlobImageData>,
460 visible_rect: DeviceIntRect,
461 tile_size: Option<TileSize>,
462 ) {
463 self.resource_updates.push(
464 ResourceUpdate::AddBlobImage(AddBlobImage {
465 key,
466 descriptor,
467 data,
468 visible_rect,
469 tile_size: tile_size.unwrap_or(DEFAULT_TILE_SIZE),
470 })
471 );
472 }
473
474 pub fn update_blob_image(
476 &mut self,
477 key: BlobImageKey,
478 descriptor: ImageDescriptor,
479 data: Arc<BlobImageData>,
480 visible_rect: DeviceIntRect,
481 dirty_rect: &BlobDirtyRect,
482 ) {
483 self.resource_updates.push(
484 ResourceUpdate::UpdateBlobImage(UpdateBlobImage {
485 key,
486 descriptor,
487 data,
488 visible_rect,
489 dirty_rect: *dirty_rect,
490 })
491 );
492 }
493
494 pub fn delete_blob_image(&mut self, key: BlobImageKey) {
496 self.resource_updates.push(ResourceUpdate::DeleteBlobImage(key));
497 }
498
499 pub fn set_blob_image_visible_area(&mut self, key: BlobImageKey, area: DeviceIntRect) {
501 self.resource_updates.push(ResourceUpdate::SetBlobImageVisibleArea(key, area));
502 }
503
504 pub fn add_raw_font(&mut self, key: FontKey, bytes: Vec<u8>, index: u32) {
506 self.resource_updates
507 .push(ResourceUpdate::AddFont(AddFont::Raw(key, Arc::new(bytes), index)));
508 }
509
510 pub fn add_native_font(&mut self, key: FontKey, native_handle: NativeFontHandle) {
512 self.resource_updates
513 .push(ResourceUpdate::AddFont(AddFont::Native(key, native_handle)));
514 }
515
516 pub fn delete_font(&mut self, key: FontKey) {
518 self.resource_updates.push(ResourceUpdate::DeleteFont(key));
519 }
520
521 pub fn add_font_instance(
523 &mut self,
524 key: FontInstanceKey,
525 font_key: FontKey,
526 glyph_size: f32,
527 options: Option<FontInstanceOptions>,
528 platform_options: Option<FontInstancePlatformOptions>,
529 variations: Vec<FontVariation>,
530 ) {
531 self.resource_updates
532 .push(ResourceUpdate::AddFontInstance(AddFontInstance {
533 key,
534 font_key,
535 glyph_size,
536 options,
537 platform_options,
538 variations,
539 }));
540 }
541
542 pub fn delete_font_instance(&mut self, key: FontInstanceKey) {
544 self.resource_updates.push(ResourceUpdate::DeleteFontInstance(key));
545 }
546
547 pub fn set_low_priority(&mut self, low_priority: bool) {
552 self.low_priority = low_priority;
553 }
554
555 pub fn is_low_priority(&self) -> bool {
557 self.low_priority
558 }
559}
560
561pub struct DocumentTransaction {
563 pub document_id: DocumentId,
565 pub transaction: Transaction,
567}
568
569pub struct TransactionMsg {
571 pub document_id: DocumentId,
573 pub scene_ops: Vec<SceneMsg>,
575 pub frame_ops: Vec<FrameMsg>,
577 pub resource_updates: Vec<ResourceUpdate>,
579 pub generate_frame: GenerateFrame,
581 pub invalidate_rendered_frame: bool,
584 pub use_scene_builder_thread: bool,
586 pub low_priority: bool,
588
589 pub notifications: Vec<NotificationRequest>,
591 pub blob_rasterizer: Option<Box<dyn AsyncBlobImageRasterizer>>,
593 pub blob_requests: Vec<BlobImageParams>,
595 pub rasterized_blobs: Vec<(BlobImageRequest, BlobImageResult)>,
597 pub profile: TransactionProfile,
599}
600
601impl fmt::Debug for TransactionMsg {
602 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
603 writeln!(f, "threaded={}, genframe={:?}, invalidate={}, low_priority={}",
604 self.use_scene_builder_thread,
605 self.generate_frame,
606 self.invalidate_rendered_frame,
607 self.low_priority,
608 ).unwrap();
609 for scene_op in &self.scene_ops {
610 writeln!(f, "\t\t{:?}", scene_op).unwrap();
611 }
612
613 for frame_op in &self.frame_ops {
614 writeln!(f, "\t\t{:?}", frame_op).unwrap();
615 }
616
617 for resource_update in &self.resource_updates {
618 writeln!(f, "\t\t{:?}", resource_update).unwrap();
619 }
620 Ok(())
621 }
622}
623
624impl TransactionMsg {
625 pub fn is_empty(&self) -> bool {
627 !self.generate_frame.as_bool() &&
628 !self.invalidate_rendered_frame &&
629 self.scene_ops.is_empty() &&
630 self.frame_ops.is_empty() &&
631 self.resource_updates.is_empty() &&
632 self.notifications.is_empty()
633 }
634}
635
636#[derive(Clone)]
640#[cfg_attr(any(feature = "serde"), derive(Deserialize, Serialize))]
641pub struct AddImage {
642 pub key: ImageKey,
644 pub descriptor: ImageDescriptor,
646 pub data: ImageData,
648 pub tiling: Option<TileSize>,
654}
655
656#[derive(Clone)]
658#[cfg_attr(any(feature = "serde"), derive(Deserialize, Serialize))]
659pub struct UpdateImage {
660 pub key: ImageKey,
662 pub descriptor: ImageDescriptor,
664 pub data: ImageData,
666 pub dirty_rect: ImageDirtyRect,
671}
672
673#[derive(Clone)]
677#[cfg_attr(any(feature = "serde"), derive(Deserialize, Serialize))]
678pub struct AddBlobImage {
679 pub key: BlobImageKey,
681 pub descriptor: ImageDescriptor,
683 pub data: Arc<BlobImageData>,
685 pub visible_rect: DeviceIntRect,
694 pub tile_size: TileSize,
700}
701
702#[derive(Clone)]
704#[cfg_attr(any(feature = "serde"), derive(Deserialize, Serialize))]
705pub struct UpdateBlobImage {
706 pub key: BlobImageKey,
708 pub descriptor: ImageDescriptor,
710 pub data: Arc<BlobImageData>,
712 pub visible_rect: DeviceIntRect,
714 pub dirty_rect: BlobDirtyRect,
717}
718
719#[derive(Clone)]
724#[cfg_attr(any(feature = "serde"), derive(Deserialize, Serialize))]
725pub enum AddFont {
726 Raw(FontKey, Arc<Vec<u8>>, u32),
728 Native(FontKey, NativeFontHandle),
730}
731
732#[derive(Clone)]
737#[cfg_attr(any(feature = "serde"), derive(Deserialize, Serialize))]
738pub struct AddFontInstance {
739 pub key: FontInstanceKey,
741 pub font_key: FontKey,
743 pub glyph_size: f32,
745 pub options: Option<FontInstanceOptions>,
747 pub platform_options: Option<FontInstancePlatformOptions>,
749 pub variations: Vec<FontVariation>,
751}
752
753pub enum SceneMsg {
755 UpdateEpoch(PipelineId, Epoch),
757 SetRootPipeline(PipelineId),
759 RemovePipeline(PipelineId),
761 SetDisplayList {
763 display_list: BuiltDisplayList,
765 epoch: Epoch,
767 pipeline_id: PipelineId,
769 background: Option<ColorF>,
771 viewport_size: LayoutSize,
773 preserve_frame_state: bool,
775 },
776 SetDocumentView {
778 device_rect: DeviceIntRect,
780 },
781 SetQualitySettings {
783 settings: QualitySettings,
785 },
786}
787
788pub enum FrameMsg {
790 UpdateEpoch(PipelineId, Epoch),
792 HitTest(Option<PipelineId>, WorldPoint, Sender<HitTestResult>),
794 RequestHitTester(Sender<Arc<dyn ApiHitTester>>),
796 ScrollNodeWithId(LayoutPoint, ExternalScrollId, ScrollClamping),
798 GetScrollNodeState(Sender<Vec<ScrollNodeState>>),
800 UpdateDynamicProperties(DynamicProperties),
802 AppendDynamicTransformProperties(Vec<PropertyValue<LayoutTransform>>),
804 SetIsTransformAsyncZooming(bool, PropertyBindingId),
806}
807
808impl fmt::Debug for SceneMsg {
809 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
810 f.write_str(match *self {
811 SceneMsg::UpdateEpoch(..) => "SceneMsg::UpdateEpoch",
812 SceneMsg::SetDisplayList { .. } => "SceneMsg::SetDisplayList",
813 SceneMsg::RemovePipeline(..) => "SceneMsg::RemovePipeline",
814 SceneMsg::SetDocumentView { .. } => "SceneMsg::SetDocumentView",
815 SceneMsg::SetRootPipeline(..) => "SceneMsg::SetRootPipeline",
816 SceneMsg::SetQualitySettings { .. } => "SceneMsg::SetQualitySettings",
817 })
818 }
819}
820
821impl fmt::Debug for FrameMsg {
822 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
823 f.write_str(match *self {
824 FrameMsg::UpdateEpoch(..) => "FrameMsg::UpdateEpoch",
825 FrameMsg::HitTest(..) => "FrameMsg::HitTest",
826 FrameMsg::RequestHitTester(..) => "FrameMsg::RequestHitTester",
827 FrameMsg::ScrollNodeWithId(..) => "FrameMsg::ScrollNodeWithId",
828 FrameMsg::GetScrollNodeState(..) => "FrameMsg::GetScrollNodeState",
829 FrameMsg::UpdateDynamicProperties(..) => "FrameMsg::UpdateDynamicProperties",
830 FrameMsg::AppendDynamicTransformProperties(..) => "FrameMsg::AppendDynamicTransformProperties",
831 FrameMsg::SetIsTransformAsyncZooming(..) => "FrameMsg::SetIsTransformAsyncZooming",
832 })
833 }
834}
835
836bitflags!{
837 pub struct CaptureBits: u8 {
840 const SCENE = 0x1;
842 const FRAME = 0x2;
844 const TILE_CACHE = 0x4;
846 const EXTERNAL_RESOURCES = 0x8;
848 }
849}
850
851bitflags!{
852 pub struct ClearCache: u8 {
854 const IMAGES = 0b1;
856 const GLYPHS = 0b10;
858 const GLYPH_DIMENSIONS = 0b100;
860 const RENDER_TASKS = 0b1000;
862 const TEXTURE_CACHE = 0b10000;
864 const RENDER_TARGETS = 0b100000;
866 }
867}
868
869#[derive(Clone, Debug)]
872pub struct CapturedDocument {
873 pub document_id: DocumentId,
875 pub root_pipeline_id: Option<PipelineId>,
877}
878
879#[derive(Clone)]
881pub enum DebugCommand {
882 SetFlags(DebugFlags),
884 EnableDualSourceBlending(bool),
886 SaveCapture(PathBuf, CaptureBits),
888 LoadCapture(PathBuf, Option<(u32, u32)>, Sender<CapturedDocument>),
890 StartCaptureSequence(PathBuf, CaptureBits),
892 StopCaptureSequence,
894 ClearCaches(ClearCache),
896 EnableNativeCompositor(bool),
898 EnableMultithreading(bool),
900 SetBatchingLookback(u32),
902 InvalidateGpuCache,
904 SimulateLongSceneBuild(u32),
907 SetPictureTileSize(Option<DeviceIntSize>),
909}
910
911pub enum ApiMsg {
913 CloneApi(Sender<IdNamespace>),
915 CloneApiByClient(IdNamespace),
917 AddDocument(DocumentId, DeviceIntSize),
919 UpdateDocuments(Vec<Box<TransactionMsg>>),
921 MemoryPressure,
923 ReportMemory(Sender<Box<MemoryReport>>),
925 DebugCommand(DebugCommand),
927 SceneBuilderResult(SceneBuilderResult),
929}
930
931impl fmt::Debug for ApiMsg {
932 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
933 f.write_str(match *self {
934 ApiMsg::CloneApi(..) => "ApiMsg::CloneApi",
935 ApiMsg::CloneApiByClient(..) => "ApiMsg::CloneApiByClient",
936 ApiMsg::AddDocument(..) => "ApiMsg::AddDocument",
937 ApiMsg::UpdateDocuments(..) => "ApiMsg::UpdateDocuments",
938 ApiMsg::MemoryPressure => "ApiMsg::MemoryPressure",
939 ApiMsg::ReportMemory(..) => "ApiMsg::ReportMemory",
940 ApiMsg::DebugCommand(..) => "ApiMsg::DebugCommand",
941 ApiMsg::SceneBuilderResult(..) => "ApiMsg::SceneBuilderResult",
942 })
943 }
944}
945
946pub struct RenderApiSender {
951 api_sender: Sender<ApiMsg>,
952 scene_sender: Sender<SceneBuilderRequest>,
953 low_priority_scene_sender: Sender<SceneBuilderRequest>,
954 blob_image_handler: Option<Box<dyn BlobImageHandler>>,
955 shared_font_instances: SharedFontInstanceMap,
956}
957
958impl RenderApiSender {
959 pub fn new(
961 api_sender: Sender<ApiMsg>,
962 scene_sender: Sender<SceneBuilderRequest>,
963 low_priority_scene_sender: Sender<SceneBuilderRequest>,
964 blob_image_handler: Option<Box<dyn BlobImageHandler>>,
965 shared_font_instances: SharedFontInstanceMap,
966 ) -> Self {
967 RenderApiSender {
968 api_sender,
969 scene_sender,
970 low_priority_scene_sender,
971 blob_image_handler,
972 shared_font_instances,
973 }
974 }
975
976 pub fn create_api(&self) -> RenderApi {
978 let (sync_tx, sync_rx) = single_msg_channel();
979 let msg = ApiMsg::CloneApi(sync_tx);
980 self.api_sender.send(msg).expect("Failed to send CloneApi message");
981 let namespace_id = sync_rx.recv().expect("Failed to receive CloneApi reply");
982 RenderApi {
983 api_sender: self.api_sender.clone(),
984 scene_sender: self.scene_sender.clone(),
985 low_priority_scene_sender: self.low_priority_scene_sender.clone(),
986 namespace_id,
987 next_id: Cell::new(ResourceId(0)),
988 resources: ApiResources::new(
989 self.blob_image_handler.as_ref().map(|handler| handler.create_similar()),
990 self.shared_font_instances.clone(),
991 ),
992 }
993 }
994
995 pub fn create_api_by_client(&self, namespace_id: IdNamespace) -> RenderApi {
1001 let msg = ApiMsg::CloneApiByClient(namespace_id);
1002 self.api_sender.send(msg).expect("Failed to send CloneApiByClient message");
1003 RenderApi {
1004 api_sender: self.api_sender.clone(),
1005 scene_sender: self.scene_sender.clone(),
1006 low_priority_scene_sender: self.low_priority_scene_sender.clone(),
1007 namespace_id,
1008 next_id: Cell::new(ResourceId(0)),
1009 resources: ApiResources::new(
1010 self.blob_image_handler.as_ref().map(|handler| handler.create_similar()),
1011 self.shared_font_instances.clone(),
1012 ),
1013 }
1014 }
1015}
1016
1017pub struct RenderApi {
1019 api_sender: Sender<ApiMsg>,
1020 scene_sender: Sender<SceneBuilderRequest>,
1021 low_priority_scene_sender: Sender<SceneBuilderRequest>,
1022 namespace_id: IdNamespace,
1023 next_id: Cell<ResourceId>,
1024 resources: ApiResources,
1025}
1026
1027impl RenderApi {
1028 pub fn get_namespace_id(&self) -> IdNamespace {
1030 self.namespace_id
1031 }
1032
1033 pub fn create_sender(&self) -> RenderApiSender {
1035 RenderApiSender::new(
1036 self.api_sender.clone(),
1037 self.scene_sender.clone(),
1038 self.low_priority_scene_sender.clone(),
1039 self.resources.blob_image_handler.as_ref().map(|handler| handler.create_similar()),
1040 self.resources.get_shared_font_instances(),
1041 )
1042 }
1043
1044 pub fn add_document(&self, initial_size: DeviceIntSize) -> DocumentId {
1050 let new_id = self.next_unique_id();
1051 self.add_document_with_id(initial_size, new_id)
1052 }
1053
1054 pub fn add_document_with_id(&self,
1056 initial_size: DeviceIntSize,
1057 id: u32) -> DocumentId {
1058 window_size_sanity_check(initial_size);
1059
1060 let document_id = DocumentId::new(self.namespace_id, id);
1061
1062 self.api_sender.send(
1068 ApiMsg::AddDocument(document_id, initial_size)
1069 ).unwrap();
1070 self.scene_sender.send(
1071 SceneBuilderRequest::AddDocument(document_id, initial_size)
1072 ).unwrap();
1073
1074 document_id
1075 }
1076
1077 pub fn delete_document(&self, document_id: DocumentId) {
1079 self.low_priority_scene_sender.send(
1080 SceneBuilderRequest::DeleteDocument(document_id)
1081 ).unwrap();
1082 }
1083
1084 pub fn generate_font_key(&self) -> FontKey {
1086 let new_id = self.next_unique_id();
1087 FontKey::new(self.namespace_id, new_id)
1088 }
1089
1090 pub fn generate_font_instance_key(&self) -> FontInstanceKey {
1092 let new_id = self.next_unique_id();
1093 FontInstanceKey::new(self.namespace_id, new_id)
1094 }
1095
1096 pub fn get_glyph_dimensions(
1102 &self,
1103 key: FontInstanceKey,
1104 glyph_indices: Vec<GlyphIndex>,
1105 ) -> Vec<Option<GlyphDimensions>> {
1106 let (sender, rx) = single_msg_channel();
1107 let msg = SceneBuilderRequest::GetGlyphDimensions(GlyphDimensionRequest {
1108 key,
1109 glyph_indices,
1110 sender
1111 });
1112 self.low_priority_scene_sender.send(msg).unwrap();
1113 rx.recv().unwrap()
1114 }
1115
1116 pub fn get_glyph_indices(&self, key: FontKey, text: &str) -> Vec<Option<u32>> {
1119 let (sender, rx) = single_msg_channel();
1120 let msg = SceneBuilderRequest::GetGlyphIndices(GlyphIndexRequest {
1121 key,
1122 text: text.to_string(),
1123 sender,
1124 });
1125 self.low_priority_scene_sender.send(msg).unwrap();
1126 rx.recv().unwrap()
1127 }
1128
1129 pub fn generate_image_key(&self) -> ImageKey {
1131 let new_id = self.next_unique_id();
1132 ImageKey::new(self.namespace_id, new_id)
1133 }
1134
1135 pub fn generate_blob_image_key(&self) -> BlobImageKey {
1137 BlobImageKey(self.generate_image_key())
1138 }
1139
1140 pub fn send_external_event(&self, evt: ExternalEvent) {
1144 let msg = SceneBuilderRequest::ExternalEvent(evt);
1145 self.low_priority_scene_sender.send(msg).unwrap();
1146 }
1147
1148 pub fn notify_memory_pressure(&self) {
1151 self.api_sender.send(ApiMsg::MemoryPressure).unwrap();
1152 }
1153
1154 pub fn report_memory(&self, _ops: malloc_size_of::MallocSizeOfOps) -> MemoryReport {
1156 let (tx, rx) = single_msg_channel();
1157 self.api_sender.send(ApiMsg::ReportMemory(tx)).unwrap();
1158 *rx.recv().unwrap()
1159 }
1160
1161 pub fn set_debug_flags(&self, flags: DebugFlags) {
1163 let cmd = DebugCommand::SetFlags(flags);
1164 self.api_sender.send(ApiMsg::DebugCommand(cmd)).unwrap();
1165 }
1166
1167 pub fn stop_render_backend(&self) {
1169 self.low_priority_scene_sender.send(SceneBuilderRequest::StopRenderBackend).unwrap();
1170 }
1171
1172 pub fn shut_down(&self, synchronously: bool) {
1174 if synchronously {
1175 let (tx, rx) = single_msg_channel();
1176 self.low_priority_scene_sender.send(SceneBuilderRequest::ShutDown(Some(tx))).unwrap();
1177 rx.recv().unwrap();
1178 } else {
1179 self.low_priority_scene_sender.send(SceneBuilderRequest::ShutDown(None)).unwrap();
1180 }
1181 }
1182
1183 pub fn generate_property_binding_key<T: Copy>(&self) -> PropertyBindingKey<T> {
1186 let new_id = self.next_unique_id();
1187 PropertyBindingKey {
1188 id: PropertyBindingId {
1189 namespace: self.namespace_id,
1190 uid: new_id,
1191 },
1192 _phantom: PhantomData,
1193 }
1194 }
1195
1196 #[inline]
1197 fn next_unique_id(&self) -> u32 {
1198 let ResourceId(id) = self.next_id.get();
1199 self.next_id.set(ResourceId(id + 1));
1200 id
1201 }
1202
1203 #[doc(hidden)]
1205 pub fn send_message(&self, msg: ApiMsg) {
1206 self.api_sender.send(msg).unwrap();
1207 }
1208
1209 fn frame_message(&self, msg: FrameMsg, document_id: DocumentId) -> Box<TransactionMsg> {
1211 Box::new(TransactionMsg {
1212 document_id,
1213 scene_ops: Vec::new(),
1214 frame_ops: vec![msg],
1215 resource_updates: Vec::new(),
1216 notifications: Vec::new(),
1217 generate_frame: GenerateFrame::No,
1218 invalidate_rendered_frame: false,
1219 use_scene_builder_thread: false,
1220 low_priority: false,
1221 blob_rasterizer: None,
1222 blob_requests: Vec::new(),
1223 rasterized_blobs: Vec::new(),
1224 profile: TransactionProfile::new(),
1225 })
1226 }
1227
1228 fn send_frame_msg(&self, document_id: DocumentId, msg: FrameMsg) {
1230 self.api_sender
1234 .send(ApiMsg::UpdateDocuments(vec![self.frame_message(msg, document_id)]))
1235 .unwrap()
1236 }
1237
1238 pub fn send_transaction(&mut self, document_id: DocumentId, transaction: Transaction) {
1240 let mut transaction = transaction.finalize(document_id);
1241
1242 self.resources.update(&mut transaction);
1243
1244 if transaction.generate_frame.as_bool() {
1245 transaction.profile.start_time(profiler::API_SEND_TIME);
1246 transaction.profile.start_time(profiler::TOTAL_FRAME_CPU_TIME);
1247 }
1248
1249 if transaction.use_scene_builder_thread {
1250 let sender = if transaction.low_priority {
1251 &mut self.low_priority_scene_sender
1252 } else {
1253 &mut self.scene_sender
1254 };
1255
1256 sender.send(SceneBuilderRequest::Transactions(vec![transaction])).unwrap();
1257 } else {
1258 self.api_sender.send(ApiMsg::UpdateDocuments(vec![transaction])).unwrap();
1259 }
1260 }
1261
1262 pub fn hit_test(&self,
1268 document_id: DocumentId,
1269 pipeline_id: Option<PipelineId>,
1270 point: WorldPoint,
1271 ) -> HitTestResult {
1272 let (tx, rx) = single_msg_channel();
1273
1274 self.send_frame_msg(
1275 document_id,
1276 FrameMsg::HitTest(pipeline_id, point, tx)
1277 );
1278 rx.recv().unwrap()
1279 }
1280
1281 pub fn request_hit_tester(&self, document_id: DocumentId) -> HitTesterRequest {
1283 let (tx, rx) = single_msg_channel();
1284 self.send_frame_msg(
1285 document_id,
1286 FrameMsg::RequestHitTester(tx)
1287 );
1288
1289 HitTesterRequest { rx }
1290 }
1291
1292 pub fn get_scroll_node_state(&self, document_id: DocumentId) -> Vec<ScrollNodeState> {
1294 let (tx, rx) = single_msg_channel();
1295 self.send_frame_msg(document_id, FrameMsg::GetScrollNodeState(tx));
1296 rx.recv().unwrap()
1297 }
1298
1299 #[doc(hidden)]
1302 pub fn wake_scene_builder(&self) {
1303 self.scene_sender.send(SceneBuilderRequest::WakeUp).unwrap();
1304 }
1305
1306 pub fn flush_scene_builder(&self) {
1310 let (tx, rx) = single_msg_channel();
1311 self.low_priority_scene_sender.send(SceneBuilderRequest::Flush(tx)).unwrap();
1312 rx.recv().unwrap(); }
1314
1315 pub fn save_capture(&self, path: PathBuf, bits: CaptureBits) {
1317 let msg = ApiMsg::DebugCommand(DebugCommand::SaveCapture(path, bits));
1318 self.send_message(msg);
1319 }
1320
1321 pub fn load_capture(&self, path: PathBuf, ids: Option<(u32, u32)>) -> Vec<CapturedDocument> {
1323 self.flush_scene_builder();
1326
1327 let (tx, rx) = unbounded_channel();
1328 let msg = ApiMsg::DebugCommand(DebugCommand::LoadCapture(path, ids, tx));
1329 self.send_message(msg);
1330
1331 let mut documents = Vec::new();
1332 while let Ok(captured_doc) = rx.recv() {
1333 documents.push(captured_doc);
1334 }
1335 documents
1336 }
1337
1338 pub fn start_capture_sequence(&self, path: PathBuf, bits: CaptureBits) {
1340 let msg = ApiMsg::DebugCommand(DebugCommand::StartCaptureSequence(path, bits));
1341 self.send_message(msg);
1342 }
1343
1344 pub fn stop_capture_sequence(&self) {
1346 let msg = ApiMsg::DebugCommand(DebugCommand::StopCaptureSequence);
1347 self.send_message(msg);
1348 }
1349
1350 pub fn send_debug_cmd(&mut self, cmd: DebugCommand) {
1352 if let DebugCommand::EnableMultithreading(enable) = cmd {
1353 self.resources.enable_multithreading(enable);
1355 }
1356 let msg = ApiMsg::DebugCommand(cmd);
1357 self.send_message(msg);
1358 }
1359}
1360
1361impl Drop for RenderApi {
1362 fn drop(&mut self) {
1363 let msg = SceneBuilderRequest::ClearNamespace(self.namespace_id);
1364 let _ = self.low_priority_scene_sender.send(msg);
1365 }
1366}
1367
1368
1369fn window_size_sanity_check(size: DeviceIntSize) {
1370 use crate::render_task::MAX_RENDER_TASK_SIZE;
1373 if size.width > MAX_RENDER_TASK_SIZE || size.height > MAX_RENDER_TASK_SIZE {
1374 panic!("Attempting to create a {}x{} window/document", size.width, size.height);
1375 }
1376}
1377
1378#[repr(C)]
1382#[allow(missing_docs)]
1383#[derive(AddAssign, Clone, Debug, Default)]
1384pub struct MemoryReport {
1385 pub clip_stores: usize,
1389 pub gpu_cache_metadata: usize,
1390 pub gpu_cache_cpu_mirror: usize,
1391 pub render_tasks: usize,
1392 pub hit_testers: usize,
1393 pub fonts: usize,
1394 pub weak_fonts: usize,
1395 pub images: usize,
1396 pub rasterized_blobs: usize,
1397 pub shader_cache: usize,
1398 pub interning: InterningMemoryReport,
1399 pub display_list: usize,
1400 pub upload_staging_memory: usize,
1401 pub swgl: usize,
1402
1403 pub gpu_cache_textures: usize,
1407 pub vertex_data_textures: usize,
1408 pub render_target_textures: usize,
1409 pub picture_tile_textures: usize,
1410 pub atlas_textures: usize,
1411 pub standalone_textures: usize,
1412 pub texture_cache_structures: usize,
1413 pub depth_target_textures: usize,
1414 pub texture_upload_pbos: usize,
1415 pub swap_chain: usize,
1416 pub render_texture_hosts: usize,
1417 pub upload_staging_textures: usize,
1418}