debug_commands.rs (7697B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 5 use crate::command::{Command, CommandList, CommandDescriptor}; 6 use crate::command::{CommandContext, CommandOutput}; 7 use webrender_api::DebugFlags; 8 use webrender_api::debugger::DebuggerTextureContent; 9 10 // Implementation of a basic set of debug commands to demonstrate functionality 11 12 // Register the debug commands in this source file 13 pub fn register(cmd_list: &mut CommandList) { 14 cmd_list.register_command(Box::new(PingCommand)); 15 cmd_list.register_command(Box::new(GenerateFrameCommand)); 16 cmd_list.register_command(Box::new(ToggleProfilerCommand)); 17 cmd_list.register_command(Box::new(GetSpatialTreeCommand)); 18 cmd_list.register_command(Box::new(GetCompositeConfigCommand)); 19 cmd_list.register_command(Box::new(GetCompositeViewCommand)); 20 cmd_list.register_command(Box::new(GetTexturesCommand { kind: None })); 21 cmd_list.register_command(Box::new(GetTexturesCommand { kind: Some("atlas") })); 22 cmd_list.register_command(Box::new(GetTexturesCommand { kind: Some("standalone") })); 23 cmd_list.register_command(Box::new(GetTexturesCommand { kind: Some("render-target") })); 24 cmd_list.register_command(Box::new(GetTexturesCommand { kind: Some("tile") })); 25 } 26 27 struct PingCommand; 28 struct GenerateFrameCommand; 29 struct ToggleProfilerCommand; 30 struct GetSpatialTreeCommand; 31 struct GetCompositeConfigCommand; 32 struct GetCompositeViewCommand; 33 struct GetTexturesCommand { kind: Option<&'static str> } 34 35 impl Command for PingCommand { 36 fn descriptor(&self) -> CommandDescriptor { 37 CommandDescriptor { 38 name: "ping", 39 help: "Test connection to specified host", 40 ..Default::default() 41 } 42 } 43 44 fn run( 45 &mut self, 46 ctx: &mut CommandContext, 47 ) -> CommandOutput { 48 match ctx.net.get("ping") { 49 Ok(output) => { 50 CommandOutput::Log(output.expect("empty response")) 51 } 52 Err(err) => { 53 CommandOutput::Err(err) 54 } 55 } 56 } 57 } 58 59 impl Command for GenerateFrameCommand { 60 fn descriptor(&self) -> CommandDescriptor { 61 CommandDescriptor { 62 name: "generate-frame", 63 help: "Generate and render one frame", 64 alias: Some("f"), 65 ..Default::default() 66 } 67 } 68 69 fn run( 70 &mut self, 71 ctx: &mut CommandContext, 72 ) -> CommandOutput { 73 match ctx.net.post("generate-frame") { 74 Ok(..) => { 75 CommandOutput::Log("ok".to_string()) 76 } 77 Err(err) => { 78 CommandOutput::Err(err) 79 } 80 } 81 } 82 } 83 84 impl Command for ToggleProfilerCommand { 85 fn descriptor(&self) -> CommandDescriptor { 86 CommandDescriptor { 87 name: "toggle-profiler", 88 help: "Toggle the on-screen profiler overlay", 89 alias: Some("p"), 90 ..Default::default() 91 } 92 } 93 94 fn run( 95 &mut self, 96 ctx: &mut CommandContext, 97 ) -> CommandOutput { 98 match ctx.net.get("debug-flags") { 99 Ok(flags_string) => { 100 let mut flags: DebugFlags = serde_json::from_str(flags_string.as_ref().unwrap()).unwrap(); 101 flags ^= DebugFlags::PROFILER_DBG; 102 103 match ctx.net.post_with_content("debug-flags", &flags) { 104 Ok(output) => { 105 CommandOutput::Log(output.expect("empty response")) 106 } 107 Err(err) => { 108 CommandOutput::Err(err) 109 } 110 } 111 } 112 Err(err) => { 113 CommandOutput::Err(err) 114 } 115 } 116 } 117 } 118 119 impl Command for GetSpatialTreeCommand { 120 fn descriptor(&self) -> CommandDescriptor { 121 CommandDescriptor { 122 name: "get-spatial-tree", 123 help: "Print the current spatial tree to console", 124 ..Default::default() 125 } 126 } 127 128 fn run( 129 &mut self, 130 ctx: &mut CommandContext, 131 ) -> CommandOutput { 132 match ctx.net.get_with_query( 133 "query", 134 &[("type", "spatial-tree")], 135 ) { 136 Ok(output) => { 137 CommandOutput::TextDocument { 138 title: "Spatial Tree".to_string(), 139 content: output.expect("empty response"), 140 } 141 } 142 Err(err) => { 143 CommandOutput::Err(err) 144 } 145 } 146 } 147 } 148 149 impl Command for GetCompositeConfigCommand { 150 fn descriptor(&self) -> CommandDescriptor { 151 CommandDescriptor { 152 name: "get-composite-cfg", 153 help: "Print the current compositing config to the console", 154 ..Default::default() 155 } 156 } 157 158 fn run( 159 &mut self, 160 ctx: &mut CommandContext, 161 ) -> CommandOutput { 162 match ctx.net.get_with_query( 163 "query", 164 &[("type", "composite-config")], 165 ) { 166 Ok(output) => { 167 CommandOutput::TextDocument { 168 title: "Composite Config".into(), 169 content: output.expect("empty response"), 170 } 171 } 172 Err(err) => { 173 CommandOutput::Err(err) 174 } 175 } 176 } 177 } 178 179 impl Command for GetCompositeViewCommand { 180 fn descriptor(&self) -> CommandDescriptor { 181 CommandDescriptor { 182 name: "get-composite-view", 183 help: "Print the current compositing config to the console", 184 ..Default::default() 185 } 186 } 187 188 fn run( 189 &mut self, 190 ctx: &mut CommandContext, 191 ) -> CommandOutput { 192 match ctx.net.get_with_query( 193 "query", 194 &[("type", "composite-view")], 195 ) { 196 Ok(output) => { 197 CommandOutput::SerdeDocument { 198 kind: "composite-view".into(), 199 content: output.expect("empty response"), 200 } 201 } 202 Err(err) => { 203 CommandOutput::Err(err) 204 } 205 } 206 } 207 } 208 209 impl Command for GetTexturesCommand { 210 fn descriptor(&self) -> CommandDescriptor { 211 CommandDescriptor { 212 name: match self.kind { 213 Some("atlas") => "get-atlas-textures", 214 Some("standalone") => "get-standalone-textures", 215 Some("render-target") => "get-target-textures", 216 Some("tile") => "get-tile-textures", 217 _ => "get-textures", 218 }, 219 help: "Fetch all gpu textures", 220 ..Default::default() 221 } 222 } 223 224 fn run( 225 &mut self, 226 ctx: &mut CommandContext, 227 ) -> CommandOutput { 228 let kind = match self.kind { 229 Some("atlas") => "atlas-textures", 230 Some("standalone") => "standalone-textures", 231 Some("render-target") => "target-textures", 232 Some("tile") => "tile-textures", 233 _ => "textures", 234 }; 235 match ctx.net.get_with_query( 236 "query", 237 &[("type", kind)], 238 ) { 239 Ok(output) => { 240 let mut textures: Vec<DebuggerTextureContent> = serde_json::from_str( 241 output.unwrap().as_str() 242 ).unwrap(); 243 textures.sort_by(|a, b| a.name.cmp(&b.name)); 244 CommandOutput::Textures(textures) 245 } 246 Err(err) => { 247 CommandOutput::Err(err) 248 } 249 } 250 } 251 }