summaryrefslogtreecommitdiffstats
path: root/dom/canvas/test/reftest/colors/generate_color_canvas_reftests.py
blob: 8c1e5f378844caf1807c3e03bf68c483cff64f65 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
#! python3

# Typecheck:
# `pip -m mypy generate_color_canvas_reftests.py`
#
# Run:
# `./generate_color_canvas_reftests.py [--write]`

import functools
import json
import math
import pathlib
import re
import sys
from typing import Iterable, NamedTuple, TypeVar

ARGS = sys.argv[1:]
DEST = pathlib.Path(__file__).parent / "_generated_reftest.list"

COL_DELIM = "  "
COL_ALIGNMENT = 4

# -

T = TypeVar("T")
U = TypeVar("U")

# -


# crossCombine([{a:false},{a:5}], [{},{b:5}])
# [{a:false}, {a:true}, {a:false,b:5}, {a:true,b:5}]
def cross_combine(*args_tup: list[dict]) -> list[dict]:
    args = list(args_tup)
    for i, a in enumerate(args):
        assert type(a) == list, f"Arg{i} is {type(a)}, expected {list}."

    def cross_combine2(listA, listB):
        listC = []
        for a in listA:
            for b in listB:
                c = dict()
                c.update(a)
                c.update(b)
                listC.append(c)
        return listC

    res: list[dict] = [dict()]
    while True:
        try:
            next = args.pop(0)
        except IndexError:
            break
        res = cross_combine2(res, next)
    return res


# keyed_alternatives('count', [1,2,3]) -> [{count: 1}, {count: 2}, {count: 3}]
def keyed_alternatives(key: T, vals: Iterable[U]) -> list[dict[T, U]]:
    """
    res = []
    for v in vals:
        d = dict()
        d[key] = v
        res.append(d)
    return res
    """
    # return [dict([[key,v]]) for v in vals]
    return [{key: v} for v in vals]


# -


def eprint(*args, **kwargs):
    sys.stdout.flush()
    print(*args, file=sys.stderr, **kwargs)
    sys.stderr.flush()


# -

# color_canvas.html?e_width=100&e_height=100&e_context=css&e_options={}&e_cspace=&e_webgl_format=&e_color=rgb(127,0,0)

CSPACE_LIST = ["srgb", "display-p3"]
CANVAS_CSPACES = keyed_alternatives("e_cspace", CSPACE_LIST)

RGB_LIST = [
    "0.000 0.000 0.000",
    "0.200 0.200 0.200",  # 0.2*255 = 51
    "0.200 0.000 0.000",
    "0.000 0.200 0.000",
    "0.000 0.000 0.200",
    "0.502 0.502 0.502",  # 0.502*255 = 128.01
    "0.502 0.000 0.000",
    "0.000 0.502 0.000",
    "0.000 0.000 0.502",
    "1.000 1.000 1.000",
    #'1.000 0.000 0.000', # These will hit gamut clipping on most displays.
    #'0.000 1.000 0.000',
    #'0.000 0.000 1.000',
]

WEBGL_COLORS = keyed_alternatives("e_color", [f"color(srgb {rgb})" for rgb in RGB_LIST])

C2D_COLORS = []
for cspace in CSPACE_LIST:
    C2D_COLORS += keyed_alternatives(
        "e_color", [f"color({cspace} {rgb})" for rgb in RGB_LIST]
    )

# -

WEBGL_FORMATS = keyed_alternatives(
    "e_webgl_format",
    [
        "RGBA8",
        # Bug 1883748: (webgl.drawingbufferStorage)
        #'SRGB8_ALPHA8',
        #'RGBA16F',
    ],
)
WEBGL = cross_combine(
    [{"e_context": "webgl"}], WEBGL_FORMATS, CANVAS_CSPACES, WEBGL_COLORS
)

# -

C2D_OPTIONS_COMBOS = cross_combine(
    keyed_alternatives("willReadFrequently", ["true", "false"]),  # E.g. D2D vs Skia
    # keyed_alternatives('alpha', ['true','false'])
)
C2D_OPTIONS = [
    json.dumps(config, separators=(",", ":")) for config in C2D_OPTIONS_COMBOS
]

