Skip to content

Commit

Permalink
vaev-layout: Reduced the amount of background layer generated.
Browse files Browse the repository at this point in the history
  • Loading branch information
sleepy-monax committed Sep 30, 2024
1 parent 4e05fd1 commit 4fb0c75
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 32 deletions.
65 changes: 39 additions & 26 deletions src/vaev-layout/paint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@

namespace Vaev::Layout {

static Paint::Borders _paintBorders(Frag &frag) {
static void _paintBorders(Frag &frag, Gfx::Color currentColor, Paint::Stack &stack) {
Paint::Borders paint;

auto currentColor = Gfx::BLACK;
currentColor = resolve(frag.style->color, currentColor);

auto bordersLayout = frag.layout.borders;
Expand All @@ -35,32 +34,46 @@ static Paint::Borders _paintBorders(Frag &frag) {
paint.end.style = bordersStyle->end.style;
paint.end.fill = resolve(bordersStyle->end.color, currentColor);

return paint;
stack.add(makeStrong<Paint::Borders>(std::move(paint)));
}

static void _paintInner(Frag &frag, Paint::Stack &stack) {
static void _paintBackground(Frag &frag, Gfx::Color currentColor, Paint::Stack &stack) {
auto const &backgrounds = frag.style->backgrounds;

Gfx::Color currentColor = Gfx::BLACK;
currentColor = resolve(frag.style->color, currentColor);
if (isEmpty(backgrounds))
return;

if (backgrounds.len()) {
Paint::Box paint;
Paint::Box paint;

paint.backgrounds.ensure(backgrounds.len());
for (auto &bg : backgrounds) {
paint.backgrounds.pushBack(resolve(bg.fill, currentColor));
}
paint.backgrounds.ensure(backgrounds.len());
for (auto &bg : backgrounds) {
auto color = resolve(bg.fill, currentColor);

paint.radii = frag.layout.radii.cast<f64>();
paint.bound = frag.layout.borderBox().cast<f64>();
// Skip transparent backgrounds
if (color.alpha == 0)
continue;

stack.add(makeStrong<Paint::Box>(std::move(paint)));
paint.backgrounds.pushBack(color);
}

for (auto &c : frag.children()) {
paint.radii = frag.layout.radii.cast<f64>();
paint.bound = frag.layout.borderBox().cast<f64>();

// Skip if there are no backgrounds to paint
if (isEmpty(paint.backgrounds))
return;

stack.add(makeStrong<Paint::Box>(std::move(paint)));
}

static void _paintInner(Frag &frag, Paint::Stack &stack) {
Gfx::Color currentColor = Gfx::BLACK;
currentColor = resolve(frag.style->color, currentColor);

_paintBackground(frag, currentColor, stack);

for (auto &c : frag.children())
paint(c, stack);
}

if (auto run = frag.content.is<Strong<Text::Run>>()) {
Math::Vec2f baseline = {0, frag.font.metrics().ascend};
Expand All @@ -71,21 +84,21 @@ static void _paintInner(Frag &frag, Paint::Stack &stack) {
));
}

if (not frag.layout.borders.zero()) {
auto paint = _paintBorders(frag);
stack.add(makeStrong<Paint::Borders>(std::move(paint)));
}
if (not frag.layout.borders.zero())
_paintBorders(frag, currentColor, stack);
}

void paint(Frag &frag, Paint::Stack &stack) {
if (frag.style->zIndex == ZIndex::AUTO) {
_paintInner(frag, stack);
} else {
auto innerStack = makeStrong<Paint::Stack>();
innerStack->zIndex = frag.style->zIndex.value;
_paintInner(frag, *innerStack);
stack.add(std::move(innerStack));
return;
}

// Z-index is not auto, we need to create a new stacking context
auto innerStack = makeStrong<Paint::Stack>();
innerStack->zIndex = frag.style->zIndex.value;
_paintInner(frag, *innerStack);
stack.add(std::move(innerStack));
}

} // namespace Vaev::Layout
9 changes: 3 additions & 6 deletions src/vaev-style/styles.h
Original file line number Diff line number Diff line change
Expand Up @@ -176,15 +176,10 @@ struct BackgroundAttachmentProp {
return {BackgroundAttachment::SCROLL};
}

void apply(Computed &) const {
// TODO
}

Res<> parse(Computed &c) const {
void apply(Computed &c) const {
c.backgrounds.resize(max(c.backgrounds.len(), value.len()));
for (usize i = 0; i < value.len(); ++i)
c.backgrounds[i].attachment = value[i];
return Ok();
}
};

Expand All @@ -204,6 +199,7 @@ struct BackgroundColorProp {

Res<> parse(Cursor<Css::Sst> &c) {
eatWhitespace(c);
value.clear();
while (not c.ended()) {
value.pushBack(try$(parseValue<Color>(c)));
eatWhitespace(c);
Expand Down Expand Up @@ -276,6 +272,7 @@ struct BackgroundProp {

Res<> parse(Cursor<Css::Sst> &c) {
eatWhitespace(c);
value.clear();
while (not c.ended()) {
value.pushBack(try$(parseValue<Color>(c)));
eatWhitespace(c);
Expand Down

0 comments on commit 4fb0c75

Please sign in to comment.