1 /*
2     KWin - the KDE window manager
3     This file is part of the KDE project.
4 
5     SPDX-FileCopyrightText: 2006-2007 Rivo Laks <rivolaks@hot.ee>
6     SPDX-FileCopyrightText: 2010, 2011 Martin Gräßlin <mgraesslin@kde.org>
7 
8     SPDX-License-Identifier: GPL-2.0-or-later
9 */
10 
11 #ifndef KWIN_GLTEXTURE_H
12 #define KWIN_GLTEXTURE_H
13 
14 #include <kwinglutils_export.h>
15 
16 #include <QSize>
17 #include <QRegion>
18 #include <QSharedPointer>
19 #include <QExplicitlySharedDataPointer>
20 #include <QMatrix4x4>
21 
22 #include <epoxy/gl.h>
23 
24 class QImage;
25 class QPixmap;
26 
27 /** @addtogroup kwineffects */
28 /** @{ */
29 
30 namespace KWin
31 {
32 
33 class GLVertexBuffer;
34 class GLTexturePrivate;
35 
36 enum TextureCoordinateType {
37     NormalizedCoordinates = 0,
38     UnnormalizedCoordinates,
39 };
40 
41 class KWINGLUTILS_EXPORT GLTexture
42 {
43 public:
44     explicit GLTexture(GLenum target);
45     GLTexture(const GLTexture& tex);
46     explicit GLTexture(const QImage& image, GLenum target = GL_TEXTURE_2D);
47     explicit GLTexture(const QPixmap& pixmap, GLenum target = GL_TEXTURE_2D);
48     explicit GLTexture(const QString& fileName);
49     GLTexture(GLenum internalFormat, int width, int height, int levels = 1, bool needsMutability = false);
50     explicit GLTexture(GLenum internalFormat, const QSize &size, int levels = 1, bool needsMutability = false);
51 
52     /**
53      * Creates the underlying texture object. Returns @c true if the texture has been created
54      * successfully; otherwise returns @c false. Note that this does not allocate any storage
55      * for the texture.
56      */
57     bool create();
58 
59     /**
60      * Create a GLTexture wrapper around an existing texture.
61      * Management of the underlying texture remains the responsibility of the caller.
62      * @since 5.18
63      */
64     explicit GLTexture(GLuint textureId, GLenum internalFormat, const QSize &size, int levels = 1);
65     virtual ~GLTexture();
66 
67     GLTexture & operator = (const GLTexture& tex);
68 
69     bool isNull() const;
70     QSize size() const;
71     void setSize(const QSize &size);
72     int width() const;
73     int height() const;
74     /**
75      * @since 4.7
76      */
77     bool isYInverted() const;
78     /**
79      * @since 4.8
80      */
81     void setYInverted(bool inverted);
82 
83     /**
84      * Specifies which component of a texel is placed in each respective
85      * component of the vector returned to the shader.
86      *
87      * Valid values are GL_RED, GL_GREEN, GL_BLUE, GL_ALPHA, GL_ONE and GL_ZERO.
88      *
89      * @see swizzleSupported()
90      * @since 5.2
91      */
92     void setSwizzle(GLenum red, GLenum green, GLenum blue, GLenum alpha);
93 
94     /**
95      * Returns a matrix that transforms texture coordinates of the given type,
96      * taking the texture target and the y-inversion flag into account.
97      *
98      * @since 4.11
99      */
100     QMatrix4x4 matrix(TextureCoordinateType type) const;
101 
102     void update(const QImage& image, const QPoint &offset = QPoint(0, 0), const QRect &src = QRect());
103     virtual void discard();
104     void bind();
105     void unbind();
106     void render(const QRegion &region, const QRect& rect, bool hardwareClipping = false);
107 
108     GLuint texture() const;
109     GLenum target() const;
110     GLenum filter() const;
111     GLenum internalFormat() const;
112 
113     QImage toImage() const;
114 
115     /** @short
116      * Make the texture fully transparent
117      */
118     void clear();
119     bool isDirty() const;
120     void setFilter(GLenum filter);
121     void setWrapMode(GLenum mode);
122     void setDirty();
123 
124     void generateMipmaps();
125 
126     static bool framebufferObjectSupported();
127 
128     /**
129      * Returns true if texture swizzle is supported, and false otherwise
130      *
131      * Texture swizzle requires OpenGL 3.3, GL_ARB_texture_swizzle, or OpenGL ES 3.0.
132      *
133      * @since 5.2
134      */
135     static bool supportsSwizzle();
136 
137     /**
138      * Returns @c true if texture formats R* are supported, and @c false otherwise.
139      *
140      * This requires OpenGL 3.0, GL_ARB_texture_rg or OpenGL ES 3.0 or GL_EXT_texture_rg.
141      *
142      * @since 5.2.1
143      */
144     static bool supportsFormatRG();
145 
146 protected:
147     QExplicitlySharedDataPointer<GLTexturePrivate> d_ptr;
148     GLTexture(GLTexturePrivate& dd);
149 
150 private:
151     Q_DECLARE_PRIVATE(GLTexture)
152 };
153 
154 } // namespace
155 
156 /** @} */
157 
158 #endif
159