|
| 1 | +/* |
| 2 | + * Twin - A Tiny Window System |
| 3 | + * Copyright (c) 2024 National Cheng Kung University, Taiwan |
| 4 | + * All rights reserved. |
| 5 | + */ |
| 6 | + |
| 7 | +#include <pixman.h> |
| 8 | +#include "twin_private.h" |
| 9 | + |
| 10 | +static void twin_argb32_to_pixman_color(twin_argb32_t argb, |
| 11 | + pixman_color_t *color) |
| 12 | +{ |
| 13 | + /* Extract ARGB every Byte */ |
| 14 | + uint16_t a = (argb >> 24) & 0xFF; |
| 15 | + uint16_t r = (argb >> 16) & 0xFF; |
| 16 | + uint16_t g = (argb >> 8) & 0xFF; |
| 17 | + uint16_t b = argb & 0xFF; |
| 18 | + |
| 19 | + /* 8bits -> 16bits (255 -> 65535) */ |
| 20 | + color->alpha = (a << 8) + a; |
| 21 | + color->red = (r << 8) + r; |
| 22 | + color->green = (g << 8) + g; |
| 23 | + color->blue = (b << 8) + b; |
| 24 | +} |
| 25 | + |
| 26 | +static const pixman_format_code_t twin_pixman_format[3] = { |
| 27 | + [TWIN_A8] = PIXMAN_a8, |
| 28 | + [TWIN_RGB16] = PIXMAN_r5g6b5, |
| 29 | + [TWIN_ARGB32] = PIXMAN_a8r8g8b8}; |
| 30 | + |
| 31 | +static pixman_format_code_t twin_to_pixman_format( |
| 32 | + const twin_format_t twin_format) |
| 33 | +{ |
| 34 | + return twin_pixman_format[twin_format]; |
| 35 | +} |
| 36 | + |
| 37 | +static const pixman_op_t twin_pixman_op[2] = |
| 38 | + {[TWIN_OVER] = PIXMAN_OP_OVER, [TWIN_SOURCE] = PIXMAN_OP_SRC}; |
| 39 | + |
| 40 | +static pixman_op_t twin_to_pixman_op(const twin_operator_t twin_op) |
| 41 | +{ |
| 42 | + return twin_pixman_op[twin_op]; |
| 43 | +} |
| 44 | + |
| 45 | +#define create_pixman_image_from_twin_pixmap(pixmap) \ |
| 46 | + ({ \ |
| 47 | + typeof(pixmap) _pixmap = (pixmap); \ |
| 48 | + pixman_image_create_bits(twin_to_pixman_format((_pixmap)->format), \ |
| 49 | + (_pixmap)->width, (_pixmap)->height, \ |
| 50 | + (_pixmap)->p.argb32, (_pixmap)->stride); \ |
| 51 | + }) |
| 52 | + |
| 53 | +static void pixmap_matrix_scale(pixman_image_t *src, twin_matrix_t *matrix) |
| 54 | +{ |
| 55 | + pixman_transform_t transform; |
| 56 | + pixman_transform_init_identity(&transform); |
| 57 | + |
| 58 | + pixman_fixed_t sx = matrix->m[0][0], sy = matrix->m[1][1]; |
| 59 | + |
| 60 | + pixman_transform_scale(&transform, NULL, sx, sy); |
| 61 | + pixman_image_set_transform(src, &transform); |
| 62 | +} |
| 63 | + |
| 64 | +void twin_composite(twin_pixmap_t *_dst, |
| 65 | + twin_coord_t dst_x, |
| 66 | + twin_coord_t dst_y, |
| 67 | + twin_operand_t *_src, |
| 68 | + twin_coord_t src_x, |
| 69 | + twin_coord_t src_y, |
| 70 | + twin_operand_t *_msk, |
| 71 | + twin_coord_t msk_x, |
| 72 | + twin_coord_t msk_y, |
| 73 | + twin_operator_t operator, |
| 74 | + twin_coord_t width, |
| 75 | + twin_coord_t height) |
| 76 | +{ |
| 77 | + pixman_image_t *src; |
| 78 | + if (_src->source_kind == TWIN_SOLID) { |
| 79 | + pixman_color_t source_pixel; |
| 80 | + twin_argb32_to_pixman_color(_src->u.argb, &source_pixel); |
| 81 | + src = pixman_image_create_solid_fill(&source_pixel); |
| 82 | + } else { |
| 83 | + twin_pixmap_t *src_pixmap = _src->u.pixmap; |
| 84 | + src = create_pixman_image_from_twin_pixmap(src_pixmap); |
| 85 | + |
| 86 | + if (!twin_matrix_is_identity(&(src_pixmap->transform))) |
| 87 | + pixmap_matrix_scale(src, &(src_pixmap->transform)); |
| 88 | + } |
| 89 | + |
| 90 | + pixman_image_t *dst = create_pixman_image_from_twin_pixmap(_dst); |
| 91 | + |
| 92 | + /* Set origin */ |
| 93 | + twin_coord_t ox, oy; |
| 94 | + twin_pixmap_get_origin(_dst, &ox, &oy); |
| 95 | + ox += dst_x; |
| 96 | + oy += dst_y; |
| 97 | + |
| 98 | + if (!_msk) { |
| 99 | + pixman_image_composite(twin_to_pixman_op(operator), src, NULL, dst, |
| 100 | + src_x, src_y, 0, 0, ox, oy, width, height); |
| 101 | + } else { |
| 102 | + pixman_image_t *msk = |
| 103 | + create_pixman_image_from_twin_pixmap(_msk->u.pixmap); |
| 104 | + pixman_image_composite(twin_to_pixman_op(operator), src, msk, dst, |
| 105 | + src_x, src_y, msk_x, msk_y, ox, oy, width, |
| 106 | + height); |
| 107 | + pixman_image_unref(msk); |
| 108 | + } |
| 109 | + |
| 110 | + pixman_image_unref(src); |
| 111 | + pixman_image_unref(dst); |
| 112 | +} |
| 113 | + |
| 114 | +void twin_fill(twin_pixmap_t *_dst, |
| 115 | + twin_argb32_t pixel, |
| 116 | + twin_operator_t operator, |
| 117 | + twin_coord_t left, |
| 118 | + twin_coord_t top, |
| 119 | + twin_coord_t right, |
| 120 | + twin_coord_t bottom) |
| 121 | +{ |
| 122 | + /* offset */ |
| 123 | + left += _dst->origin_x; |
| 124 | + top += _dst->origin_y; |
| 125 | + right += _dst->origin_x; |
| 126 | + bottom += _dst->origin_y; |
| 127 | + |
| 128 | + /* clip */ |
| 129 | + if (left < _dst->clip.left) |
| 130 | + left = _dst->clip.left; |
| 131 | + if (right > _dst->clip.right) |
| 132 | + right = _dst->clip.right; |
| 133 | + if (top < _dst->clip.top) |
| 134 | + top = _dst->clip.top; |
| 135 | + if (bottom > _dst->clip.bottom) |
| 136 | + bottom = _dst->clip.bottom; |
| 137 | + |
| 138 | + pixman_image_t *dst = create_pixman_image_from_twin_pixmap(_dst); |
| 139 | + pixman_color_t color; |
| 140 | + twin_argb32_to_pixman_color(pixel, &color); |
| 141 | + pixman_image_fill_rectangles( |
| 142 | + twin_to_pixman_op(operator), dst, &color, 1, |
| 143 | + &(pixman_rectangle16_t){left, top, right - left, bottom - top}); |
| 144 | + |
| 145 | + twin_pixmap_damage(_dst, left, top, right, bottom); |
| 146 | + |
| 147 | + pixman_image_unref(dst); |
| 148 | +} |
| 149 | + |
| 150 | +/* Same function in draw.c */ |
| 151 | +static twin_argb32_t _twin_apply_alpha(twin_argb32_t v) |
| 152 | +{ |
| 153 | + uint16_t t1, t2, t3; |
| 154 | + twin_a8_t alpha = twin_get_8(v, |
| 155 | +#if __BYTE_ORDER == __BIG_ENDIAN |
| 156 | + 0 |
| 157 | +#else |
| 158 | + 24 |
| 159 | +#endif |
| 160 | + ); |
| 161 | + |
| 162 | + /* clear RGB data if alpha is zero */ |
| 163 | + if (!alpha) |
| 164 | + return 0; |
| 165 | + |
| 166 | +#if __BYTE_ORDER == __BIG_ENDIAN |
| 167 | + /* twin needs ARGB format */ |
| 168 | + return alpha << 24 | twin_int_mult(twin_get_8(v, 24), alpha, t1) << 16 | |
| 169 | + twin_int_mult(twin_get_8(v, 16), alpha, t2) << 8 | |
| 170 | + twin_int_mult(twin_get_8(v, 8), alpha, t3) << 0; |
| 171 | +#else |
| 172 | + return alpha << 24 | twin_int_mult(twin_get_8(v, 0), alpha, t1) << 16 | |
| 173 | + twin_int_mult(twin_get_8(v, 8), alpha, t2) << 8 | |
| 174 | + twin_int_mult(twin_get_8(v, 16), alpha, t3) << 0; |
| 175 | +#endif |
| 176 | +} |
| 177 | + |
| 178 | +void twin_premultiply_alpha(twin_pixmap_t *px) |
| 179 | +{ |
| 180 | + int x, y; |
| 181 | + twin_pointer_t p; |
| 182 | + |
| 183 | + if (px->format != TWIN_ARGB32) |
| 184 | + return; |
| 185 | + |
| 186 | + for (y = 0; y < px->height; y++) { |
| 187 | + p.b = px->p.b + y * px->stride; |
| 188 | + |
| 189 | + for (x = 0; x < px->width; x++) |
| 190 | + p.argb32[x] = _twin_apply_alpha(p.argb32[x]); |
| 191 | + } |
| 192 | +} |
0 commit comments