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 namespace gl {
27 
28  class Context {
29  protected:
30  QWindow* _window { nullptr };
31  static void destroyContext(QOpenGLContext* context);
32 #ifdef Q_OS_WIN
33  static bool USE_CUSTOM_CONTEXT;
34 
35  uint32_t _version { 0x0401 };
36  HWND _hwnd { 0 };
37  HDC _hdc { 0 };
38  HGLRC _hglrc { 0 };
39 #endif
40  QOpenGLContext* _qglContext { nullptr };
41 
42  private:
43  Context(const Context& other);
44  void qtCreate(QOpenGLContext* shareContext);
45 #ifdef Q_OS_WIN
46  void createWrapperContext();
47 #endif
48 
49  public:
50  static bool enableDebugLogger();
51  static void debugMessageHandler(const QOpenGLDebugMessage &debugMessage);
52  static void setupDebugLogging(QOpenGLContext* context);
53 
54  Context();
55  Context(QWindow* window);
56  void release();
57  virtual ~Context();
58 
59  QWindow* getWindow() const { return _window; }
60  void clear();
61  void setWindow(QWindow* window);
62  bool makeCurrent();
63  static void makeCurrent(QOpenGLContext* context, QSurface* surface);
64  void swapBuffers();
65  void doneCurrent();
66  virtual void create(QOpenGLContext* shareContext = nullptr);
67  QOpenGLContext* qglContext() const { return _qglContext; }
68  void moveToThread(QThread* thread);
69 
70  static size_t evalSurfaceMemoryUsage(uint32_t width, uint32_t height, uint32_t pixelSize);
71  static size_t getSwapchainMemoryUsage();
72  static void updateSwapchainMemoryUsage(size_t prevSize, size_t newSize);
73 
74  private:
75  static std::atomic<size_t> _totalSwapchainMemoryUsage;
76 
77  size_t _swapchainMemoryUsage { 0 };
78  size_t _swapchainPixelSize { 0 };
79  void updateSwapchainMemoryCounter();
80  };
81 
82  class OffscreenContext : public Context {
83  using Parent = Context;
84  public:
85  virtual ~OffscreenContext();
86  void create(QOpenGLContext* shareContext = nullptr) override;
87  };
88 
89 }
90 
91 #endif // hifi_gpu_GPUConfig_h