Skip to content

Commit

Permalink
Merge pull request #20167 from igorkorsukov/engraving/layout_optimisa…
Browse files Browse the repository at this point in the history
…tion_2

[engraving] Added micro optimisation
  • Loading branch information
RomanPudashkin committed Nov 24, 2023
2 parents 1587787 + 609188a commit 4958cae
Show file tree
Hide file tree
Showing 29 changed files with 156 additions and 169 deletions.
4 changes: 2 additions & 2 deletions src/engraving/dom/articulation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ int Articulation::subtype() const
void Articulation::setUp(bool val)
{
Articulation::LayoutData* ldata = mutldata();
ldata->setUp(val);
ldata->up = val;

//! NOTE member of Articulation m_symId - this is `given` data
//! member of LayoutData m_symId - this is layout data
Expand All @@ -141,7 +141,7 @@ void Articulation::setUp(bool val)
}
}

ldata->setSymId(m_symId);
ldata->symId = m_symId;
}

//---------------------------------------------------------
Expand Down
21 changes: 6 additions & 15 deletions src/engraving/dom/articulation.h
Original file line number Diff line number Diff line change
Expand Up @@ -176,31 +176,22 @@ class Articulation : public EngravingItem

struct LayoutData : public EngravingItem::LayoutData
{
ld_field<bool> up = { "[Articulation] up", true };
ld_field<SymId> symId = { "[Articulation] symId", SymId::noSym };

void reset() override
{
EngravingItem::LayoutData::reset();
m_up.reset();
m_symId.reset();
up.reset();
symId.reset();
}

bool isSetUp() const { return m_up.has_value(); }
bool up(LD_ACCESS mode = LD_ACCESS::CHECK) const { return m_up.value(mode); }
void setUp(bool val) { m_up.set_value(val); }

bool isSetSymId() const { return m_symId.has_value(); }
SymId symId(LD_ACCESS mode = LD_ACCESS::CHECK) const { return m_symId.value(mode); }
void setSymId(SymId val) { m_symId.set_value(val); }

private:
ld_field<bool> m_up = { "up", true };
ld_field<SymId> m_symId = { "symId", SymId::noSym };
};
DECLARE_LAYOUTDATA_METHODS(Articulation)

void setUp(bool val);

//! --- DEPRECATED ---
bool up() const { return ldata()->up(); }
bool up() const { return ldata()->up; }
//! ------------------

protected:
Expand Down
4 changes: 2 additions & 2 deletions src/engraving/dom/bracket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ void Bracket::editDrag(EditData& ed)
{
double bracketHeight = ldata()->bracketHeight();
bracketHeight += ed.delta.y();
mutldata()->setBracketHeight(bracketHeight);
mutldata()->bracketHeight.set_value(bracketHeight);

renderer()->layoutItem(this);
}
Expand Down Expand Up @@ -191,7 +191,7 @@ void Bracket::endEditDrag(EditData&)

double sy = system()->staff(staffIdx1)->y();
double ey = system()->staff(staffIdx2)->y() + score()->staff(staffIdx2)->staffHeight();
mutldata()->setBracketHeight(ey - sy);
mutldata()->bracketHeight.set_value(ey - sy);
bracketItem()->undoChangeProperty(Pid::BRACKET_SPAN, staffIdx2 - staffIdx1 + 1);
}

Expand Down
16 changes: 3 additions & 13 deletions src/engraving/dom/bracket.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,22 +100,12 @@ class Bracket final : public EngravingItem

