draw_util: refactor surface_initialized macro into function

This makes it possible to set a breakpoint in gdb on a line in the function and
get a backtrace of un-initialized surface access.
This commit is contained in:
Michael Stapelberg 2022-11-03 23:02:19 +01:00 committed by Michael Stapelberg
parent a59423df81
commit 6e6af01b7a

View File

@ -20,13 +20,13 @@ extern xcb_visualtype_t *visual_type;
/* Forward declarations */ /* Forward declarations */
static void draw_util_set_source_color(surface_t *surface, color_t color); static void draw_util_set_source_color(surface_t *surface, color_t color);
#define RETURN_UNLESS_SURFACE_INITIALIZED(surface) \ static bool surface_initialized(surface_t *surface) {
do { \ if (surface->id == XCB_NONE) {
if ((surface)->id == XCB_NONE) { \ ELOG("Surface %p is not initialized, skipping drawing.\n", surface);
ELOG("Surface %p is not initialized, skipping drawing.\n", surface); \ return false;
return; \ }
} \ return true;
} while (0) }
/* /*
* Initialize the surface to represent the given drawable. * Initialize the surface to represent the given drawable.
@ -131,7 +131,9 @@ color_t draw_util_hex_to_color(const char *color) {
* *
*/ */
static void draw_util_set_source_color(surface_t *surface, color_t color) { static void draw_util_set_source_color(surface_t *surface, color_t color) {
RETURN_UNLESS_SURFACE_INITIALIZED(surface); if (!surface_initialized(surface)) {
return;
}
cairo_set_source_rgba(surface->cr, color.red, color.green, color.blue, color.alpha); cairo_set_source_rgba(surface->cr, color.red, color.green, color.blue, color.alpha);
} }
@ -143,7 +145,9 @@ static void draw_util_set_source_color(surface_t *surface, color_t color) {
* *
*/ */
void draw_util_text(i3String *text, surface_t *surface, color_t fg_color, color_t bg_color, int x, int y, int max_width) { void draw_util_text(i3String *text, surface_t *surface, color_t fg_color, color_t bg_color, int x, int y, int max_width) {
RETURN_UNLESS_SURFACE_INITIALIZED(surface); if (!surface_initialized(surface)) {
return;
}
/* Flush any changes before we draw the text as this might use XCB directly. */ /* Flush any changes before we draw the text as this might use XCB directly. */
CAIRO_SURFACE_FLUSH(surface->surface); CAIRO_SURFACE_FLUSH(surface->surface);
@ -162,7 +166,9 @@ void draw_util_text(i3String *text, surface_t *surface, color_t fg_color, color_
* *
*/ */
void draw_util_image(cairo_surface_t *image, surface_t *surface, int x, int y, int width, int height) { void draw_util_image(cairo_surface_t *image, surface_t *surface, int x, int y, int width, int height) {
RETURN_UNLESS_SURFACE_INITIALIZED(surface); if (!surface_initialized(surface)) {
return;
}
cairo_save(surface->cr); cairo_save(surface->cr);
@ -186,7 +192,9 @@ void draw_util_image(cairo_surface_t *image, surface_t *surface, int x, int y, i
* *
*/ */
void draw_util_rectangle(surface_t *surface, color_t color, double x, double y, double w, double h) { void draw_util_rectangle(surface_t *surface, color_t color, double x, double y, double w, double h) {
RETURN_UNLESS_SURFACE_INITIALIZED(surface); if (!surface_initialized(surface)) {
return;
}
cairo_save(surface->cr); cairo_save(surface->cr);
@ -211,7 +219,9 @@ void draw_util_rectangle(surface_t *surface, color_t color, double x, double y,
* *
*/ */
void draw_util_clear_surface(surface_t *surface, color_t color) { void draw_util_clear_surface(surface_t *surface, color_t color) {
RETURN_UNLESS_SURFACE_INITIALIZED(surface); if (!surface_initialized(surface)) {
return;
}
cairo_save(surface->cr); cairo_save(surface->cr);
@ -236,8 +246,10 @@ void draw_util_clear_surface(surface_t *surface, color_t color) {
*/ */
void draw_util_copy_surface(surface_t *src, surface_t *dest, double src_x, double src_y, void draw_util_copy_surface(surface_t *src, surface_t *dest, double src_x, double src_y,
double dest_x, double dest_y, double width, double height) { double dest_x, double dest_y, double width, double height) {
RETURN_UNLESS_SURFACE_INITIALIZED(src); if (!surface_initialized(src) ||
RETURN_UNLESS_SURFACE_INITIALIZED(dest); !surface_initialized(dest)) {
return;
}
cairo_save(dest->cr); cairo_save(dest->cr);