Kirigami2

shadowedborderrectanglematerial.cpp
1 /*
2  * SPDX-FileCopyrightText: 2020 Arjen Hiemstra <[email protected]>
3  *
4  * SPDX-License-Identifier: LGPL-2.0-or-later
5  */
6 
7 #include "shadowedborderrectanglematerial.h"
8 
9 #include <QOpenGLContext>
10 
11 QSGMaterialType ShadowedBorderRectangleMaterial::staticType;
12 
13 ShadowedBorderRectangleMaterial::ShadowedBorderRectangleMaterial()
14 {
16 }
17 
18 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
20 #else
21 QSGMaterialShader *ShadowedBorderRectangleMaterial::createShader(QSGRendererInterface::RenderMode) const
22 #endif
23 {
24  return new ShadowedBorderRectangleShader{shaderType};
25 }
26 
27 QSGMaterialType *ShadowedBorderRectangleMaterial::type() const
28 {
29  return &staticType;
30 }
31 
32 int ShadowedBorderRectangleMaterial::compare(const QSGMaterial *other) const
33 {
34  auto material = static_cast<const ShadowedBorderRectangleMaterial *>(other);
35 
36  auto result = ShadowedRectangleMaterial::compare(other);
37  /* clang-format off */
38  if (result == 0
39  && material->borderColor == borderColor
40  && qFuzzyCompare(material->borderWidth, borderWidth)) { /* clang-format on */
41  return 0;
42  }
43 
44  return result;
45 }
46 
47 ShadowedBorderRectangleShader::ShadowedBorderRectangleShader(ShadowedRectangleMaterial::ShaderType shaderType)
48  : ShadowedRectangleShader(shaderType)
49 {
50  setShader(shaderType, QStringLiteral("shadowedborderrectangle"));
51 }
52 
53 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
55 {
57  m_borderWidthLocation = program()->uniformLocation("borderWidth");
58  m_borderColorLocation = program()->uniformLocation("borderColor");
59 }
60 
62 {
63  ShadowedRectangleShader::updateState(state, newMaterial, oldMaterial);
64 
65  auto p = program();
66 
67  if (!oldMaterial || newMaterial->compare(oldMaterial) != 0 || state.isCachedMaterialDataDirty()) {
68  auto material = static_cast<ShadowedBorderRectangleMaterial *>(newMaterial);
69  p->setUniformValue(m_borderWidthLocation, material->borderWidth);
70  p->setUniformValue(m_borderColorLocation, material->borderColor);
71  }
72 }
73 #else
74 bool ShadowedBorderRectangleShader::updateUniformData(QSGMaterialShader::RenderState &state, QSGMaterial *newMaterial, QSGMaterial *oldMaterial)
75 {
76  bool changed = ShadowedRectangleShader::updateUniformData(state, newMaterial, oldMaterial);
77  QByteArray *buf = state.uniformData();
78  Q_ASSERT(buf->size() >= 160);
79 
80  if (!oldMaterial || newMaterial->compare(oldMaterial) != 0) {
81  const auto material = static_cast<ShadowedBorderRectangleMaterial *>(newMaterial);
82  memcpy(buf->data() + 136, &material->borderWidth, 8);
83  float c[4];
84  material->borderColor.getRgbF(&c[0], &c[1], &c[2], &c[3]);
85  memcpy(buf->data() + 144, c, 16);
86  changed = true;
87  }
88 
89  return changed;
90 }
91 #endif
void setFlag(QSGMaterial::Flags flags, bool on)
virtual void initialize()
A material rendering a rectangle with a shadow and a border.
virtual int compare(const QSGMaterial *other) const const
virtual QSGMaterialShader * createShader() const const=0
virtual void updateState(const QSGMaterialShader::RenderState &state, QSGMaterial *newMaterial, QSGMaterial *oldMaterial)
bool isCachedMaterialDataDirty() const const
int size() const const
char * data()
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Tue Feb 7 2023 04:14:24 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.