File: debug.py

package info (click to toggle)
pyopengl 3.1.6%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 14,732 kB
  • sloc: python: 106,016; makefile: 8
file content (245 lines) | stat: -rw-r--r-- 7,524 bytes parent folder | download
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
"""Debug utilities for EGL operations"""
from OpenGL.EGL import *
import itertools


def eglErrorName(value):
    """Returns error constant if known, otherwise returns value"""
    return KNOWN_ERRORS.get(value, value)


KNOWN_ERRORS = {
    EGL_SUCCESS: EGL_SUCCESS,
    EGL_NOT_INITIALIZED: EGL_NOT_INITIALIZED,
    EGL_BAD_ACCESS: EGL_BAD_ACCESS,
    EGL_BAD_ALLOC: EGL_BAD_ALLOC,
    EGL_BAD_ATTRIBUTE: EGL_BAD_ATTRIBUTE,
    EGL_BAD_CONTEXT: EGL_BAD_CONTEXT,
    EGL_BAD_CONFIG: EGL_BAD_CONFIG,
    EGL_BAD_CURRENT_SURFACE: EGL_BAD_CURRENT_SURFACE,
    EGL_BAD_DISPLAY: EGL_BAD_DISPLAY,
    EGL_BAD_SURFACE: EGL_BAD_SURFACE,
    EGL_BAD_MATCH: EGL_BAD_MATCH,
    EGL_BAD_PARAMETER: EGL_BAD_PARAMETER,
    EGL_BAD_NATIVE_PIXMAP: EGL_BAD_NATIVE_PIXMAP,
    EGL_BAD_NATIVE_WINDOW: EGL_BAD_NATIVE_WINDOW,
    EGL_CONTEXT_LOST: EGL_CONTEXT_LOST,
}


def write_ppm(buf, filename):
    """Write height * width * 3-component buffer as ppm to filename
    
    This lets us write a simple image format without
    using any libraries that can be viewed on most
    linux workstations.
    """
    with open(filename, "w") as f:
        (h, w, c) = buf.shape
        f.write("P3\n")
        f.write("# ascii ppm file created by pyopengl\n")
        f.write("%i %i\n" % (w, h))
        f.write("255\n")
        for y in range(h - 1, -1, -1):
            for x in range(w):
                pixel = buf[y, x]
                l = " %3d %3d %3d" % (pixel[0], pixel[1], pixel[2])
                f.write(l)
            f.write("\n")


def debug_config(display, config):
    """Get debug display for the given configuration"""
    result = {}
    value = EGLint()
    for attr in CONFIG_ATTRS:
        if not eglGetConfigAttrib(display, config, attr, value):
            log.warning("Failed to get attribute %s from config", attr)
            continue
        if attr in BITMASK_FIELDS:
            attr_value = {}
            for subattr in BITMASK_FIELDS[attr]:
                if value.value & subattr:
                    attr_value[subattr.name] = True
        else:
            attr_value = value.value
        result[attr.name] = attr_value
    return result


def debug_configs(display, configs=None, max_count=256):
    """Present a formatted list of configs for the display"""
    if configs is None:
        configs = (EGLConfig * max_count)()
        num_configs = EGLint()
        eglGetConfigs(display, configs, max_count, num_configs)
        if not num_configs.value:
            return []
        configs = configs[: num_configs.value]
    debug_configs = [debug_config(display, cfg) for cfg in configs]
    return debug_configs


SURFACE_TYPE_BITS = [
    EGL_MULTISAMPLE_RESOLVE_BOX_BIT,
    EGL_PBUFFER_BIT,
    EGL_PIXMAP_BIT,
    EGL_SWAP_BEHAVIOR_PRESERVED_BIT,
    EGL_VG_ALPHA_FORMAT_PRE_BIT,
    EGL_VG_COLORSPACE_LINEAR_BIT,
    EGL_WINDOW_BIT,
]
RENDERABLE_TYPE_BITS = [
    EGL_OPENGL_BIT,
    EGL_OPENGL_ES_BIT,
    EGL_OPENGL_ES2_BIT,
    EGL_OPENGL_ES3_BIT,
    EGL_OPENVG_BIT,
]
CAVEAT_BITS = [
    EGL_NONE,
    EGL_SLOW_CONFIG,
    EGL_NON_CONFORMANT_CONFIG,
]
TRANSPARENT_BITS = [
    EGL_NONE,
    EGL_TRANSPARENT_RGB,
]

