cairo: replace cairo with pixman in decos, particles and modules

All decoration, particle and module interfaces now takes a
pixman_image_t parameter, and all drawing is done using pixman APIs.

The wayland/xcb backends implement a new interface functions,
get_pixman_image(), that should return a pixman image instance that is
suitable for rendering.

In the wayland backend, the image uses the same backing data as the
cairo surface.

In the XCB backend, we create a new image each time, and then blit it
to the cairo surface at commit time.
This commit is contained in:
Daniel Eklöf 2019-09-22 01:56:58 +02:00
parent 95385863ae
commit c11fee4ce3
No known key found for this signature in database
GPG key ID: 5BBD4992C116573F
24 changed files with 162 additions and 141 deletions

View file

@ -11,7 +11,8 @@ struct backend {
void (*expose)(const struct bar *bar),
void (*on_mouse)(struct bar *bar, enum mouse_event event,
int x, int y));
void (*commit_surface)(const struct bar *bar);
pixman_image_t *(*get_pixman_image)(const struct bar *bar);
void (*commit_pixman)(const struct bar *bar, pixman_image_t *pix);
void (*refresh)(const struct bar *bar);
void (*set_cursor)(struct bar *bar, const char *cursor);
};

View file

@ -61,33 +61,21 @@ static void
expose(const struct bar *_bar)
{
const struct private *bar = _bar->private;
pixman_image_t *pix = bar->backend.iface->get_pixman_image(_bar);
double r, g, b, a;
r = bar->background.red;
g = bar->background.green;
b = bar->background.blue;
a = bar->background.alpha;
cairo_set_source_rgba(bar->cairo, r, g, b, a);
cairo_set_operator(bar->cairo, CAIRO_OPERATOR_SOURCE);
cairo_paint(bar->cairo);
pixman_image_fill_rectangles(
PIXMAN_OP_SRC, pix, &bar->background, 1,
&(pixman_rectangle16_t){0, 0, bar->width, bar->height_with_border});
if (bar->border.width > 0) {
r = bar->border.color.red;
g = bar->border.color.green;
b = bar->border.color.blue;
a = bar->border.color.alpha;
cairo_set_line_width(bar->cairo, bar->border.width);
cairo_set_source_rgba(bar->cairo, r, g, b, a);
cairo_set_operator(bar->cairo, CAIRO_OPERATOR_OVER);
cairo_rectangle(
bar->cairo,
bar->border.width / 2.0,
bar->border.width / 2.0,
bar->width - bar->border.width,
bar->height_with_border - bar->border.width);
cairo_stroke(bar->cairo);
pixman_image_fill_rectangles(
PIXMAN_OP_OVER, pix, &bar->border.color, 4,
(pixman_rectangle16_t[]){
{0, 0, bar->width, bar->border.width},
{0, 0, bar->border.width, bar->height},
{bar->width - bar->border.width, 0, bar->border.width, bar->height},
{0, bar->height - bar->border.width, bar->width, bar->border.width},
});
}
for (size_t i = 0; i < bar->left.count; i++) {
@ -127,14 +115,14 @@ expose(const struct bar *_bar)
int x = bar->border.width + bar->left_margin - bar->left_spacing;
for (size_t i = 0; i < bar->left.count; i++) {
const struct exposable *e = bar->left.exps[i];
e->expose(e, bar->cairo, x + bar->left_spacing, y, bar->height);
e->expose(e, pix, x + bar->left_spacing, y, bar->height);
x += bar->left_spacing + e->width + bar->right_spacing;
}
x = bar->width / 2 - center_width / 2 - bar->left_spacing;
for (size_t i = 0; i < bar->center.count; i++) {
const struct exposable *e = bar->center.exps[i];
e->expose(e, bar->cairo, x + bar->left_spacing, y, bar->height);
e->expose(e, pix, x + bar->left_spacing, y, bar->height);
x += bar->left_spacing + e->width + bar->right_spacing;
}
@ -146,12 +134,11 @@ expose(const struct bar *_bar)
for (size_t i = 0; i < bar->right.count; i++) {
const struct exposable *e = bar->right.exps[i];
e->expose(e, bar->cairo, x + bar->left_spacing, y, bar->height);
e->expose(e, pix, x + bar->left_spacing, y, bar->height);
x += bar->left_spacing + e->width + bar->right_spacing;
}
cairo_surface_flush(bar->cairo_surface);
bar->backend.iface->commit_surface(_bar);
bar->backend.iface->commit_pixman(_bar, pix);
}

View file

@ -26,11 +26,11 @@ struct bar_config {
int left_spacing, right_spacing;
int left_margin, right_margin;
struct rgba background;
pixman_color_t background;
struct {
int width;
struct rgba color;
pixman_color_t color;
int left_margin, right_margin;
int top_margin, bottom_margin;
} border;

View file

@ -14,11 +14,11 @@ struct private {
int left_spacing, right_spacing;
int left_margin, right_margin;
struct rgba background;
pixman_color_t background;
struct {
int width;
struct rgba color;
pixman_color_t color;
int left_margin, right_margin;
int top_margin, bottom_margin;
} border;

View file

@ -901,12 +901,31 @@ frame_callback(void *data, struct wl_callback *wl_callback, uint32_t callback_da
;//printf("nothing more to do\n");
}
static pixman_image_t *
get_pixman_image(const struct bar *_bar)
{
struct private *bar = _bar->private;
cairo_surface_t *surf = cairo_get_target(bar->cairo);
cairo_surface_flush(surf);
return pixman_image_create_bits_no_clear(
PIXMAN_a8r8g8b8,
cairo_image_surface_get_width(surf),
cairo_image_surface_get_height(surf),
(uint32_t *)cairo_image_surface_get_data(surf),
cairo_image_surface_get_stride(surf));
}
static void
commit_surface(const struct bar *_bar)
commit_pixman(const struct bar *_bar, pixman_image_t *pix)
{
struct private *bar = _bar->private;
struct wayland_backend *backend = bar->backend.data;
pixman_image_unref(pix);
cairo_surface_mark_dirty(cairo_get_target(bar->cairo));
//printf("commit: %dxl%d\n", backend->width, backend->height);
assert(backend->next_buffer != NULL);
@ -973,7 +992,8 @@ const struct backend wayland_backend_iface = {
.setup = &setup,
.cleanup = &cleanup,
.loop = &loop,
.commit_surface = &commit_surface,
.get_pixman_image = &get_pixman_image,
.commit_pixman = &commit_pixman,
.refresh = &refresh,
.set_cursor = &set_cursor,
};

View file

@ -370,10 +370,56 @@ loop(struct bar *_bar,
}
static void
commit_surface(const struct bar *_bar)
free_pixman_bits(pixman_image_t *pix, void *data)
{
free(data);
}
static pixman_image_t *
get_pixman_image(const struct bar *_bar)
{
const struct private *bar = _bar->private;
uint32_t *bits = malloc(
bar->width * bar->height_with_border * sizeof(uint32_t));
pixman_image_t *ret = pixman_image_create_bits_no_clear(
PIXMAN_a8r8g8b8,
bar->width,
bar->height_with_border,
bits,
bar->width * sizeof(uint32_t));
if (ret == NULL) {
free(bits);
return NULL;
}
pixman_image_set_destroy_function(ret, &free_pixman_bits, bits);
return ret;
}
static void
commit_pixman(const struct bar *_bar, pixman_image_t *pix)
{
const struct private *bar = _bar->private;
const struct xcb_backend *backend = bar->backend.data;
/* Blit pixman image to our XCB surface */
cairo_surface_t *surf = cairo_image_surface_create_for_data(
(unsigned char *)pixman_image_get_data(pix),
CAIRO_FORMAT_ARGB32,
bar->width,
bar->height_with_border,
cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, bar->width));
cairo_set_source_surface(bar->cairo, surf, 0, 0);
cairo_set_operator(bar->cairo, CAIRO_OPERATOR_SOURCE);
cairo_paint(bar->cairo);
cairo_surface_flush(bar->cairo_surface);
cairo_surface_destroy(surf);
pixman_image_unref(pix);
xcb_copy_area(backend->conn, backend->pixmap, backend->win, backend->gc,
0, 0, 0, 0, bar->width, bar->height_with_border);
xcb_flush(backend->conn);
@ -430,7 +476,8 @@ const struct backend xcb_backend_iface = {
.setup = &setup,
.cleanup = &cleanup,
.loop = &loop,
.commit_surface = &commit_surface,
.get_pixman_image = &get_pixman_image,
.commit_pixman = &commit_pixman,
.refresh = &refresh,
.set_cursor = &set_cursor,
};

View file

@ -13,6 +13,10 @@
#include "module.h"
#include "plugin.h"
#define LOG_MODULE "config"
#define LOG_ENABLE_DBG 0
#include "log.h"
static uint8_t
hex_nibble(char hex)
{
@ -36,7 +40,7 @@ hex_byte(const char hex[2])
return upper << 4 | lower;
}
struct rgba
pixman_color_t
conf_to_color(const struct yml_node *node)
{
const char *hex = yml_value_as_string(node);
@ -44,30 +48,26 @@ conf_to_color(const struct yml_node *node)
assert(hex != NULL);
assert(strlen(hex) == 8);
uint8_t red = hex_byte(&hex[0]);
uint8_t green = hex_byte(&hex[2]);
uint8_t blue = hex_byte(&hex[4]);
uint8_t alpha = hex_byte(&hex[6]);
uint16_t red = hex_byte(&hex[0]);
uint16_t green = hex_byte(&hex[2]);
uint16_t blue = hex_byte(&hex[4]);
uint16_t alpha = hex_byte(&hex[6]);
struct rgba rgba = {
(double)red / 255.0,
(double)green / 255.0,
(double)blue / 255.0,
(double)alpha / 255.0
alpha |= alpha << 8;
int alpha_div = 0xffff / alpha;
return (pixman_color_t){
.red = (red << 8 | red) / alpha_div,
.green = (green << 8 | green) / alpha_div,
.blue = (blue << 8 | blue) / alpha_div,
.alpha = alpha,
};
assert(rgba.red >= 0.0 && rgba.red <= 1.0);
assert(rgba.green >= 0.0 && rgba.green <= 1.0);
assert(rgba.blue >= 0.0 && rgba.blue <= 1.0);
assert(rgba.alpha >= 0.0 && rgba.alpha <= 1.0);
return rgba;
}
struct font *
conf_to_font(const struct yml_node *node)
{
return font_from_name(yml_value_as_string(node));;
return font_from_name(yml_value_as_string(node));
}
struct deco *
@ -155,7 +155,7 @@ conf_to_particle(const struct yml_node *node, struct conf_inherit inherited)
*/
struct font *font = font_node != NULL
? conf_to_font(font_node) : font_clone(inherited.font);
struct rgba foreground = foreground_node != NULL
pixman_color_t foreground = foreground_node != NULL
? conf_to_color(foreground_node) : inherited.foreground;
/* Instantiate base/common particle */
@ -264,7 +264,7 @@ conf_to_bar(const struct yml_node *bar, enum bar_backend backend)
* foreground color at top-level.
*/
struct font *font = font_from_name("sans");
struct rgba foreground = (struct rgba){1.0, 1.0, 1.0, 1.0}; /* White */
pixman_color_t foreground = {0xffff, 0xffff, 0xffff, 0xffff}; /* White */
const struct yml_node *font_node = yml_get_value(bar, "font");
if (font_node != NULL) {

View file

@ -14,12 +14,12 @@ struct bar *conf_to_bar(const struct yml_node *bar, enum bar_backend backend);
* Utility functions, for e.g. modules
*/
struct rgba conf_to_color(const struct yml_node *node);
pixman_color_t conf_to_color(const struct yml_node *node);
struct font *conf_to_font(const struct yml_node *node);
struct conf_inherit {
const struct font *font;
struct rgba foreground;
pixman_color_t foreground;
};
struct particle *conf_to_particle(

View file

@ -1,10 +1,10 @@
#pragma once
#include <cairo.h>
#include <pixman.h>
struct deco {
void *private;
void (*expose)(const struct deco *deco, cairo_t *cr,
void (*expose)(const struct deco *deco, pixman_image_t *pix,
int x, int y, int width, int height);
void (*destroy)(struct deco *deco);
};

View file

@ -6,7 +6,8 @@
#include "../plugin.h"
struct private {
struct rgba color;
//struct rgba color;
pixman_color_t color;
};
static void
@ -18,17 +19,16 @@ destroy(struct deco *deco)
}
static void
expose(const struct deco *deco, cairo_t *cr, int x, int y, int width, int height)
expose(const struct deco *deco, pixman_image_t *pix, int x, int y, int width, int height)
{
const struct private *d = deco->private;
cairo_set_source_rgba(
cr, d->color.red, d->color.green, d->color.blue, d->color.alpha);
cairo_rectangle(cr, x, y, width, height);
cairo_fill(cr);
pixman_image_fill_rectangles(
PIXMAN_OP_OVER, pix, &d->color, 1,
&(pixman_rectangle16_t){x, y, width, height});
}
static struct deco *
background_new(struct rgba color)
background_new(pixman_color_t color)
{
struct private *priv = calloc(1, sizeof(*priv));
priv->color = color;

View file

@ -1,4 +1,4 @@
deco_sdk = declare_dependency(dependencies: [cairo, cairo_ft])
deco_sdk = declare_dependency(dependencies: [pixman])
decorations = []
foreach deco : ['background', 'stack', 'underline']

View file

@ -24,11 +24,11 @@ destroy(struct deco *deco)
}
static void
expose(const struct deco *deco, cairo_t *cr, int x, int y, int width, int height)
expose(const struct deco *deco, pixman_image_t *pix, int x, int y, int width, int height)
{
const struct private *d = deco->private;
for (size_t i = 0; i < d->count; i++)
d->decos[i]->expose(d->decos[i], cr, x, y, width, height);
d->decos[i]->expose(d->decos[i], pix, x, y, width, height);
}
static struct deco *

View file

@ -7,7 +7,7 @@
struct private {
int size;
struct rgba color;
pixman_color_t color;
};
static void
@ -19,17 +19,16 @@ destroy(struct deco *deco)
}
static void
expose(const struct deco *deco, cairo_t *cr, int x, int y, int width, int height)
expose(const struct deco *deco, pixman_image_t *pix, int x, int y, int width, int height)
{
const struct private *d = deco->private;
cairo_set_source_rgba(
cr, d->color.red, d->color.green, d->color.blue, d->color.alpha);
cairo_rectangle(cr, x, y + height - d->size, width, d->size);
cairo_fill(cr);
pixman_image_fill_rectangles(
PIXMAN_OP_OVER, pix, &d->color, 1,
&(pixman_rectangle16_t){x, y + height - d->size, width, d->size});
}
static struct deco *
underline_new(int size, struct rgba color)
underline_new(int size, pixman_color_t color)
{
struct private *priv = calloc(1, sizeof(*priv));
priv->size = size;

View file

@ -1,4 +1,4 @@
module_sdk = declare_dependency(dependencies: [cairo, cairo_ft, threads])
module_sdk = declare_dependency(dependencies: [pixman, threads])
modules = []

View file

@ -29,7 +29,7 @@ particle_default_destroy(struct particle *particle)
struct particle *
particle_common_new(int left_margin, int right_margin,
const char *on_click_template,
struct font *font, struct rgba foreground,
struct font *font, pixman_color_t foreground,
struct deco *deco)
{
struct particle *p = calloc(1, sizeof(*p));
@ -52,11 +52,11 @@ exposable_default_destroy(struct exposable *exposable)
void
exposable_render_deco(const struct exposable *exposable,
cairo_t *cr, int x, int y, int height)
pixman_image_t *pix, int x, int y, int height)
{
const struct deco *deco = exposable->particle->deco;
if (deco != NULL)
deco->expose(deco, cr, x, y, exposable->width, height);
deco->expose(deco, pix, x, y, exposable->width, height);
}

View file

@ -1,5 +1,5 @@
#pragma once
#include <cairo.h>
#include <pixman.h>
#include "color.h"
#include "decoration.h"
@ -14,7 +14,7 @@ struct particle {
int left_margin, right_margin;
char *on_click_template;
struct rgba foreground;
pixman_color_t foreground;
struct font *font;
struct deco *deco;
@ -37,7 +37,7 @@ struct exposable {
void (*destroy)(struct exposable *exposable);
int (*begin_expose)(struct exposable *exposable);
void (*expose)(const struct exposable *exposable, cairo_t *cr,
void (*expose)(const struct exposable *exposable, pixman_image_t *pix,
int x, int y, int height);
void (*on_mouse)(struct exposable *exposable, struct bar *bar,
@ -46,7 +46,7 @@ struct exposable {
struct particle *particle_common_new(
int left_margin, int right_margin, const char *on_click_template,
struct font *font, struct rgba foreground, struct deco *deco);
struct font *font, pixman_color_t foreground, struct deco *deco);
void particle_default_destroy(struct particle *particle);
@ -54,7 +54,7 @@ struct exposable *exposable_common_new(
const struct particle *particle, const char *on_click);
void exposable_default_destroy(struct exposable *exposable);
void exposable_render_deco(
const struct exposable *exposable, cairo_t *cr, int x, int y, int height);
const struct exposable *exposable, pixman_image_t *pix, int x, int y, int height);
void exposable_default_on_mouse(
struct exposable *exposable, struct bar *bar,

View file

@ -49,7 +49,7 @@ dynlist_begin_expose(struct exposable *exposable)
}
static void
dynlist_expose(const struct exposable *exposable, cairo_t *cr, int x, int y, int height)
dynlist_expose(const struct exposable *exposable, pixman_image_t *pix, int x, int y, int height)
{
const struct private *e = exposable->private;
@ -60,7 +60,7 @@ dynlist_expose(const struct exposable *exposable, cairo_t *cr, int x, int y, int
for (size_t i = 0; i < e->count; i++) {
const struct exposable *ee = e->exposables[i];
ee->expose(ee, cr, x + left_spacing, y, height);
ee->expose(ee, pix, x + left_spacing, y, height);
x += left_spacing + e->widths[i] + right_spacing;
}
}

View file

@ -14,9 +14,9 @@ begin_expose(struct exposable *exposable)
}
static void
expose(const struct exposable *exposable, cairo_t *cr, int x, int y, int height)
expose(const struct exposable *exposable, pixman_image_t *pix, int x, int y, int height)
{
exposable_render_deco(exposable, cr, x, y, height);
exposable_render_deco(exposable, pix, x, y, height);
}
static struct exposable *

View file

@ -55,11 +55,11 @@ begin_expose(struct exposable *exposable)
}
static void
expose(const struct exposable *exposable, cairo_t *cr, int x, int y, int height)
expose(const struct exposable *exposable, pixman_image_t *pix, int x, int y, int height)
{
const struct eprivate *e = exposable->private;
exposable_render_deco(exposable, cr, x, y, height);
exposable_render_deco(exposable, pix, x, y, height);
int left_margin = exposable->particle->left_margin;
int left_spacing = e->left_spacing;
@ -68,7 +68,7 @@ expose(const struct exposable *exposable, cairo_t *cr, int x, int y, int height)
x += left_margin - left_spacing;
for (size_t i = 0; i < e->count; i++) {
const struct exposable *ee = e->exposables[i];
ee->expose(ee, cr, x + left_spacing, y, height);
ee->expose(ee, pix, x + left_spacing, y, height);
x += left_spacing + e->widths[i] + right_spacing;
}
}

View file

@ -49,13 +49,13 @@ begin_expose(struct exposable *exposable)
}
static void
expose(const struct exposable *exposable, cairo_t *cr, int x, int y, int height)
expose(const struct exposable *exposable, pixman_image_t *pix, int x, int y, int height)
{
struct eprivate *e = exposable->private;
exposable_render_deco(exposable, cr, x, y, height);
exposable_render_deco(exposable, pix, x, y, height);
e->exposable->expose(
e->exposable, cr, x + exposable->particle->left_margin, y, height);
e->exposable, pix, x + exposable->particle->left_margin, y, height);
}
static void

View file

@ -1,4 +1,4 @@
particle_sdk = declare_dependency(dependencies: [cairo, cairo_ft])
particle_sdk = declare_dependency(dependencies: [freetype, fontconfig, pixman])
particles = []
foreach particle : ['empty', 'list', 'map', 'progress-bar', 'ramp', 'string']

View file

@ -70,15 +70,15 @@ begin_expose(struct exposable *exposable)
}
static void
expose(const struct exposable *exposable, cairo_t *cr, int x, int y, int height)
expose(const struct exposable *exposable, pixman_image_t *pix, int x, int y, int height)
{
const struct eprivate *e = exposable->private;
exposable_render_deco(exposable, cr, x, y, height);
exposable_render_deco(exposable, pix, x, y, height);
x += exposable->particle->left_margin;
for (size_t i = 0; i < e->count; i++) {
e->exposables[i]->expose(e->exposables[i], cr, x, y, height);
e->exposables[i]->expose(e->exposables[i], pix, x, y, height);
x += e->exposables[i]->width;
}
}

View file

@ -43,13 +43,13 @@ begin_expose(struct exposable *exposable)
}
static void
expose(const struct exposable *exposable, cairo_t *cr, int x, int y, int height)
expose(const struct exposable *exposable, pixman_image_t *pix, int x, int y, int height)
{
struct eprivate *e = exposable->private;
exposable_render_deco(exposable, cr, x, y, height);
exposable_render_deco(exposable, pix, x, y, height);
e->exposable->expose(
e->exposable, cr, x + exposable->particle->left_margin, y, height);
e->exposable, pix, x + exposable->particle->left_margin, y, height);
}
static void

View file

@ -70,22 +70,10 @@ begin_expose(struct exposable *exposable)
return exposable->width;
}
static inline pixman_color_t
color_hex_to_pixman_with_alpha(uint32_t color, uint16_t alpha)
{
int alpha_div = 0xffff / alpha;
return (pixman_color_t){
.red = ((color >> 16 & 0xff) | (color >> 8 & 0xff00)) / alpha_div,
.green = ((color >> 8 & 0xff) | (color >> 0 & 0xff00)) / alpha_div,
.blue = ((color >> 0 & 0xff) | (color << 8 & 0xff00)) / alpha_div,
.alpha = alpha,
};
}
static void
expose(const struct exposable *exposable, cairo_t *cr, int x, int y, int height)
expose(const struct exposable *exposable, pixman_image_t *pix, int x, int y, int height)
{
exposable_render_deco(exposable, cr, x, y, height);
exposable_render_deco(exposable, pix, x, y, height);
const struct eprivate *e = exposable->private;
const struct font *font = exposable->particle->font;
@ -112,24 +100,6 @@ expose(const struct exposable *exposable, cairo_t *cr, int x, int y, int height)
(double)(height + font->fextents.ascent + font->fextents.descent) / 2.0 -
(font->fextents.descent > 0 ? font->fextents.descent : 0);
/* TODO: get rid of cairo?... */
cairo_surface_t *surf = cairo_get_target(cr);
cairo_surface_flush(surf);
pixman_image_t *pix = pixman_image_create_bits_no_clear(
PIXMAN_a8r8g8b8,
cairo_image_surface_get_width(surf),
cairo_image_surface_get_height(surf),
(uint32_t *)cairo_image_surface_get_data(surf),
cairo_image_surface_get_stride(surf));
uint32_t hex_color =
(uint32_t)(uint8_t)(exposable->particle->foreground.red * 255) << 16 |
(uint32_t)(uint8_t)(exposable->particle->foreground.green * 255) << 8 |
(uint32_t)(uint8_t)(exposable->particle->foreground.blue * 255) << 0;
uint16_t alpha = exposable->particle->foreground.alpha * 65535;
pixman_color_t fg = color_hex_to_pixman_with_alpha(hex_color, alpha);
x += exposable->particle->left_margin;
/* Loop glyphs and render them, one by one */
@ -145,7 +115,7 @@ expose(const struct exposable *exposable, cairo_t *cr, int x, int y, int height)
glyph->width, glyph->height);
} else {
/* Glyph surface is an alpha mask */
pixman_image_t *src = pixman_image_create_solid_fill(&fg);
pixman_image_t *src = pixman_image_create_solid_fill(&exposable->particle->foreground);
pixman_image_composite32(
PIXMAN_OP_OVER, src, glyph->pix, pix, 0, 0, 0, 0,
x + glyph->x, baseline - glyph->y,
@ -155,9 +125,6 @@ expose(const struct exposable *exposable, cairo_t *cr, int x, int y, int height)
x += glyph->x_advance;
}
pixman_image_unref(pix);
cairo_surface_mark_dirty(surf);
}
static struct exposable *