File indexing completed on 2024-04-28 03:55:59

0001 /*
0002  *  SPDX-FileCopyrightText: 2020 Arjen Hiemstra <ahiemstra@heimr.nl>
0003  *
0004  *  SPDX-License-Identifier: LGPL-2.0-or-later
0005  */
0006 
0007 #include "shadowedborderrectanglematerial.h"
0008 
0009 #include <QOpenGLContext>
0010 
0011 QSGMaterialType ShadowedBorderRectangleMaterial::staticType;
0012 
0013 ShadowedBorderRectangleMaterial::ShadowedBorderRectangleMaterial()
0014 {
0015     setFlag(QSGMaterial::Blending, true);
0016 }
0017 
0018 QSGMaterialShader *ShadowedBorderRectangleMaterial::createShader(QSGRendererInterface::RenderMode) const
0019 {
0020     return new ShadowedBorderRectangleShader{shaderType};
0021 }
0022 
0023 QSGMaterialType *ShadowedBorderRectangleMaterial::type() const
0024 {
0025     return &staticType;
0026 }
0027 
0028 int ShadowedBorderRectangleMaterial::compare(const QSGMaterial *other) const
0029 {
0030     auto material = static_cast<const ShadowedBorderRectangleMaterial *>(other);
0031 
0032     auto result = ShadowedRectangleMaterial::compare(other);
0033     /* clang-format off */
0034     if (result == 0
0035         && material->borderColor == borderColor
0036         && qFuzzyCompare(material->borderWidth, borderWidth)) { /* clang-format on */
0037         return 0;
0038     }
0039 
0040     return QSGMaterial::compare(other);
0041 }
0042 
0043 ShadowedBorderRectangleShader::ShadowedBorderRectangleShader(ShadowedRectangleMaterial::ShaderType shaderType)
0044     : ShadowedRectangleShader(shaderType)
0045 {
0046     setShader(shaderType, QStringLiteral("shadowedborderrectangle"));
0047 }
0048 
0049 bool ShadowedBorderRectangleShader::updateUniformData(QSGMaterialShader::RenderState &state, QSGMaterial *newMaterial, QSGMaterial *oldMaterial)
0050 {
0051     bool changed = ShadowedRectangleShader::updateUniformData(state, newMaterial, oldMaterial);
0052     QByteArray *buf = state.uniformData();
0053     Q_ASSERT(buf->size() >= 160);
0054 
0055     if (!oldMaterial || newMaterial->compare(oldMaterial) != 0) {
0056         const auto material = static_cast<ShadowedBorderRectangleMaterial *>(newMaterial);
0057         memcpy(buf->data() + 136, &material->borderWidth, 8);
0058         float c[4];
0059         material->borderColor.getRgbF(&c[0], &c[1], &c[2], &c[3]);
0060         memcpy(buf->data() + 144, c, 16);
0061         changed = true;
0062     }
0063 
0064     return changed;
0065 }