summaryrefslogtreecommitdiff
path: root/framework/wflinfo.py
blob: d3a79437d07eae320174655464571dc6bcb6ecbe (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
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
# Copyright (c) 2015-2016 Intel Corporation

# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:

# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.

# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

from __future__ import (
    absolute_import, division, print_function, unicode_literals
)
import errno
import os
import subprocess
import sys
import threading

import six

from framework import exceptions
from framework.core import lazy_property
from framework.options import OPTIONS
# from framework.test import piglit_test


class StopWflinfo(exceptions.PiglitException):
    """Exception called when wlfinfo getter should stop."""
    def __init__(self, reason):
        super(StopWflinfo, self).__init__()
        self.reason = reason


class ProfileInfo(object):
    """Information about a single profile (core, compat, es1, es2, etc)."""

    def __init__(self, shader_version, language_version, extensions):
        self.shader_version = shader_version
        self.api_version = language_version
        self.extensions = extensions


class WflInfo(object):
    """Class representing platform information as provided by wflinfo.

    The design of this is odd to say the least, it's basically a bag with some
    lazy property evaluators in it, used to avoid calculating the values
    provided by wflinfo more than once.

    The problems:
    - Needs to be shared with all subclasses
    - Needs to evaluate only once
    - cannot evaluate until user sets OPTIONS.env['PIGLIT_PLATFORM']

    This solves all of that.

    """
    __shared_state = {}
    __core_init = False
    __core_lock = threading.Lock()
    __compat_init = False
    __compat_lock = threading.Lock()
    __es1_init = False
    __es1_lock = threading.Lock()
    __es2_init = False
    __es2_lock = threading.Lock()

    def __new__(cls, *args, **kwargs):
        # Implement the borg pattern:
        # https://code.activestate.com/recipes/66531-singleton-we-dont-need-no-stinkin-singleton-the-bo/
        #
        # This is something like a singleton, but much easier to implement
        self = super(WflInfo, cls).__new__(cls, *args, **kwargs)
        self.__dict__ = cls.__shared_state
        return self

    @staticmethod
    def __call_wflinfo(opts):
        """Helper to call wflinfo and reduce code duplication.

        This catches and handles CalledProcessError and OSError.ernno == 2
        gracefully: it passes them to allow platforms without a particular
        gl/gles version or wflinfo (resepctively) to work.

        Arguments:
        opts -- arguments to pass to wflinfo other than verbose and platform

        """
        with open(os.devnull, 'w') as d:
            try:
                # Get the piglit platform string and, if needed, convert it
                # to something that wflinfo understands.
                platform = OPTIONS.env['PIGLIT_PLATFORM']
                if platform == "mixed_glx_egl":
                    platform = "glx"

                if sys.platform in ['windows', 'cygwin']:
                    bin = 'wflinfo.exe'
                else:
                    bin = 'wflinfo'

                cmd = [bin, '--platform', platform] + opts

                # setup execution environment where we extend the PATH env var
                # to include the piglit TEST_BIN_DIR
                new_env = os.environ.copy()
                # new_env['PATH'] = ':'.join([piglit_test.TEST_BIN_DIR,
                                            # os.environ['PATH']])

                raw = subprocess.check_output(cmd, env=new_env, stderr=d)

            except subprocess.CalledProcessError:
                # When we hit this error it usually going to be because we have
                # an incompatible platform/profile combination
                raise StopWflinfo('Called')
            except OSError as e:
                # If we get a 'no wflinfo' warning then just return
                print("wflinfo utility not found.", file=sys.stderr)
                if e.errno == errno.ENOENT:
                    raise StopWflinfo('OSError')
                raise
        return raw.decode('utf-8')

    @staticmethod
    def __getline(lines, name):
        """Find a line in a list return it."""
        for line in lines:
            if line.startswith(name):
                return line
        raise Exception('Unreachable')

    def __get_shader_version(self, profile):
        """Calculate the maximum OpenGL Shader Language version."""
        ret = 0.0
        if profile in ['core', 'compat', 'none']:
            try:
                raw = self.__call_wflinfo(
                    ['--verbose', '--api', 'gl', '--profile', profile])
            except StopWflinfo as e:
                if e.reason not in ['Called', 'OSError']:
                    raise
            else:
                try:
                    # GLSL versions are M.mm formatted
                    line = self.__getline(raw.split('\n'), 'OpenGL shading language')
                    ret = float(line.split(":")[1][:5])
                except (IndexError, ValueError):
                    # This is caused by wflinfo returning an error
                    pass
        elif profile in ['gles2', 'gles3']:
            try:
                raw = self.__call_wflinfo(['--verbose', '--api', profile])
            except StopWflinfo as e:
                if e.reason not in ['Called', 'OSError']:
                    raise
            else:
                try:
                    # GLSL ES version numbering is insane.
                    # For version >= 3 the numbers are 3.00, 3.10, etc.
                    # For version 2, they are 1.0.xx
                    ret = float(self.__getline(
                        raw.split('\n'),
                        'OpenGL shading language').split()[-1][:3])
                except (IndexError, ValueError):
                    # Handle wflinfo internal errors
                    pass
        return ret

    def __get_language_version(self, profile):
        ret = 0.0
        if profile in ['core', 'compat', 'none']:
            try:
                raw = self.__call_wflinfo(['--api', 'gl', '--profile', profile])
            except StopWflinfo as e:
                if e.reason not in ['Called', 'OSError']:
                    raise
            else:
                try:
                    # Grab the GL version string, trim any release_number values
                    ret = float(self.__getline(
                        raw.split('\n'),
                        'OpenGL version string').split()[3][:3])
                except (IndexError, ValueError):
                    # This is caused by wlfinfo returning an error
                    pass
        else:
            try:
                raw = self.__call_wflinfo(['--api', profile])
            except StopWflinfo as e:
                if e.reason not in ['Called', 'OSError']:
                    raise
            else:
                try:
                    # Yes, search for "OpenGL version string" in GLES
                    # GLES doesn't support patch versions.
                    ret = float(self.__getline(
                        raw.split('\n'),
                        'OpenGL version string').split()[5])
                except (IndexError, ValueError):
                    # This is caused by wlfinfo returning an error
                    pass
        return ret

    def __get_extensions(self, profile):
        """Call wflinfo to get opengl extensions.

        This provides a very conservative set of extensions, it provides every
        extension from gles1, 2 and 3 and from GL both core and compat profile
        as a single set. This may let a few tests execute that will still skip
        manually, but it helps to ensure that this method never skips when it
        shouldn't.

        """
        _trim = len('OpenGL extensions: ')
        all_ = set()

        def helper(args):
            """Helper function to reduce code duplication."""
            # This is a pretty fragile function but it really does help with
            # duplication
            ret = self.__call_wflinfo(args)
            all_.update(set(self.__getline(
                ret.split('\n'), 'OpenGL extensions')[_trim:].split()))

        try:
            if profile in ['core', 'compat', 'none']:
                helper(['--verbose', '--api', 'gl', '--profile', profile])
            else:
                helper(['--verbose', '--api', profile])
        except StopWflinfo as e:
            # Handle wflinfo not being installed by returning an empty set. This
            # will essentially make FastSkipMixin a no-op.
            if e.reason in ['OSError', 'Called']:
                return set()
            raise

        # Don't return a set with only WFLINFO_GL_ERROR.
        ret = {e.strip() for e in all_}
        if ret == {'WFLINFO_GL_ERROR'}:
            return set()
        return ret

    def __build_info(self, profile):
        return ProfileInfo(
            self.__get_shader_version(profile),
            self.__get_language_version(profile),
            self.__get_extensions(profile)
        )

    @lazy_property
    def core(self):
        with self.__core_lock:
            if not self.__core_init:
                self.__core_init = True
                return self.__build_info('core')
        return self.core

    @lazy_property
    def compat(self):
        with self.__compat_lock:
            if not self.__compat_init:
                self.__compat_init = True
                comp = self.__build_info('compat')
                if comp.api_version == 0.0:
                    # In this case there are not compat profiles, try agian
                    # with a "legacy" profile, which could be promoted to
                    # compat
                    return self.__build_info('none')
                return comp
        return self.compat

    @lazy_property
    def es1(self):
        with self.__es1_lock:
            if not self.__es1_init:
                self.__es1_init = True
                return self.__build_info('gles1')
        return self.es1

    @lazy_property
    def es2(self):
        with self.__es2_lock:
            if not self.__es2_init:
                self.__es2_init = True
                return self.__build_info('gles2')
        return self.es2

    @lazy_property
    def es3(self):
        return self.es2

    @property
    def gles_version(self):
        v = max([self.es1.api_version, self.es2.api_version])
        if v == 0.0:
            return None
        return v

    @property
    def gl_version(self):
        v = max([self.core.api_version, self.compat.api_version])
        if v == 0.0:
            return None
        return v

    @property
    def glsl_version(self):
        v = max([self.core.shader_version, self.compat.shader_version])
        if v == 0.0:
            return None
        return v

    @property
    def glsl_es_version(self):
        v = max([self.es1.shader_version, self.es2.shader_version])
        if v == 0.0:
            return None
        return v

    @property
    def gl_extensions(self):
        return set.union(self.es1.extensions, self.es2.extensions,
                         self.core.extensions, self.compat.extensions)