SkWGL.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*
  2. * Copyright 2011 Google Inc.
  3. *
  4. * Use of this source code is governed by a BSD-style license that can be
  5. * found in the LICENSE file.
  6. */
  7. #include "include/core/SkRefCnt.h"
  8. #ifndef SkWGL_DEFINED
  9. #define SkWGL_DEFINED
  10. #include "src/core/SkLeanWindows.h"
  11. /**
  12. * Working with WGL extensions can be a pain. Among the reasons is that You must
  13. * have a GL context to get the proc addresses, but you want to use the procs to
  14. * create a context in the first place. So you have to create a dummy GL ctx to
  15. * get the proc addresses.
  16. *
  17. * This file helps by providing SkCreateWGLInterface(). It returns a struct of
  18. * function pointers that it initializes. It also has a helper function to query
  19. * for WGL extensions. It handles the fact that wglGetExtensionsString is itself
  20. * an extension.
  21. */
  22. #define SK_WGL_DRAW_TO_WINDOW 0x2001
  23. #define SK_WGL_ACCELERATION 0x2003
  24. #define SK_WGL_SUPPORT_OPENGL 0x2010
  25. #define SK_WGL_DOUBLE_BUFFER 0x2011
  26. #define SK_WGL_COLOR_BITS 0x2014
  27. #define SK_WGL_RED_BITS 0x2015
  28. #define SK_WGL_GREEN_BITS 0x2017
  29. #define SK_WGL_BLUE_BITS 0x2019
  30. #define SK_WGL_ALPHA_BITS 0x201B
  31. #define SK_WGL_STENCIL_BITS 0x2023
  32. #define SK_WGL_FULL_ACCELERATION 0x2027
  33. #define SK_WGL_SAMPLE_BUFFERS 0x2041
  34. #define SK_WGL_SAMPLES 0x2042
  35. #define SK_WGL_CONTEXT_MAJOR_VERSION 0x2091
  36. #define SK_WGL_CONTEXT_MINOR_VERSION 0x2092
  37. #define SK_WGL_CONTEXT_LAYER_PLANE 0x2093
  38. #define SK_WGL_CONTEXT_FLAGS 0x2094
  39. #define SK_WGL_CONTEXT_PROFILE_MASK 0x9126
  40. #define SK_WGL_CONTEXT_DEBUG_BIT 0x0001
  41. #define SK_WGL_CONTEXT_FORWARD_COMPATIBLE_BIT 0x0002
  42. #define SK_WGL_CONTEXT_CORE_PROFILE_BIT 0x00000001
  43. #define SK_WGL_CONTEXT_COMPATIBILITY_PROFILE_BIT 0x00000002
  44. #define SK_WGL_CONTEXT_ES2_PROFILE_BIT 0x00000004
  45. #define SK_ERROR_INVALID_VERSION 0x2095
  46. #define SK_ERROR_INVALID_PROFILE 0x2096
  47. DECLARE_HANDLE(HPBUFFER);
  48. class SkWGLExtensions {
  49. public:
  50. SkWGLExtensions();
  51. /**
  52. * Determines if an extensions is available for a given DC.
  53. * WGL_extensions_string is considered a prerequisite for all other
  54. * extensions. It is necessary to check this before calling other class
  55. * functions.
  56. */
  57. bool hasExtension(HDC dc, const char* ext) const;
  58. const char* getExtensionsString(HDC hdc) const;
  59. BOOL choosePixelFormat(HDC hdc, const int*, const FLOAT*, UINT, int*, UINT*) const;
  60. BOOL getPixelFormatAttribiv(HDC, int, int, UINT, const int*, int*) const;
  61. BOOL getPixelFormatAttribfv(HDC hdc, int, int, UINT, const int*, FLOAT*) const;
  62. HGLRC createContextAttribs(HDC, HGLRC, const int *) const;
  63. BOOL swapInterval(int interval) const;
  64. HPBUFFER createPbuffer(HDC, int , int, int, const int*) const;
  65. HDC getPbufferDC(HPBUFFER) const;
  66. int releasePbufferDC(HPBUFFER, HDC) const;
  67. BOOL destroyPbuffer(HPBUFFER) const;
  68. /**
  69. * WGL doesn't have precise rules for the ordering of formats returned
  70. * by wglChoosePixelFormat. This function helps choose among the set of
  71. * formats returned by wglChoosePixelFormat. The rules in decreasing
  72. * priority are:
  73. * * Choose formats with the smallest sample count that is >=
  74. * desiredSampleCount (or the largest sample count if all formats have
  75. * fewer samples than desiredSampleCount.) If desiredSampleCount is 1 then
  76. * all msaa formats are excluded from consideration.
  77. * * Choose formats with the fewest color samples when coverage sampling
  78. * is available.
  79. * * If the above rules leave multiple formats, choose the one that
  80. * appears first in the formats array parameter.
  81. */
  82. int selectFormat(const int formats[],
  83. int formatCount,
  84. HDC dc,
  85. int desiredSampleCount) const;
  86. private:
  87. typedef const char* (WINAPI *GetExtensionsStringProc)(HDC);
  88. typedef BOOL (WINAPI *ChoosePixelFormatProc)(HDC, const int *, const FLOAT *, UINT, int *, UINT *);
  89. typedef BOOL (WINAPI *GetPixelFormatAttribivProc)(HDC, int, int, UINT, const int*, int*);
  90. typedef BOOL (WINAPI *GetPixelFormatAttribfvProc)(HDC, int, int, UINT, const int*, FLOAT*);
  91. typedef HGLRC (WINAPI *CreateContextAttribsProc)(HDC, HGLRC, const int *);
  92. typedef BOOL (WINAPI* SwapIntervalProc)(int);
  93. typedef HPBUFFER (WINAPI* CreatePbufferProc)(HDC, int , int, int, const int*);
  94. typedef HDC (WINAPI* GetPbufferDCProc)(HPBUFFER);
  95. typedef int (WINAPI* ReleasePbufferDCProc)(HPBUFFER, HDC);
  96. typedef BOOL (WINAPI* DestroyPbufferProc)(HPBUFFER);
  97. static GetExtensionsStringProc fGetExtensionsString;
  98. static ChoosePixelFormatProc fChoosePixelFormat;
  99. static GetPixelFormatAttribfvProc fGetPixelFormatAttribfv;
  100. static GetPixelFormatAttribivProc fGetPixelFormatAttribiv;
  101. static CreateContextAttribsProc fCreateContextAttribs;
  102. static SwapIntervalProc fSwapInterval;
  103. static CreatePbufferProc fCreatePbuffer;
  104. static GetPbufferDCProc fGetPbufferDC;
  105. static ReleasePbufferDCProc fReleasePbufferDC;
  106. static DestroyPbufferProc fDestroyPbuffer;
  107. };
  108. enum SkWGLContextRequest {
  109. /** Requests to create core profile context if possible, otherwise
  110. compatibility profile. */
  111. kGLPreferCoreProfile_SkWGLContextRequest,
  112. /** Requests to create compatibility profile context if possible, otherwise
  113. core profile. */
  114. kGLPreferCompatibilityProfile_SkWGLContextRequest,
  115. /** Requests to create GL ES profile context. */
  116. kGLES_SkWGLContextRequest
  117. };
  118. /**
  119. * Helper to create an OpenGL context for a DC using WGL. Configs with a sample count >= to
  120. * msaaSampleCount are preferred but if none is available then a context with a lower sample count
  121. * (including non-MSAA) will be created. If msaaSampleCount is 1 then this will fail if a non-msaa
  122. * context cannot be created. If preferCoreProfile is true but a core profile cannot be created
  123. * then a compatible profile context will be created.
  124. */
  125. HGLRC SkCreateWGLContext(HDC dc, int msaaSampleCount, bool deepColor, SkWGLContextRequest context,
  126. HGLRC shareContext = nullptr);
  127. /**
  128. * Helper class for creating a pbuffer context and deleting all the handles when finished. This
  129. * requires that a device context has been created. However, the pbuffer gets its own device
  130. * context. The original device context can be released once the pbuffer context is created.
  131. */
  132. class SkWGLPbufferContext : public SkRefCnt {
  133. public:
  134. static sk_sp<SkWGLPbufferContext> Create(HDC parentDC, SkWGLContextRequest contextType,
  135. HGLRC shareContext);
  136. virtual ~SkWGLPbufferContext();
  137. HDC getDC() const { return fDC; }
  138. HGLRC getGLRC() const { return fGLRC; }
  139. private:
  140. SkWGLPbufferContext(HPBUFFER pbuffer, HDC dc, HGLRC glrc);
  141. HPBUFFER fPbuffer;
  142. HDC fDC;
  143. HGLRC fGLRC;
  144. SkWGLExtensions fExtensions;
  145. };
  146. #endif