C2D = cross_combine(
    [{"e_context": "2d"}],
    keyed_alternatives("e_options", C2D_OPTIONS),
    CANVAS_CSPACES,
    C2D_COLORS,
)

# -

COMBOS: list[dict[str, str]] = cross_combine(WEBGL + C2D)

eprint(f"{len(COMBOS)} combinations...")

# -

Config = dict[str, str]


class CssColor(NamedTuple):
    cspace: str
    rgb: str

    def rgb_vals(self) -> tuple[float, float, float]:
        (r, g, b) = [float(z) for z in self.rgb.split(" ")]
        return (r, g, b)

    def is_same_color(x, y) -> bool:
        if x == y:
            return True
        (r, g, b) = x.rgb_vals()
        if x.rgb == y.rgb and r == g and g == b:
            return True
        return False


class Reftest(NamedTuple):
    notes: list[str]
    op: str
    test_config: Config
    ref_config: Config


def make_ref_config(color: CssColor) -> Config:
    return {
        "e_context": "css",
        "e_color": f"color({color.cspace} {color.rgb})",
    }


class ColorReftest(NamedTuple):
    notes: list[str]
    test_config: Config
    ref_color: CssColor

    def to_reftest(self):
        ref_config = make_ref_config(self.ref_color)
        return Reftest(self.notes.copy(), "==", self.test_config.copy(), ref_config)


class Expectation(NamedTuple):
    notes: list[str]
    test_config: Config
    color: CssColor


def parse_css_color(s: str) -> CssColor:
    m = re.match("color[(]([^)]+)[)]", s)
    assert m, s
    (cspace, rgb) = m.group(1).split(" ", 1)
    return CssColor(cspace, rgb)


def correct_color_from_test_config(test_config: Config) -> CssColor:
    canvas_cspace = test_config["e_cspace"]
    if not canvas_cspace:
        canvas_cspace = "srgb"

    correct_color = parse_css_color(test_config["e_color"])
    if test_config["e_context"] == "webgl":
        # Webgl ignores the color's cspace, because webgl has no concept of
        # source colorspace for clears/draws to the backbuffer.
        # This (correct) behavior is as if the color's cspace were overwritten by the
        # cspace of the canvas. (so expect that)
        correct_color = CssColor(canvas_cspace, correct_color.rgb)

    return correct_color


# -------------------------------------
# -------------------------------------
# -------------------------------------
# Choose (multiple?) reference configs given a test config.


def reftests_from_config(test_config: Config) -> Iterable[ColorReftest]:
    correct_color = correct_color_from_test_config(test_config)

    if test_config["e_context"] == "2d":
        # Canvas2d generally has the same behavior as css, so expect all passing.
        yield ColorReftest([], test_config, correct_color)
        return

    assert test_config["e_context"] == "webgl", test_config["e_context"]

    # -

    def reftests_from_expected_color(
        notes: list[str], expected_color: CssColor
    ) -> Iterable[ColorReftest]:
        # If expecting failure, generate two tests, both expecting failure:
        # 1. expected-fail test == correct_color
        # 2. expected-pass test == (incorrect) expected_color
        # If we fix an error, we'll see one unexpected-pass and one unexpected-fail.
        # If we get a new wrong answer, we'll see one unexpected-fail.

        if not expected_color.is_same_color(correct_color):
            yield ColorReftest(notes + ["fails"], test_config, correct_color)
            yield ColorReftest(notes, test_config, expected_color)
        else:
            yield ColorReftest(notes, test_config, correct_color)

    # -
    # On Mac, (with the pref) we do tag the IOSurface with the right cspace.
    # On other platforms, Webgl always outputs in the display color profile
    # right now. This is the same as "srgb".

    expected_color_srgb = CssColor("srgb", correct_color.rgb)
    # Mac
    yield from reftests_from_expected_color(["skip-if(!cocoaWidget)"], correct_color)
    # Win, Lin, Android
    yield from reftests_from_expected_color(
        ["skip-if(cocoaWidget) "], expected_color_srgb
    )


# -


