render: implement rounded corners in native pixman
Before adoption: this commit came in with the history the project adopted, so no landing records it.
This means we no longer depend on cairo for rendering, *except* for SVG icons, when the rsvg backend is used. But, a nanosvg build no longer needs to link against cairo "just" to get rounded corners.
authored by Daniel Eklöf · committed by Daniel Eklöf · parent d8fadb1 · Browse files · 1 file·+63 added−54 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
1 file+63 added−54 removed
- other:+63 added−54 removed
Provenance
- Commit
ec1fcdc- Tree
e363640- Author
Daniel Eklöf <daniel@ekloef.se>- Committer
Daniel Eklöf <daniel@ekloef.se>- Parent
d8fadb1
1 file changed+63 added−54 removed
modified:render.c+63 added-54 removed
73 unmodified lines74 ? round(pt_or_px->pt * scale * dpi / 72.)75 : pt_or_px->px;76 }77 78+static void79+fill_rounded_rectangle(pixman_op_t op, pixman_image_t* dest,80+ const pixman_color_t* color, int16_t x, int16_t y,81+ uint16_t width, uint16_t height, uint16_t radius)82+{83+ int rect_count = ( radius + radius ) + 1;84+ pixman_rectangle16_t rects[rect_count];85+86+ for (int i = 0; i <= radius; i++){87+ uint16_t ydist = radius - i;88+ uint16_t curve = sqrt(radius * radius - ydist * ydist);89+90+ rects[i] = (pixman_rectangle16_t){91+ x + radius - curve, y + i, width - radius * 2 + curve * 2, 192+ };93+94+ rects[radius + i] = (pixman_rectangle16_t){95+ x + radius - curve, y + height - i, width - radius * 2 + curve * 2, 196+ };97+ }98+99+ rects[(radius * 2)] = (pixman_rectangle16_t){100+ x, y + radius, width, height + 1 - radius * 2101+ };102+ pixman_image_fill_rectangles(op, dest, color,rect_count, rects);103+}104+105 void106 render_background(const struct render *render, struct buffer *buf)107 {81- bool use_pixman =82-#if defined(FUZZEL_ENABLE_CAIRO)83- render->conf->border.radius == 084-#else85- true86-#endif87- ;108+ unsigned bw = render->conf->border.size;109 89- if (use_pixman) {90- unsigned bw = render->conf->border.size;110+ pixman_color_t bg = rgba2pixman(render->conf->colors.background);111+ pixman_color_t border_color = rgba2pixman(render->conf->colors.border);112 92- pixman_color_t bg = rgba2pixman(render->conf->colors.background);113+ if (render->conf->border.radius == 0) {114 pixman_image_fill_rectangles(115 PIXMAN_OP_SRC, buf->pix, &bg,116 1, &(pixman_rectangle16_t){117 bw, bw, buf->width - 2 * bw, buf->height - 2 * bw});118 98- pixman_color_t border_color = rgba2pixman(render->conf->colors.border);119 pixman_image_fill_rectangles(120 PIXMAN_OP_SRC, buf->pix, &border_color,121 4, (pixman_rectangle16_t[]){122 {0, 0, buf->width, bw}, /* top */1 unmodified line124 {buf->width - bw, bw, bw, buf->height - 2 * bw}, /* right */125 {0, buf->height - bw, buf->width, bw} /* bottom */126 });127 } else {108-#if defined(FUZZEL_ENABLE_CAIRO)109- /* Erase */110- cairo_set_operator(buf->cairo, CAIRO_OPERATOR_CLEAR);111- cairo_rectangle(buf->cairo, 0, 0, buf->width, buf->height);112- cairo_fill(buf->cairo);128+ const int msaa_scale = 2;129+ const double brd_sz_scaled = bw * msaa_scale;130+ const double brd_rad_scaled = render->conf->border.radius * msaa_scale;131+ int w = buf->width * msaa_scale;132+ int h = buf->height * msaa_scale;133 114- /*115- * Lines in cairo are *between* pixels.116- *117- * To get a sharp 1px line, we need to draw it with118- * line-width=2.119- *120- * Thus, we need to draw the path offset:ed with half that121- * (=actual border width).122- */123- const double b = render->border_size;124- const double w = max(buf->width - 2 * b, 0.);125- const double h = max(buf->height - 2 * b, 0.);134+ pixman_image_t *bg_img;135+ if (msaa_scale != 1){136+ bg_img = pixman_image_create_bits(PIXMAN_a8r8g8b8, w, h, NULL, w*4);137+ } else {138+ bg_img = buf->pix;139+ }140 127- const double from_degree = M_PI / 180;128- const double radius = render->conf->border.radius * render->scale;129-130- /* Path describing an arc:ed rectangle */131- cairo_new_path(buf->cairo);132- cairo_arc(buf->cairo, b + w - radius, b + h - radius, radius,133- 0.0 * from_degree, 90.0 * from_degree);134- cairo_arc(buf->cairo, b + radius, b + h - radius, radius,135- 90.0 * from_degree, 180.0 * from_degree);136- cairo_arc(buf->cairo, b + radius, b + radius, radius,137- 180.0 * from_degree, 270.0 * from_degree);138- cairo_arc(buf->cairo, b + w - radius, b + radius, radius,139- 270.0 * from_degree, 360.0 * from_degree);140- cairo_close_path(buf->cairo);141-141 /* Border */143- const struct rgba *bc = &render->conf->colors.border;144- cairo_set_operator(buf->cairo, CAIRO_OPERATOR_SOURCE);145- cairo_set_line_width(buf->cairo, 2 * b);146- cairo_set_source_rgba(buf->cairo, bc->r, bc->g, bc->b, bc->a);147- cairo_stroke_preserve(buf->cairo);142+ fill_rounded_rectangle(143+ PIXMAN_OP_SRC, bg_img, &border_color, 0, 0, w, h, brd_rad_scaled);144 145 /* Background */150- const struct rgba *bg = &render->conf->colors.background;151- cairo_set_source_rgba(buf->cairo, bg->r, bg->g, bg->b, bg->a);152- cairo_fill(buf->cairo);153-#else154- assert(false);155-#endif146+ fill_rounded_rectangle(147+ PIXMAN_OP_SRC, bg_img, &bg, brd_sz_scaled, brd_sz_scaled,148+ w-(brd_sz_scaled*2),149+ h-(brd_sz_scaled*2),150+ brd_rad_scaled - brd_sz_scaled);151+152+ if (msaa_scale != 1){153+ pixman_f_transform_t ftrans;154+ pixman_transform_t trans;155+ pixman_f_transform_init_scale(&ftrans, msaa_scale, msaa_scale);156+ pixman_transform_from_pixman_f_transform(&trans, &ftrans);157+ pixman_image_set_transform(bg_img, &trans);158+ pixman_image_set_filter(bg_img, PIXMAN_FILTER_BILINEAR, NULL, 0);159+160+ pixman_image_composite32(161+ PIXMAN_OP_SRC, bg_img, NULL, buf->pix, 0, 0, 0, 0, 0, 0,162+ buf->width, buf->height);163+ pixman_image_unref(bg_img);164+ }165 }166 }167 168 static void707 unmodified lines