struct LayoutData : public EngravingItem::LayoutData {
SymId braceSymbol = SymId::noSym;

PainterPath path;
Shape shape;
ld_field<double> bracketHeight = { "[Bracket] bracketHeight", 0.0 };
ld_field<double> bracketWidth = { "[Bracket] bracketWidth", 0.0 };

bool isSetBracketHeight() const { return m_bracketHeight.has_value(); }
void setBracketHeight(double v) { m_bracketHeight.set_value(v); }
double bracketHeight(LD_ACCESS mode = LD_ACCESS::CHECK) const { return m_bracketHeight.value(mode); }
double h2(LD_ACCESS mode = LD_ACCESS::CHECK) const { return m_bracketHeight.value(mode) * 0.5; }

bool isSetBracketWidth() const { return m_bracketWidth.has_value(); }
void setBracketWidth(double v) { m_bracketWidth.set_value(v); }
double bracketWidth(LD_ACCESS mode = LD_ACCESS::CHECK) const { return m_bracketWidth.value(mode); }

private:
ld_field<double> m_bracketHeight = { "bracketHeight", 0.0 };
ld_field<double> m_bracketWidth = { "bracketWidth", 0.0 };
double h2(LD_ACCESS mode = LD_ACCESS::CHECK) const { return bracketHeight.value(mode) * 0.5; }
};
DECLARE_LAYOUTDATA_METHODS(Bracket)

Expand Down
1 change: 0 additions & 1 deletion src/engraving/dom/engravingitem.h
Original file line number Diff line number Diff line change
Expand Up @@ -608,7 +608,6 @@ class EngravingItem : public EngravingObject

//! --- Old Interface ---
void setbbox(const mu::RectF& r) { mutldata()->setBbox(r); }
void addbbox(const mu::RectF& r) { mutldata()->addBbox(r); }
double height() const { return ldata()->bbox().height(); }
void setHeight(double v) { mutldata()->setHeight(v); }

Expand Down
7 changes: 1 addition & 6 deletions src/engraving/dom/harmony.h
Original file line number Diff line number Diff line change
Expand Up @@ -199,12 +199,7 @@ class Harmony final : public TextBase
void setIsDrawEditMode(bool val) { m_isDrawEditMode = val; }

struct LayoutData : public TextBase::LayoutData {
bool isSetHarmonyHeight() const { return m_harmonyHeight.has_value(); }
double harmonyHeight() const { return m_harmonyHeight.value(LD_ACCESS::CHECK); }
void setHarmonyHeight(double h) { m_harmonyHeight.set_value(h); }

private:
ld_field<double> m_harmonyHeight = { "harmonyHeight", 0.0 }; // used for calculating the height is frame while editing.
ld_field<double> harmonyHeight = { "[Harmony] harmonyHeight", 0.0 }; // used for calculating the height is frame while editing.
};
DECLARE_LAYOUTDATA_METHODS(Harmony)

Expand Down
7 changes: 1 addition & 6 deletions src/engraving/dom/mmrest.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,9 @@ class MMRest final : public Rest
SymIdList restSyms; // stores symbols when using old-style rests
double symsWidth = 0.0; // width of symbols with spacing when using old-style

bool isSetRestWidth() const { return m_restWidth.has_value(); }
void setRestWidth(double v) { m_restWidth.set_value(v); }
double restWidth() const { return m_restWidth.value(LD_ACCESS::CHECK); }
ld_field<double> restWidth = { "[MMRest] restWidth", 0.0 }; // width of multimeasure rest

void setNumberSym(int n) { numberSym = timeSigSymIdsFromString(String::number(n)); }

private:
ld_field<double> m_restWidth = { "restWidth", 0.0 }; // width of multimeasure rest
};
DECLARE_LAYOUTDATA_METHODS(MMRest)

Expand Down
8 changes: 4 additions & 4 deletions src/engraving/dom/note.h
Original file line number Diff line number Diff line change
Expand Up @@ -446,10 +446,10 @@ class Note final : public EngravingItem
void updateFrettingForTiesAndBends();

struct LayoutData : public EngravingItem::LayoutData {
ld_field<bool> useTablature = { "Note::useTablature", false };
ld_field<SymId> cachedNoteheadSym = { "Note::cachedNoteheadSym", SymId::noSym }; // use in draw to avoid recomputing at every update
ld_field<SymId> cachedSymNull = { "Note::cachedSymNull", SymId::noSym }; // additional symbol for some transparent notehead
ld_field<bool> mirror = { "Note::mirror", false }; // True if note is mirrored at stem.
ld_field<bool> useTablature = { "[Note] useTablature", false };
ld_field<SymId> cachedNoteheadSym = { "[Note] cachedNoteheadSym", SymId::noSym }; // use in draw to avoid recomputing at every update
ld_field<SymId> cachedSymNull = { "[Note] cachedSymNull", SymId::noSym }; // additional symbol for some transparent notehead
ld_field<bool> mirror = { "[Note] mirror", false }; // True if note is mirrored at stem.
};
DECLARE_LAYOUTDATA_METHODS(Note)

