summaryrefslogtreecommitdiffstats
path: root/gfx/gl/GLContextProviderWGL.cpp
blob: 9786e31307b39d05691b801a218dbb2812b2deda (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*-
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "GLContextProvider.h"
#include "GLContextWGL.h"
#include "GLLibraryLoader.h"
#include "nsDebug.h"
#include "nsIWidget.h"
#include "gfxPlatform.h"
#include "gfxWindowsSurface.h"

#include "gfxCrashReporterUtils.h"

#include "prenv.h"

#include "mozilla/gfx/gfxVars.h"
#include "mozilla/Preferences.h"
#include "mozilla/ScopeExit.h"
#include "mozilla/StaticPrefs_gl.h"
#include "mozilla/StaticPtr.h"
#include "mozilla/layers/CompositorOptions.h"
#include "mozilla/widget/CompositorWidget.h"
#include "mozilla/widget/WinCompositorWidget.h"

namespace mozilla {
namespace gl {

using namespace mozilla::gfx;
using namespace mozilla::widget;

WGLLibrary sWGLLib;

/*
ScopedWindow::~ScopedWindow()
{
    if (mDC) {
        MOZ_ALWAYS_TRUE( ReleaseDC(mDC) );
    }
    if (mWindow) {
        MOZ_ALWAYS_TRUE( DestroyWindow(mWindow) );
    }
}
*/
static HWND CreateDummyWindow() {
  WNDCLASSW wc{};
  if (!GetClassInfoW(GetModuleHandle(nullptr), L"GLContextWGLClass", &wc)) {
    wc = {};
    wc.style = CS_OWNDC;
    wc.hInstance = GetModuleHandle(nullptr);
    wc.lpfnWndProc = DefWindowProc;
    wc.lpszClassName = L"GLContextWGLClass";
    if (!RegisterClassW(&wc)) {
      NS_WARNING("Failed to register GLContextWGLClass?!");
      // er. failed to register our class?
      return nullptr;
    }
  }

  return CreateWindowW(L"GLContextWGLClass", L"GLContextWGL", 0, 0, 0, 1, 1,
                       nullptr, nullptr, GetModuleHandle(nullptr), nullptr);
}

static inline bool HasExtension(const char* aExtensions,
                                const char* aRequiredExtension) {
  return GLContext::ListHasExtension(
      reinterpret_cast<const GLubyte*>(aExtensions), aRequiredExtension);
}

SymbolLoader WGLLibrary::GetSymbolLoader() const {
  auto ret = SymbolLoader(*mOGLLibrary);
  ret.mPfn = SymbolLoader::GetProcAddressT(mSymbols.fGetProcAddress);
  return ret;
}

bool WGLLibrary::EnsureInitialized() {
  if (mInitialized) return true;

  mozilla::ScopedGfxFeatureReporter reporter("WGL");

  std::wstring libGLFilename = L"Opengl32.dll";
  // SU_SPIES_DIRECTORY is for AMD CodeXL/gDEBugger
  if (_wgetenv(L"SU_SPIES_DIRECTORY")) {
    libGLFilename =
        std::wstring(_wgetenv(L"SU_SPIES_DIRECTORY")) + L"\\opengl32.dll";
  }

  if (!mOGLLibrary) {
    mOGLLibrary = LoadLibraryWithFlags(libGLFilename.c_str());
    if (!mOGLLibrary) {
      NS_WARNING("Couldn't load OpenGL library.");
      return false;
    }
  }

#define SYMBOL(X)                 \
  {                               \
    (PRFuncPtr*)&mSymbols.f##X, { \
      { "wgl" #X }                \
    }                             \
  }
#define END_OF_SYMBOLS \
  {                    \
    nullptr, {}        \
  }

  {
    const auto loader = SymbolLoader(*mOGLLibrary);
    const SymLoadStruct earlySymbols[] = {SYMBOL(CreateContext),
                                          SYMBOL(MakeCurrent),
                                          SYMBOL(GetProcAddress),
                                          SYMBOL(DeleteContext),
                                          SYMBOL(GetCurrentContext),
                                          SYMBOL(GetCurrentDC),
                                          END_OF_SYMBOLS};

    if (!loader.LoadSymbols(earlySymbols)) {
      NS_WARNING(
          "Couldn't find required entry points in OpenGL DLL (early init)");
      return false;
    }
  }

  mDummyWindow = CreateDummyWindow();
  MOZ_ASSERT(mDummyWindow);
  if (!mDummyWindow) return false;
  auto cleanup = MakeScopeExit([&]() { Reset(); });

  mRootDc = GetDC(mDummyWindow);
  MOZ_ASSERT(mRootDc);
  if (!mRootDc) return false;

  // --

  {
    PIXELFORMATDESCRIPTOR pfd{};
    pfd.nSize = sizeof(PIXELFORMATDESCRIPTOR);
    pfd.nVersion = 1;
    pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL;
    // pfd.iPixelType = PFD_TYPE_RGBA;
    // pfd.cColorBits = 24;
    // pfd.cRedBits = 8;
    // pfd.cGreenBits = 8;
    // pfd.cBlueBits = 8;
    // pfd.cAlphaBits = 8;
    pfd.iLayerType = PFD_MAIN_PLANE;

    const auto pixelFormat = ChoosePixelFormat(mRootDc, &pfd);
    MOZ_ASSERT(pixelFormat);
    if (!pixelFormat) return false;
    const bool setPixelFormatOk = SetPixelFormat(mRootDc, pixelFormat, nullptr);
    MOZ_ASSERT(setPixelFormatOk);
    if (!setPixelFormatOk) return false;
  }

  // --

  // create rendering context
  mDummyGlrc = mSymbols.fCreateContext(mRootDc);
  if (!mDummyGlrc) return false;

  const auto curCtx = mSymbols.fGetCurrentContext();
  const auto curDC = mSymbols.fGetCurrentDC();

  GLContext::ResetTLSCurrentContext();

  if (!mSymbols.fMakeCurrent(mRootDc, mDummyGlrc)) {
    NS_WARNING("wglMakeCurrent failed");
    return false;
  }
  const auto resetContext = MakeScopeExit([&]() {
    GLContext::ResetTLSCurrentContext();
    mSymbols.fMakeCurrent(curDC, curCtx);
  });

  const auto loader = GetSymbolLoader();

  // Now we can grab all the other symbols that we couldn't without having
  // a context current.
  // clang-format off
    const SymLoadStruct reqExtSymbols[] = {
        { (PRFuncPtr*)&mSymbols.fCreatePbuffer, {{ "wglCreatePbufferARB", "wglCreatePbufferEXT" }} },
        { (PRFuncPtr*)&mSymbols.fDestroyPbuffer, {{ "wglDestroyPbufferARB", "wglDestroyPbufferEXT" }} },
        { (PRFuncPtr*)&mSymbols.fGetPbufferDC, {{ "wglGetPbufferDCARB", "wglGetPbufferDCEXT" }} },
        { (PRFuncPtr*)&mSymbols.fReleasePbufferDC, {{ "wglReleasePbufferDCARB", "wglReleasePbufferDCEXT" }} },
    //    { (PRFuncPtr*)&mSymbols.fBindTexImage, {{ "wglBindTexImageARB", "wglBindTexImageEXT" }} },
    //    { (PRFuncPtr*)&mSymbols.fReleaseTexImage, {{ "wglReleaseTexImageARB", "wglReleaseTexImageEXT" }} },
        { (PRFuncPtr*)&mSymbols.fChoosePixelFormat, {{ "wglChoosePixelFormatARB", "wglChoosePixelFormatEXT" }} },
    //    { (PRFuncPtr*)&mSymbols.fGetPixelFormatAttribiv, {{ "wglGetPixelFormatAttribivARB", "wglGetPixelFormatAttribivEXT" }} },
        SYMBOL(GetExtensionsStringARB),
        END_OF_SYMBOLS
    };
  // clang-format on
  if (!loader.LoadSymbols(reqExtSymbols)) {
    NS_WARNING("reqExtSymbols missing");
    return false;
  }

  // --

  const auto extString = mSymbols.fGetExtensionsStringARB(mRootDc);
  MOZ_ASSERT(extString);

  // --

  if (HasExtension(extString, "WGL_ARB_create_context")) {
    const SymLoadStruct createContextSymbols[] = {
        SYMBOL(CreateContextAttribsARB), END_OF_SYMBOLS};
    if (loader.LoadSymbols(createContextSymbols)) {
      if (HasExtension(extString, "WGL_ARB_create_context_robustness")) {
        mHasRobustness = true;
      }
    } else {
      NS_ERROR(
          "WGL_ARB_create_context announced without supplying its functions.");
      ClearSymbols(createContextSymbols);
    }
  }

  // --

  bool hasDXInterop2 = HasExtension(extString, "WGL_NV_DX_interop2");
  if (gfxVars::DXInterop2Blocked() &&
      !StaticPrefs::gl_ignore_dx_interop2_blacklist()) {
    hasDXInterop2 = false;
  }

  if (hasDXInterop2) {
    const SymLoadStruct dxInteropSymbols[] = {
        SYMBOL(DXSetResourceShareHandleNV),
        SYMBOL(DXOpenDeviceNV),
        SYMBOL(DXCloseDeviceNV),
        SYMBOL(DXRegisterObjectNV),
        SYMBOL(DXUnregisterObjectNV),
        SYMBOL(DXObjectAccessNV),
        SYMBOL(DXLockObjectsNV),
        SYMBOL(DXUnlockObjectsNV),
        END_OF_SYMBOLS};
    if (!loader.LoadSymbols(dxInteropSymbols)) {
      NS_ERROR(
          "WGL_NV_DX_interop2 announceed without supplying its functions.");
      ClearSymbols(dxInteropSymbols);
    }
  }

  // --

  cleanup.release();

  mInitialized = true;

  reporter.SetSuccessful();
  return true;
}

#undef SYMBOL
#undef END_OF_SYMBOLS

void WGLLibrary::Reset() {
  if (mDummyGlrc) {
    (void)mSymbols.fDeleteContext(mDummyGlrc);
    mDummyGlrc = nullptr;
  }
  if (mRootDc) {
    (void)ReleaseDC(mDummyWindow, mRootDc);
    mRootDc = nullptr;
  }
  if (mDummyWindow) {
    (void)DestroyWindow(mDummyWindow);
    mDummyWindow = nullptr;
  }
}

GLContextWGL::GLContextWGL(const GLContextDesc& desc, HDC aDC, HGLRC aContext,
                           HWND aWindow)
    : GLContext(desc, nullptr, false),
      mDC(aDC),
      mContext(aContext),
      mWnd(aWindow),
      mPBuffer(nullptr),
      mPixelFormat(0) {}

GLContextWGL::GLContextWGL(const GLContextDesc& desc, HANDLE aPbuffer, HDC aDC,
                           HGLRC aContext, int aPixelFormat)
    : GLContext(desc, nullptr, false),
      mDC(aDC),
      mContext(aContext),
      mWnd(nullptr),
      mPBuffer(aPbuffer),
      mPixelFormat(aPixelFormat) {}

GLContextWGL::~GLContextWGL() {
  MarkDestroyed();

  (void)sWGLLib.mSymbols.fDeleteContext(mContext);

  if (mPBuffer) {
    (void)sWGLLib.mSymbols.fReleasePbufferDC(mPBuffer, mDC);
    (void)sWGLLib.mSymbols.fDestroyPbuffer(mPBuffer);
  }
  if (mWnd) {
    (void)ReleaseDC(mWnd, mDC);
    DestroyWindow(mWnd);
  }
}

bool GLContextWGL::MakeCurrentImpl() const {
  GLContext::ResetTLSCurrentContext();

  const bool succeeded = sWGLLib.mSymbols.fMakeCurrent(mDC, mContext);
  NS_ASSERTION(succeeded, "Failed to make GL context current!");
  return succeeded;
}

bool GLContextWGL::IsCurrentImpl() const {
  return sWGLLib.mSymbols.fGetCurrentContext() == mContext;
}

bool GLContextWGL::SwapBuffers() {
  if (!mIsDoubleBuffered) return false;
  return ::SwapBuffers(mDC);
}

void GLContextWGL::GetWSIInfo(nsCString* const out) const {
  out->AppendLiteral("wglGetExtensionsString: ");
  out->Append(sWGLLib.mSymbols.fGetExtensionsStringARB(mDC));
}

HGLRC
WGLLibrary::CreateContextWithFallback(const HDC dc,
                                      const bool tryRobustBuffers) const {
  if (mHasRobustness) {
    if (tryRobustBuffers) {
      const int attribs[] = {LOCAL_WGL_CONTEXT_FLAGS_ARB,
                             LOCAL_WGL_CONTEXT_ROBUST_ACCESS_BIT_ARB,
                             LOCAL_WGL_CONTEXT_RESET_NOTIFICATION_STRATEGY_ARB,
                             LOCAL_WGL_LOSE_CONTEXT_ON_RESET_ARB, 0};
      const auto context =
          mSymbols.fCreateContextAttribsARB(dc, nullptr, attribs);
      if (context) return context;
    }

    const int attribs[] = {LOCAL_WGL_CONTEXT_RESET_NOTIFICATION_STRATEGY_ARB,
                           LOCAL_WGL_LOSE_CONTEXT_ON_RESET_ARB, 0};
    const auto context =
        mSymbols.fCreateContextAttribsARB(dc, nullptr, attribs);
    if (context) return context;
  }
  if (mSymbols.fCreateContextAttribsARB) {
    const auto context =
        mSymbols.fCreateContextAttribsARB(dc, nullptr, nullptr);
    if (context) return context;
  }
  return mSymbols.fCreateContext(dc);
}

static RefPtr<GLContext> CreateForWidget(const HWND window,
                                         const bool isWebRender,
                                         const bool requireAccelerated) {
  auto& wgl = sWGLLib;
  if (!wgl.EnsureInitialized()) return nullptr;

  const auto dc = GetDC(window);
  if (!dc) return nullptr;
  auto cleanupDc = MakeScopeExit([&]() { (void)ReleaseDC(window, dc); });

  int chosenFormat;
  UINT foundFormats = 0;

  if (!foundFormats) {
    const int kAttribs[] = {LOCAL_WGL_DRAW_TO_WINDOW_ARB,
                            true,
                            LOCAL_WGL_SUPPORT_OPENGL_ARB,
                            true,
                            LOCAL_WGL_DOUBLE_BUFFER_ARB,
                            true,
                            LOCAL_WGL_ACCELERATION_ARB,
                            LOCAL_WGL_FULL_ACCELERATION_ARB,
                            0};
    const int kAttribsForWebRender[] = {LOCAL_WGL_DRAW_TO_WINDOW_ARB,
                                        true,
                                        LOCAL_WGL_SUPPORT_OPENGL_ARB,
                                        true,
                                        LOCAL_WGL_DOUBLE_BUFFER_ARB,
                                        true,
                                        LOCAL_WGL_DEPTH_BITS_ARB,
                                        24,
                                        LOCAL_WGL_ACCELERATION_ARB,
                                        LOCAL_WGL_FULL_ACCELERATION_ARB,
                                        0};
    const int* attribs;
    if (isWebRender) {
      attribs = kAttribsForWebRender;
    } else {
      attribs = kAttribs;
    }

    if (!wgl.mSymbols.fChoosePixelFormat(wgl.RootDc(), attribs, nullptr, 1,
                                         &chosenFormat, &foundFormats)) {
      foundFormats = 0;
    }
  }
  if (!foundFormats) {
    if (requireAccelerated) return nullptr;

    const int kAttribs[] = {LOCAL_WGL_DRAW_TO_WINDOW_ARB,
                            true,
                            LOCAL_WGL_SUPPORT_OPENGL_ARB,
                            true,
                            LOCAL_WGL_DOUBLE_BUFFER_ARB,
                            true,
                            0};
    const int kAttribsForWebRender[] = {LOCAL_WGL_DRAW_TO_WINDOW_ARB,
                                        true,
                                        LOCAL_WGL_SUPPORT_OPENGL_ARB,
                                        true,
                                        LOCAL_WGL_DOUBLE_BUFFER_ARB,
                                        true,
                                        LOCAL_WGL_DEPTH_BITS_ARB,
                                        24,
                                        0};

    const int* attribs;
    if (isWebRender) {
      attribs = kAttribsForWebRender;
    } else {
      attribs = kAttribs;
    }

    if (!wgl.mSymbols.fChoosePixelFormat(wgl.RootDc(), attribs, nullptr, 1,
                                         &chosenFormat, &foundFormats)) {
      foundFormats = 0;
    }
  }
  if (!foundFormats) return nullptr;

  // We need to make sure we call SetPixelFormat -after- calling
  // EnsureInitialized, otherwise it can load/unload the dll and
  // wglCreateContext will fail.

  SetPixelFormat(dc, chosenFormat, nullptr);
  const auto context = sWGLLib.CreateContextWithFallback(dc, false);
  if (!context) return nullptr;

  const RefPtr<GLContextWGL> gl = new GLContextWGL({}, dc, context);
  cleanupDc.release();
  gl->mIsDoubleBuffered = true;
  if (!gl->Init()) return nullptr;

  return gl;
}

already_AddRefed<GLContext> GLContextProviderWGL::CreateForCompositorWidget(
    CompositorWidget* aCompositorWidget, bool aHardwareWebRender,
    bool aForceAccelerated) {
  if (!aCompositorWidget) {
    MOZ_ASSERT(false);
    return nullptr;
  }
  return CreateForWidget(aCompositorWidget->AsWindows()->GetHwnd(),
                         aHardwareWebRender, aForceAccelerated)
      .forget();
}

/*static*/
already_AddRefed<GLContext> GLContextProviderWGL::CreateHeadless(
    const GLContextCreateDesc& desc, nsACString* const out_failureId) {
  auto& wgl = sWGLLib;
  if (!wgl.EnsureInitialized()) return nullptr;

  int chosenFormat;
  UINT foundFormats = 0;

  if (!foundFormats) {
    const int kAttribs[] = {LOCAL_WGL_DRAW_TO_PBUFFER_ARB,
                            true,
                            LOCAL_WGL_SUPPORT_OPENGL_ARB,
                            true,
                            LOCAL_WGL_ACCELERATION_ARB,
                            LOCAL_WGL_FULL_ACCELERATION_ARB,
                            0};
    if (!wgl.mSymbols.fChoosePixelFormat(wgl.RootDc(), kAttribs, nullptr, 1,
                                         &chosenFormat, &foundFormats)) {
      foundFormats = 0;
    }
  }
  if (!foundFormats) {
    const int kAttribs[] = {LOCAL_WGL_DRAW_TO_PBUFFER_ARB, true,
                            LOCAL_WGL_SUPPORT_OPENGL_ARB, true, 0};
    if (!wgl.mSymbols.fChoosePixelFormat(wgl.RootDc(), kAttribs, nullptr, 1,
                                         &chosenFormat, &foundFormats)) {
      foundFormats = 0;
    }
  }
  if (!foundFormats) return nullptr;
  const int kPbufferAttribs[] = {0};
  const auto pbuffer = wgl.mSymbols.fCreatePbuffer(wgl.RootDc(), chosenFormat,
                                                   1, 1, kPbufferAttribs);
  if (!pbuffer) return nullptr;
  auto cleanupPbuffer =
      MakeScopeExit([&]() { (void)wgl.mSymbols.fDestroyPbuffer(pbuffer); });

  const auto dc = wgl.mSymbols.fGetPbufferDC(pbuffer);
  if (!dc) return nullptr;
  auto cleanupDc = MakeScopeExit(
      [&]() { (void)wgl.mSymbols.fReleasePbufferDC(pbuffer, dc); });

  const auto context = wgl.CreateContextWithFallback(dc, true);
  if (!context) return nullptr;

  const auto fullDesc = GLContextDesc{desc, true};
  const RefPtr<GLContextWGL> gl =
      new GLContextWGL(fullDesc, pbuffer, dc, context, chosenFormat);
  cleanupPbuffer.release();
  cleanupDc.release();
  if (!gl->Init()) return nullptr;

  return RefPtr<GLContext>(gl.get()).forget();
}

/*static*/
GLContext* GLContextProviderWGL::GetGlobalContext() { return nullptr; }

/*static*/
void GLContextProviderWGL::Shutdown() {}

} /* namespace gl */
} /* namespace mozilla */