def amended_notes_from_reftest(reftest: ColorReftest) -> list[str]:
    notes = reftest.notes[:]

    ref_rgb_vals = reftest.ref_color.rgb_vals()
    is_green_only = ref_rgb_vals == (0, ref_rgb_vals[1], 0)
    if (
        "fails" in reftest.notes
        and reftest.test_config["e_context"] == "webgl"
        and reftest.test_config["e_cspace"] == "display-p3"
        and is_green_only
    ):
        # Android's display bitdepth rounds srgb green and p3 green to the same thing.
        notes[notes.index("fails")] = "fails-if(!Android)"

    return notes


# -------------------------------------
# -------------------------------------
# -------------------------------------
# Ok, back to implementation.


def encode_url_v(k, v):
    if k == "e_color":
        # reftest harness can't deal with spaces in urls, and 'color(srgb%201%200%200)' is hard to read.
        v = v.replace(" ", ",")

    assert " " not in v, (k, v)
    return v


# Cool:
assert encode_url_v("e_color", "color(srgb 0 0 0)") == "color(srgb,0,0,0)"
# Unfortunate, but tolerable:
assert encode_url_v("e_color", "color(srgb   0   0   0)") == "color(srgb,,,0,,,0,,,0)"

# -


def url_from_config(kvs: Config) -> str:
    parts = [f"{k}={encode_url_v(k,v)}" for k, v in kvs.items()]
    url = "color_canvas.html?" + "&".join(parts)
    return url


# -

color_reftests: list[ColorReftest] = []
for c in COMBOS:
    color_reftests += reftests_from_config(c)
color_reftests = [
    ColorReftest(amended_notes_from_reftest(r), r.test_config, r.ref_color)
    for r in color_reftests
]
reftests = [r.to_reftest() for r in color_reftests]

# -

HEADINGS = ["# annotations", "op", "test", "reference"]
table: list[list[str]] = [HEADINGS]
table = [
    [
        " ".join(r.notes),
        r.op,
        url_from_config(r.test_config),
        url_from_config(r.ref_config),
    ]
    for r in reftests
]

# -


def round_to(a, b: int) -> int:
    return int(math.ceil(a / b) * b)


def aligned_lines_from_table(
    rows: list[list[str]], col_delim="  ", col_alignment=4
) -> Iterable[str]:
    max_col_len = functools.reduce(
        lambda accum, input: [max(r, len(c)) for r, c in zip(accum, input)],
        rows,
        [0 for _ in rows[0]],
    )
    max_col_len = [round_to(x, col_alignment) for x in max_col_len]

    for i, row in enumerate(rows):
        parts = [s + (" " * (col_len - len(s))) for s, col_len in zip(row, max_col_len)]
        line = col_delim.join(parts)
        yield line


# -

GENERATED_FILE_LINE = "### Generated, do not edit. ###"

lines = list(aligned_lines_from_table(table, COL_DELIM, COL_ALIGNMENT))
WARN_EVERY_N_LINES = 5
i = WARN_EVERY_N_LINES - 1
while i < len(lines):
    lines.insert(i, "  " + GENERATED_FILE_LINE)
    i += WARN_EVERY_N_LINES

# -

GENERATED_BY_ARGS = [f"./{pathlib.Path(__file__).name}"] + ARGS

REFTEST_LIST_PREAMBLE = f"""\
{GENERATED_FILE_LINE}
 {GENERATED_FILE_LINE}
  {GENERATED_FILE_LINE}

# Generated by `{' '.join(GENERATED_BY_ARGS)}`.
# -

defaults pref(webgl.colorspaces.prototype,true)

{GENERATED_FILE_LINE}
# -

# Ensure not white-screening:
!=  {url_from_config({})+'='}  about:blank
# Ensure differing results with different args:
!=  {url_from_config({'e_color':'color(srgb 1 0 0)'})}  {url_from_config({'e_color':'color(srgb 0 1 0)'})}

{GENERATED_FILE_LINE}
# -
"""

lines.insert(0, REFTEST_LIST_PREAMBLE)
lines.append("")

# -

for line in lines:
    print(line)

if "--write" not in ARGS:
    eprint("Use --write to write. Exiting...")
    sys.exit(0)

# -

eprint("Concatenating...")
file_str = "\n".join([line.rstrip() for line in lines])

eprint(f"Writing to {DEST}...")
DEST.write_bytes(file_str.encode())
eprint("Done!")

sys.exit(0)