Expand Down
26 changes: 13 additions & 13 deletions src/engraving/dom/rest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,14 +122,14 @@ void Rest::setOffset(const mu::PointF& o)

LayoutData* ldata = mutldata();

if (ldata->sym() == SymId::restWhole && (line <= -2 || line >= 3)) {
ldata->setSym(SymId::restWholeLegerLine);
} else if (ldata->sym() == SymId::restWholeLegerLine && (line > -2 && line < 4)) {
ldata->setSym(SymId::restWhole);
} else if (ldata->sym() == SymId::restHalf && (line <= -3 || line >= 3)) {
ldata->setSym(SymId::restHalfLegerLine);
} else if (ldata->sym() == SymId::restHalfLegerLine && (line > -3 && line < 3)) {
ldata->setSym(SymId::restHalf);
if (ldata->sym == SymId::restWhole && (line <= -2 || line >= 3)) {
ldata->sym = SymId::restWholeLegerLine;
} else if (ldata->sym == SymId::restWholeLegerLine && (line > -2 && line < 4)) {
ldata->sym = SymId::restWhole;
} else if (ldata->sym == SymId::restHalf && (line <= -3 || line >= 3)) {
ldata->sym = SymId::restHalfLegerLine;
} else if (ldata->sym == SymId::restHalfLegerLine && (line > -3 && line < 3)) {
ldata->sym = SymId::restHalf;
}

EngravingItem::setOffset(o);
Expand Down Expand Up @@ -333,21 +333,21 @@ SymId Rest::getSymbol(DurationType type, int line, int lines) const

void Rest::updateSymbol(int line, int lines, LayoutData* ldata) const
{
ldata->setSym(getSymbol(durationType().type(), line, lines));
ldata->sym = getSymbol(durationType().type(), line, lines);
}

double Rest::symWidthNoLedgerLines(LayoutData* ldata) const
{
if (ldata->sym() == SymId::restHalfLegerLine) {
if (ldata->sym == SymId::restHalfLegerLine) {
return symWidth(SymId::restHalf);
}
if (ldata->sym() == SymId::restWholeLegerLine) {
if (ldata->sym == SymId::restWholeLegerLine) {
return symWidth(SymId::restWhole);
}
if (ldata->sym() == SymId::restDoubleWholeLegerLine) {
if (ldata->sym == SymId::restDoubleWholeLegerLine) {
return symWidth(SymId::restDoubleWhole);
}
return symWidth(ldata->sym());
return symWidth(ldata->sym);
}

//---------------------------------------------------------
Expand Down
8 changes: 1 addition & 7 deletions src/engraving/dom/rest.h
Original file line number Diff line number Diff line change
Expand Up @@ -136,13 +136,7 @@ class Rest : public ChordRest

struct LayoutData : public ChordRest::LayoutData {
std::vector<Rest*> mergedRests; // Rests from other voices that may be merged with this

bool isSetSym() const { return m_sym.has_value(); }
SymId sym() const { return m_sym.value(LD_ACCESS::CHECK); }
void setSym(SymId v) { m_sym.set_value(v); }

private:
ld_field<SymId> m_sym = { "sym", SymId::restQuarter };
ld_field<SymId> sym = { "[Rest] sym", SymId::restQuarter };
};
DECLARE_LAYOUTDATA_METHODS(Rest)

Expand Down
12 changes: 12 additions & 0 deletions src/engraving/infrastructure/ld_access.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ class ld_field_debug
return value();
}

operator T() const {
return value();
}

inline T& mut_value()
{
if (!m_val.has_value()) {
Expand All @@ -95,6 +99,8 @@ class ld_field_debug
m_val = std::make_optional<T>(v);
}

ld_field_debug& operator=(const T& v) { m_val = v; return *this; }

private:
const char* m_name = nullptr;
T m_def;
Expand Down Expand Up @@ -122,6 +128,10 @@ class ld_field_prod
return value();
}

operator T() const {
return value();
}

inline T& mut_value()
{
return m_val;
Expand All @@ -132,6 +142,8 @@ class ld_field_prod
m_val = v;
}

ld_field_prod& operator=(const T& v) { m_val = v; return *this; }

private:
T m_val;
};
Expand Down
5 changes: 3 additions & 2 deletions src/engraving/rendering/dev/chordlayout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1457,6 +1457,7 @@ void ChordLayout::skipAccidentals(Segment* segment, track_idx_t startTrack, trac

void ChordLayout::layoutChords1(LayoutContext& ctx, Segment* segment, staff_idx_t staffIdx)
{
TRACEFUNC;
const Staff* staff = ctx.dom().staff(staffIdx);
const bool isTab = staff->isTabStaff(segment->tick());
const track_idx_t startTrack = staffIdx * VOICES;
Expand Down Expand Up @@ -3709,7 +3710,7 @@ void ChordLayout::fillShape(const Rest* item, Rest::LayoutData* ldata, const Lay

if (!item->isGap()) {
shape.add(chordRestShape(item, conf));
shape.add(item->symBbox(ldata->sym()), item);
shape.add(item->symBbox(ldata->sym), item);
for (const NoteDot* dot : item->dotList()) {
shape.add(item->symBbox(SymId::augmentationDot).translated(dot->pos()), dot);
}
Expand Down Expand Up @@ -3739,7 +3740,7 @@ void ChordLayout::fillShape(const MMRest* item, MMRest::LayoutData* ldata, const
Shape shape(Shape::Type::Composite);

double vStrokeHeight = conf.styleMM(Sid::mmRestHBarVStrokeHeight);
shape.add(RectF(0.0, -(vStrokeHeight * .5), ldata->restWidth(), vStrokeHeight));
shape.add(RectF(0.0, -(vStrokeHeight * .5), ldata->restWidth, vStrokeHeight));
if (item->numberVisible()) {
shape.add(item->numberRect());
}
Expand Down
5 changes: 3 additions & 2 deletions src/engraving/rendering/dev/measurelayout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -751,6 +751,7 @@ static void layoutDrumsetChord(Chord* c, const Drumset* drumset, const StaffType

void MeasureLayout::getNextMeasure(LayoutContext& ctx)
{
TRACEFUNC;
ctx.mutState().setPrevMeasure(ctx.mutState().curMeasure());
ctx.mutState().setCurMeasure(ctx.mutState().nextMeasure());
if (!ctx.state().curMeasure()) {
Expand Down Expand Up @@ -1267,7 +1268,7 @@ void MeasureLayout::layoutMeasureElements(Measure* m, LayoutContext& ctx)
w = x2 - x1 - d;
}
MMRest::LayoutData* mmrestLD = mmrest->mutldata();
mmrestLD->setRestWidth(w);
mmrestLD->restWidth = w;
TLayout::layoutMMRest(mmrest, mmrest->mutldata(), ctx);
mmrestLD->setPosX(headerException ? (x1 - s.x()) : (x1 - s.x() + d));
} else if (e->isMeasureRepeat() && !(toMeasureRepeat(e)->numMeasures() % 2)) {
Expand Down Expand Up @@ -2076,7 +2077,7 @@ void MeasureLayout::stretchMeasureInPracticeMode(Measure* m, double targetWidth,
double d = ctx.conf().styleMM(Sid::multiMeasureRestMargin);
double w = x2 - x1 - 2 * d;

mmrest->mutldata()->setRestWidth(w);
mmrest->mutldata()->restWidth = w;
TLayout::layoutMMRest(mmrest, mmrest->mutldata(), ctx);
e->setPos(x1 - s.x() + d, e->staff()->staffHeight() * .5); // center vertically in measure
s.createShape(staffIdx);
Expand Down
1 change: 1 addition & 0 deletions src/engraving/rendering/dev/pagelayout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,7 @@ void PageLayout::collectPage(LayoutContext& ctx)

void PageLayout::layoutPage(LayoutContext& ctx, Page* page, double restHeight, double footerPadding)
{
TRACEFUNC;
if (restHeight < 0.0) {
LOGN("restHeight < 0.0: %f\n", restHeight);
restHeight = 0;
Expand Down
2 changes: 2 additions & 0 deletions src/engraving/rendering/dev/scorepageviewlayout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ using namespace mu::engraving::rendering::dev;

void ScorePageViewLayout::layoutPageView(Score* score, LayoutContext& ctx, const Fraction& stick, const Fraction& etick)
{
TRACEFUNC;

ctx.mutState().setEndTick(etick);

//---------------------------------------------------
Expand Down
9 changes: 6 additions & 3 deletions src/engraving/rendering/dev/systemlayout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1918,7 +1918,7 @@ double SystemLayout::totalBracketOffset(LayoutContext& ctx)
Bracket* dummyBr = Factory::createBracket(ctx.mutDom().dummyParent(), /*isAccessibleEnabled=*/ false);
dummyBr->setBracketItem(bi);
dummyBr->setStaffSpan(firstStaff, lastStaff);
dummyBr->mutldata()->setBracketHeight(3.5 * dummyBr->spatium() * 2); // default
dummyBr->mutldata()->bracketHeight.set_value(3.5 * dummyBr->spatium() * 2); // default
TLayout::layoutBracket(dummyBr, dummyBr->mutldata(), ctx.conf());
for (staff_idx_t stfIdx = firstStaff; stfIdx <= lastStaff; ++stfIdx) {
bracketWidth[stfIdx] += dummyBr->ldata()->bracketWidth();
Expand Down Expand Up @@ -1956,7 +1956,7 @@ double SystemLayout::layoutBrackets(System* system, LayoutContext& ctx)
}
Bracket* b = SystemLayout::createBracket(system, ctx, bi, i, static_cast<int>(staffIdx), bl, system->firstMeasure());
if (b != nullptr) {
b->mutldata()->setBracketHeight(3.5 * b->spatium() * 2); // dummy
b->mutldata()->bracketHeight.set_value(3.5 * b->spatium() * 2); // dummy
TLayout::layoutBracket(b, b->mutldata(), ctx.conf());
bracketWidth[i] = std::max(bracketWidth[i], b->ldata()->bracketWidth());
}
Expand Down Expand Up @@ -2114,6 +2114,7 @@ Bracket* SystemLayout::createBracket(System* system, LayoutContext& ctx, Bracket

void SystemLayout::layout2(System* system, LayoutContext& ctx)
{
TRACEFUNC;
Box* vb = system->vbox();
if (vb) {
TLayout::layoutBox(vb, vb->mutldata(), ctx);
Expand Down Expand Up @@ -2273,6 +2274,7 @@ void SystemLayout::layout2(System* system, LayoutContext& ctx)

void SystemLayout::restoreLayout2(System* system, LayoutContext& ctx)
{
TRACEFUNC;
if (system->vbox()) {
return;
}
Expand Down Expand Up @@ -2334,7 +2336,7 @@ void SystemLayout::layoutBracketsVertical(System* system, LayoutContext& ctx)

Bracket::LayoutData* bldata = b->mutldata();
bldata->setPosY(sy);
bldata->setBracketHeight(ey - sy);
bldata->bracketHeight = ey - sy;
TLayout::layoutBracket(b, bldata, ctx.conf());
}
}
Expand Down Expand Up @@ -2488,6 +2490,7 @@ void SystemLayout::setInstrumentNames(System* system, LayoutContext& ctx, bool l

double SystemLayout::minDistance(const System* top, const System* bottom, LayoutContext& ctx)
{
TRACEFUNC;
if (top->vbox() && !bottom->vbox()) {
return std::max(double(top->vbox()->bottomGap()), bottom->minTop());
} else if (!top->vbox() && bottom->vbox()) {
Expand Down

0 comments on commit 4958cae

Please sign in to comment.