Truly independent web browser
<!DOCTYPE html>
<script src="../include.js"></script>
<script src="../work-test.js"></script>
<style>
@keyframes pulse { from { transform: scaleY(0.5); } to { transform: scaleY(1); } }
@keyframes fade { from { opacity: 0.5; } to { opacity: 1; } }
@keyframes dim { from { filter: brightness(0.5); } to { filter: brightness(1); } }
@keyframes dim-through-a-filter-element { from { filter: url(#f); } to { filter: url(#f); } }
.bar { animation: pulse 1s infinite; }
.fading { animation: fade 1s infinite; }
.dimming { animation: dim 1s infinite; }
.dimming-through-a-filter-element { animation: dim-through-a-filter-element 1s infinite; }
#cases { width: 100px; height: 100px; }
</style>
<svg id="cases" viewBox="0 0 24 24"></svg>
<script>
function compositorAnimations() {
return `[${internals.dumpCompositorAnimations().split("\n").filter(Boolean).join(", ")}]`;
}
function nextTask() {
const { promise, resolve } = Promise.withResolvers();
const channel = new MessageChannel();
channel.port1.onmessage = resolve;
channel.port2.postMessage(null);
return promise;
}
const rect = `<rect class="bar" width="5" height="5"/>`;
const fadingRect = `<rect class="fading" width="5" height="5"/>`;
const dimmingRect = `<rect class="dimming" width="5" height="5"/>`;
const urlFilter = `<filter id="f"><feOffset dx="1"/></filter>`;
const onTheCompositor = {
"a rect": rect,
"a rect in a group": `<g>${rect}</g>`,
"a rect in a transformed group": `<g transform="translate(1 1)">${rect}</g>`,
"a group": `<g class="bar"><rect width="5" height="5"/></g>`,
"a rect in a translucent group": `<g opacity="0.5">${rect}</g>`,
"a rect in a group with a filter function": `<g style="filter: blur(1px)">${rect}</g>`,
"a rect in a group with a clip path in user space": `<clipPath id="c"><rect width="9" height="9"/></clipPath><g clip-path="url(#c)">${rect}</g>`,
"a rect in a group with a mask in user space": `<mask id="m" maskUnits="userSpaceOnUse" x="0" y="0" width="9" height="9"><rect width="9" height="9" fill="white"/></mask><g mask="url(#m)">${rect}</g>`,
"a use element": `<defs><rect id="u" width="5" height="5"/></defs><use class="bar" href="#u"/>`,
"a use element with a position": `<defs><rect id="u" width="5" height="5"/></defs><use class="bar" href="#u" x="1" y="2"/>`,
"a nested svg": `<svg class="bar" width="5" height="5"><rect width="5" height="5"/></svg>`,
"a fading rect": fadingRect,
"a fading group": `<g class="fading"><rect width="5" height="5"/></g>`,
"a fading use element": `<defs><rect id="u" width="5" height="5"/></defs><use class="fading" href="#u" x="1"/>`,
"a fading rect in a group with a filter element": `${urlFilter}<g filter="url(#f)">${fadingRect}</g>`,
"a dimming rect": dimmingRect,
"a dimming group": `<g class="dimming"><rect width="5" height="5"/></g>`,
"a dimming use element": `<defs><rect id="u" width="5" height="5"/></defs><use class="dimming" href="#u" x="1"/>`,
"a dimming rect in a group with a filter element": `${urlFilter}<g filter="url(#f)">${dimmingRect}</g>`,
};
// The compositor knows no bounding boxes. That of a group includes the transforms of the boxes inside it, so what
// reads that box needs the main thread to keep those transforms current. Opacity and filter functions leave bounding
// boxes alone. The content of a resource is painted by the main thread each time it is used, so has no nodes for the
// compositor, and the compositor has no filter elements.
const onTheMainThread = {
"a rect in a group with a filter element": `${urlFilter}<g filter="url(#f)">${rect}</g>`,
"a rect in a group with a clip path in its bounding box": `<clipPath id="c" clipPathUnits="objectBoundingBox"><rect width="1" height="1"/></clipPath><g clip-path="url(#c)">${rect}</g>`,
"a rect in a group with a clip path shape": `<g style="clip-path: circle(50%)">${rect}</g>`,
"a rect in a group with a mask in its bounding box": `<mask id="m"><rect width="9" height="9" fill="white"/></mask><g mask="url(#m)">${rect}</g>`,
"a rect in a group with a mask image": `<g style="mask-image: linear-gradient(black, white)">${rect}</g>`,
"a rect in a group transformed about its fill box": `<g style="transform: rotate(5deg); transform-box: fill-box">${rect}</g>`,
"a rect in a group rotated about its fill box": `<g style="rotate: 5deg; transform-box: fill-box">${rect}</g>`,
"a rect in a group scaled about its fill box": `<g style="scale: 2; transform-box: fill-box">${rect}</g>`,
"a rect with a non-scaling stroke": `<rect class="bar" width="5" height="5" stroke="red" vector-effect="non-scaling-stroke"/>`,
"a group around a non-scaling stroke": `<g class="bar"><rect width="5" height="5" stroke="red" vector-effect="non-scaling-stroke"/></g>`,
"a rect in a mask": `<mask id="n">${rect}</mask><rect mask="url(#n)" width="9" height="9"/>`,
"a fading rect in a mask": `<mask id="n">${fadingRect}</mask><rect mask="url(#n)" width="9" height="9"/>`,
"a dimming rect in a mask": `<mask id="n">${dimmingRect}</mask><rect mask="url(#n)" width="9" height="9"/>`,
"a rect animating a filter element": `${urlFilter}<rect class="dimming-through-a-filter-element" width="5" height="5"/>`,
};
asyncTest(async done => {
internals.setManualRenderingOpportunities(true);
await nextTask();
let frameTime = performance.now();
const renderFrames = async () => {
for (let i = 0; i < 4; ++i) {
internals.injectRenderingOpportunity((frameTime += 16));
await nextTask();
}
};
const check = async (heading, group) => {
println(heading);
for (const [name, markup] of Object.entries(group)) {
cases.innerHTML = markup;
await renderFrames();
internals.updateCompositorAnimations();
println(` ${name}: ${compositorAnimations()}`);
}
};
await check("runs on the compositor:", onTheCompositor);
await check("runs on the compositor, though it must not:", onTheMainThread);
cases.innerHTML = onTheCompositor["a rect in a group"];
await renderFrames();
await renderFrames();
internals.beginStyleTrace();
internals.beginRenderingTrace();
await renderFrames();
const sampled = parseStyleTrace(internals.takeStyleTrace()).filter(event => event.outcome?.startsWith("sampled"));
const mainThreadWork = internals.takeRenderingTrace().split("\n").filter(line => /^(animation frame pump request|update$)/.test(line));
println(`the main thread samples: [${sampled.map(event => event.element).join(", ")}]`);
println(`frame requests and rendering updates of the main thread: [${mainThreadWork.join(", ")}]`);
// What a script reads comes from the current transform, while the compositor draws it.
cases.innerHTML = `<g><rect width="5" height="10"/></g>`;
const group = cases.firstElementChild;
const timeline = internals.createInternalAnimationTimeline();
const animation = new Animation(new KeyframeEffect(group.firstElementChild,
[{ transform: "scaleY(1)" }, { transform: "scaleY(0.5)" }],
{ duration: 1000, iterations: Infinity }), timeline);
animation.play();
timeline.setTime(0);
await renderFrames();
internals.updateCompositorAnimations();
println(`the box a script reads runs on the compositor: ${compositorAnimations()}`);
println(`height of its group at the start: ${group.getBBox().height}`);
await nextTask();
timeline.setTimeForObservation(500);
println(`height of its group halfway: ${group.getBBox().height}`);
done();
});
</script>