Home | History | Annotate | Download | only in win
      1 /*
      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 #include "SkLeanWindows.h"
     14 
     15 /**
     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
     19  * get the proc addresses.
     20  *
     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 
     27 #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
     32 #define SK_WGL_RED_BITS                             0x2015
     33 #define SK_WGL_GREEN_BITS                           0x2017
     34 #define SK_WGL_BLUE_BITS                            0x2019
     35 #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
     40 #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
     52 
     53 DECLARE_HANDLE(HPBUFFER);
     54 
     55 class SkWGLExtensions {
     56 public:
     57     SkWGLExtensions();
     58     /**
     59      * Determines if an extensions is available for a given DC.
     60      * WGL_extensions_string is considered a prerequisite for all other
     61      * 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 
     72     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 
     79     /**
     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,
     95                      int desiredSampleCount) const;
     96 private:
     97     typedef const char* (WINAPI *GetExtensionsStringProc)(HDC);
     98     typedef BOOL (WINAPI *ChoosePixelFormatProc)(HDC, const int *, const FLOAT *, UINT, int *, UINT *);
     99     typedef BOOL (WINAPI *GetPixelFormatAttribivProc)(HDC, int, int, UINT, const int*, int*);
    100     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);
    107 
    108     GetExtensionsStringProc fGetExtensionsString;
    109     ChoosePixelFormatProc fChoosePixelFormat;
    110     GetPixelFormatAttribfvProc fGetPixelFormatAttribfv;
    111     GetPixelFormatAttribivProc fGetPixelFormatAttribiv;
    112     CreateContextAttribsProc fCreateContextAttribs;
    113     SwapIntervalProc fSwapInterval;
    114     CreatePbufferProc fCreatePbuffer;
    115     GetPbufferDCProc fGetPbufferDC;
    116     ReleasePbufferDCProc fReleasePbufferDC;
    117     DestroyPbufferProc fDestroyPbuffer;
    118 };
    119 
    120 enum 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 };
    130 /**
    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  */
    136 HGLRC SkCreateWGLContext(HDC dc, int msaaSampleCount, bool deepColor, SkWGLContextRequest context,
    137                          HGLRC shareContext = nullptr);
    138 
    139 /**
    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  */
    144 class SkWGLPbufferContext : public SkRefCnt {
    145 public:
    146     static SkWGLPbufferContext* Create(HDC parentDC, int msaaSampleCount,
    147                                        SkWGLContextRequest contextType, HGLRC shareContext);
    148 
    149     virtual ~SkWGLPbufferContext();
    150 
    151     HDC getDC() const { return fDC; }
    152     HGLRC getGLRC() const { return fGLRC; }
    153 
    154 private:
    155     SkWGLPbufferContext(HPBUFFER pbuffer, HDC dc, HGLRC glrc);
    156 
    157     HPBUFFER        fPbuffer;
    158     HDC             fDC;
    159     HGLRC           fGLRC;
    160     SkWGLExtensions fExtensions;
    161 };
    162 
    163 #endif
    164