Skip to content

Commit

Permalink
better code:
Browse files Browse the repository at this point in the history
* use a texture pixel format that matches how we paint the screen: only use alpha (RGBA) if we can paint with it, otherwise use plain RGB
* add docstring for "pixel_format"
* better pixel upload format code: constify and clean it up

git-svn-id: https://xpra.org/svn/Xpra/trunk@5261 3bb7dfac-3a0b-4e04-842a-767bc560f471
  • Loading branch information
totaam committed Jan 24, 2014
1 parent 6f38316 commit 752efbf
Showing 1 changed file with 33 additions and 14 deletions.
47 changes: 33 additions & 14 deletions src/xpra/client/gl/gl_window_backing.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,21 @@
from OpenGL.GL.ARB.framebuffer_object import GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, glGenFramebuffers, glBindFramebuffer, glFramebufferTexture2D


PIXEL_FORMAT_TO_CONSTANT = {
"BGR" : GL_BGR,
"RGB" : GL_RGB,
"BGRA" : GL_BGRA,
"BGRA" : GL_BGRA,
"RGBA" : GL_RGBA,
}
CONSTANT_TO_PIXEL_FORMAT = {
GL_BGR : "BGR",
GL_RGB : "RGB",
GL_BGRA : "BGRA",
GL_RGBA : "RGBA",
}


#debugging variables:
GL_DEBUG_OUTPUT = None
GL_DEBUG_OUTPUT_SYNCHRONOUS = None
Expand Down Expand Up @@ -131,11 +146,19 @@ def __init__(self, wid, w, h, has_alpha):
else:
log.warn("failed to enable transparency on screen %s", screen)
self._has_alpha = False
#this is how many bpp we keep in the texture
#(pixels are always stored in 32bpp - but this makes it clearer when we do/don't support alpha)
if self._has_alpha:
self.texture_pixel_format = GL_RGBA
else:
self.texture_pixel_format = GL_RGB
#this is the pixel format we are currently updating the fbo with
#can be: "YUV420P", "YUV422P", "YUV444P", "GBRP" or None when not initialized yet.
self.pixel_format = None
self._backing.show()
self._backing.connect("expose_event", self.gl_expose_event)
self.textures = None # OpenGL texture IDs
self.shaders = None
self.pixel_format = None
self.size = 0, 0
self.texture_size = 0, 0
self.gl_setup = False
Expand Down Expand Up @@ -254,7 +277,7 @@ def gl_init(self):
glBindTexture(GL_TEXTURE_RECTANGLE_ARB, self.textures[TEX_FBO])
# nvidia needs this even though we don't use mipmaps (repeated through this file):
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 0)
glTexImage2D(GL_TEXTURE_RECTANGLE_ARB, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, None)
glTexImage2D(GL_TEXTURE_RECTANGLE_ARB, 0, self.texture_pixel_format, w, h, 0, self.texture_pixel_format, GL_UNSIGNED_BYTE, None)
glBindFramebuffer(GL_FRAMEBUFFER, self.offscreen_fbo)
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_RECTANGLE_ARB, self.textures[TEX_FBO], 0)
glClear(GL_COLOR_BUFFER_BIT)
Expand Down Expand Up @@ -418,28 +441,24 @@ def _do_paint_rgb(self, bpp, img_data, x, y, width, height, rowstride, options,

rgb_format = options.get("rgb_format", None)
self.gl_marker("%s %sbpp update at %d,%d, size %d,%d, stride is %d, row length %d, alignment %d" % (rgb_format, bpp, x, y, width, height, rowstride, row_length, alignment))
# Upload data as temporary RGB texture
#Older clients may not tell us the pixel format, so we must infer it:
if bpp==24:
if rgb_format=="BGR":
pformat = GL_BGR
else:
assert rgb_format in ("RGB", None), "invalid 24-bit format: %s" % rgb_format
pformat = GL_RGB
default_format = "RGB"
else:
assert bpp==32
if rgb_format=="BGRA":
pformat = GL_BGRA
else:
assert rgb_format in ("RGBA", None), "invalid 32-bit format: %s" % rgb_format
pformat = GL_RGBA
default_format = "RGBA"
#convert it to a GL constant:
pformat = PIXEL_FORMAT_TO_CONSTANT.get(rgb_format or default_format)
assert pformat is not None

# Upload data as temporary RGB texture
glBindTexture(GL_TEXTURE_RECTANGLE_ARB, self.textures[TEX_RGB])
glPixelStorei(GL_UNPACK_ROW_LENGTH, row_length)
glPixelStorei(GL_UNPACK_ALIGNMENT, alignment)
glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 0)
glTexImage2D(GL_TEXTURE_RECTANGLE_ARB, 0, GL_RGBA, width, height, 0, pformat, GL_UNSIGNED_BYTE, img_data)
glTexImage2D(GL_TEXTURE_RECTANGLE_ARB, 0, self.texture_pixel_format, width, height, 0, pformat, GL_UNSIGNED_BYTE, img_data)

# Draw textured RGB quad at the right coordinates
glBegin(GL_QUADS)
Expand Down

0 comments on commit 752efbf

Please sign in to comment.