qgl.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  1. /****************************************************************************
  2. **
  3. ** Copyright (C) 2016 The Qt Company Ltd.
  4. ** Contact: https://www.qt.io/licensing/
  5. **
  6. ** This file is part of the QtOpenGL module of the Qt Toolkit.
  7. **
  8. ** $QT_BEGIN_LICENSE:LGPL$
  9. ** Commercial License Usage
  10. ** Licensees holding valid commercial Qt licenses may use this file in
  11. ** accordance with the commercial license agreement provided with the
  12. ** Software or, alternatively, in accordance with the terms contained in
  13. ** a written agreement between you and The Qt Company. For licensing terms
  14. ** and conditions see https://www.qt.io/terms-conditions. For further
  15. ** information use the contact form at https://www.qt.io/contact-us.
  16. **
  17. ** GNU Lesser General Public License Usage
  18. ** Alternatively, this file may be used under the terms of the GNU Lesser
  19. ** General Public License version 3 as published by the Free Software
  20. ** Foundation and appearing in the file LICENSE.LGPL3 included in the
  21. ** packaging of this file. Please review the following information to
  22. ** ensure the GNU Lesser General Public License version 3 requirements
  23. ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
  24. **
  25. ** GNU General Public License Usage
  26. ** Alternatively, this file may be used under the terms of the GNU
  27. ** General Public License version 2.0 or (at your option) the GNU General
  28. ** Public license version 3 or any later version approved by the KDE Free
  29. ** Qt Foundation. The licenses are as published by the Free Software
  30. ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
  31. ** included in the packaging of this file. Please review the following
  32. ** information to ensure the GNU General Public License requirements will
  33. ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
  34. ** https://www.gnu.org/licenses/gpl-3.0.html.
  35. **
  36. ** $QT_END_LICENSE$
  37. **
  38. ****************************************************************************/
  39. #ifndef QGL_H
  40. #define QGL_H
  41. #ifndef QT_NO_OPENGL
  42. #include <QtGui/qopengl.h>
  43. #include <QtWidgets/qwidget.h>
  44. #include <QtGui/qpaintengine.h>
  45. #include <QtOpenGL/qglcolormap.h>
  46. #include <QtCore/qmap.h>
  47. #include <QtCore/qscopedpointer.h>
  48. #include <QtGui/QSurfaceFormat>
  49. QT_BEGIN_NAMESPACE
  50. class QPixmap;
  51. class QGLWidgetPrivate;
  52. class QGLContextPrivate;
  53. // Namespace class:
  54. namespace QGL
  55. {
  56. enum FormatOption {
  57. DoubleBuffer = 0x0001,
  58. DepthBuffer = 0x0002,
  59. Rgba = 0x0004,
  60. AlphaChannel = 0x0008,
  61. AccumBuffer = 0x0010,
  62. StencilBuffer = 0x0020,
  63. StereoBuffers = 0x0040,
  64. DirectRendering = 0x0080,
  65. HasOverlay = 0x0100,
  66. SampleBuffers = 0x0200,
  67. DeprecatedFunctions = 0x0400,
  68. SingleBuffer = DoubleBuffer << 16,
  69. NoDepthBuffer = DepthBuffer << 16,
  70. ColorIndex = Rgba << 16,
  71. NoAlphaChannel = AlphaChannel << 16,
  72. NoAccumBuffer = AccumBuffer << 16,
  73. NoStencilBuffer = StencilBuffer << 16,
  74. NoStereoBuffers = StereoBuffers << 16,
  75. IndirectRendering = DirectRendering << 16,
  76. NoOverlay = HasOverlay << 16,
  77. NoSampleBuffers = SampleBuffers << 16,
  78. NoDeprecatedFunctions = DeprecatedFunctions << 16
  79. };
  80. Q_DECLARE_FLAGS(FormatOptions, FormatOption)
  81. }
  82. Q_DECLARE_OPERATORS_FOR_FLAGS(QGL::FormatOptions)
  83. class QGLFormatPrivate;
  84. class Q_OPENGL_EXPORT QGLFormat
  85. {
  86. public:
  87. QGLFormat();
  88. QGLFormat(QGL::FormatOptions options, int plane = 0);
  89. QGLFormat(const QGLFormat &other);
  90. QGLFormat &operator=(const QGLFormat &other);
  91. ~QGLFormat();
  92. void setDepthBufferSize(int size);
  93. int depthBufferSize() const;
  94. void setAccumBufferSize(int size);
  95. int accumBufferSize() const;
  96. void setRedBufferSize(int size);
  97. int redBufferSize() const;
  98. void setGreenBufferSize(int size);
  99. int greenBufferSize() const;
  100. void setBlueBufferSize(int size);
  101. int blueBufferSize() const;
  102. void setAlphaBufferSize(int size);
  103. int alphaBufferSize() const;
  104. void setStencilBufferSize(int size);
  105. int stencilBufferSize() const;
  106. void setSampleBuffers(bool enable);
  107. bool sampleBuffers() const;
  108. void setSamples(int numSamples);
  109. int samples() const;
  110. void setSwapInterval(int interval);
  111. int swapInterval() const;
  112. bool doubleBuffer() const;
  113. void setDoubleBuffer(bool enable);
  114. bool depth() const;
  115. void setDepth(bool enable);
  116. bool rgba() const;
  117. void setRgba(bool enable);
  118. bool alpha() const;
  119. void setAlpha(bool enable);
  120. bool accum() const;
  121. void setAccum(bool enable);
  122. bool stencil() const;
  123. void setStencil(bool enable);
  124. bool stereo() const;
  125. void setStereo(bool enable);
  126. bool directRendering() const;
  127. void setDirectRendering(bool enable);
  128. bool hasOverlay() const;
  129. void setOverlay(bool enable);
  130. int plane() const;
  131. void setPlane(int plane);
  132. void setOption(QGL::FormatOptions opt);
  133. bool testOption(QGL::FormatOptions opt) const;
  134. static QGLFormat defaultFormat();
  135. static void setDefaultFormat(const QGLFormat& f);
  136. static QGLFormat defaultOverlayFormat();
  137. static void setDefaultOverlayFormat(const QGLFormat& f);
  138. static bool hasOpenGL();
  139. static bool hasOpenGLOverlays();
  140. void setVersion(int major, int minor);
  141. int majorVersion() const;
  142. int minorVersion() const;
  143. enum OpenGLContextProfile {
  144. NoProfile,
  145. CoreProfile,
  146. CompatibilityProfile
  147. };
  148. void setProfile(OpenGLContextProfile profile);
  149. OpenGLContextProfile profile() const;
  150. enum OpenGLVersionFlag {
  151. OpenGL_Version_None = 0x00000000,
  152. OpenGL_Version_1_1 = 0x00000001,
  153. OpenGL_Version_1_2 = 0x00000002,
  154. OpenGL_Version_1_3 = 0x00000004,
  155. OpenGL_Version_1_4 = 0x00000008,
  156. OpenGL_Version_1_5 = 0x00000010,
  157. OpenGL_Version_2_0 = 0x00000020,
  158. OpenGL_Version_2_1 = 0x00000040,
  159. OpenGL_ES_Common_Version_1_0 = 0x00000080,
  160. OpenGL_ES_CommonLite_Version_1_0 = 0x00000100,
  161. OpenGL_ES_Common_Version_1_1 = 0x00000200,
  162. OpenGL_ES_CommonLite_Version_1_1 = 0x00000400,
  163. OpenGL_ES_Version_2_0 = 0x00000800,
  164. OpenGL_Version_3_0 = 0x00001000,
  165. OpenGL_Version_3_1 = 0x00002000,
  166. OpenGL_Version_3_2 = 0x00004000,
  167. OpenGL_Version_3_3 = 0x00008000,
  168. OpenGL_Version_4_0 = 0x00010000,
  169. OpenGL_Version_4_1 = 0x00020000,
  170. OpenGL_Version_4_2 = 0x00040000,
  171. OpenGL_Version_4_3 = 0x00080000
  172. };
  173. Q_DECLARE_FLAGS(OpenGLVersionFlags, OpenGLVersionFlag)
  174. static OpenGLVersionFlags openGLVersionFlags();
  175. static QGLFormat fromSurfaceFormat(const QSurfaceFormat &format);
  176. static QSurfaceFormat toSurfaceFormat(const QGLFormat &format);
  177. private:
  178. QGLFormatPrivate *d;
  179. void detach();
  180. friend Q_OPENGL_EXPORT bool operator==(const QGLFormat&, const QGLFormat&);
  181. friend Q_OPENGL_EXPORT bool operator!=(const QGLFormat&, const QGLFormat&);
  182. #ifndef QT_NO_DEBUG_STREAM
  183. friend Q_OPENGL_EXPORT QDebug operator<<(QDebug, const QGLFormat &);
  184. #endif
  185. };
  186. Q_DECLARE_OPERATORS_FOR_FLAGS(QGLFormat::OpenGLVersionFlags)
  187. Q_OPENGL_EXPORT bool operator==(const QGLFormat&, const QGLFormat&);
  188. Q_OPENGL_EXPORT bool operator!=(const QGLFormat&, const QGLFormat&);
  189. #ifndef QT_NO_DEBUG_STREAM
  190. Q_OPENGL_EXPORT QDebug operator<<(QDebug, const QGLFormat &);
  191. #endif
  192. class QGLFunctions;
  193. class Q_OPENGL_EXPORT QGLContext
  194. {
  195. Q_DECLARE_PRIVATE(QGLContext)
  196. public:
  197. QGLContext(const QGLFormat& format, QPaintDevice* device);
  198. QGLContext(const QGLFormat& format);
  199. virtual ~QGLContext();
  200. virtual bool create(const QGLContext* shareContext = Q_NULLPTR);
  201. bool isValid() const;
  202. bool isSharing() const;
  203. void reset();
  204. static bool areSharing(const QGLContext *context1, const QGLContext *context2);
  205. QGLFormat format() const;
  206. QGLFormat requestedFormat() const;
  207. void setFormat(const QGLFormat& format);
  208. void moveToThread(QThread *thread);
  209. virtual void makeCurrent();
  210. virtual void doneCurrent();
  211. virtual void swapBuffers() const;
  212. QGLFunctions *functions() const;
  213. enum BindOption {
  214. NoBindOption = 0x0000,
  215. InvertedYBindOption = 0x0001,
  216. MipmapBindOption = 0x0002,
  217. PremultipliedAlphaBindOption = 0x0004,
  218. LinearFilteringBindOption = 0x0008,
  219. MemoryManagedBindOption = 0x0010, // internal flag
  220. CanFlipNativePixmapBindOption = 0x0020, // internal flag
  221. TemporarilyCachedBindOption = 0x0040, // internal flag
  222. DefaultBindOption = LinearFilteringBindOption
  223. | InvertedYBindOption
  224. | MipmapBindOption,
  225. InternalBindOption = MemoryManagedBindOption
  226. | PremultipliedAlphaBindOption
  227. };
  228. Q_DECLARE_FLAGS(BindOptions, BindOption)
  229. GLuint bindTexture(const QImage &image, GLenum target, GLint format,
  230. BindOptions options);
  231. GLuint bindTexture(const QPixmap &pixmap, GLenum target, GLint format,
  232. BindOptions options);
  233. GLuint bindTexture(const QImage &image, GLenum target = GL_TEXTURE_2D,
  234. GLint format = GL_RGBA);
  235. GLuint bindTexture(const QPixmap &pixmap, GLenum target = GL_TEXTURE_2D,
  236. GLint format = GL_RGBA);
  237. GLuint bindTexture(const QString &fileName);
  238. void deleteTexture(GLuint tx_id);
  239. void drawTexture(const QRectF &target, GLuint textureId, GLenum textureTarget = GL_TEXTURE_2D);
  240. void drawTexture(const QPointF &point, GLuint textureId, GLenum textureTarget = GL_TEXTURE_2D);
  241. static void setTextureCacheLimit(int size);
  242. static int textureCacheLimit();
  243. QFunctionPointer getProcAddress(const QString &proc) const;
  244. QPaintDevice* device() const;
  245. QColor overlayTransparentColor() const;
  246. static const QGLContext* currentContext();
  247. static QGLContext *fromOpenGLContext(QOpenGLContext *platformContext);
  248. QOpenGLContext *contextHandle() const;
  249. protected:
  250. virtual bool chooseContext(const QGLContext* shareContext = Q_NULLPTR);
  251. bool deviceIsPixmap() const;
  252. bool windowCreated() const;
  253. void setWindowCreated(bool on);
  254. bool initialized() const;
  255. void setInitialized(bool on);
  256. uint colorIndex(const QColor& c) const;
  257. void setValid(bool valid);
  258. void setDevice(QPaintDevice *pDev);
  259. protected:
  260. static QGLContext* currentCtx;
  261. private:
  262. QGLContext(QOpenGLContext *windowContext);
  263. QScopedPointer<QGLContextPrivate> d_ptr;
  264. friend class QGLPixelBuffer;
  265. friend class QGLPixelBufferPrivate;
  266. friend class QGLWidget;
  267. friend class QGLWidgetPrivate;
  268. friend class QGLGlyphCache;
  269. friend class QGL2PaintEngineEx;
  270. friend class QGL2PaintEngineExPrivate;
  271. friend class QGLEngineShaderManager;
  272. friend class QGLTextureGlyphCache;
  273. friend struct QGLGlyphTexture;
  274. friend class QGLContextGroup;
  275. friend class QGLPixmapBlurFilter;
  276. friend class QGLTexture;
  277. friend QGLFormat::OpenGLVersionFlags QGLFormat::openGLVersionFlags();
  278. friend class QGLFramebufferObject;
  279. friend class QGLFramebufferObjectPrivate;
  280. friend class QGLFBOGLPaintDevice;
  281. friend class QGLPaintDevice;
  282. friend class QGLWidgetGLPaintDevice;
  283. friend class QX11GLSharedContexts;
  284. friend class QGLContextResourceBase;
  285. friend class QSGDistanceFieldGlyphCache;
  286. private:
  287. Q_DISABLE_COPY(QGLContext)
  288. };
  289. Q_DECLARE_OPERATORS_FOR_FLAGS(QGLContext::BindOptions)
  290. class Q_OPENGL_EXPORT QGLWidget : public QWidget
  291. {
  292. Q_OBJECT
  293. Q_DECLARE_PRIVATE(QGLWidget)
  294. public:
  295. explicit QGLWidget(QWidget* parent=Q_NULLPTR,
  296. const QGLWidget* shareWidget = Q_NULLPTR, Qt::WindowFlags f=Qt::WindowFlags());
  297. explicit QGLWidget(QGLContext *context, QWidget* parent=Q_NULLPTR,
  298. const QGLWidget* shareWidget = Q_NULLPTR, Qt::WindowFlags f=Qt::WindowFlags());
  299. explicit QGLWidget(const QGLFormat& format, QWidget* parent=Q_NULLPTR,
  300. const QGLWidget* shareWidget = Q_NULLPTR, Qt::WindowFlags f=Qt::WindowFlags());
  301. ~QGLWidget();
  302. void qglColor(const QColor& c) const;
  303. void qglClearColor(const QColor& c) const;
  304. bool isValid() const;
  305. bool isSharing() const;
  306. void makeCurrent();
  307. void doneCurrent();
  308. bool doubleBuffer() const;
  309. void swapBuffers();
  310. QGLFormat format() const;
  311. void setFormat(const QGLFormat& format);
  312. QGLContext* context() const;
  313. void setContext(QGLContext* context, const QGLContext* shareContext = Q_NULLPTR,
  314. bool deleteOldContext = true);
  315. QPixmap renderPixmap(int w = 0, int h = 0, bool useContext = false);
  316. QImage grabFrameBuffer(bool withAlpha = false);
  317. void makeOverlayCurrent();
  318. const QGLContext* overlayContext() const;
  319. static QImage convertToGLFormat(const QImage& img);
  320. const QGLColormap & colormap() const;
  321. void setColormap(const QGLColormap & map);
  322. void renderText(int x, int y, const QString & str,
  323. const QFont & fnt = QFont());
  324. void renderText(double x, double y, double z, const QString & str,
  325. const QFont & fnt = QFont());
  326. QPaintEngine *paintEngine() const Q_DECL_OVERRIDE;
  327. GLuint bindTexture(const QImage &image, GLenum target, GLint format,
  328. QGLContext::BindOptions options);
  329. GLuint bindTexture(const QPixmap &pixmap, GLenum target, GLint format,
  330. QGLContext::BindOptions options);
  331. GLuint bindTexture(const QImage &image, GLenum target = GL_TEXTURE_2D,
  332. GLint format = GL_RGBA);
  333. GLuint bindTexture(const QPixmap &pixmap, GLenum target = GL_TEXTURE_2D,
  334. GLint format = GL_RGBA);
  335. GLuint bindTexture(const QString &fileName);
  336. void deleteTexture(GLuint tx_id);
  337. void drawTexture(const QRectF &target, GLuint textureId, GLenum textureTarget = GL_TEXTURE_2D);
  338. void drawTexture(const QPointF &point, GLuint textureId, GLenum textureTarget = GL_TEXTURE_2D);
  339. public Q_SLOTS:
  340. virtual void updateGL();
  341. virtual void updateOverlayGL();
  342. protected:
  343. bool event(QEvent *) Q_DECL_OVERRIDE;
  344. virtual void initializeGL();
  345. virtual void resizeGL(int w, int h);
  346. virtual void paintGL();
  347. virtual void initializeOverlayGL();
  348. virtual void resizeOverlayGL(int w, int h);
  349. virtual void paintOverlayGL();
  350. void setAutoBufferSwap(bool on);
  351. bool autoBufferSwap() const;
  352. void paintEvent(QPaintEvent*) Q_DECL_OVERRIDE;
  353. void resizeEvent(QResizeEvent*) Q_DECL_OVERRIDE;
  354. virtual void glInit();
  355. virtual void glDraw();
  356. QGLWidget(QGLWidgetPrivate &dd,
  357. const QGLFormat &format = QGLFormat(),
  358. QWidget *parent = Q_NULLPTR,
  359. const QGLWidget* shareWidget = Q_NULLPTR,
  360. Qt::WindowFlags f = Qt::WindowFlags());
  361. private:
  362. Q_DISABLE_COPY(QGLWidget)
  363. friend class QGLDrawable;
  364. friend class QGLPixelBuffer;
  365. friend class QGLPixelBufferPrivate;
  366. friend class QGLContext;
  367. friend class QGLContextPrivate;
  368. friend class QGLOverlayWidget;
  369. friend class QGLPaintDevice;
  370. friend class QGLWidgetGLPaintDevice;
  371. };
  372. //
  373. // QGLFormat inline functions
  374. //
  375. inline bool QGLFormat::doubleBuffer() const
  376. {
  377. return testOption(QGL::DoubleBuffer);
  378. }
  379. inline bool QGLFormat::depth() const
  380. {
  381. return testOption(QGL::DepthBuffer);
  382. }
  383. inline bool QGLFormat::rgba() const
  384. {
  385. return testOption(QGL::Rgba);
  386. }
  387. inline bool QGLFormat::alpha() const
  388. {
  389. return testOption(QGL::AlphaChannel);
  390. }
  391. inline bool QGLFormat::accum() const
  392. {
  393. return testOption(QGL::AccumBuffer);
  394. }
  395. inline bool QGLFormat::stencil() const
  396. {
  397. return testOption(QGL::StencilBuffer);
  398. }
  399. inline bool QGLFormat::stereo() const
  400. {
  401. return testOption(QGL::StereoBuffers);
  402. }
  403. inline bool QGLFormat::directRendering() const
  404. {
  405. return testOption(QGL::DirectRendering);
  406. }
  407. inline bool QGLFormat::hasOverlay() const
  408. {
  409. return testOption(QGL::HasOverlay);
  410. }
  411. inline bool QGLFormat::sampleBuffers() const
  412. {
  413. return testOption(QGL::SampleBuffers);
  414. }
  415. QT_END_NAMESPACE
  416. #endif // QT_NO_OPENGL
  417. #endif // QGL_H