blob: 5322d916b5ac2cf2141c267687b267c02e193434 [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
13/**
14 * Working with WGL extensions can be a pain. Among the reasons is that You must
15 * have a GL context to get the proc addresses, but you want to use the procs to
16 * create a context in the first place. So you have to create a dummy GL ctx to
rmistryc3cf5a52014-08-26 10:43:14 -070017 * get the proc addresses.
bsalomon@google.combd7c6412011-12-01 16:34:28 +000018 *
19 * This file helps by providing SkCreateWGLInterface(). It returns a struct of
20 * function pointers that it initializes. It also has a helper function to query
21 * for WGL extensions. It handles the fact that wglGetExtensionsString is itself
22 * an extension.
23 */
24
25#if !defined(WIN32_LEAN_AND_MEAN)
26 #define WIN32_LEAN_AND_MEAN
27 #define SK_LOCAL_LEAN_AND_MEAN
28#endif
bungeman@google.com0d9e3da2013-12-03 15:23:37 +000029#include <windows.h>
bsalomon@google.combd7c6412011-12-01 16:34:28 +000030#if defined(SK_LOCAL_LEAN_AND_MEAN)
31 #undef WIN32_LEAN_AND_MEAN
32 #undef SK_LOCAL_LEAN_AND_MEAN
33#endif
34
bsalomon@google.com8a189b02012-04-17 12:43:00 +000035#define SK_WGL_DRAW_TO_WINDOW 0x2001
36#define SK_WGL_ACCELERATION 0x2003
37#define SK_WGL_SUPPORT_OPENGL 0x2010
38#define SK_WGL_DOUBLE_BUFFER 0x2011
39#define SK_WGL_COLOR_BITS 0x2014
40#define SK_WGL_ALPHA_BITS 0x201B
41#define SK_WGL_STENCIL_BITS 0x2023
42#define SK_WGL_FULL_ACCELERATION 0x2027
43#define SK_WGL_SAMPLE_BUFFERS 0x2041
44#define SK_WGL_SAMPLES 0x2042
bsalomon@google.com8a189b02012-04-17 12:43:00 +000045#define SK_WGL_CONTEXT_MAJOR_VERSION 0x2091
46#define SK_WGL_CONTEXT_MINOR_VERSION 0x2092
47#define SK_WGL_CONTEXT_LAYER_PLANE 0x2093
48#define SK_WGL_CONTEXT_FLAGS 0x2094
49#define SK_WGL_CONTEXT_PROFILE_MASK 0x9126
50#define SK_WGL_CONTEXT_DEBUG_BIT 0x0001
51#define SK_WGL_CONTEXT_FORWARD_COMPATIBLE_BIT 0x0002
52#define SK_WGL_CONTEXT_CORE_PROFILE_BIT 0x00000001
53#define SK_WGL_CONTEXT_COMPATIBILITY_PROFILE_BIT 0x00000002
54#define SK_WGL_CONTEXT_ES2_PROFILE_BIT 0x00000004
55#define SK_ERROR_INVALID_VERSION 0x2095
56#define SK_ERROR_INVALID_PROFILE 0x2096
bsalomon@google.combd7c6412011-12-01 16:34:28 +000057
bsalomon9245b7e2014-07-01 07:20:11 -070058DECLARE_HANDLE(HPBUFFER);
59
bsalomon@google.combd7c6412011-12-01 16:34:28 +000060class SkWGLExtensions {
61public:
62 SkWGLExtensions();
63 /**
64 * Determines if an extensions is available for a given DC.
bsalomon@google.comeedef252011-12-01 16:50:24 +000065 * WGL_extensions_string is considered a prerequisite for all other
bsalomon@google.combd7c6412011-12-01 16:34:28 +000066 * extensions. It is necessary to check this before calling other class
67 * functions.
68 */
69 bool hasExtension(HDC dc, const char* ext) const;
70
71 const char* getExtensionsString(HDC hdc) const;
72 BOOL choosePixelFormat(HDC hdc, const int*, const FLOAT*, UINT, int*, UINT*) const;
73 BOOL getPixelFormatAttribiv(HDC, int, int, UINT, const int*, int*) const;
74 BOOL getPixelFormatAttribfv(HDC hdc, int, int, UINT, const int*, FLOAT*) const;
75 HGLRC createContextAttribs(HDC, HGLRC, const int *) const;
76
bsalomon9245b7e2014-07-01 07:20:11 -070077 BOOL swapInterval(int interval) const;
78
79 HPBUFFER createPbuffer(HDC, int , int, int, const int*) const;
80 HDC getPbufferDC(HPBUFFER) const;
81 int releasePbufferDC(HPBUFFER, HDC) const;
82 BOOL destroyPbuffer(HPBUFFER) const;
83
bsalomon@google.com8a189b02012-04-17 12:43:00 +000084 /**
85 * WGL doesn't have precise rules for the ordering of formats returned
86 * by wglChoosePixelFormat. This function helps choose among the set of
87 * formats returned by wglChoosePixelFormat. The rules in decreasing
88 * priority are:
89 * * Choose formats with the smallest sample count that is >=
90 * desiredSampleCount (or the largest sample count if all formats have
91 * fewer samples than desiredSampleCount.)
92 * * Choose formats with the fewest color samples when coverage sampling
93 * is available.
94 * * If the above rules leave multiple formats, choose the one that
95 * appears first in the formats array parameter.
96 */
97 int selectFormat(const int formats[],
98 int formatCount,
99 HDC dc,
bsalomon9245b7e2014-07-01 07:20:11 -0700100 int desiredSampleCount) const;
bsalomon@google.combd7c6412011-12-01 16:34:28 +0000101private:
bsalomon9245b7e2014-07-01 07:20:11 -0700102 typedef const char* (WINAPI *GetExtensionsStringProc)(HDC);
103 typedef BOOL (WINAPI *ChoosePixelFormatProc)(HDC, const int *, const FLOAT *, UINT, int *, UINT *);
bsalomon@google.combd7c6412011-12-01 16:34:28 +0000104 typedef BOOL (WINAPI *GetPixelFormatAttribivProc)(HDC, int, int, UINT, const int*, int*);
bsalomon9245b7e2014-07-01 07:20:11 -0700105 typedef BOOL (WINAPI *GetPixelFormatAttribfvProc)(HDC, int, int, UINT, const int*, FLOAT*);
106 typedef HGLRC (WINAPI *CreateContextAttribsProc)(HDC, HGLRC, const int *);
107 typedef BOOL (WINAPI* SwapIntervalProc)(int);
108 typedef HPBUFFER (WINAPI* CreatePbufferProc)(HDC, int , int, int, const int*);
109 typedef HDC (WINAPI* GetPbufferDCProc)(HPBUFFER);
110 typedef int (WINAPI* ReleasePbufferDCProc)(HPBUFFER, HDC);
111 typedef BOOL (WINAPI* DestroyPbufferProc)(HPBUFFER);
bsalomon@google.combd7c6412011-12-01 16:34:28 +0000112
113 GetExtensionsStringProc fGetExtensionsString;
114 ChoosePixelFormatProc fChoosePixelFormat;
115 GetPixelFormatAttribfvProc fGetPixelFormatAttribfv;
116 GetPixelFormatAttribivProc fGetPixelFormatAttribiv;
117 CreateContextAttribsProc fCreateContextAttribs;
bsalomon9245b7e2014-07-01 07:20:11 -0700118 SwapIntervalProc fSwapInterval;
119 CreatePbufferProc fCreatePbuffer;
120 GetPbufferDCProc fGetPbufferDC;
121 ReleasePbufferDCProc fReleasePbufferDC;
122 DestroyPbufferProc fDestroyPbuffer;
bsalomon@google.combd7c6412011-12-01 16:34:28 +0000123};
124
kkinnunen80549fc2014-06-30 06:36:31 -0700125enum SkWGLContextRequest {
126 /** Requests to create core profile context if possible, otherwise
127 compatibility profile. */
128 kGLPreferCoreProfile_SkWGLContextRequest,
129 /** Requests to create compatibility profile context if possible, otherwise
130 core profile. */
131 kGLPreferCompatibilityProfile_SkWGLContextRequest,
132 /** Requests to create GL ES profile context. */
133 kGLES_SkWGLContextRequest
134};
bsalomon@google.comb7f20f22013-03-05 19:13:09 +0000135/**
136 * Helper to create an OpenGL context for a DC using WGL. Configs with a sample count >= to
137 * msaaSampleCount are preferred but if none is available then a context with a lower sample count
138 * (including non-MSAA) will be created. If preferCoreProfile is true but a core profile cannot be
139 * created then a compatible profile context will be created.
140 */
kkinnunen80549fc2014-06-30 06:36:31 -0700141HGLRC SkCreateWGLContext(HDC dc, int msaaSampleCount, SkWGLContextRequest context);
bsalomon@google.comb7f20f22013-03-05 19:13:09 +0000142
bsalomon9245b7e2014-07-01 07:20:11 -0700143/**
144 * Helper class for creating a pbuffer context and deleting all the handles when finished. This
145 * requires that a device context has been created. However, the pbuffer gets its own device
146 * context. The original device context can be released once the pbuffer context is created.
147 */
148class SkWGLPbufferContext : public SkRefCnt {
149public:
150 static SkWGLPbufferContext* Create(HDC parentDC, int msaaSampleCount,
151 SkWGLContextRequest contextType);
152
153 virtual ~SkWGLPbufferContext();
154
155 HDC getDC() const { return fDC; }
156 HGLRC getGLRC() const { return fGLRC; }
157
158private:
159 SkWGLPbufferContext(HPBUFFER pbuffer, HDC dc, HGLRC glrc);
160
161 HPBUFFER fPbuffer;
162 HDC fDC;
163 HGLRC fGLRC;
164 SkWGLExtensions fExtensions;
165};
166
bsalomon@google.combd7c6412011-12-01 16:34:28 +0000167#endif