Commit 073efd90 authored by Paul McCarthy's avatar Paul McCarthy 🚵
Browse files

RF: Ortho SliceCanvases now share GLObjects using the gl.resources module

parent 216d3233
......@@ -97,13 +97,13 @@ def get(key, createFunc=None, *args, **kwargs):
r = _resources.get(key, None)
if r is None and createFunc is None:
raise KeyError('Resource {} does not exist'.format(str(key)))
raise KeyError(f'Resource {key} does not exist')
if r is not None:
r.refcount += 1
log.debug('Resource {} reference count '
'increased to {}'.format(str(key), r.refcount))
log.debug('Resource %s reference count '
'increased to %s', str(key), r.refcount)
return r.resource
......@@ -126,7 +126,7 @@ def set(key, resource, overwrite=False):
"""
if (not overwrite) and (key in _resources):
raise KeyError('Resource {} already exists'.format(str(key)))
raise KeyError(f'Resource {key} already exists')
if not overwrite:
log.debug('Adding resource {}'.format(str(key)))
......@@ -135,11 +135,11 @@ def set(key, resource, overwrite=False):
r.refcount += 1
_resources[key] = r
log.debug('Resource {} reference count '
'increased to {}'.format(str(key), r.refcount))
log.debug('Resource %s reference count '
'increased to %s', str(key), r.refcount)
else:
log.debug('Updating resource {}'.format(str(key)))
log.debug('Updating resource %s', str(key))
_resources[key].resource = resource
......@@ -157,18 +157,18 @@ def delete(key):
r = _resources[key]
r.refcount -= 1
log.debug('Resource {} reference count '
'decreased to {}'.format(str(key), r.refcount))
log.debug('Resource %s reference count '
'decreased to %s', str(key), r.refcount)
if r.refcount <= 0:
log.debug('Destroying resource {}'.format(str(key)))
log.debug('Destroying resource %s', str(key))
_resources.pop(key)
r.resource.destroy()
class _Resource(object):
class _Resource:
"""Internal type which is used to encapsulate a resource, and the
number of active references to that resources. The following attributes
are available on a ``_Resource``:
......
......@@ -24,6 +24,7 @@ import fsleyes_props as props
import fsleyes.strings as strings
import fsleyes.displaycontext.canvasopts as canvasopts
import fsleyes.gl.routines as glroutines
import fsleyes.gl.resources as glresources
import fsleyes.gl.globject as globject
import fsleyes.gl.annotations as annotations
......@@ -66,6 +67,10 @@ class SliceCanvas:
the overlay, and creates a new one.
``GLObject`` instances may be shared amongst multiple ``SliceCanvas``
instances - this is achieved with the :mod:`.resources` module.
The ``SliceCanvas`` also uses an :class:`.Annotations` instance, for
drawing simple annotations on top of the overlays. This ``Annotations``
instance can be accessed with the :meth:`getAnnotations` method.
......@@ -213,7 +218,7 @@ class SliceCanvas:
# be False - see genGLObject.
if globj:
globj.deregister(self.name)
globj.destroy()
glresources.delete(self.globjectId(overlay))
self._annotations.destroy()
......@@ -455,6 +460,16 @@ class SliceCanvas:
return self._annotations
def globjectId(self, overlay):
"""Returns a key that can be used to uniquely identify a
:class:`.GLObject` for the given overlay. ``GLObject`` instances may be
shared between different ``SliceCanvas`` instances (specifically, the
three canvases of an :class:`.OrthoPanel`) using the :mod:`.resources`
module, using this key.
"""
return ('GLObject', id(self.displayCtx), overlay)
def getGLObject(self, overlay):
"""Returns the :class:`.GLObject` associated with the given
``overlay``, or ``None`` if there isn't one.
......@@ -543,10 +558,11 @@ class SliceCanvas:
# Tell the previous GLObject (if
# any) to clean up after itself
globj = self._glObjects.pop(overlay, None)
if globj:
globj.deregister(self.name)
globj.destroy()
glresources.delete(self.globjectId(overlay))
self.__genGLObject(overlay, refresh)
......@@ -602,10 +618,12 @@ class SliceCanvas:
self._glObjects.pop(overlay)
return
globj = globject.createGLObject(overlay,
self.overlayList,
self.displayCtx,
False)
globj = glresources.get(self.globjectId(overlay),
globject.createGLObject,
overlay,
self.overlayList,
self.displayCtx,
False)
if globj is not None:
globj.register(self.name, self.__onGLObjectUpdate)
......@@ -663,7 +681,7 @@ class SliceCanvas:
if ovl not in self.overlayList:
self._glObjects.pop(ovl)
if globj:
globj.destroy()
glresources.delete(self.globjectId(ovl))
# Create a GL object for any new overlays,
# and attach a listener to their display
......@@ -713,7 +731,8 @@ class SliceCanvas:
# GLObject, we presume that it hasn't been created
# yet, so we'll tell genGLObject to create one for
# it.
if globj is None: self.__genGLObject(ovl)
if globj is None:
self.__genGLObject(ovl)
# If there is a value for this overlay in
# the globjects dictionary, but it evaluates
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment