File indexing completed on 2024-05-12 07:52:10

0001 /*
0002  * SPDX-FileCopyrightText: 2021 Arjen Hiemstra <ahiemstra@heimr.nl>
0003  *
0004  * SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0005  */
0006 
0007 #include "BarChartMaterial.h"
0008 
0009 BarChartMaterial::BarChartMaterial()
0010 {
0011     setFlag(QSGMaterial::Blending);
0012 }
0013 
0014 BarChartMaterial::~BarChartMaterial()
0015 {
0016 }
0017 
0018 QSGMaterialType *BarChartMaterial::type() const
0019 {
0020     static QSGMaterialType type;
0021     return &type;
0022 }
0023 
0024 QSGMaterialShader *BarChartMaterial::createShader(QSGRendererInterface::RenderMode) const
0025 {
0026     return new BarChartShader();
0027 }
0028 
0029 int BarChartMaterial::compare(const QSGMaterial *other) const
0030 {
0031     auto material = static_cast<const BarChartMaterial *>(other);
0032 
0033     /* clang-format off */
0034     if (material->aspect == aspect
0035         && qFuzzyCompare(material->radius, radius)
0036         && material->backgroundColor == backgroundColor) { /* clang-format on */
0037         return 0;
0038     }
0039 
0040     return QSGMaterial::compare(other);
0041 }
0042 
0043 BarChartShader::BarChartShader()
0044 {
0045     setShaders(QStringLiteral("barchart.vert"), QStringLiteral("barchart.frag"));
0046 }
0047 
0048 BarChartShader::~BarChartShader()
0049 {
0050 }
0051 
0052 bool BarChartShader::updateUniformData(QSGMaterialShader::RenderState &state, QSGMaterial *newMaterial, QSGMaterial *oldMaterial)
0053 {
0054     bool changed = false;
0055 
0056     UniformDataStream uniformData(state);
0057 
0058     if (state.isMatrixDirty()) {
0059         uniformData << state.combinedMatrix();
0060         changed = true;
0061     } else {
0062         uniformData.skip<QMatrix4x4>();
0063     }
0064 
0065     if (state.isOpacityDirty()) {
0066         uniformData << state.opacity();
0067         changed = true;
0068     } else {
0069         uniformData.skip<float>();
0070     }
0071 
0072     if (!oldMaterial || newMaterial->compare(oldMaterial) != 0) {
0073         const auto material = static_cast<BarChartMaterial *>(newMaterial);
0074         uniformData << material->aspect;
0075         uniformData << material->radius;
0076         uniformData << material->backgroundColor;
0077         changed = true;
0078     }
0079 
0080     return changed;
0081 }