File indexing completed on 2025-03-23 11:13:58
0001 /* 0002 KWin - the KDE window manager 0003 This file is part of the KDE project. 0004 0005 SPDX-FileCopyrightText: 2006-2007 Rivo Laks <rivolaks@hot.ee> 0006 SPDX-FileCopyrightText: 2010, 2011 Martin Gräßlin <mgraesslin@kde.org> 0007 0008 SPDX-License-Identifier: GPL-2.0-or-later 0009 */ 0010 0011 #pragma once 0012 0013 #include <kwinglutils_export.h> 0014 0015 #include <QExplicitlySharedDataPointer> 0016 #include <QMatrix4x4> 0017 #include <QRegion> 0018 #include <QSize> 0019 0020 #include <epoxy/gl.h> 0021 0022 class QImage; 0023 class QPixmap; 0024 0025 /** @addtogroup kwineffects */ 0026 /** @{ */ 0027 0028 namespace KWin 0029 { 0030 0031 class GLVertexBuffer; 0032 class GLTexturePrivate; 0033 0034 enum TextureCoordinateType { 0035 NormalizedCoordinates = 0, 0036 UnnormalizedCoordinates, 0037 }; 0038 0039 class KWINGLUTILS_EXPORT GLTexture 0040 { 0041 public: 0042 explicit GLTexture(GLenum target); 0043 GLTexture(const GLTexture &tex); 0044 explicit GLTexture(const QImage &image, GLenum target = GL_TEXTURE_2D); 0045 explicit GLTexture(const QPixmap &pixmap, GLenum target = GL_TEXTURE_2D); 0046 explicit GLTexture(const QString &fileName); 0047 GLTexture(GLenum internalFormat, int width, int height, int levels = 1, bool needsMutability = false); 0048 explicit GLTexture(GLenum internalFormat, const QSize &size, int levels = 1, bool needsMutability = false); 0049 0050 /** 0051 * Creates the underlying texture object. Returns @c true if the texture has been created 0052 * successfully; otherwise returns @c false. Note that this does not allocate any storage 0053 * for the texture. 0054 */ 0055 bool create(); 0056 0057 /** 0058 * Create a GLTexture wrapper around an existing texture. 0059 * Management of the underlying texture remains the responsibility of the caller. 0060 * @since 5.18 0061 */ 0062 explicit GLTexture(GLuint textureId, GLenum internalFormat, const QSize &size, int levels = 1); 0063 virtual ~GLTexture(); 0064 0065 GLTexture &operator=(const GLTexture &tex); 0066 0067 bool isNull() const; 0068 QSize size() const; 0069 void setSize(const QSize &size); 0070 int width() const; 0071 int height() const; 0072 /** 0073 * @since 4.7 0074 */ 0075 bool isYInverted() const; 0076 /** 0077 * @since 4.8 0078 */ 0079 void setYInverted(bool inverted); 0080 0081 /** 0082 * Specifies which component of a texel is placed in each respective 0083 * component of the vector returned to the shader. 0084 * 0085 * Valid values are GL_RED, GL_GREEN, GL_BLUE, GL_ALPHA, GL_ONE and GL_ZERO. 0086 * 0087 * @see swizzleSupported() 0088 * @since 5.2 0089 */ 0090 void setSwizzle(GLenum red, GLenum green, GLenum blue, GLenum alpha); 0091 0092 /** 0093 * Returns a matrix that transforms texture coordinates of the given type, 0094 * taking the texture target and the y-inversion flag into account. 0095 * 0096 * @since 4.11 0097 */ 0098 QMatrix4x4 matrix(TextureCoordinateType type) const; 0099 0100 void update(const QImage &image, const QPoint &offset = QPoint(0, 0), const QRect &src = QRect()); 0101 virtual void discard(); 0102 void bind(); 0103 void unbind(); 0104 void render(const QRect &rect, qreal scale); 0105 void render(const QRegion ®ion, const QRect &rect, qreal scale, bool hardwareClipping = false); 0106 0107 GLuint texture() const; 0108 GLenum target() const; 0109 GLenum filter() const; 0110 GLenum internalFormat() const; 0111 0112 QImage toImage() const; 0113 0114 /** @short 0115 * Make the texture fully transparent 0116 */ 0117 void clear(); 0118 /** 0119 * @deprecated track modifications to the texture yourself 0120 */ 0121 void setDirty(); 0122 bool isDirty() const; 0123 void setFilter(GLenum filter); 0124 void setWrapMode(GLenum mode); 0125 0126 void generateMipmaps(); 0127 0128 static bool framebufferObjectSupported(); 0129 0130 /** 0131 * Returns true if texture swizzle is supported, and false otherwise 0132 * 0133 * Texture swizzle requires OpenGL 3.3, GL_ARB_texture_swizzle, or OpenGL ES 3.0. 0134 * 0135 * @since 5.2 0136 */ 0137 static bool supportsSwizzle(); 0138 0139 /** 0140 * Returns @c true if texture formats R* are supported, and @c false otherwise. 0141 * 0142 * This requires OpenGL 3.0, GL_ARB_texture_rg or OpenGL ES 3.0 or GL_EXT_texture_rg. 0143 * 0144 * @since 5.2.1 0145 */ 0146 static bool supportsFormatRG(); 0147 0148 protected: 0149 QExplicitlySharedDataPointer<GLTexturePrivate> d_ptr; 0150 GLTexture(GLTexturePrivate &dd); 0151 0152 private: 0153 Q_DECLARE_PRIVATE(GLTexture) 0154 }; 0155 0156 } // namespace 0157 0158 /** @} */