| 1 | module render |
| 2 | |
| 3 | import math |
| 4 | import vx11 |
| 5 | |
| 6 | #include "@VMODROOT/render/tests/abi.h" |
| 7 | |
| 8 | fn C.vx11_render_test_fill_format(&Format) |
| 9 | fn C.vx11_render_test_fill_attributes(&PictureAttributes) |
| 10 | fn C.vx11_render_test_fill_index(&IndexValue) |
| 11 | fn C.vx11_render_test_fill_anim(&AnimCursor) |
| 12 | fn C.vx11_render_test_fill_glyph(&GlyphInfo) |
| 13 | fn C.vx11_render_test_fill_elt(&GlyphElt32) |
| 14 | fn C.vx11_render_test_fill_trapezoid(&Trapezoid) |
| 15 | fn C.vx11_render_test_fill_radial(&RadialGradient) |
| 16 | fn C.vx11_render_test_fill_trap(&Trap) |
| 17 | fn C.vx11_render_test_fill_filters(&Filters, &i16) |
| 18 | fn C.vx11_render_test_constants(&i32) &vx11.CLong |
| 19 | fn C.vx11_render_test_string(i32) &char |
| 20 | |
| 21 | const all_ones = ~vx11.CULong(0) |
| 22 | |
| 23 | fn test_c_long_and_short_field_widths() { |
| 24 | mut f := Format{} |
| 25 | C.vx11_render_test_fill_format(&f) |
| 26 | assert f.id == PictFormat(all_ones) |
| 27 | assert f.@type == i32(-2147483648) |
| 28 | assert f.depth == i32(2147483647) |
| 29 | assert f.direct.red == i16(-32768) |
| 30 | assert f.direct.alphaMask == i16(32767) |
| 31 | assert f.colormap == vx11.Colormap(all_ones) |
| 32 | mut a := PictureAttributes{} |
| 33 | C.vx11_render_test_fill_attributes(&a) |
| 34 | assert a.alpha_map == Picture(all_ones) |
| 35 | assert a.clip_mask == vx11.Pixmap(all_ones) |
| 36 | assert a.dither == vx11.Atom(all_ones) |
| 37 | assert a.component_alpha == i32(-2147483648) |
| 38 | mut index := IndexValue{} |
| 39 | C.vx11_render_test_fill_index(&index) |
| 40 | assert index.pixel == all_ones |
| 41 | assert index.alpha == u16(0xffff) |
| 42 | mut frame := AnimCursor{} |
| 43 | C.vx11_render_test_fill_anim(&frame) |
| 44 | assert frame.cursor == vx11.Cursor(all_ones) |
| 45 | assert frame.delay == all_ones |
| 46 | mut glyph := GlyphInfo{} |
| 47 | C.vx11_render_test_fill_glyph(&glyph) |
| 48 | assert glyph.width == u16(0xffff) |
| 49 | assert glyph.yOff == i16(-32768) |
| 50 | mut elt := GlyphElt32{ chars: unsafe { nil } } |
| 51 | C.vx11_render_test_fill_elt(&elt) |
| 52 | assert elt.glyphset == GlyphSet(all_ones) |
| 53 | assert elt.yOff == i32(-2147483648) |
| 54 | mut trapezoid := Trapezoid{} |
| 55 | C.vx11_render_test_fill_trapezoid(&trapezoid) |
| 56 | assert trapezoid.bottom == Fixed(2147483647) |
| 57 | assert trapezoid.right.p2.y == Fixed(-2147483648) |
| 58 | mut radial := RadialGradient{} |
| 59 | C.vx11_render_test_fill_radial(&radial) |
| 60 | assert radial.outer.radius == Fixed(-2147483648) |
| 61 | mut trap := Trap{} |
| 62 | C.vx11_render_test_fill_trap(&trap) |
| 63 | assert trap.bottom.y == Fixed(-2147483648) |
| 64 | mut aliases := [i16(0)] |
| 65 | mut filters := Filters{ filter: unsafe { nil }, alias: unsafe { nil } } |
| 66 | C.vx11_render_test_fill_filters(&filters, aliases.data) |
| 67 | assert filters.nfilter == i32(2147483647) |
| 68 | assert unsafe { filters.alias[0] } == i16(-32768) |
| 69 | } |
| 70 | |
| 71 | fn test_constants_match_headers() { |
| 72 | mut count := i32(0) |
| 73 | values := C.vx11_render_test_constants(&count) |
| 74 | expected := [ |
| 75 | i64(major_version), |
| 76 | i64(minor_version), |
| 77 | i64(x_query_version), |
| 78 | i64(x_query_pict_formats), |
| 79 | i64(x_query_pict_index_values), |
| 80 | i64(x_query_dithers), |
| 81 | i64(x_create_picture), |
| 82 | i64(x_change_picture), |
| 83 | i64(x_set_picture_clip_rectangles), |
| 84 | i64(x_free_picture), |
| 85 | i64(x_composite), |
| 86 | i64(x_scale), |
| 87 | i64(x_trapezoids), |
| 88 | i64(x_triangles), |
| 89 | i64(x_tri_strip), |
| 90 | i64(x_tri_fan), |
| 91 | i64(x_color_trapezoids), |
| 92 | i64(x_color_triangles), |
| 93 | i64(x_create_glyph_set), |
| 94 | i64(x_reference_glyph_set), |
| 95 | i64(x_free_glyph_set), |
| 96 | i64(x_add_glyphs), |
| 97 | i64(x_add_glyphs_from_picture), |
| 98 | i64(x_free_glyphs), |
| 99 | i64(x_composite_glyphs8), |
| 100 | i64(x_composite_glyphs16), |
| 101 | i64(x_composite_glyphs32), |
| 102 | i64(x_fill_rectangles), |
| 103 | i64(x_create_cursor), |
| 104 | i64(x_set_picture_transform), |
| 105 | i64(x_query_filters), |
| 106 | i64(x_set_picture_filter), |
| 107 | i64(x_create_anim_cursor), |
| 108 | i64(x_add_traps), |
| 109 | i64(x_create_solid_fill), |
| 110 | i64(x_create_linear_gradient), |
| 111 | i64(x_create_radial_gradient), |
| 112 | i64(x_create_conical_gradient), |
| 113 | i64(number_requests), |
| 114 | i64(bad_pict_format), |
| 115 | i64(bad_picture), |
| 116 | i64(bad_pict_op), |
| 117 | i64(bad_glyph_set), |
| 118 | i64(bad_glyph), |
| 119 | i64(number_errors), |
| 120 | i64(pict_type_indexed), |
| 121 | i64(pict_type_direct), |
| 122 | i64(op_minimum), |
| 123 | i64(op_clear), |
| 124 | i64(op_src), |
| 125 | i64(op_dst), |
| 126 | i64(op_over), |
| 127 | i64(op_over_reverse), |
| 128 | i64(op_in), |
| 129 | i64(op_in_reverse), |
| 130 | i64(op_out), |
| 131 | i64(op_out_reverse), |
| 132 | i64(op_atop), |
| 133 | i64(op_atop_reverse), |
| 134 | i64(op_xor), |
| 135 | i64(op_add), |
| 136 | i64(op_saturate), |
| 137 | i64(op_maximum), |
| 138 | i64(op_disjoint_minimum), |
| 139 | i64(op_disjoint_clear), |
| 140 | i64(op_disjoint_src), |
| 141 | i64(op_disjoint_dst), |
| 142 | i64(op_disjoint_over), |
| 143 | i64(op_disjoint_over_reverse), |
| 144 | i64(op_disjoint_in), |
| 145 | i64(op_disjoint_in_reverse), |
| 146 | i64(op_disjoint_out), |
| 147 | i64(op_disjoint_out_reverse), |
| 148 | i64(op_disjoint_atop), |
| 149 | i64(op_disjoint_atop_reverse), |
| 150 | i64(op_disjoint_xor), |
| 151 | i64(op_disjoint_maximum), |
| 152 | i64(op_conjoint_minimum), |
| 153 | i64(op_conjoint_clear), |
| 154 | i64(op_conjoint_src), |
| 155 | i64(op_conjoint_dst), |
| 156 | i64(op_conjoint_over), |
| 157 | i64(op_conjoint_over_reverse), |
| 158 | i64(op_conjoint_in), |
| 159 | i64(op_conjoint_in_reverse), |
| 160 | i64(op_conjoint_out), |
| 161 | i64(op_conjoint_out_reverse), |
| 162 | i64(op_conjoint_atop), |
| 163 | i64(op_conjoint_atop_reverse), |
| 164 | i64(op_conjoint_xor), |
| 165 | i64(op_conjoint_maximum), |
| 166 | i64(op_blend_minimum), |
| 167 | i64(op_multiply), |
| 168 | i64(op_screen), |
| 169 | i64(op_overlay), |
| 170 | i64(op_darken), |
| 171 | i64(op_lighten), |
| 172 | i64(op_color_dodge), |
| 173 | i64(op_color_burn), |
| 174 | i64(op_hard_light), |
| 175 | i64(op_soft_light), |
| 176 | i64(op_difference), |
| 177 | i64(op_exclusion), |
| 178 | i64(op_hsl_hue), |
| 179 | i64(op_hsl_saturation), |
| 180 | i64(op_hsl_color), |
| 181 | i64(op_hsl_luminosity), |
| 182 | i64(op_blend_maximum), |
| 183 | i64(poly_edge_sharp), |
| 184 | i64(poly_edge_smooth), |
| 185 | i64(poly_mode_precise), |
| 186 | i64(poly_mode_imprecise), |
| 187 | i64(cp_repeat), |
| 188 | i64(cp_alpha_map), |
| 189 | i64(cp_alpha_x_origin), |
| 190 | i64(cp_alpha_y_origin), |
| 191 | i64(cp_clip_x_origin), |
| 192 | i64(cp_clip_y_origin), |
| 193 | i64(cp_clip_mask), |
| 194 | i64(cp_graphics_exposure), |
| 195 | i64(cp_subwindow_mode), |
| 196 | i64(cp_poly_edge), |
| 197 | i64(cp_poly_mode), |
| 198 | i64(cp_dither), |
| 199 | i64(cp_component_alpha), |
| 200 | i64(cp_last_bit), |
| 201 | i64(filter_alias_none), |
| 202 | i64(subpixel_unknown), |
| 203 | i64(subpixel_horizontal_rgb), |
| 204 | i64(subpixel_horizontal_bgr), |
| 205 | i64(subpixel_vertical_rgb), |
| 206 | i64(subpixel_vertical_bgr), |
| 207 | i64(subpixel_none), |
| 208 | i64(repeat_none), |
| 209 | i64(repeat_normal), |
| 210 | i64(repeat_pad), |
| 211 | i64(repeat_reflect), |
| 212 | i64(format_id), |
| 213 | i64(format_type), |
| 214 | i64(format_depth), |
| 215 | i64(format_red), |
| 216 | i64(format_red_mask), |
| 217 | i64(format_green), |
| 218 | i64(format_green_mask), |
| 219 | i64(format_blue), |
| 220 | i64(format_blue_mask), |
| 221 | i64(format_alpha), |
| 222 | i64(format_alpha_mask), |
| 223 | i64(format_colormap), |
| 224 | i64(standard_argb32), |
| 225 | i64(standard_rgb24), |
| 226 | i64(standard_a8), |
| 227 | i64(standard_a4), |
| 228 | i64(standard_a1), |
| 229 | i64(standard_num), |
| 230 | ] |
| 231 | assert int(count) == expected.len |
| 232 | for i, value in expected { |
| 233 | assert value == i64(unsafe { values[i] }), 'constant ${i}' |
| 234 | } |
| 235 | texts := [name, filter_nearest, filter_bilinear, filter_convolution, filter_fast, filter_good, |
| 236 | filter_best] |
| 237 | for i, text in texts { |
| 238 | assert text == unsafe { cstring_to_vstring(C.vx11_render_test_string(i32(i))) } |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | fn test_fixed_point_conversion() { |
| 243 | assert fixed(1.0) == 65536 |
| 244 | assert fixed(0.5) == 32768 |
| 245 | assert fixed(-2.0) == -131072 |
| 246 | assert fixed(0.0) == 0 |
| 247 | assert fixed_to_double(fixed(-3.25)) == -3.25 |
| 248 | assert to_fixed(32767.5)! == fixed(32767.5) |
| 249 | assert to_fixed(-32768.0)! == Fixed(-2147483648) |
| 250 | for bad in [32768.0, -32768.5, math.inf(1), math.nan()] { |
| 251 | if _ := to_fixed(bad) { |
| 252 | assert false, 'accepted ${bad}' |
| 253 | } |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | fn test_transforms() { |
| 258 | t := identity_transform() |
| 259 | for row in 0 .. 3 { |
| 260 | for col in 0 .. 3 { |
| 261 | assert t.matrix[row][col] == if row == col { 65536 } else { 0 } |
| 262 | } |
| 263 | } |
| 264 | s := scale_transform(0.5, 2)! |
| 265 | assert s.matrix[0][0] == 32768 && s.matrix[1][1] == 131072 && s.matrix[2][2] == 65536 |
| 266 | if _ := scale_transform(40000, 1) { |
| 267 | assert false |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | fn test_premultiplied_colors() { |
| 272 | c := premultiplied(0xffff, 0x8000, 0, 0x8000) |
| 273 | assert c.red == 0x8000 && c.green == 0x4000 && c.blue == 0 && c.alpha == 0x8000 |
| 274 | opaque := premultiplied(0x1234, 0x5678, 0x9abc, 0xffff) |
| 275 | assert opaque.red == 0x1234 && opaque.green == 0x5678 && opaque.blue == 0x9abc |
| 276 | clear := premultiplied(0xffff, 0xffff, 0xffff, 0) |
| 277 | assert clear.red == 0 && clear.alpha == 0 |
| 278 | } |
| 279 | |
| 280 | fn test_operator_and_repeat_versions() { |
| 281 | old := &Extension{ connection: unsafe { nil }, major: 0, minor: 1 } |
| 282 | modern := &Extension{ connection: unsafe { nil }, major: 0, minor: 11 } |
| 283 | old.check_op(op_saturate)! |
| 284 | modern.check_op(op_hsl_luminosity)! |
| 285 | modern.check_op(op_conjoint_xor)! |
| 286 | for op in [op_disjoint_over, op_multiply] { |
| 287 | if _ := old.check_op(op) { |
| 288 | assert false, 'old server accepted ${op}' |
| 289 | } |
| 290 | } |
| 291 | for op in [-1, 14, 0x1c, 0x2c, 0x3f] { |
| 292 | if _ := modern.check_op(op) { |
| 293 | assert false, 'accepted invalid operator ${op}' |
| 294 | } |
| 295 | } |
| 296 | old.check_repeat(repeat_normal)! |
| 297 | modern.check_repeat(repeat_reflect)! |
| 298 | if _ := old.check_repeat(repeat_pad) { |
| 299 | assert false |
| 300 | } |
| 301 | if _ := modern.check_repeat(4) { |
| 302 | assert false |
| 303 | } |
| 304 | assert modern.supports(0, 10) && !old.supports(0, 2) && old.supports(0, 0) |
| 305 | } |
| 306 | |
| 307 | fn test_gradient_stop_validation() { |
| 308 | white := Color{ red: 0xffff, green: 0xffff, blue: 0xffff, alpha: 0xffff } |
| 309 | offsets, colors := stop_arrays([ColorStop{0, white}, ColorStop{0.5, white}, ColorStop{0.5, white}, |
| 310 | ColorStop{1, white}])! |
| 311 | assert offsets == [Fixed(0), 32768, 32768, 65536] |
| 312 | assert colors.len == 4 |
| 313 | for bad in [[]ColorStop{}, [ColorStop{0.5, white}, ColorStop{0.25, white}], |
| 314 | [ColorStop{-0.1, white}], [ColorStop{1.5, white}]] { |
| 315 | if _, _ := stop_arrays(bad) { |
| 316 | assert false, 'accepted ${bad.len} stops' |
| 317 | } |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | fn test_rectangle_limits() { |
| 322 | raw := x_rectangles(unsafe { nil }, [Rectangle{-32768, 32767, 65535, 0}], -1)! |
| 323 | assert raw[0].x == -32768 && raw[0].y == 32767 && raw[0].width == 65535 |
| 324 | for bad in [Rectangle{32768, 0, 1, 1}, Rectangle{0, 0, -1, 1}, Rectangle{0, 0, 1, 65536}] { |
| 325 | if _ := x_rectangles(unsafe { nil }, [bad], -1) { |
| 326 | assert false |
| 327 | } |
| 328 | } |
| 329 | } |
| 330 | |
| 331 | fn test_glyph_strides() { |
| 332 | e := &Extension{ connection: unsafe { nil } } |
| 333 | a1 := &GlyphSetHandle{ extension: e, bits_per_pixel: 1 } |
| 334 | a8 := &GlyphSetHandle{ extension: e, bits_per_pixel: 8 } |
| 335 | argb := &GlyphSetHandle{ extension: e, bits_per_pixel: 32 } |
| 336 | assert a1.stride(1) == 4 && a1.stride(32) == 4 && a1.stride(33) == 8 |
| 337 | assert a8.stride(1) == 4 && a8.stride(5) == 8 |
| 338 | assert argb.stride(3) == 12 |
| 339 | assert a8.stride(0) == 0 |
| 340 | } |
| 341 | |