blob: 315be24cd9c15846676cb9b9fafdb4dc5851b229 [file] [log] [blame]
bsalomon@google.combd7c6412011-12-01 16:34:28 +00001/*
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
8#include "SkRefCnt.h"
9
10#ifndef SkWGL_DEFINED
11#define SkWGL_DEFINED
12
halcanary4dbbd042016-06-07 17:21:10 -070013#include "SkLeanWindows.h"
14
bsalomon@google.combd7c6412011-12-01 16:34:28 +000015/**
16 * Working with WGL extensions can be a pain. Among the reasons is that You must
17 * have a GL context to get the proc addresses, but you want to use the procs to
18 * create a context in the first place. So you have to create a dummy GL ctx to
rmistryc3cf5a52014-08-26 10:43:14 -070019 * get the proc addresses.
bsalomon@google.combd7c6412011-12-01 16:34:28 +000020 *
21 * This file helps by providing SkCreateWGLInterface(). It returns a struct of
22 * function pointers that it initializes. It also has a helper function to query
23 * for WGL extensions. It handles the fact that wglGetExtensionsString is itself
24 * an extension.
25 */
26
bsalomon@google.com8a189b02012-04-17 12:43:00 +000027#define SK_WGL_DRAW_TO_WINDOW 0x2001
28#define SK_WGL_ACCELERATION 0x2003
29#define SK_WGL_SUPPORT_OPENGL 0x2010
30#define SK_WGL_DOUBLE_BUFFER 0x2011
31#define SK_WGL_COLOR_BITS 0x2014
brianosman2d1ee792016-05-05 12:24:31 -070032#define SK_WGL_RED_BITS 0x2015
33#define SK_WGL_GREEN_BITS 0x2017
34#define SK_WGL_BLUE_BITS 0x2019
bsalomon@google.com8a189b02012-04-17 12:43:00 +000035#define SK_WGL_ALPHA_BITS 0x201B
36#define SK_WGL_STENCIL_BITS 0x2023
37#define SK_WGL_FULL_ACCELERATION 0x2027
38#define SK_WGL_SAMPLE_BUFFERS 0x2041
39#define SK_WGL_SAMPLES 0x2042
bsalomon@google.com8a189b02012-04-17 12:43:00 +000040#define SK_WGL_CONTEXT_MAJOR_VERSION 0x2091
41#define SK_WGL_CONTEXT_MINOR_VERSION 0x2092
42#define SK_WGL_CONTEXT_LAYER_PLANE 0x2093
43#define SK_WGL_CONTEXT_FLAGS 0x2094
44#define SK_WGL_CONTEXT_PROFILE_MASK 0x9126
45#define SK_WGL_CONTEXT_DEBUG_BIT 0x0001
46#define SK_WGL_CONTEXT_FORWARD_COMPATIBLE_BIT 0x0002
47#define SK_WGL_CONTEXT_CORE_PROFILE_BIT 0x00000001
48#define SK_WGL_CONTEXT_COMPATIBILITY_PROFILE_BIT 0x00000002
49#define SK_WGL_CONTEXT_ES2_PROFILE_BIT 0x00000004
50#define SK_ERROR_INVALID_VERSION 0x2095
51#define SK_ERROR_INVALID_PROFILE 0x2096
bsalomon@google.combd7c6412011-12-01 16:34:28 +000052
bsalomon9245b7e2014-07-01 07:20:11 -070053DECLARE_HANDLE(HPBUFFER);
54
bsalomon@google.combd7c6412011-12-01 16:34:28 +000055class SkWGLExtensions {
56public:
57 SkWGLExtensions();
58 /**
59 * Determines if an extensions is available for a given DC.
bsalomon@google.comeedef252011-12-01 16:50:24 +000060 * WGL_extensions_string is considered a prerequisite for all other
bsalomon@google.combd7c6412011-12-01 16:34:28 +000061 * extensions. It is necessary to check this before calling other class
62 * functions.
63 */
64 bool hasExtension(HDC dc, const char* ext) const;
65
66 const char* getExtensionsString(HDC hdc) const;
67 BOOL choosePixelFormat(HDC hdc, const int*, const FLOAT*, UINT, int*, UINT*) const;
68 BOOL getPixelFormatAttribiv(HDC, int, int, UINT, const int*, int*) const;
69 BOOL getPixelFormatAttribfv(HDC hdc, int, int, UINT, const int*, FLOAT*) const;
70 HGLRC createContextAttribs(HDC, HGLRC, const int *) const;
71
bsalomon9245b7e2014-07-01 07:20:11 -070072 BOOL swapInterval(int interval) const;
73
74 HPBUFFER createPbuffer(HDC, int , int, int, const int*) const;
75 HDC getPbufferDC(HPBUFFER) const;
76 int releasePbufferDC(HPBUFFER, HDC) const;
77 BOOL destroyPbuffer(HPBUFFER) const;
78
bsalomon@google.com8a189b02012-04-17 12:43:00 +000079 /**
80 * WGL doesn't have precise rules for the ordering of formats returned
81 * by wglChoosePixelFormat. This function helps choose among the set of
82 * formats returned by wglChoosePixelFormat. The rules in decreasing
83 * priority are:
84 * * Choose formats with the smallest sample count that is >=
85 * desiredSampleCount (or the largest sample count if all formats have
86 * fewer samples than desiredSampleCount.)
87 * * Choose formats with the fewest color samples when coverage sampling
88 * is available.
89 * * If the above rules leave multiple formats, choose the one that
90 * appears first in the formats array parameter.
91 */
92 int selectFormat(const int formats[],
93 int formatCount,
94 HDC dc,
bsalomon9245b7e2014-07-01 07:20:11 -070095 int desiredSampleCount) const;
bsalomon@google.combd7c6412011-12-01 16:34:28 +000096private:
bsalomon9245b7e2014-07-01 07:20:11 -070097 typedef const char* (WINAPI *GetExtensionsStringProc)(HDC);
98 typedef BOOL (WINAPI *ChoosePixelFormatProc)(HDC, const int *, const FLOAT *, UINT, int *, UINT *);
bsalomon@google.combd7c6412011-12-01 16:34:28 +000099 typedef BOOL (WINAPI *GetPixelFormatAttribivProc)(HDC, int, int, UINT, const int*, int*);
bsalomon9245b7e2014-07-01 07:20:11 -0700100 typedef BOOL (WINAPI *GetPixelFormatAttribfvProc)(HDC, int, int, UINT, const int*, FLOAT*);
101 typedef HGLRC (WINAPI *CreateContextAttribsProc)(HDC, HGLRC, const int *);
102 typedef BOOL (WINAPI* SwapIntervalProc)(int);
103 typedef HPBUFFER (WINAPI* CreatePbufferProc)(HDC, int , int, int, const int*);
104 typedef HDC (WINAPI* GetPbufferDCProc)(HPBUFFER);
105 typedef int (WINAPI* ReleasePbufferDCProc)(HPBUFFER, HDC);
106 typedef BOOL (WINAPI* DestroyPbufferProc)(HPBUFFER);
bsalomon@google.combd7c6412011-12-01 16:34:28 +0000107
Brian Osman6269f712017-08-16 15:14:59 -0400108 static GetExtensionsStringProc fGetExtensionsString;
109 static ChoosePixelFormatProc fChoosePixelFormat;
110 static GetPixelFormatAttribfvProc fGetPixelFormatAttribfv;
111 static GetPixelFormatAttribivProc fGetPixelFormatAttribiv;
112 static CreateContextAttribsProc fCreateContextAttribs;
113 static SwapIntervalProc fSwapInterval;
114 static CreatePbufferProc fCreatePbuffer;
115 static GetPbufferDCProc fGetPbufferDC;
116 static ReleasePbufferDCProc fReleasePbufferDC;
117 static DestroyPbufferProc fDestroyPbuffer;
bsalomon@google.combd7c6412011-12-01 16:34:28 +0000118};
119
kkinnunen80549fc2014-06-30 06:36:31 -0700120enum SkWGLContextRequest {
121 /** Requests to create core profile context if possible, otherwise
122 compatibility profile. */
123 kGLPreferCoreProfile_SkWGLContextRequest,
124 /** Requests to create compatibility profile context if possible, otherwise
125 core profile. */
126 kGLPreferCompatibilityProfile_SkWGLContextRequest,
127 /** Requests to create GL ES profile context. */
128 kGLES_SkWGLContextRequest
129};
bsalomon@google.comb7f20f22013-03-05 19:13:09 +0000130/**
131 * Helper to create an OpenGL context for a DC using WGL. Configs with a sample count >= to
132 * msaaSampleCount are preferred but if none is available then a context with a lower sample count
133 * (including non-MSAA) will be created. If preferCoreProfile is true but a core profile cannot be
134 * created then a compatible profile context will be created.
135 */
Brian Osman60c774d2017-02-21 16:58:08 -0500136HGLRC SkCreateWGLContext(HDC dc, int msaaSampleCount, bool deepColor, SkWGLContextRequest context,
137 HGLRC shareContext = nullptr);
bsalomon@google.comb7f20f22013-03-05 19:13:09 +0000138
bsalomon9245b7e2014-07-01 07:20:11 -0700139/**
140 * Helper class for creating a pbuffer context and deleting all the handles when finished. This
141 * requires that a device context has been created. However, the pbuffer gets its own device
142 * context. The original device context can be released once the pbuffer context is created.
143 */
144class SkWGLPbufferContext : public SkRefCnt {
145public:
146 static SkWGLPbufferContext* Create(HDC parentDC, int msaaSampleCount,
Brian Osman60c774d2017-02-21 16:58:08 -0500147 SkWGLContextRequest contextType, HGLRC shareContext);
bsalomon9245b7e2014-07-01 07:20:11 -0700148
149 virtual ~SkWGLPbufferContext();
150
151 HDC getDC() const { return fDC; }
152 HGLRC getGLRC() const { return fGLRC; }
153
154private:
155 SkWGLPbufferContext(HPBUFFER pbuffer, HDC dc, HGLRC glrc);
156
157 HPBUFFER fPbuffer;
158 HDC fDC;
159 HGLRC fGLRC;
160 SkWGLExtensions fExtensions;
161};
162
bsalomon@google.combd7c6412011-12-01 16:34:28 +0000163#endif