Overte C++ Documentation
gl/src/gl/Context.h
1 //
2 // Created by Bradley Austin Davis on 2016/08/21
3 // Copyright 2013-2016 High Fidelity, Inc.
4 //
5 // Distributed under the Apache License, Version 2.0.
6 // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
7 //
8 
9 #ifndef hifi_gl_context_h
10 #define hifi_gl_context_h
11 
12 #include <stdint.h>
13 #include <QtGlobal>
14 #include <atomic>
15 
16 #if defined(Q_OS_WIN)
17 #include <Windows.h>
18 #endif
19 
20 class QSurface;
21 class QWindow;
22 class QOpenGLContext;
23 class QThread;
24 class QOpenGLDebugMessage;
25 
26 #if defined(Q_OS_WIN) && (defined(USE_GLES) || defined(USE_KHR_ROBUSTNESS))
27 //#if defined(Q_OS_WIN)
28 #define GL_CUSTOM_CONTEXT
29 #endif
30 namespace gl {
31 
32  class Context {
33  protected:
34  QWindow* _window { nullptr };
35  static void destroyContext(QOpenGLContext* context);
36 #if defined(GL_CUSTOM_CONTEXT)
37  static bool USE_CUSTOM_CONTEXT;
38 
39  uint32_t _version { 0x0401 };
40  HWND _hwnd { 0 };
41  HDC _hdc { 0 };
42  HGLRC _hglrc { 0 };
43 #endif
44  QOpenGLContext* _qglContext { nullptr };
45 
46  private:
47  Context(const Context& other);
48  void qtCreate(QOpenGLContext* shareContext);
49 #if defined(GL_CUSTOM_CONTEXT)
50  void createWrapperContext();
51 #endif
52 
53  public:
54  static bool enableDebugLogger();
55  static void debugMessageHandler(const QOpenGLDebugMessage &debugMessage);
56  static void setupDebugLogging(QOpenGLContext* context);
57 
58  Context();
59  Context(QWindow* window);
60  void release();
61  virtual ~Context();
62 
63  QWindow* getWindow() const { return _window; }
64  void clear();
65  void setWindow(QWindow* window);
66  bool makeCurrent();
67  static void makeCurrent(QOpenGLContext* context, QSurface* surface);
68  void swapBuffers();
69  void doneCurrent();
70  virtual void create(QOpenGLContext* shareContext = nullptr);
71  QOpenGLContext* qglContext() const { return _qglContext; }
72  void moveToThread(QThread* thread);
73 
74  static size_t evalSurfaceMemoryUsage(uint32_t width, uint32_t height, uint32_t pixelSize);
75  static size_t getSwapchainMemoryUsage();
76  static void updateSwapchainMemoryUsage(size_t prevSize, size_t newSize);
77 
78  private:
79  static std::atomic<size_t> _totalSwapchainMemoryUsage;
80 
81  size_t _swapchainMemoryUsage { 0 };
82  size_t _swapchainPixelSize { 0 };
83  void updateSwapchainMemoryCounter();
84  };
85 
86  class OffscreenContext : public Context {
87  using Parent = Context;
88  public:
89  virtual ~OffscreenContext();
90  void create(QOpenGLContext* shareContext = nullptr) override;
91  };
92 
93 }
94 
95 #endif // hifi_gpu_GPUConfig_h