Skip to content

Commit 6fa62cc

Browse files
committed
Refactor draw code to use cairo_surface_set_device_scale
1 parent 4384521 commit 6fa62cc

File tree

11 files changed

+121
-133
lines changed

11 files changed

+121
-133
lines changed

src/draw.c

+70-99
Large diffs are not rendered by default.

src/draw.h

+1-4
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,7 @@ void draw_setup(void);
5757

5858
void draw(void);
5959

60-
void draw_rounded_rect(cairo_t *c, float x, float y, int width, int height, int corner_radius, double scale, enum corner_pos corners);
61-
62-
// TODO get rid of this function by passing scale to everything that needs it.
63-
double draw_get_scale(void);
60+
void draw_rounded_rect(cairo_t *c, int x, int y, int width, int height, int corner_radius, enum corner_pos corners);
6461

6562
void draw_deinit(void);
6663

src/icon.c

+12-12
Original file line numberDiff line numberDiff line change
@@ -83,12 +83,12 @@ static void pixbuf_data_to_cairo_data(
8383
}
8484
}
8585

86-
int get_icon_width(cairo_surface_t *icon, double scale) {
87-
return round(cairo_image_surface_get_width(icon) / scale);
86+
int get_icon_width(cairo_surface_t *icon) {
87+
return cairo_image_surface_get_width(icon);
8888
}
8989

90-
int get_icon_height(cairo_surface_t *icon, double scale) {
91-
return round(cairo_image_surface_get_height(icon) / scale);
90+
int get_icon_height(cairo_surface_t *icon) {
91+
return cairo_image_surface_get_height(icon);
9292
}
9393

9494
cairo_surface_t *gdk_pixbuf_to_cairo_surface(GdkPixbuf *pixbuf)
@@ -161,7 +161,7 @@ static bool icon_size_clamp(int *w, int *h, int min_size, int max_size) {
161161
* necessary, it returns the same pixbuf. Transfers full
162162
* ownership of the reference.
163163
*/
164-
static GdkPixbuf *icon_pixbuf_scale_to_size(GdkPixbuf *pixbuf, double dpi_scale, int min_size, int max_size)
164+
static GdkPixbuf *icon_pixbuf_scale_to_size(GdkPixbuf *pixbuf, int min_size, int max_size)
165165
{
166166
ASSERT_OR_RET(pixbuf, NULL);
167167

@@ -170,8 +170,8 @@ static GdkPixbuf *icon_pixbuf_scale_to_size(GdkPixbuf *pixbuf, double dpi_scale,
170170

171171
// TODO immediately rescale icon upon scale changes
172172
if(icon_size_clamp(&w, &h, min_size, max_size)) {
173-
w = round(w * dpi_scale);
174-
h = round(h * dpi_scale);
173+
//w = round(w * dpi_scale);
174+
//h = round(h * dpi_scale);
175175
}
176176
GdkPixbuf *scaled = gdk_pixbuf_scale_simple(
177177
pixbuf,
@@ -183,7 +183,7 @@ static GdkPixbuf *icon_pixbuf_scale_to_size(GdkPixbuf *pixbuf, double dpi_scale,
183183
return pixbuf;
184184
}
185185

186-
GdkPixbuf *get_pixbuf_from_file(const char *filename, int min_size, int max_size, double scale)
186+
GdkPixbuf *get_pixbuf_from_file(const char *filename, int min_size, int max_size)
187187
{
188188
GError *error = NULL;
189189
gint w, h;
@@ -196,8 +196,8 @@ GdkPixbuf *get_pixbuf_from_file(const char *filename, int min_size, int max_size
196196
// TODO immediately rescale icon upon scale changes
197197
icon_size_clamp(&w, &h, min_size, max_size);
198198
pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
199-
round(w * scale),
200-
round(h * scale),
199+
w,
200+
h,
201201
TRUE,
202202
&error);
203203

@@ -270,7 +270,7 @@ char *get_path_from_icon_name(const char *iconname, int size)
270270
return new_name;
271271
}
272272

273-
GdkPixbuf *icon_get_for_data(GVariant *data, char **id, double dpi_scale, int min_size, int max_size)
273+
GdkPixbuf *icon_get_for_data(GVariant *data, char **id, int min_size, int max_size)
274274
{
275275
ASSERT_OR_RET(data, NULL);
276276
ASSERT_OR_RET(id, NULL);
@@ -380,7 +380,7 @@ GdkPixbuf *icon_get_for_data(GVariant *data, char **id, double dpi_scale, int mi
380380
g_free(data_chk);
381381
g_variant_unref(data_variant);
382382

383-
pixbuf = icon_pixbuf_scale_to_size(pixbuf, dpi_scale, min_size, max_size);
383+
pixbuf = icon_pixbuf_scale_to_size(pixbuf, min_size, max_size);
384384

385385
return pixbuf;
386386
}

src/icon.h

+4-4
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ cairo_surface_t *gdk_pixbuf_to_cairo_surface(GdkPixbuf *pixbuf);
1818
* @return an instance of `GdkPixbuf`
1919
* @retval NULL: file does not exist, not readable, etc..
2020
*/
21-
GdkPixbuf *get_pixbuf_from_file(const char *filename, int min_size, int max_size, double scale);
21+
GdkPixbuf *get_pixbuf_from_file(const char *filename, int min_size, int max_size);
2222

2323

2424
/**
@@ -27,12 +27,12 @@ GdkPixbuf *get_pixbuf_from_file(const char *filename, int min_size, int max_size
2727
* If scale is 2 for example, the icon will render in twice the size, but
2828
* get_icon_width still returns the same size as when scale is 1.
2929
*/
30-
int get_icon_width(cairo_surface_t *icon, double scale);
30+
int get_icon_width(cairo_surface_t *icon);
3131

3232
/**
3333
* Get the unscaled icon height, see get_icon_width.
3434
*/
35-
int get_icon_height(cairo_surface_t *icon, double scale);
35+
int get_icon_height(cairo_surface_t *icon);
3636

3737
/** Retrieve a path from an icon name.
3838
*
@@ -62,7 +62,7 @@ char *get_path_from_icon_name(const char *iconname, int size);
6262
* @return an instance of `GdkPixbuf` derived from the GVariant
6363
* @retval NULL: GVariant parameter nulled, invalid or in wrong format
6464
*/
65-
GdkPixbuf *icon_get_for_data(GVariant *data, char **id, double dpi_scale, int min_size, int max_size);
65+
GdkPixbuf *icon_get_for_data(GVariant *data, char **id, int min_size, int max_size);
6666

6767
#endif
6868
/* vim: set ft=c tabstop=8 shiftwidth=8 expandtab textwidth=0: */

src/notification.c

+2-5
Original file line numberDiff line numberDiff line change
@@ -356,9 +356,7 @@ void notification_icon_replace_path(struct notification *n, const char *new_icon
356356
g_free(n->icon_path);
357357
n->icon_path = get_path_from_icon_name(new_icon, n->min_icon_size);
358358
if (n->icon_path) {
359-
GdkPixbuf *pixbuf = get_pixbuf_from_file(n->icon_path,
360-
n->min_icon_size, n->max_icon_size,
361-
draw_get_scale());
359+
GdkPixbuf *pixbuf = get_pixbuf_from_file(n->icon_path, n->min_icon_size, n->max_icon_size);
362360
if (pixbuf) {
363361
n->icon = gdk_pixbuf_to_cairo_surface(pixbuf);
364362
g_object_unref(pixbuf);
@@ -377,8 +375,7 @@ void notification_icon_replace_data(struct notification *n, GVariant *new_icon)
377375
n->icon = NULL;
378376
g_clear_pointer(&n->icon_id, g_free);
379377

380-
GdkPixbuf *icon = icon_get_for_data(new_icon, &n->icon_id,
381-
draw_get_scale(), n->min_icon_size, n->max_icon_size);
378+
GdkPixbuf *icon = icon_get_for_data(new_icon, &n->icon_id, n->min_icon_size, n->max_icon_size);
382379
n->icon = gdk_pixbuf_to_cairo_surface(icon);
383380
if (icon)
384381
g_object_unref(icon);

src/output.c

+2
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ const struct output output_x11 = {
2828

2929
x_display_surface,
3030
x_win_get_context,
31+
x_win_get_surface,
3132

3233
get_active_screen,
3334

@@ -51,6 +52,7 @@ const struct output output_wl = {
5152

5253
wl_display_surface,
5354
wl_win_get_context,
55+
wl_win_get_surface,
5456

5557
wl_get_active_screen,
5658

src/output.h

+2
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ struct screen_info {
2929
int dpi;
3030
};
3131

32+
// NOTE: Refactor the output struct
3233
struct output {
3334
bool (*init)(void);
3435
void (*deinit)(void);
@@ -42,6 +43,7 @@ struct output {
4243
void (*display_surface)(cairo_surface_t *srf, window win, const struct dimensions*);
4344

4445
cairo_t* (*win_get_context)(window);
46+
cairo_surface_t* (*win_get_surface)(window);
4547

4648
const struct screen_info* (*get_active_screen)(void);
4749

src/wayland/wl.c

+20-5
Original file line numberDiff line numberDiff line change
@@ -583,10 +583,10 @@ void wl_win_hide(window win) {
583583

584584
void wl_display_surface(cairo_surface_t *srf, window winptr, const struct dimensions* dim) {
585585
/* struct window_wl *win = (struct window_wl*)winptr; */
586-
int scale = wl_get_scale();
587-
LOG_D("Buffer size (scaled) %ix%i", dim->w * scale, dim->h * scale);
588-
ctx.current_buffer = get_next_buffer(ctx.shm, ctx.buffers,
589-
dim->w * scale, dim->h * scale);
586+
double scale = wl_get_scale();
587+
int w = round(dim->w * scale), h = round(dim->h * scale);
588+
LOG_D("Buffer size (scaled) %ix%i", w, h);
589+
ctx.current_buffer = get_next_buffer(ctx.shm, ctx.buffers, w, h);
590590

591591
if(ctx.current_buffer == NULL) {
592592
return;
@@ -595,7 +595,7 @@ void wl_display_surface(cairo_surface_t *srf, window winptr, const struct dimens
595595
cairo_t *c = ctx.current_buffer->cairo;
596596
cairo_save(c);
597597
cairo_set_source_surface(c, srf, 0, 0);
598-
cairo_rectangle(c, 0, 0, dim->w * scale, dim->h * scale);
598+
cairo_rectangle(c, 0, 0, w, h);
599599
cairo_fill(c);
600600
cairo_restore(c);
601601

@@ -618,6 +618,18 @@ cairo_t* wl_win_get_context(window winptr) {
618618
return win->c_ctx;
619619
}
620620

621+
cairo_surface_t* wl_win_get_surface(window winptr) {
622+
struct window_wl *win = (struct window_wl*)winptr;
623+
ctx.current_buffer = get_next_buffer(ctx.shm, ctx.buffers, 500, 500);
624+
625+
if(ctx.current_buffer == NULL) {
626+
return NULL;
627+
}
628+
629+
win->c_surface = ctx.current_buffer->surface;
630+
return win->c_surface;
631+
}
632+
621633
const struct screen_info* wl_get_active_screen(void) {
622634
static struct screen_info scr = {
623635
.w = 3840,
@@ -685,6 +697,9 @@ bool wl_have_fullscreen_window(void) {
685697
}
686698

687699
double wl_get_scale(void) {
700+
if (settings.scale > 0)
701+
return settings.scale;
702+
688703
int scale = 0;
689704
struct dunst_output *output = get_configured_output();
690705
if (output) {

src/wayland/wl.h

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ void wl_win_hide(window);
1818

1919
void wl_display_surface(cairo_surface_t *srf, window win, const struct dimensions*);
2020
cairo_t* wl_win_get_context(window);
21+
cairo_surface_t* wl_win_get_surface(window);
2122

2223
const struct screen_info* wl_get_active_screen(void);
2324

src/x11/x.c

+6-4
Original file line numberDiff line numberDiff line change
@@ -118,10 +118,7 @@ static void x_win_corners_shape(struct window_x11 *win, const int rad)
118118
cairo_paint(cr);
119119
cairo_set_source_rgba(cr, 1, 1, 1, 1);
120120

121-
draw_rounded_rect(cr, 0, 0,
122-
width, height,
123-
rad, 1,
124-
settings.corners);
121+
draw_rounded_rect(cr, 0, 0, width, height, rad, settings.corners);
125122
cairo_fill(cr);
126123

127124
cairo_show_page(cr);
@@ -197,6 +194,11 @@ cairo_t* x_win_get_context(window winptr)
197194
return ((struct window_x11*)win)->c_ctx;
198195
}
199196

197+
cairo_surface_t* x_win_get_surface(window win)
198+
{
199+
return ((struct window_x11*)win)->root_surface;
200+
}
201+
200202
static void setopacity(Window win, unsigned long opacity)
201203
{
202204
Atom _NET_WM_WINDOW_OPACITY =

src/x11/x.h

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ void x_win_hide(window);
3232
void x_display_surface(cairo_surface_t *srf, window, const struct dimensions *dim);
3333

3434
cairo_t* x_win_get_context(window);
35+
cairo_surface_t* x_win_get_surface(window);
3536

3637
/* X misc */
3738
bool x_is_idle(void);

0 commit comments

Comments
 (0)