summaryrefslogtreecommitdiff
path: root/lsfg_vk/configuration.py
blob: f5e29810b312891e69760e8f690d021f2b58b7cd (plain)
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
"""
Configuration service for lsfg script management.
"""

import re
from pathlib import Path
from typing import Dict, Any

from .base_service import BaseService
from .constants import LSFG_SCRIPT_TEMPLATE
from .types import ConfigurationResponse, ConfigurationData


class ConfigurationService(BaseService):
    """Service for managing lsfg script configuration"""
    
    def get_config(self) -> ConfigurationResponse:
        """Read current lsfg script configuration
        
        Returns:
            ConfigurationResponse with current configuration or error
        """
        try:
            if not self.lsfg_script_path.exists():
                return {
                    "success": False,
                    "config": None,
                    "message": None,
                    "error": "lsfg script not found"
                }
            
            content = self.lsfg_script_path.read_text()
            config = self._parse_script_content(content)
            
            self.log.info(f"Parsed lsfg config: {config}")
            
            return {
                "success": True,
                "config": config,
                "message": None,
                "error": None
            }
            
        except (OSError, IOError) as e:
            error_msg = f"Error reading lsfg config: {str(e)}"
            self.log.error(error_msg)
            return {
                "success": False,
                "config": None,
                "message": None,
                "error": str(e)
            }
    
    def _parse_script_content(self, content: str) -> ConfigurationData:
        """Parse script content to extract configuration values
        
        Args:
            content: Script file content
            
        Returns:
            ConfigurationData with parsed values
        """
        config: ConfigurationData = {
            "enable_lsfg": False,
            "multiplier": 2,
            "flow_scale": 1.0,
            "hdr": False,
            "perf_mode": False,
            "immediate_mode": False
        }
        
        lines = content.split('\n')
        for line in lines:
            line = line.strip()
            
            # Parse ENABLE_LSFG
            if match := re.match(r'^(#\s*)?export\s+ENABLE_LSFG=(\d+)', line):
                config["enable_lsfg"] = not bool(match.group(1)) and match.group(2) == '1'
            
            # Parse LSFG_MULTIPLIER
            elif match := re.match(r'^export\s+LSFG_MULTIPLIER=(\d+)', line):
                try:
                    config["multiplier"] = int(match.group(1))
                except ValueError:
                    pass
            
            # Parse LSFG_FLOW_SCALE
            elif match := re.match(r'^export\s+LSFG_FLOW_SCALE=([0-9]*\.?[0-9]+)', line):
                try:
                    config["flow_scale"] = float(match.group(1))
                except ValueError:
                    pass
            
            # Parse LSFG_HDR
            elif match := re.match(r'^(#\s*)?export\s+LSFG_HDR=(\d+)', line):
                config["hdr"] = not bool(match.group(1)) and match.group(2) == '1'
            
            # Parse LSFG_PERF_MODE
            elif match := re.match(r'^(#\s*)?export\s+LSFG_PERF_MODE=(\d+)', line):
                config["perf_mode"] = not bool(match.group(1)) and match.group(2) == '1'
            
            # Parse MESA_VK_WSI_PRESENT_MODE
            elif match := re.match(r'^(#\s*)?export\s+MESA_VK_WSI_PRESENT_MODE=([^\s#]+)', line):
                config["immediate_mode"] = not bool(match.group(1)) and match.group(2) == 'immediate'
        
        return config
    
    def update_config(self, enable_lsfg: bool, multiplier: int, flow_scale: float, 
                     hdr: bool, perf_mode: bool, immediate_mode: bool) -> ConfigurationResponse:
        """Update lsfg script configuration
        
        Args:
            enable_lsfg: Whether to enable LSFG
            multiplier: LSFG multiplier value
            flow_scale: LSFG flow scale value
            hdr: Whether to enable HDR
            perf_mode: Whether to enable performance mode
            immediate_mode: Whether to enable immediate present mode (disable vsync)
            
        Returns:
            ConfigurationResponse with success status
        """
        try:
            # Generate script content using template
            script_content = self._generate_script_content(
                enable_lsfg, multiplier, flow_scale, hdr, perf_mode, immediate_mode
            )
            
            # Write the updated script atomically
            self._atomic_write(self.lsfg_script_path, script_content, 0o755)
            
            self.log.info(f"Updated lsfg script configuration: enable={enable_lsfg}, "
                         f"multiplier={multiplier}, flow_scale={flow_scale}, hdr={hdr}, "
                         f"perf_mode={perf_mode}, immediate_mode={immediate_mode}")
            
            return {
                "success": True,
                "config": None,
                "message": "lsfg configuration updated successfully",
                "error": None
            }
            
        except (OSError, IOError) as e:
            error_msg = f"Error updating lsfg config: {str(e)}"
            self.log.error(error_msg)
            return {
                "success": False,
                "config": None,
                "message": None,
                "error": str(e)
            }
    
    def _generate_script_content(self, enable_lsfg: bool, multiplier: int, flow_scale: float,
                               hdr: bool, perf_mode: bool, immediate_mode: bool) -> str:
        """Generate script content from configuration parameters
        
        Args:
            enable_lsfg: Whether to enable LSFG
            multiplier: LSFG multiplier value
            flow_scale: LSFG flow scale value
            hdr: Whether to enable HDR
            perf_mode: Whether to enable performance mode
            immediate_mode: Whether to enable immediate present mode
            
        Returns:
            Generated script content
        """
        return LSFG_SCRIPT_TEMPLATE.format(
            enable_lsfg="export ENABLE_LSFG=1" if enable_lsfg else "# export ENABLE_LSFG=1",
            multiplier=multiplier,
            flow_scale=flow_scale,
            hdr="export LSFG_HDR=1" if hdr else "# export LSFG_HDR=1",
            perf_mode="export LSFG_PERF_MODE=1" if perf_mode else "# export LSFG_PERF_MODE=1",
            immediate_mode="export MESA_VK_WSI_PRESENT_MODE=immediate # - disable vsync" if immediate_mode else "# export MESA_VK_WSI_PRESENT_MODE=immediate # - disable vsync"
        )