Pixmap pixmap;
GLXPixmap glx_pixmap;
unsigned texture;
+ unsigned mask_texture;
+ int use_mask;
} CompositedWindow;
typedef struct CompositedMonitor
GLXFBConfig fbconfig;
GLXWindow glx_window;
GLXContext glx_context;
- unsigned shaders[2];
+ unsigned shaders[3];
unsigned program;
- unsigned geometry_loc;
+ int geometry_loc;
+ unsigned masked_program;
+ int masked_geometry_loc;
unsigned window_vertex_buffer;
unsigned window_vertex_array;
unsigned framebuffer;
CompositedScreen *screens;
unsigned nscreens;
int damage_event;
+ int shape_event;
PFNGLXCREATECONTEXTATTRIBSARBPROC glXCreateContextAttribsARB;
PFNGLXBINDTEXIMAGEEXTPROC glXBindTexImageEXT;
PFNGLXRELEASETEXIMAGEEXTPROC glXReleaseTexImageEXT;
int dirty;
} Compositor;
-static const char *vshader =
+static const char *vshader_src =
"#version 150\n"
"uniform vec4 geometry;\n"
"in vec2 vertex;\n"
" texcoord = texture_coord;\n"
"}\n";
-static const char *fshader =
+static const char *fshader_src =
"#version 150\n"
"uniform sampler2D image;\n"
"in vec2 texcoord;\n"
" frag_color = texture(image, texcoord);\n"
"}\n";
+static const char *masked_fshader_src =
+ "#version 150\n"
+ "uniform sampler2D image;\n"
+ "uniform sampler2D mask;\n"
+ "in vec2 texcoord;\n"
+ "out vec4 frag_color;\n"
+ "void main()\n"
+ "{\n"
+ " if(texture(mask, texcoord).r==0.0)\n"
+ " discard;\n"
+ " frag_color = texture(image, texcoord);\n"
+ "}\n";
+
static const float window_vertices[] =
{
/* vertex texcoord */
return shader;
}
-unsigned link_program(unsigned *shaders, unsigned nshaders)
+unsigned link_program(unsigned vshader, unsigned fshader)
{
unsigned program;
- unsigned i;
int status;
char info_log[1024];
GLsizei length;
program = glCreateProgram();
- for(i=0; i<nshaders; ++i)
- glAttachShader(program, shaders[i]);
+ glAttachShader(program, vshader);
+ glAttachShader(program, fshader);
glBindAttribLocation(program, 0, "vertex");
glBindAttribLocation(program, 1, "texture_coord");
glBindFragDataLocation(program, 0, "frag_color");
int create_gl_resources(Compositor *compositor, CompositedScreen *screen)
{
unsigned stride;
+ int loc;
use_gl(compositor, screen);
- screen->shaders[0] = compile_shader(GL_VERTEX_SHADER, vshader);
- screen->shaders[1] = compile_shader(GL_FRAGMENT_SHADER, fshader);
- if(!screen->shaders[0] || !screen->shaders[1])
+ screen->shaders[0] = compile_shader(GL_VERTEX_SHADER, vshader_src);
+ screen->shaders[1] = compile_shader(GL_FRAGMENT_SHADER, fshader_src);
+ screen->shaders[2] = compile_shader(GL_FRAGMENT_SHADER, masked_fshader_src);
+ if(!screen->shaders[0] || !screen->shaders[1] || !screen->shaders[2])
return 0;
- screen->program = link_program(screen->shaders, 2);
+ screen->program = link_program(screen->shaders[0], screen->shaders[1]);
if(!screen->program)
return 0;
+ screen->masked_program = link_program(screen->shaders[0], screen->shaders[2]);
+ if(!screen->masked_program)
+ return 0;
+
screen->geometry_loc = glGetUniformLocation(screen->program, "geometry");
+ screen->masked_geometry_loc = glGetUniformLocation(screen->masked_program, "geometry");
+
+ loc = glGetUniformLocation(screen->masked_program, "mask");
+ if(loc>=0)
+ {
+ glUseProgram(screen->masked_program);
+ glUniform1i(loc, 1);
+ }
glGenBuffers(1, &screen->window_vertex_buffer);
glBindBuffer(GL_ARRAY_BUFFER, screen->window_vertex_buffer);
window->glx_pixmap = glXCreatePixmap(compositor->display, screen->fbconfig, window->pixmap, attribs);
}
+void update_window_mask(Compositor *compositor, CompositedWindow *window)
+{
+ Bool bounding_shaped;
+ Bool clip_shaped;
+ int xi, yi;
+ XRectangle *rects;
+ int rect_count;
+ int rect_order;
+ unsigned width;
+ unsigned height;
+ unsigned char *data;
+ int i;
+ unsigned y;
+
+ XShapeQueryExtents(compositor->display, window->window, &bounding_shaped, &xi, &yi, &width, &height, &clip_shaped, &xi, &yi, &width, &height);
+ window->use_mask = bounding_shaped;
+ if(!window->use_mask)
+ return;
+
+ rects = XShapeGetRectangles(compositor->display, window->window, ShapeBounding, &rect_count, &rect_order);
+
+ width = window->width+2*window->border;
+ height = window->height+2*window->border;
+ data = (unsigned char *)malloc(width*height);
+ memset(data, 0, width*height);
+ for(i=0; i<rect_count; ++i)
+ {
+ rects[i].x += window->border;
+ rects[i].y += window->border;
+ if(rects[i].x>=(int)width || rects[i].y>=(int)height)
+ continue;
+
+ if(rects[i].x<0)
+ {
+ if(-rects[i].x>rects[i].width)
+ continue;
+ rects[i].width += rects[i].x;
+ rects[i].x = 0;
+ }
+
+ if(rects[i].y<0)
+ {
+ if(-rects[i].y>rects[i].height)
+ continue;
+ rects[i].height += rects[i].y;
+ rects[i].y = 0;
+ }
+
+ if(rects[i].x+rects[i].width>(int)width)
+ rects[i].width = width-rects[i].x;
+ if(rects[i].y+rects[i].height>(int)height)
+ rects[i].height = height-rects[i].y;
+
+ for(y=0; y<rects[i].height; ++y)
+ {
+ unsigned char *row = data+(rects[i].y+y)*width+rects[i].x;
+ memset(row, 255, rects[i].width);
+ }
+ }
+
+ XFree(rects);
+
+ glBindTexture(GL_TEXTURE_2D, window->mask_texture);
+ glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
+ glTexImage2D(GL_TEXTURE_2D, 0, GL_R8, width, height, 0, GL_RED, GL_UNSIGNED_BYTE, data);
+
+ free(data);
+}
+
CompositedWindow *add_window(Compositor *compositor, CompositedScreen *screen, Window w)
{
CompositedWindow *window;
create_window_pixmap(compositor, screen, window);
window->texture = create_2d_texture();
+ window->mask_texture = create_2d_texture();
+
+ if(window->map_state==IsViewable)
+ update_window_mask(compositor, window);
+
+ XShapeSelectInput(compositor->display, window->window, ShapeNotifyMask);
return window;
}
else if(major_ver<1)
return with_error("XDamage 1.0 or later is required");
- if(!XShapeQueryExtension(compositor->display, &event_base, &error_base))
+ if(!XShapeQueryExtension(compositor->display, &compositor->shape_event, &error_base))
return with_error("XShape is required but was not found");
else if(!XShapeQueryVersion(compositor->display, &major_ver, &minor_ver))
return with_error("Cannot determine XShape version");
for(i=0; i<screen->nwindows; ++i)
{
glDeleteTextures(1, &screen->windows[i].texture);
+ glDeleteTextures(1, &screen->windows[i].mask_texture);
if(screen->windows[i].pixmap)
{
glXDestroyPixmap(compositor->display, screen->windows[i].glx_pixmap);
glDeleteFramebuffers(1, &screen->framebuffer);
glDeleteTextures(1, &screen->fb_texture);
glDeleteProgram(screen->program);
- glDeleteShader(screen->shaders[0]);
- glDeleteShader(screen->shaders[1]);
+ glDeleteProgram(screen->masked_program);
+ for(i=0; i<3; ++i)
+ glDeleteShader(screen->shaders[i]);
glXMakeContextCurrent(compositor->display, 0, 0, NULL);
glXDestroyContext(compositor->display, screen->glx_context);
window->map_state = IsViewable;
create_window_pixmap(compositor, screen, window);
+ update_window_mask(compositor, window);
mark_dirty(compositor, screen);
}
window->height = event->height;
window->border = event->border_width;
create_window_pixmap(compositor, screen, window);
+ update_window_mask(compositor, window);
}
reorder_window(screen, window, event->above);
mark_dirty(compositor, screen);
}
+void process_shape_event(Compositor *compositor, XShapeEvent *event)
+{
+ CompositedScreen *screen;
+ CompositedWindow *window;
+
+ if(event->kind!=ShapeBounding)
+ return;
+
+ window = find_window_global(compositor, event->window, &screen);
+ if(window && window->map_state==IsViewable)
+ {
+ update_window_mask(compositor, window);
+ mark_dirty(compositor, screen);
+ }
+}
+
int process_event(Compositor *compositor)
{
XEvent event;
default:
if(event.type==compositor->damage_event+XDamageNotify)
process_damage_event(compositor, (XDamageNotifyEvent *)&event);
+ else if(event.type==compositor->shape_event+ShapeNotify)
+ process_shape_event(compositor, (XShapeEvent *)&event);
else
printf("Event %d\n", event.type);
}
void refresh_screen(Compositor *compositor, CompositedScreen *screen)
{
unsigned i;
+ int use_mask;
use_gl(compositor, screen);
glClearColor(0.5f, 0.5f, 0.5f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT);
- glUseProgram(screen->program);
+ use_mask = -1;
glBindVertexArray(screen->window_vertex_array);
for(i=0; i<screen->nwindows; ++i)
{
if(window->map_state!=IsViewable)
continue;
+ if(window->use_mask!=use_mask)
+ {
+ use_mask = window->use_mask;
+ glUseProgram(use_mask ? screen->masked_program : screen->program);
+ }
+
+ if(window->use_mask)
+ {
+ glActiveTexture(GL_TEXTURE1);
+ glBindTexture(GL_TEXTURE_2D, window->mask_texture);
+ glActiveTexture(GL_TEXTURE0);
+ }
+
XDamageSubtract(compositor->display, window->damage, None, None);
glBindTexture(GL_TEXTURE_2D, window->texture);
compositor->glXBindTexImageEXT(compositor->display, window->glx_pixmap, GLX_FRONT_LEFT_EXT, NULL);
- glUniform4f(screen->geometry_loc,
+ glUniform4f((use_mask ? screen->masked_geometry_loc : screen->geometry_loc),
(float)window->x/screen->width, 1.0f-(float)(window->y+window->height)/screen->height,
(float)(window->width+2*window->border)/screen->width, (float)(window->height+2*window->border)/screen->height);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
glBindTexture(GL_TEXTURE_2D, screen->fb_texture);
glEnable(GL_PRIMITIVE_RESTART);
glPrimitiveRestartIndex(0xFFFF);
+ if(use_mask)
+ glUseProgram(screen->program);
for(i=0; i<screen->nmonitors; ++i)
{