1
2/*
3 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
8
9#include "SkRefCnt.h"
10
11#ifndef SkWGL_DEFINED
12#define SkWGL_DEFINED
13
14/**
15 * Working with WGL extensions can be a pain. Among the reasons is that You must
16 * have a GL context to get the proc addresses, but you want to use the procs to
17 * create a context in the first place. So you have to create a dummy GL ctx to
18 * get the proc addresses.
19 *
20 * This file helps by providing SkCreateWGLInterface(). It returns a struct of
21 * function pointers that it initializes. It also has a helper function to query
22 * for WGL extensions. It handles the fact that wglGetExtensionsString is itself
23 * an extension.
24 */
25
26#if !defined(WIN32_LEAN_AND_MEAN)
27    #define WIN32_LEAN_AND_MEAN
28    #define SK_LOCAL_LEAN_AND_MEAN
29#endif
30#include <windows.h>
31#if defined(SK_LOCAL_LEAN_AND_MEAN)
32    #undef WIN32_LEAN_AND_MEAN
33    #undef SK_LOCAL_LEAN_AND_MEAN
34#endif
35
36#define SK_WGL_DRAW_TO_WINDOW                       0x2001
37#define SK_WGL_ACCELERATION                         0x2003
38#define SK_WGL_SUPPORT_OPENGL                       0x2010
39#define SK_WGL_DOUBLE_BUFFER                        0x2011
40#define SK_WGL_COLOR_BITS                           0x2014
41#define SK_WGL_ALPHA_BITS                           0x201B
42#define SK_WGL_STENCIL_BITS                         0x2023
43#define SK_WGL_FULL_ACCELERATION                    0x2027
44#define SK_WGL_SAMPLE_BUFFERS                       0x2041
45#define SK_WGL_SAMPLES                              0x2042
46#define SK_WGL_CONTEXT_MAJOR_VERSION                0x2091
47#define SK_WGL_CONTEXT_MINOR_VERSION                0x2092
48#define SK_WGL_CONTEXT_LAYER_PLANE                  0x2093
49#define SK_WGL_CONTEXT_FLAGS                        0x2094
50#define SK_WGL_CONTEXT_PROFILE_MASK                 0x9126
51#define SK_WGL_CONTEXT_DEBUG_BIT                    0x0001
52#define SK_WGL_CONTEXT_FORWARD_COMPATIBLE_BIT       0x0002
53#define SK_WGL_CONTEXT_CORE_PROFILE_BIT             0x00000001
54#define SK_WGL_CONTEXT_COMPATIBILITY_PROFILE_BIT    0x00000002
55#define SK_WGL_CONTEXT_ES2_PROFILE_BIT              0x00000004
56#define SK_ERROR_INVALID_VERSION                    0x2095
57#define SK_ERROR_INVALID_PROFILE                    0x2096
58
59class SkWGLExtensions {
60public:
61    SkWGLExtensions();
62    /**
63     * Determines if an extensions is available for a given DC.
64     * WGL_extensions_string is considered a prerequisite for all other
65     * extensions. It is necessary to check this before calling other class
66     * functions.
67     */
68    bool hasExtension(HDC dc, const char* ext) const;
69
70    const char* getExtensionsString(HDC hdc) const;
71    BOOL choosePixelFormat(HDC hdc, const int*, const FLOAT*, UINT, int*, UINT*) const;
72    BOOL getPixelFormatAttribiv(HDC, int, int, UINT, const int*, int*) const;
73    BOOL getPixelFormatAttribfv(HDC hdc, int, int, UINT, const int*, FLOAT*) const;
74    HGLRC createContextAttribs(HDC, HGLRC, const int *) const;
75
76    /**
77     * WGL doesn't have precise rules for the ordering of formats returned
78     * by wglChoosePixelFormat. This function helps choose among the set of
79     * formats returned by wglChoosePixelFormat. The rules in decreasing
80     * priority are:
81     *     * Choose formats with the smallest sample count that is >=
82     *       desiredSampleCount (or the largest sample count if all formats have
83     *       fewer samples than desiredSampleCount.)
84     *     * Choose formats with the fewest color samples when coverage sampling
85     *       is available.
86     *     * If the above rules leave multiple formats, choose the one that
87     *       appears first in the formats array parameter.
88     */
89    int selectFormat(const int formats[],
90                     int formatCount,
91                     HDC dc,
92                     int desiredSampleCount);
93private:
94    typedef const char* (WINAPI *GetExtensionsStringProc)(HDC hdc);
95    typedef BOOL (WINAPI *ChoosePixelFormatProc)(HDC hdc, const int *, const FLOAT *, UINT, int *, UINT *);
96    typedef BOOL (WINAPI *GetPixelFormatAttribivProc)(HDC, int, int, UINT, const int*, int*);
97    typedef BOOL (WINAPI *GetPixelFormatAttribfvProc)(HDC hdc, int, int, UINT, const int*, FLOAT*);
98    typedef HGLRC (WINAPI *CreateContextAttribsProc)(HDC hDC, HGLRC, const int *);
99
100    GetExtensionsStringProc fGetExtensionsString;
101    ChoosePixelFormatProc fChoosePixelFormat;
102    GetPixelFormatAttribfvProc fGetPixelFormatAttribfv;
103    GetPixelFormatAttribivProc fGetPixelFormatAttribiv;
104    CreateContextAttribsProc fCreateContextAttribs;
105};
106
107/**
108 * Helper to create an OpenGL context for a DC using WGL. Configs with a sample count >= to
109 * msaaSampleCount are preferred but if none is available then a context with a lower sample count
110 * (including non-MSAA) will be created. If preferCoreProfile is true but a core profile cannot be
111 * created then a compatible profile context will be created.
112 */
113HGLRC SkCreateWGLContext(HDC dc, int msaaSampleCount, bool preferCoreProfile);
114
115#endif
116