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 59DECLARE_HANDLE(HPBUFFER); 60 61class SkWGLExtensions { 62public: 63 SkWGLExtensions(); 64 /** 65 * Determines if an extensions is available for a given DC. 66 * WGL_extensions_string is considered a prerequisite for all other 67 * extensions. It is necessary to check this before calling other class 68 * functions. 69 */ 70 bool hasExtension(HDC dc, const char* ext) const; 71 72 const char* getExtensionsString(HDC hdc) const; 73 BOOL choosePixelFormat(HDC hdc, const int*, const FLOAT*, UINT, int*, UINT*) const; 74 BOOL getPixelFormatAttribiv(HDC, int, int, UINT, const int*, int*) const; 75 BOOL getPixelFormatAttribfv(HDC hdc, int, int, UINT, const int*, FLOAT*) const; 76 HGLRC createContextAttribs(HDC, HGLRC, const int *) const; 77 78 BOOL swapInterval(int interval) const; 79 80 HPBUFFER createPbuffer(HDC, int , int, int, const int*) const; 81 HDC getPbufferDC(HPBUFFER) const; 82 int releasePbufferDC(HPBUFFER, HDC) const; 83 BOOL destroyPbuffer(HPBUFFER) const; 84 85 /** 86 * WGL doesn't have precise rules for the ordering of formats returned 87 * by wglChoosePixelFormat. This function helps choose among the set of 88 * formats returned by wglChoosePixelFormat. The rules in decreasing 89 * priority are: 90 * * Choose formats with the smallest sample count that is >= 91 * desiredSampleCount (or the largest sample count if all formats have 92 * fewer samples than desiredSampleCount.) 93 * * Choose formats with the fewest color samples when coverage sampling 94 * is available. 95 * * If the above rules leave multiple formats, choose the one that 96 * appears first in the formats array parameter. 97 */ 98 int selectFormat(const int formats[], 99 int formatCount, 100 HDC dc, 101 int desiredSampleCount) const; 102private: 103 typedef const char* (WINAPI *GetExtensionsStringProc)(HDC); 104 typedef BOOL (WINAPI *ChoosePixelFormatProc)(HDC, const int *, const FLOAT *, UINT, int *, UINT *); 105 typedef BOOL (WINAPI *GetPixelFormatAttribivProc)(HDC, int, int, UINT, const int*, int*); 106 typedef BOOL (WINAPI *GetPixelFormatAttribfvProc)(HDC, int, int, UINT, const int*, FLOAT*); 107 typedef HGLRC (WINAPI *CreateContextAttribsProc)(HDC, HGLRC, const int *); 108 typedef BOOL (WINAPI* SwapIntervalProc)(int); 109 typedef HPBUFFER (WINAPI* CreatePbufferProc)(HDC, int , int, int, const int*); 110 typedef HDC (WINAPI* GetPbufferDCProc)(HPBUFFER); 111 typedef int (WINAPI* ReleasePbufferDCProc)(HPBUFFER, HDC); 112 typedef BOOL (WINAPI* DestroyPbufferProc)(HPBUFFER); 113 114 GetExtensionsStringProc fGetExtensionsString; 115 ChoosePixelFormatProc fChoosePixelFormat; 116 GetPixelFormatAttribfvProc fGetPixelFormatAttribfv; 117 GetPixelFormatAttribivProc fGetPixelFormatAttribiv; 118 CreateContextAttribsProc fCreateContextAttribs; 119 SwapIntervalProc fSwapInterval; 120 CreatePbufferProc fCreatePbuffer; 121 GetPbufferDCProc fGetPbufferDC; 122 ReleasePbufferDCProc fReleasePbufferDC; 123 DestroyPbufferProc fDestroyPbuffer; 124}; 125 126enum SkWGLContextRequest { 127 /** Requests to create core profile context if possible, otherwise 128 compatibility profile. */ 129 kGLPreferCoreProfile_SkWGLContextRequest, 130 /** Requests to create compatibility profile context if possible, otherwise 131 core profile. */ 132 kGLPreferCompatibilityProfile_SkWGLContextRequest, 133 /** Requests to create GL ES profile context. */ 134 kGLES_SkWGLContextRequest 135}; 136/** 137 * Helper to create an OpenGL context for a DC using WGL. Configs with a sample count >= to 138 * msaaSampleCount are preferred but if none is available then a context with a lower sample count 139 * (including non-MSAA) will be created. If preferCoreProfile is true but a core profile cannot be 140 * created then a compatible profile context will be created. 141 */ 142HGLRC SkCreateWGLContext(HDC dc, int msaaSampleCount, SkWGLContextRequest context); 143 144/** 145 * Helper class for creating a pbuffer context and deleting all the handles when finished. This 146 * requires that a device context has been created. However, the pbuffer gets its own device 147 * context. The original device context can be released once the pbuffer context is created. 148 */ 149class SkWGLPbufferContext : public SkRefCnt { 150public: 151 static SkWGLPbufferContext* Create(HDC parentDC, int msaaSampleCount, 152 SkWGLContextRequest contextType); 153 154 virtual ~SkWGLPbufferContext(); 155 156 HDC getDC() const { return fDC; } 157 HGLRC getGLRC() const { return fGLRC; } 158 159private: 160 SkWGLPbufferContext(HPBUFFER pbuffer, HDC dc, HGLRC glrc); 161 162 HPBUFFER fPbuffer; 163 HDC fDC; 164 HGLRC fGLRC; 165 SkWGLExtensions fExtensions; 166}; 167 168#endif 169