File: _osmesa.py

package info (click to toggle)
python-vispy 0.6.6-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 21,344 kB
  • sloc: python: 57,412; javascript: 6,810; makefile: 63; sh: 5
file content (237 lines) | stat: -rw-r--r-- 7,645 bytes parent folder | download | duplicates (2)
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
# -*- coding: utf-8 -*-
# vispy: testskip
# Copyright (c) Vispy Development Team. All Rights Reserved.
# Distributed under the (new) BSD License. See LICENSE.txt for more info.
"""
OSMesa backend for offscreen rendering on Linux/Unix
"""
from __future__ import division
from ...util.ptime import time
from ..base import (BaseApplicationBackend, BaseCanvasBackend,
                    BaseTimerBackend)
from ...gloo import gl
from time import sleep

try:
    from ...ext import osmesa
except Exception as exp:
    available, testable, why_not, which = False, False, str(exp), None
else:
    available, testable, why_not, which = True, True, None, 'OSMesa'

# -------------------------------------------------------------- capability ---
capability = dict(
    # if True they mean:
    title=True,          # can set title on the fly
    size=True,           # can set size on the fly
    position=False,       # can set position on the fly
    show=True,           # can show/hide window XXX ?
    vsync=False,          # can set window to sync to blank
    resizable=False,      # can toggle resizability (e.g., no user resizing)
    decorate=True,       # can toggle decorations
    fullscreen=False,     # fullscreen window support
    context=True,        # can share contexts between windows
    multi_window=True,   # can use multiple windows at once
    scroll=False,         # scroll-wheel events are supported
    parent=False,         # can pass native widget backend parent
    always_on_top=False,  # can be made always-on-top
)


_VP_OSMESA_ALL_WINDOWS = []


def _get_osmesa_windows():
    return [win for win in _VP_OSMESA_ALL_WINDOWS
            if isinstance(win, CanvasBackend)]


# ------------------------------------------------------------- application ---
class ApplicationBackend(BaseApplicationBackend):

    def __init__(self):
        BaseApplicationBackend.__init__(self)
        self._timers = list()

    def _add_timer(self, timer):
        if timer not in self._timers:
            self._timers.append(timer)

    def _vispy_get_backend_name(self):
        return 'osmesa'

    def _vispy_process_events(self):
        for timer in self._timers:
            timer._tick()
        wins = _get_osmesa_windows()
        for win in wins:
            if win._needs_draw:
                win._needs_draw = False
                win._on_draw()

    def _vispy_run(self):
        wins = _get_osmesa_windows()
        while not all(w.closed for w in wins):
            self._vispy_process_events()
        self._vispy_quit()

    def _vispy_quit(self):
        wins = _get_osmesa_windows()
        for win in wins:
            win._vispy_close()
        for timer in self._timers:
            timer._vispy_stop()
        self._timers = []

    def _vispy_get_native_app(self):
        return osmesa


class OSMesaContext(object):
    """
    A wrapper around an OSMesa context that destroy the context when
    garbage collected
    """
    def __init__(self):
        self.context = osmesa.OSMesaCreateContext()

    def make_current(self, pixels, width, height):
        return osmesa.OSMesaMakeCurrent(self.context, pixels, width, height)

    def __del__(self):
        osmesa.OSMesaDestroyContext(self.context)


# ------------------------------------------------------------------ canvas ---
class CanvasBackend(BaseCanvasBackend):
    """OSMesa backend for Canvas"""

    # args are for BaseCanvasBackend, kwargs are for us.
    def __init__(self, *args, **kwargs):
        BaseCanvasBackend.__init__(self, *args)
        # We use _process_backend_kwargs() to "serialize" the kwargs
        # and to check whether they match this backend's capability
        p = self._process_backend_kwargs(kwargs)

        # Deal with config
        # TODO: We do not support setting config
        # ... use context.config
        # Deal with context
        p.context.shared.add_ref('osmesa', self)
        if p.context.shared.ref is self:
            self._native_context = OSMesaContext()
        else:
            self._native_context = p.context.shared.ref._native_context

        self._closed = False
        self._pixels = None
        self._vispy_set_size(*p.size)
        _VP_OSMESA_ALL_WINDOWS.append(self)

        self._vispy_canvas.set_current()
        self._vispy_canvas.events.initialize()

    def _vispy_set_current(self):
        if self._native_context is None:
            raise RuntimeError('Native context is None')
        if self._pixels is None:
            raise RuntimeError('Pixel buffer has already been deleted')

        ok = self._native_context.make_current(self._pixels, self._size[0],
                                               self._size[1])
        if not ok:
            raise RuntimeError('Failed attaching OSMesa rendering buffer')

    def _vispy_swap_buffers(self):
        if self._pixels is None:
            raise RuntimeError('No pixel buffer')
        gl.glFinish()

    def _vispy_set_title(self, title):
        pass

    def _vispy_set_size(self, w, h):
        self._pixels = osmesa.allocate_pixels_buffer(w, h)
        self._size = (w, h)
        self._vispy_canvas.events.resize(size=(w, h))
        self._vispy_set_current()
        self._vispy_update()

    def _vispy_set_position(self, x, y):
        pass

    def _vispy_set_visible(self, visible):
        if visible:
            self._vispy_set_current()
            self._vispy_update()

    def _vispy_set_fullscreen(self, fullscreen):
        pass

    def _vispy_update(self):
        # This is checked by osmesa ApplicationBackend in process_events
        self._needs_draw = True

    def _vispy_close(self):
        if self.closed:
            return
        # We do not set self._native_context = None here because this causes
        # trouble in case a canvas is closed multiple times (as in
        # app.test_run()). The problem occurs in gloo's glir._gl_initialize
        # when it tries to call glGetString(GL_VERSION).
        # But OSMesa requires a context to be attached when calling
        # glGetString otherwise it returns an empty string, which gloo doesn't
        # like
        self._closed = True
        return

    def _vispy_warmup(self):
        etime = time() + 0.1
        while time() < etime:
            sleep(0.01)
            self._vispy_canvas.set_current()
            self._vispy_canvas.app.process_events()

    def _vispy_get_size(self):
        if self._pixels is None:
            return
        return self._size

    @property
    def closed(self):
        return self._closed

    def _vispy_get_position(self):
        return 0, 0

    def _vispy_get_fullscreen(self):
        return False

    def _on_draw(self):
        # This is called by the osmesa ApplicationBackend
        if self._vispy_canvas is None or self._pixels is None:
            raise RuntimeError('draw with no canvas or pixels attached')
            return
        self._vispy_set_current()
        self._vispy_canvas.events.draw(region=None)  # (0, 0, w, h)


# ------------------------------------------------------------------- timer ---
class TimerBackend(BaseTimerBackend):

    def __init__(self, vispy_timer):
        BaseTimerBackend.__init__(self, vispy_timer)
        vispy_timer._app._backend._add_timer(self)
        self._vispy_stop()

    def _vispy_start(self, interval):
        self._interval = interval
        self._next_time = time() + self._interval

    def _vispy_stop(self):
        self._next_time = float('inf')

    def _tick(self):
        if time() > self._next_time:
            self._vispy_timer._timeout()
            self._next_time = time() + self._interval