tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

GLXLibrary.h (10141B)


      1 /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*-
      2 * This Source Code Form is subject to the terms of the Mozilla Public
      3 * License, v. 2.0. If a copy of the MPL was not distributed with this
      4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      5 
      6 #ifndef GFX_GLXLIBRARY_H
      7 #define GFX_GLXLIBRARY_H
      8 
      9 #include "GLContext.h"
     10 #include "mozilla/Assertions.h"
     11 #include "mozilla/DataMutex.h"
     12 #include "mozilla/gfx/XlibDisplay.h"
     13 #include "prlink.h"
     14 typedef realGLboolean GLboolean;
     15 
     16 // stuff from glx.h
     17 #include "X11/Xlib.h"
     18 #include "X11/Xutil.h"  // for XVisualInfo
     19 #include "X11UndefineNone.h"
     20 typedef struct __GLXcontextRec* GLXContext;
     21 typedef XID GLXPixmap;
     22 typedef XID GLXDrawable;
     23 /* GLX 1.3 and later */
     24 typedef struct __GLXFBConfigRec* GLXFBConfig;
     25 // end of stuff from glx.h
     26 #include "prenv.h"
     27 
     28 struct PRLibrary;
     29 class gfxASurface;
     30 
     31 namespace mozilla {
     32 namespace gl {
     33 
     34 class GLContextGLX;
     35 
     36 class GLXLibrary final {
     37 public:
     38  bool EnsureInitialized(Display* aDisplay);
     39 
     40 private:
     41  class WrapperScope final {
     42    const GLXLibrary& mGlx;
     43    const char* const mFuncName;
     44    Display* const mDisplay;
     45 
     46   public:
     47    WrapperScope(const GLXLibrary& glx, const char* const funcName,
     48                 Display* aDisplay);
     49    ~WrapperScope();
     50  };
     51 
     52 public:
     53 #ifdef DEBUG
     54 #  define DECL_WRAPPER_SCOPE(display) \
     55    const WrapperScope wrapperScope(*this, __func__, display);
     56 #else
     57 #  define DECL_WRAPPER_SCOPE(display)
     58 #endif
     59 
     60  void fDestroyContext(Display* display, GLXContext context) const {
     61    DECL_WRAPPER_SCOPE(display)
     62    return mSymbols.fDestroyContext(display, context);
     63  }
     64 
     65  Bool fMakeCurrent(Display* display, GLXDrawable drawable,
     66                    GLXContext context) const {
     67    DECL_WRAPPER_SCOPE(display)
     68    GLContext::ResetTLSCurrentContext();
     69    return mSymbols.fMakeCurrent(display, drawable, context);
     70  }
     71 
     72  XVisualInfo* fGetConfig(Display* display, XVisualInfo* info, int attrib,
     73                          int* value) const {
     74    DECL_WRAPPER_SCOPE(display)
     75    return mSymbols.fGetConfig(display, info, attrib, value);
     76  }
     77 
     78  GLXContext fGetCurrentContext() const {
     79    DECL_WRAPPER_SCOPE(nullptr)
     80    return mSymbols.fGetCurrentContext();
     81  }
     82 
     83  GLXFBConfig* fChooseFBConfig(Display* display, int screen,
     84                               const int* attrib_list, int* nelements) const {
     85    DECL_WRAPPER_SCOPE(display)
     86    return mSymbols.fChooseFBConfig(display, screen, attrib_list, nelements);
     87  }
     88 
     89  XVisualInfo* fChooseVisual(Display* display, int screen,
     90                             int* attrib_list) const {
     91    DECL_WRAPPER_SCOPE(display)
     92    return mSymbols.fChooseVisual(display, screen, attrib_list);
     93  }
     94 
     95  GLXFBConfig* fGetFBConfigs(Display* display, int screen,
     96                             int* nelements) const {
     97    DECL_WRAPPER_SCOPE(display)
     98    return mSymbols.fGetFBConfigs(display, screen, nelements);
     99  }
    100 
    101  GLXContext fCreateNewContext(Display* display, GLXFBConfig config,
    102                               int render_type, GLXContext share_list,
    103                               Bool direct) const {
    104    DECL_WRAPPER_SCOPE(display)
    105    return mSymbols.fCreateNewContext(display, config, render_type, share_list,
    106                                      direct);
    107  }
    108 
    109  int fGetFBConfigAttrib(Display* display, GLXFBConfig config, int attribute,
    110                         int* value) const {
    111    DECL_WRAPPER_SCOPE(display)
    112    return mSymbols.fGetFBConfigAttrib(display, config, attribute, value);
    113  }
    114 
    115  void fSwapBuffers(Display* display, GLXDrawable drawable) const {
    116    DECL_WRAPPER_SCOPE(display)
    117    return mSymbols.fSwapBuffers(display, drawable);
    118  }
    119 
    120  const char* fQueryExtensionsString(Display* display, int screen) const {
    121    DECL_WRAPPER_SCOPE(display)
    122    return mSymbols.fQueryExtensionsString(display, screen);
    123  }
    124 
    125  const char* fGetClientString(Display* display, int screen) const {
    126    DECL_WRAPPER_SCOPE(display)
    127    return mSymbols.fGetClientString(display, screen);
    128  }
    129 
    130  const char* fQueryServerString(Display* display, int screen, int name) const {
    131    DECL_WRAPPER_SCOPE(display)
    132    return mSymbols.fQueryServerString(display, screen, name);
    133  }
    134 
    135  GLXPixmap fCreatePixmap(Display* display, GLXFBConfig config, Pixmap pixmap,
    136                          const int* attrib_list) const {
    137    DECL_WRAPPER_SCOPE(display)
    138    return mSymbols.fCreatePixmap(display, config, pixmap, attrib_list);
    139  }
    140 
    141  GLXPixmap fCreateGLXPixmapWithConfig(Display* display, GLXFBConfig config,
    142                                       Pixmap pixmap) const {
    143    DECL_WRAPPER_SCOPE(display)
    144    return mSymbols.fCreateGLXPixmapWithConfig(display, config, pixmap);
    145  }
    146 
    147  void fDestroyPixmap(Display* display, GLXPixmap pixmap) const {
    148    DECL_WRAPPER_SCOPE(display)
    149    return mSymbols.fDestroyPixmap(display, pixmap);
    150  }
    151 
    152  Bool fQueryVersion(Display* display, int* major, int* minor) const {
    153    DECL_WRAPPER_SCOPE(display)
    154    return mSymbols.fQueryVersion(display, major, minor);
    155  }
    156 
    157  void fBindTexImage(Display* display, GLXDrawable drawable, int buffer,
    158                     const int* attrib_list) const {
    159    DECL_WRAPPER_SCOPE(display)
    160    return mSymbols.fBindTexImageEXT(display, drawable, buffer, attrib_list);
    161  }
    162 
    163  void fReleaseTexImage(Display* display, GLXDrawable drawable,
    164                        int buffer) const {
    165    DECL_WRAPPER_SCOPE(display)
    166    return mSymbols.fReleaseTexImageEXT(display, drawable, buffer);
    167  }
    168 
    169  void fWaitGL() const {
    170    DECL_WRAPPER_SCOPE(nullptr)
    171    return mSymbols.fWaitGL();
    172  }
    173 
    174  void fWaitX() const {
    175    DECL_WRAPPER_SCOPE(nullptr)
    176    return mSymbols.fWaitX();
    177  }
    178 
    179  GLXContext fCreateContextAttribs(Display* display, GLXFBConfig config,
    180                                   GLXContext share_list, Bool direct,
    181                                   const int* attrib_list) const {
    182    DECL_WRAPPER_SCOPE(display)
    183    return mSymbols.fCreateContextAttribsARB(display, config, share_list,
    184                                             direct, attrib_list);
    185  }
    186 
    187  int fGetVideoSync(unsigned int* count) const {
    188    DECL_WRAPPER_SCOPE(nullptr)
    189    return mSymbols.fGetVideoSyncSGI(count);
    190  }
    191 
    192  int fWaitVideoSync(int divisor, int remainder, unsigned int* count) const {
    193    DECL_WRAPPER_SCOPE(nullptr)
    194    return mSymbols.fWaitVideoSyncSGI(divisor, remainder, count);
    195  }
    196 
    197  void fSwapInterval(Display* dpy, GLXDrawable drawable, int interval) const {
    198    DECL_WRAPPER_SCOPE(dpy)
    199    return mSymbols.fSwapIntervalEXT(dpy, drawable, interval);
    200  }
    201 
    202  int fQueryDrawable(Display* dpy, GLXDrawable drawable, int attribute,
    203                     unsigned int* value) const {
    204    DECL_WRAPPER_SCOPE(dpy)
    205    return mSymbols.fQueryDrawable(dpy, drawable, attribute, value);
    206  }
    207 #undef DECL_WRAPPER_SCOPE
    208 
    209  ////
    210 
    211  bool HasRobustness() { return mHasRobustness; }
    212  bool HasVideoMemoryPurge() { return mHasVideoMemoryPurge; }
    213  bool HasCreateContextAttribs() { return mHasCreateContextAttribs; }
    214  bool SupportsTextureFromPixmap(gfxASurface* aSurface);
    215  bool SupportsVideoSync(Display* aDisplay);
    216  bool SupportsSwapControl() const { return bool(mSymbols.fSwapIntervalEXT); }
    217  bool SupportsBufferAge() const {
    218    MOZ_ASSERT(mInitialized);
    219    return mHasBufferAge;
    220  }
    221  bool IsATI() { return mIsATI; }
    222  bool IsMesa() { return mClientIsMesa; }
    223 
    224  auto GetGetProcAddress() const { return mSymbols.fGetProcAddress; }
    225 
    226  std::shared_ptr<gfx::XlibDisplay> GetDisplay();
    227 
    228 private:
    229  struct {
    230    void(GLAPIENTRY* fDestroyContext)(Display*, GLXContext);
    231    Bool(GLAPIENTRY* fMakeCurrent)(Display*, GLXDrawable, GLXContext);
    232    XVisualInfo*(GLAPIENTRY* fGetConfig)(Display*, XVisualInfo*, int, int*);
    233    GLXContext(GLAPIENTRY* fGetCurrentContext)();
    234    void*(GLAPIENTRY* fGetProcAddress)(const char*);
    235    GLXFBConfig*(GLAPIENTRY* fChooseFBConfig)(Display*, int, const int*, int*);
    236    XVisualInfo*(GLAPIENTRY* fChooseVisual)(Display*, int, const int*);
    237    GLXFBConfig*(GLAPIENTRY* fGetFBConfigs)(Display*, int, int*);
    238    GLXContext(GLAPIENTRY* fCreateNewContext)(Display*, GLXFBConfig, int,
    239                                              GLXContext, Bool);
    240    int(GLAPIENTRY* fGetFBConfigAttrib)(Display*, GLXFBConfig, int, int*);
    241    void(GLAPIENTRY* fSwapBuffers)(Display*, GLXDrawable);
    242    const char*(GLAPIENTRY* fQueryExtensionsString)(Display*, int);
    243    const char*(GLAPIENTRY* fGetClientString)(Display*, int);
    244    const char*(GLAPIENTRY* fQueryServerString)(Display*, int, int);
    245    GLXPixmap(GLAPIENTRY* fCreatePixmap)(Display*, GLXFBConfig, Pixmap,
    246                                         const int*);
    247    GLXPixmap(GLAPIENTRY* fCreateGLXPixmapWithConfig)(Display*, GLXFBConfig,
    248                                                      Pixmap);
    249    void(GLAPIENTRY* fDestroyPixmap)(Display*, GLXPixmap);
    250    Bool(GLAPIENTRY* fQueryVersion)(Display*, int*, int*);
    251    void(GLAPIENTRY* fWaitGL)();
    252    void(GLAPIENTRY* fWaitX)();
    253    void(GLAPIENTRY* fBindTexImageEXT)(Display*, GLXDrawable, int, const int*);
    254    void(GLAPIENTRY* fReleaseTexImageEXT)(Display*, GLXDrawable, int);
    255    GLXContext(GLAPIENTRY* fCreateContextAttribsARB)(Display*, GLXFBConfig,
    256                                                     GLXContext, Bool,
    257                                                     const int*);
    258    int(GLAPIENTRY* fGetVideoSyncSGI)(unsigned int*);
    259    int(GLAPIENTRY* fWaitVideoSyncSGI)(int, int, unsigned int*);
    260    void(GLAPIENTRY* fSwapIntervalEXT)(Display*, GLXDrawable, int);
    261    int(GLAPIENTRY* fQueryDrawable)(Display*, GLXDrawable, int, unsigned int*);
    262  } mSymbols = {};
    263 
    264  bool mInitialized = false;
    265  bool mTriedInitializing = false;
    266  bool mDebug = false;
    267  bool mHasRobustness = false;
    268  bool mHasVideoMemoryPurge = false;
    269  bool mHasCreateContextAttribs = false;
    270  bool mHasVideoSync = false;
    271  bool mHasBufferAge = false;
    272  bool mIsATI = false;
    273  bool mIsNVIDIA = false;
    274  bool mClientIsMesa = false;
    275  PRLibrary* mOGLLibrary = nullptr;
    276  StaticDataMutex<std::weak_ptr<gfx::XlibDisplay>> mOwnDisplay{
    277      "GLXLibrary::mOwnDisplay"};
    278 };
    279 
    280 // a global GLXLibrary instance
    281 extern GLXLibrary sGLXLibrary;
    282 
    283 } /* namespace gl */
    284 } /* namespace mozilla */
    285 #endif /* GFX_GLXLIBRARY_H */