| 1 |
#include <uthash.h> |
| 2 |
#include <xcb/xcb.h> |
| 3 |
|
| 4 |
#include <picom/types.h> |
| 5 |
|
| 6 |
#include "backend/backend.h" |
| 7 |
#include "backend/backend_common.h" |
| 8 |
#include "common.h" |
| 9 |
#include "compiler.h" |
| 10 |
#include "config.h" |
| 11 |
#include "log.h" |
| 12 |
#include "region.h" |
| 13 |
#include "utils/misc.h" |
| 14 |
#include "utils/uthash_extra.h" |
| 15 |
#include "x.h" |
| 16 |
|
| 17 |
struct dummy_image { |
| 18 |
enum backend_image_format format; |
| 19 |
xcb_pixmap_t pixmap; |
| 20 |
struct list_node siblings; |
| 21 |
UT_hash_handle hh; |
| 22 |
}; |
| 23 |
|
| 24 |
struct dummy_data { |
| 25 |
struct backend_base base; |
| 26 |
struct dummy_image *pixmap_images; |
| 27 |
struct list_node non_pixmap_images; |
| 28 |
|
| 29 |
struct dummy_image back_buffer; |
| 30 |
}; |
| 31 |
|
| 32 |
const struct backend_operations dummy_ops; |
| 33 |
|
| 34 |
struct backend_base *dummy_init(session_t *ps attr_unused, xcb_window_t target attr_unused) { |
| 35 |
auto ret = ccalloc(1, struct dummy_data); |
| 36 |
init_backend_base(&ret->base, ps); |
| 37 |
ret->base.ops = dummy_ops; |
| 38 |
list_init_head(&ret->non_pixmap_images); |
| 39 |
return &ret->base; |
| 40 |
} |
| 41 |
|
| 42 |
void dummy_deinit(struct backend_base *data) { |
| 43 |
auto dummy = (struct dummy_data *)data; |
| 44 |
HASH_ITER2(dummy->pixmap_images, img) { |
| 45 |
log_warn("Backend image %p for pixmap %#010x is not freed", img, img->pixmap); |
| 46 |
HASH_DEL(dummy->pixmap_images, img); |
| 47 |
xcb_free_pixmap(data->c->c, img->pixmap); |
| 48 |
free(img); |
| 49 |
} |
| 50 |
list_foreach_safe(struct dummy_image, img, &dummy->non_pixmap_images, siblings) { |
| 51 |
log_warn("Backend image %p for non-pixmap is not freed", img); |
| 52 |
list_remove(&img->siblings); |
| 53 |
free(img); |
| 54 |
} |
| 55 |
free(dummy); |
| 56 |
} |
| 57 |
|
| 58 |
static void dummy_check_image(struct backend_base *base, image_handle image) { |
| 59 |
auto dummy = (struct dummy_data *)base; |
| 60 |
auto img = (struct dummy_image *)image; |
| 61 |
if (img == (struct dummy_image *)&dummy->back_buffer) { |
| 62 |
return; |
| 63 |
} |
| 64 |
if (!img->pixmap) { |
| 65 |
return; |
| 66 |
} |
| 67 |
struct dummy_image *tmp = NULL; |
| 68 |
HASH_FIND_INT(dummy->pixmap_images, &img->pixmap, tmp); |
| 69 |
if (!tmp) { |
| 70 |
log_warn("Using an invalid (possibly freed) image"); |
| 71 |
assert(false); |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
bool dummy_blit(struct backend_base *base, ivec2 origin attr_unused, image_handle target, |
| 76 |
const struct backend_blit_args *args) { |
| 77 |
dummy_check_image(base, target); |
| 78 |
dummy_check_image(base, args->source_image); |
| 79 |
if (args->source_mask) { |
| 80 |
auto mask = (struct dummy_image *)args->source_mask->image; |
| 81 |
if (mask->format != BACKEND_IMAGE_FORMAT_MASK) { |
| 82 |
log_error("Invalid mask image format"); |
| 83 |
assert(false); |
| 84 |
return false; |
| 85 |
} |
| 86 |
dummy_check_image(base, args->source_mask->image); |
| 87 |
} |
| 88 |
return true; |
| 89 |
} |
| 90 |
|
| 91 |
bool dummy_blur(struct backend_base *base, ivec2 origin attr_unused, image_handle target, |
| 92 |
const struct backend_blur_args *args) { |
| 93 |
dummy_check_image(base, target); |
| 94 |
dummy_check_image(base, args->source_image); |
| 95 |
if (args->source_mask) { |
| 96 |
auto mask = (struct dummy_image *)args->source_mask->image; |
| 97 |
if (mask->format != BACKEND_IMAGE_FORMAT_MASK) { |
| 98 |
log_error("Invalid mask image format"); |
| 99 |
assert(false); |
| 100 |
return false; |
| 101 |
} |
| 102 |
dummy_check_image(base, args->source_mask->image); |
| 103 |
} |
| 104 |
return true; |
| 105 |
} |
| 106 |
|
| 107 |
image_handle dummy_bind_pixmap(struct backend_base *base, xcb_pixmap_t pixmap, |
| 108 |
struct xvisual_info fmt attr_unused) { |
| 109 |
auto dummy = (struct dummy_data *)base; |
| 110 |
struct dummy_image *img = NULL; |
| 111 |
HASH_FIND_INT(dummy->pixmap_images, &pixmap, img); |
| 112 |
if (img) { |
| 113 |
log_error("Pixmap %#010x is already bound to an image", pixmap); |
| 114 |
return NULL; |
| 115 |
} |
| 116 |
|
| 117 |
img = ccalloc(1, struct dummy_image); |
| 118 |
img->format = BACKEND_IMAGE_FORMAT_PIXMAP; |
| 119 |
img->pixmap = pixmap; |
| 120 |
|
| 121 |
HASH_ADD_INT(dummy->pixmap_images, pixmap, img); |
| 122 |
return (image_handle)img; |
| 123 |
} |
| 124 |
|
| 125 |
xcb_pixmap_t dummy_release_image(backend_t *base, image_handle image) { |
| 126 |
auto dummy = (struct dummy_data *)base; |
| 127 |
if ((struct dummy_image *)image == &dummy->back_buffer) { |
| 128 |
return XCB_NONE; |
| 129 |
} |
| 130 |
auto img = (struct dummy_image *)image; |
| 131 |
xcb_pixmap_t pixmap = XCB_NONE; |
| 132 |
if (img->pixmap) { |
| 133 |
HASH_DEL(dummy->pixmap_images, img); |
| 134 |
pixmap = img->pixmap; |
| 135 |
} else { |
| 136 |
list_remove(&img->siblings); |
| 137 |
} |
| 138 |
free(img); |
| 139 |
return pixmap; |
| 140 |
} |
| 141 |
|
| 142 |
int dummy_buffer_age(struct backend_base *base attr_unused) { |
| 143 |
return 2; |
| 144 |
} |
| 145 |
|
| 146 |
bool dummy_apply_alpha(struct backend_base *base, image_handle target, |
| 147 |
double alpha attr_unused, const region_t *reg attr_unused) { |
| 148 |
dummy_check_image(base, target); |
| 149 |
return true; |
| 150 |
} |
| 151 |
|
| 152 |
bool dummy_copy_area(struct backend_base *base, ivec2 origin attr_unused, image_handle target, |
| 153 |
image_handle source, const region_t *reg attr_unused) { |
| 154 |
dummy_check_image(base, target); |
| 155 |
dummy_check_image(base, source); |
| 156 |
return true; |
| 157 |
} |
| 158 |
|
| 159 |
bool dummy_clear(struct backend_base *base, image_handle target, |
| 160 |
struct color color attr_unused) { |
| 161 |
dummy_check_image(base, target); |
| 162 |
return true; |
| 163 |
} |
| 164 |
|
| 165 |
image_handle dummy_new_image(struct backend_base *base, enum backend_image_format format, |
| 166 |
ivec2 size attr_unused) { |
| 167 |
auto new_img = ccalloc(1, struct dummy_image); |
| 168 |
auto dummy = (struct dummy_data *)base; |
| 169 |
list_insert_after(&dummy->non_pixmap_images, &new_img->siblings); |
| 170 |
new_img->format = format; |
| 171 |
return (image_handle)new_img; |
| 172 |
} |
| 173 |
|
| 174 |
image_handle dummy_back_buffer(struct backend_base *base) { |
| 175 |
auto dummy = (struct dummy_data *)base; |
| 176 |
return (image_handle)&dummy->back_buffer; |
| 177 |
} |
| 178 |
|
| 179 |
void *dummy_create_blur_context(struct backend_base *base attr_unused, |
| 180 |
enum blur_method method attr_unused, |
| 181 |
enum backend_image_format format attr_unused, |
| 182 |
void *args attr_unused) { |
| 183 |
static int dummy_context; |
| 184 |
return &dummy_context; |
| 185 |
} |
| 186 |
|
| 187 |
void dummy_destroy_blur_context(struct backend_base *base attr_unused, void *ctx attr_unused) { |
| 188 |
} |
| 189 |
|
| 190 |
void dummy_get_blur_size(void *ctx attr_unused, int *width, int *height) { |
| 191 |
|
| 192 |
|
| 193 |
*width = 5; |
| 194 |
*height = 5; |
| 195 |
} |
| 196 |
|
| 197 |
uint32_t dummy_image_capabilities(struct backend_base *base attr_unused, |
| 198 |
image_handle image attr_unused) { |
| 199 |
return BACKEND_IMAGE_CAP_SRC | BACKEND_IMAGE_CAP_DST; |
| 200 |
} |
| 201 |
|
| 202 |
bool dummy_is_format_supported(struct backend_base *base attr_unused, |
| 203 |
enum backend_image_format format attr_unused) { |
| 204 |
return true; |
| 205 |
} |
| 206 |
|
| 207 |
static int dummy_max_buffer_age(struct backend_base *base attr_unused) { |
| 208 |
return 5; |
| 209 |
} |
| 210 |
|
| 211 |
#define PICOM_BACKEND_DUMMY_MAJOR (0UL) |
| 212 |
#define PICOM_BACKEND_DUMMY_MINOR (1UL) |
| 213 |
|
| 214 |
static void dummy_version(struct backend_base * , uint64_t *major, uint64_t *minor) { |
| 215 |
*major = PICOM_BACKEND_DUMMY_MAJOR; |
| 216 |
*minor = PICOM_BACKEND_DUMMY_MINOR; |
| 217 |
} |
| 218 |
|
| 219 |
const struct backend_operations dummy_ops = { |
| 220 |
.apply_alpha = dummy_apply_alpha, |
| 221 |
.back_buffer = dummy_back_buffer, |
| 222 |
.blit = dummy_blit, |
| 223 |
.blur = dummy_blur, |
| 224 |
.clear = dummy_clear, |
| 225 |
.copy_area = dummy_copy_area, |
| 226 |
.copy_area_quantize = dummy_copy_area, |
| 227 |
.image_capabilities = dummy_image_capabilities, |
| 228 |
.is_format_supported = dummy_is_format_supported, |
| 229 |
.new_image = dummy_new_image, |
| 230 |
.bind_pixmap = dummy_bind_pixmap, |
| 231 |
.quirks = backend_no_quirks, |
| 232 |
.version = dummy_version, |
| 233 |
.release_image = dummy_release_image, |
| 234 |
|
| 235 |
.init = dummy_init, |
| 236 |
.deinit = dummy_deinit, |
| 237 |
.buffer_age = dummy_buffer_age, |
| 238 |
.max_buffer_age = dummy_max_buffer_age, |
| 239 |
|
| 240 |
.create_blur_context = dummy_create_blur_context, |
| 241 |
.destroy_blur_context = dummy_destroy_blur_context, |
| 242 |
.get_blur_size = dummy_get_blur_size, |
| 243 |
}; |
| 244 |
|
| 245 |
BACKEND_ENTRYPOINT(dummy_register) { |
| 246 |
if (!backend_register(PICOM_BACKEND_MAJOR, PICOM_BACKEND_MINOR, "dummy", |
| 247 |
dummy_ops.init, false)) { |
| 248 |
log_error("Failed to register dummy backend"); |
| 249 |
} |
| 250 |
} |