CONFIG_ATTRS = [
    EGL_CONFIG_ID,
    EGL_RED_SIZE,
    EGL_GREEN_SIZE,
    EGL_BLUE_SIZE,
    EGL_DEPTH_SIZE,
    EGL_ALPHA_SIZE,
    EGL_ALPHA_MASK_SIZE,
    EGL_BUFFER_SIZE,
    EGL_STENCIL_SIZE,
    EGL_BIND_TO_TEXTURE_RGB,
    EGL_BIND_TO_TEXTURE_RGBA,
    EGL_COLOR_BUFFER_TYPE,
    EGL_CONFIG_CAVEAT,
    EGL_CONFORMANT,
    EGL_LEVEL,
    EGL_LUMINANCE_SIZE,
    EGL_MAX_PBUFFER_WIDTH,
    EGL_MAX_PBUFFER_HEIGHT,
    EGL_MAX_PBUFFER_PIXELS,
    EGL_MIN_SWAP_INTERVAL,
    EGL_MAX_SWAP_INTERVAL,
    EGL_NATIVE_RENDERABLE,
    EGL_NATIVE_VISUAL_ID,
    EGL_NATIVE_VISUAL_TYPE,
    EGL_RENDERABLE_TYPE,
    EGL_SAMPLE_BUFFERS,
    EGL_SAMPLES,
    EGL_SURFACE_TYPE,
    EGL_TRANSPARENT_TYPE,
    EGL_TRANSPARENT_RED_VALUE,
    EGL_TRANSPARENT_GREEN_VALUE,
    EGL_TRANSPARENT_BLUE_VALUE,
]

BITMASK_FIELDS = dict(
    [
        (EGL_SURFACE_TYPE, SURFACE_TYPE_BITS),
        (EGL_RENDERABLE_TYPE, RENDERABLE_TYPE_BITS),
        (EGL_CONFORMANT, RENDERABLE_TYPE_BITS),
        (EGL_CONFIG_CAVEAT, CAVEAT_BITS),
        (EGL_TRANSPARENT_TYPE, TRANSPARENT_BITS),
    ]
)


def bit_renderer(bit):
    def render(value):
        if bit.name in value:
            return " Y"
        else:
            return " ."

    return render


CONFIG_FORMAT = [
    (EGL_CONFIG_ID, "0x%x", "id", "cfg"),
    (EGL_BUFFER_SIZE, "%i", "sz", "bf"),
    (EGL_LEVEL, "%i", "l", "lv"),
    (EGL_RED_SIZE, "%i", "r", "cbuf"),
    (EGL_GREEN_SIZE, "%i", "g", "cbuf"),
    (EGL_BLUE_SIZE, "%i", "b", "cbuf"),
    (EGL_ALPHA_SIZE, "%i", "a", "cbuf"),
    (EGL_DEPTH_SIZE, "%i", "th", "dp"),
    (EGL_STENCIL_SIZE, "%i", "t", "s"),
    (EGL_SAMPLES, "%i", "ns", "mult"),
    (EGL_SAMPLE_BUFFERS, "%i", "bu", "mult"),
    (EGL_NATIVE_VISUAL_ID, "0x%x", "id", "visual"),
    (EGL_RENDERABLE_TYPE, bit_renderer(EGL_OPENGL_BIT), "gl", "render"),
    (EGL_RENDERABLE_TYPE, bit_renderer(EGL_OPENGL_ES_BIT), "es", "render"),
    (EGL_RENDERABLE_TYPE, bit_renderer(EGL_OPENGL_ES2_BIT), "e2", "render"),
    (EGL_RENDERABLE_TYPE, bit_renderer(EGL_OPENGL_ES3_BIT), "e3", "render"),
    (EGL_RENDERABLE_TYPE, bit_renderer(EGL_OPENVG_BIT), "vg", "render"),
    (EGL_SURFACE_TYPE, bit_renderer(EGL_WINDOW_BIT), "wn", "surface"),
    (EGL_SURFACE_TYPE, bit_renderer(EGL_PBUFFER_BIT), "pb", "surface"),
    (EGL_SURFACE_TYPE, bit_renderer(EGL_PIXMAP_BIT), "px", "surface"),
]


def format_debug_configs(debug_configs, formats=CONFIG_FORMAT):
    """Format config for compact debugging display
    
    Produces a config summary display for a set of 
    debug_configs as a text-mode table.

    Uses `formats` (default `CONFIG_FORMAT`) to determine 
    which fields are extracted and how they are formatted
    along with the column/subcolum set to be rendered in
    the overall header.

    returns formatted ASCII table for display in debug
    logs or utilities
    """
    columns = []
    for (key, format, subcol, col) in formats:
        column = []
        max_width = 0
        for row in debug_configs:
            if isinstance(row, EGLConfig):
                raise TypeError(row, "Call debug_config(display,config)")
            try:
                value = row[key.name]
            except KeyError:
                formatted = "_"
            else:
                if isinstance(format, str):
                    formatted = format % (value)
                else:
                    formatted = format(value)
            max_width = max((len(formatted), max_width))
            column.append(formatted)
        columns.append(
            {
                "rows": column,
                "key": key,
                "format": format,
                "subcol": subcol,
                "col": col,
                "width": max_width,
            }
        )
    headers = []
    subheaders = []
    rows = [headers, subheaders]
    last_column = None
    last_column_width = 0
    for header, subcols in itertools.groupby(columns, lambda x: x["col"]):
        subcols = list(subcols)
        width = sum([col["width"] for col in subcols]) + (len(subcols) - 1)
        headers.append(header.center(width, ".")[:width])
    for column in columns:
        subheaders.append(column["subcol"].rjust(column["width"])[: column["width"]])
    rows.extend(
        zip(*[[v.rjust(col["width"], " ") for v in col["rows"]] for col in columns])
    )
    return "\n".join([" ".join(row) for row in rows])