1/*
2 * SPDX-FileCopyrightText: 2020 Arjen Hiemstra <ahiemstra@heimr.nl>
3 *
4 * SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6
7#include "shadowedtexturenode.h"
8
9#include "shadowedbordertexturematerial.h"
10
11template<typename T>
12inline void preprocessTexture(QSGMaterial *material, QSGTextureProvider *provider)
13{
14 auto m = static_cast<T *>(material);
15 // Since we handle texture coordinates differently in the shader, we
16 // need to remove the texture from the atlas for now.
17 if (provider->texture()->isAtlasTexture()) {
18 // Blegh, I have no idea why "removedFromAtlas" doesn't just return
19 // the texture when it's not an atlas.
20 m->textureSource = provider->texture()->removedFromAtlas();
21 } else {
22 m->textureSource = provider->texture();
23 }
24 if (QSGDynamicTexture *dynamic_texture = qobject_cast<QSGDynamicTexture *>(m->textureSource)) {
25 dynamic_texture->updateTexture();
26 }
27}
28
29ShadowedTextureNode::ShadowedTextureNode()
30 : ShadowedRectangleNode()
31{
32 setFlag(QSGNode::UsePreprocess);
33}
34
35ShadowedTextureNode::~ShadowedTextureNode()
36{
37 QObject::disconnect(m_textureChangeConnectionHandle);
38}
39
40void ShadowedTextureNode::setTextureSource(QSGTextureProvider *source)
41{
42 if (m_textureSource == source) {
43 return;
44 }
45
46 if (m_textureSource) {
47 m_textureSource->disconnect();
48 }
49
50 m_textureSource = source;
51 m_textureChangeConnectionHandle = QObject::connect(sender: m_textureSource.data(), signal: &QSGTextureProvider::textureChanged, slot: [this] {
52 markDirty(bits: QSGNode::DirtyMaterial);
53 });
54 markDirty(bits: QSGNode::DirtyMaterial);
55}
56
57void ShadowedTextureNode::preprocess()
58{
59 if (m_textureSource && m_material && m_textureSource->texture()) {
60 if (m_material->type() == borderlessMaterialType()) {
61 preprocessTexture<ShadowedTextureMaterial>(material: m_material, provider: m_textureSource);
62 } else {
63 preprocessTexture<ShadowedBorderTextureMaterial>(material: m_material, provider: m_textureSource);
64 }
65 }
66}
67
68ShadowedRectangleMaterial *ShadowedTextureNode::createBorderlessMaterial()
69{
70 return new ShadowedTextureMaterial{};
71}
72
73ShadowedBorderRectangleMaterial *ShadowedTextureNode::createBorderMaterial()
74{
75 return new ShadowedBorderTextureMaterial{};
76}
77
78QSGMaterialType *ShadowedTextureNode::borderlessMaterialType()
79{
80 return &ShadowedTextureMaterial::staticType;
81}
82
83QSGMaterialType *ShadowedTextureNode::borderMaterialType()
84{
85 return &ShadowedBorderTextureMaterial::staticType;
86}
87

source code of kirigami/src/scenegraph/shadowedtexturenode.cpp