blob: ebd95e7a8308f1a5a1eb8275ecd7b10e77a5872a [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
brianosman2d1ee792016-05-05 12:24:31 -070040#define SK_WGL_RED_BITS 0x2015
41#define SK_WGL_GREEN_BITS 0x2017
42#define SK_WGL_BLUE_BITS 0x2019
bsalomon@google.com8a189b02012-04-17 12:43:00 +000043#define SK_WGL_ALPHA_BITS 0x201B
44#define SK_WGL_STENCIL_BITS 0x2023
45#define SK_WGL_FULL_ACCELERATION 0x2027
46#define SK_WGL_SAMPLE_BUFFERS 0x2041
47#define SK_WGL_SAMPLES 0x2042
bsalomon@google.com8a189b02012-04-17 12:43:00 +000048#define SK_WGL_CONTEXT_MAJOR_VERSION 0x2091
49#define SK_WGL_CONTEXT_MINOR_VERSION 0x2092
50#define SK_WGL_CONTEXT_LAYER_PLANE 0x2093
51#define SK_WGL_CONTEXT_FLAGS 0x2094
52#define SK_WGL_CONTEXT_PROFILE_MASK 0x9126
53#define SK_WGL_CONTEXT_DEBUG_BIT 0x0001
54#define SK_WGL_CONTEXT_FORWARD_COMPATIBLE_BIT 0x0002
55#define SK_WGL_CONTEXT_CORE_PROFILE_BIT 0x00000001
56#define SK_WGL_CONTEXT_COMPATIBILITY_PROFILE_BIT 0x00000002
57#define SK_WGL_CONTEXT_ES2_PROFILE_BIT 0x00000004
58#define SK_ERROR_INVALID_VERSION 0x2095
59#define SK_ERROR_INVALID_PROFILE 0x2096
bsalomon@google.combd7c6412011-12-01 16:34:28 +000060
bsalomon9245b7e2014-07-01 07:20:11 -070061DECLARE_HANDLE(HPBUFFER);
62
bsalomon@google.combd7c6412011-12-01 16:34:28 +000063class SkWGLExtensions {
64public:
65 SkWGLExtensions();
66 /**
67 * Determines if an extensions is available for a given DC.
bsalomon@google.comeedef252011-12-01 16:50:24 +000068 * WGL_extensions_string is considered a prerequisite for all other
bsalomon@google.combd7c6412011-12-01 16:34:28 +000069 * extensions. It is necessary to check this before calling other class
70 * functions.
71 */
72 bool hasExtension(HDC dc, const char* ext) const;
73
74 const char* getExtensionsString(HDC hdc) const;
75 BOOL choosePixelFormat(HDC hdc, const int*, const FLOAT*, UINT, int*, UINT*) const;
76 BOOL getPixelFormatAttribiv(HDC, int, int, UINT, const int*, int*) const;
77 BOOL getPixelFormatAttribfv(HDC hdc, int, int, UINT, const int*, FLOAT*) const;
78 HGLRC createContextAttribs(HDC, HGLRC, const int *) const;
79
bsalomon9245b7e2014-07-01 07:20:11 -070080 BOOL swapInterval(int interval) const;
81
82 HPBUFFER createPbuffer(HDC, int , int, int, const int*) const;
83 HDC getPbufferDC(HPBUFFER) const;
84 int releasePbufferDC(HPBUFFER, HDC) const;
85 BOOL destroyPbuffer(HPBUFFER) const;
86
bsalomon@google.com8a189b02012-04-17 12:43:00 +000087 /**
88 * WGL doesn't have precise rules for the ordering of formats returned
89 * by wglChoosePixelFormat. This function helps choose among the set of
90 * formats returned by wglChoosePixelFormat. The rules in decreasing
91 * priority are:
92 * * Choose formats with the smallest sample count that is >=
93 * desiredSampleCount (or the largest sample count if all formats have
94 * fewer samples than desiredSampleCount.)
95 * * Choose formats with the fewest color samples when coverage sampling
96 * is available.
97 * * If the above rules leave multiple formats, choose the one that
98 * appears first in the formats array parameter.
99 */
100 int selectFormat(const int formats[],
101 int formatCount,
102 HDC dc,
bsalomon9245b7e2014-07-01 07:20:11 -0700103 int desiredSampleCount) const;
bsalomon@google.combd7c6412011-12-01 16:34:28 +0000104private:
bsalomon9245b7e2014-07-01 07:20:11 -0700105 typedef const char* (WINAPI *GetExtensionsStringProc)(HDC);
106 typedef BOOL (WINAPI *ChoosePixelFormatProc)(HDC, const int *, const FLOAT *, UINT, int *, UINT *);
bsalomon@google.combd7c6412011-12-01 16:34:28 +0000107 typedef BOOL (WINAPI *GetPixelFormatAttribivProc)(HDC, int, int, UINT, const int*, int*);
bsalomon9245b7e2014-07-01 07:20:11 -0700108 typedef BOOL (WINAPI *GetPixelFormatAttribfvProc)(HDC, int, int, UINT, const int*, FLOAT*);
109 typedef HGLRC (WINAPI *CreateContextAttribsProc)(HDC, HGLRC, const int *);
110 typedef BOOL (WINAPI* SwapIntervalProc)(int);
111 typedef HPBUFFER (WINAPI* CreatePbufferProc)(HDC, int , int, int, const int*);
112 typedef HDC (WINAPI* GetPbufferDCProc)(HPBUFFER);
113 typedef int (WINAPI* ReleasePbufferDCProc)(HPBUFFER, HDC);
114 typedef BOOL (WINAPI* DestroyPbufferProc)(HPBUFFER);
bsalomon@google.combd7c6412011-12-01 16:34:28 +0000115
116 GetExtensionsStringProc fGetExtensionsString;
117 ChoosePixelFormatProc fChoosePixelFormat;
118 GetPixelFormatAttribfvProc fGetPixelFormatAttribfv;
119 GetPixelFormatAttribivProc fGetPixelFormatAttribiv;
120 CreateContextAttribsProc fCreateContextAttribs;
bsalomon9245b7e2014-07-01 07:20:11 -0700121 SwapIntervalProc fSwapInterval;
122 CreatePbufferProc fCreatePbuffer;
123 GetPbufferDCProc fGetPbufferDC;
124 ReleasePbufferDCProc fReleasePbufferDC;
125 DestroyPbufferProc fDestroyPbuffer;
bsalomon@google.combd7c6412011-12-01 16:34:28 +0000126};
127
kkinnunen80549fc2014-06-30 06:36:31 -0700128enum SkWGLContextRequest {
129 /** Requests to create core profile context if possible, otherwise
130 compatibility profile. */
131 kGLPreferCoreProfile_SkWGLContextRequest,
132 /** Requests to create compatibility profile context if possible, otherwise
133 core profile. */
134 kGLPreferCompatibilityProfile_SkWGLContextRequest,
135 /** Requests to create GL ES profile context. */
136 kGLES_SkWGLContextRequest
137};
bsalomon@google.comb7f20f22013-03-05 19:13:09 +0000138/**
139 * Helper to create an OpenGL context for a DC using WGL. Configs with a sample count >= to
140 * msaaSampleCount are preferred but if none is available then a context with a lower sample count
141 * (including non-MSAA) will be created. If preferCoreProfile is true but a core profile cannot be
142 * created then a compatible profile context will be created.
143 */
brianosman2d1ee792016-05-05 12:24:31 -0700144HGLRC SkCreateWGLContext(HDC dc, int msaaSampleCount, bool deepColor, SkWGLContextRequest context);
bsalomon@google.comb7f20f22013-03-05 19:13:09 +0000145
bsalomon9245b7e2014-07-01 07:20:11 -0700146/**
147 * Helper class for creating a pbuffer context and deleting all the handles when finished. This
148 * requires that a device context has been created. However, the pbuffer gets its own device
149 * context. The original device context can be released once the pbuffer context is created.
150 */
151class SkWGLPbufferContext : public SkRefCnt {
152public:
153 static SkWGLPbufferContext* Create(HDC parentDC, int msaaSampleCount,
154 SkWGLContextRequest contextType);
155
156 virtual ~SkWGLPbufferContext();
157
158 HDC getDC() const { return fDC; }
159 HGLRC getGLRC() const { return fGLRC; }
160
161private:
162 SkWGLPbufferContext(HPBUFFER pbuffer, HDC dc, HGLRC glrc);
163
164 HPBUFFER fPbuffer;
165 HDC fDC;
166 HGLRC fGLRC;
167 SkWGLExtensions fExtensions;
168};
169
bsalomon@google.combd7c6412011-12-01 16:34:28 +0000170#endif