ANGLEWindowContext_win.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. * Copyright 2015 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. #define EGL_EGL_PROTOTYPES 1
  8. #include <EGL/egl.h>
  9. #include <EGL/eglext.h>
  10. #include "include/gpu/gl/GrGLAssembleInterface.h"
  11. #include "src/gpu/gl/GrGLDefines.h"
  12. #include "tools/sk_app/GLWindowContext.h"
  13. #include "tools/sk_app/win/WindowContextFactory_win.h"
  14. using sk_app::GLWindowContext;
  15. using sk_app::DisplayParams;
  16. namespace {
  17. EGLDisplay get_angle_egl_display(HDC hdc) {
  18. PFNEGLGETPLATFORMDISPLAYEXTPROC eglGetPlatformDisplayEXT;
  19. eglGetPlatformDisplayEXT =
  20. (PFNEGLGETPLATFORMDISPLAYEXTPROC)eglGetProcAddress("eglGetPlatformDisplayEXT");
  21. // We expect ANGLE to support this extension
  22. if (!eglGetPlatformDisplayEXT) {
  23. return EGL_NO_DISPLAY;
  24. }
  25. // We currently only support D3D11 ANGLE.
  26. static constexpr EGLint kType = EGL_PLATFORM_ANGLE_TYPE_D3D11_ANGLE;
  27. static constexpr EGLint attribs[] = {EGL_PLATFORM_ANGLE_TYPE_ANGLE, kType, EGL_NONE};
  28. return eglGetPlatformDisplayEXT(EGL_PLATFORM_ANGLE_ANGLE, hdc, attribs);
  29. }
  30. class ANGLEGLWindowContext_win : public GLWindowContext {
  31. public:
  32. ANGLEGLWindowContext_win(HWND, const DisplayParams&);
  33. ~ANGLEGLWindowContext_win() override;
  34. protected:
  35. void onSwapBuffers() override;
  36. sk_sp<const GrGLInterface> onInitializeContext() override;
  37. void onDestroyContext() override;
  38. private:
  39. HWND fHWND;
  40. EGLDisplay fDisplay = EGL_NO_DISPLAY;
  41. EGLContext fEGLContext = EGL_NO_CONTEXT;
  42. EGLSurface fEGLSurface = EGL_NO_SURFACE;
  43. typedef GLWindowContext INHERITED;
  44. };
  45. ANGLEGLWindowContext_win::ANGLEGLWindowContext_win(HWND wnd, const DisplayParams& params)
  46. : INHERITED(params), fHWND(wnd) {
  47. this->initializeContext();
  48. }
  49. ANGLEGLWindowContext_win::~ANGLEGLWindowContext_win() { this->destroyContext(); }
  50. sk_sp<const GrGLInterface> ANGLEGLWindowContext_win::onInitializeContext() {
  51. HDC dc = GetDC(fHWND);
  52. fDisplay = get_angle_egl_display(dc);
  53. if (EGL_NO_DISPLAY == fDisplay) {
  54. return nullptr;
  55. }
  56. EGLint majorVersion;
  57. EGLint minorVersion;
  58. if (!eglInitialize(fDisplay, &majorVersion, &minorVersion)) {
  59. SkDebugf("Could not initialize display!\n");
  60. return nullptr;
  61. }
  62. EGLint numConfigs;
  63. fSampleCount = this->getDisplayParams().fMSAASampleCount;
  64. const int sampleBuffers = fSampleCount > 1 ? 1 : 0;
  65. const int eglSampleCnt = fSampleCount > 1 ? fSampleCount : 0;
  66. const EGLint configAttribs[] = {EGL_RENDERABLE_TYPE,
  67. // We currently only support ES3.
  68. EGL_OPENGL_ES3_BIT,
  69. EGL_RED_SIZE,
  70. 8,
  71. EGL_GREEN_SIZE,
  72. 8,
  73. EGL_BLUE_SIZE,
  74. 8,
  75. EGL_ALPHA_SIZE,
  76. 8,
  77. EGL_SAMPLE_BUFFERS,
  78. sampleBuffers,
  79. EGL_SAMPLES,
  80. eglSampleCnt,
  81. EGL_NONE};
  82. EGLConfig surfaceConfig;
  83. if (!eglChooseConfig(fDisplay, configAttribs, &surfaceConfig, 1, &numConfigs)) {
  84. SkDebugf("Could not create choose config!\n");
  85. return nullptr;
  86. }
  87. // We currently only support ES3.
  88. const EGLint contextAttribs[] = {EGL_CONTEXT_CLIENT_VERSION, 3, EGL_NONE};
  89. fEGLContext = eglCreateContext(fDisplay, surfaceConfig, nullptr, contextAttribs);
  90. if (EGL_NO_CONTEXT == fEGLContext) {
  91. SkDebugf("Could not create context!\n");
  92. return nullptr;
  93. }
  94. fEGLSurface = eglCreateWindowSurface(fDisplay, surfaceConfig, fHWND, nullptr);
  95. if (EGL_NO_SURFACE == fEGLSurface) {
  96. SkDebugf("Could not create surface!\n");
  97. return nullptr;
  98. }
  99. if (!eglMakeCurrent(fDisplay, fEGLSurface, fEGLSurface, fEGLContext)) {
  100. SkDebugf("Could not make contxt current!\n");
  101. return nullptr;
  102. }
  103. sk_sp<const GrGLInterface> interface(GrGLMakeAssembledInterface(
  104. nullptr,
  105. [](void* ctx, const char name[]) -> GrGLFuncPtr { return eglGetProcAddress(name); }));
  106. if (interface) {
  107. interface->fFunctions.fClearStencil(0);
  108. interface->fFunctions.fClearColor(0, 0, 0, 0);
  109. interface->fFunctions.fStencilMask(0xffffffff);
  110. interface->fFunctions.fClear(GR_GL_STENCIL_BUFFER_BIT | GR_GL_COLOR_BUFFER_BIT);
  111. // use DescribePixelFormat to get the stencil depth.
  112. int pixelFormat = GetPixelFormat(dc);
  113. PIXELFORMATDESCRIPTOR pfd;
  114. DescribePixelFormat(dc, pixelFormat, sizeof(pfd), &pfd);
  115. fStencilBits = pfd.cStencilBits;
  116. RECT rect;
  117. GetClientRect(fHWND, &rect);
  118. fWidth = rect.right - rect.left;
  119. fHeight = rect.bottom - rect.top;
  120. interface->fFunctions.fViewport(0, 0, fWidth, fHeight);
  121. }
  122. return interface;
  123. }
  124. void ANGLEGLWindowContext_win::onDestroyContext() {
  125. eglMakeCurrent(fDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
  126. if (EGL_NO_CONTEXT != fEGLContext) {
  127. eglDestroyContext(fDisplay, fEGLContext);
  128. }
  129. if (EGL_NO_SURFACE != fEGLSurface) {
  130. eglDestroySurface(fDisplay, fEGLSurface);
  131. }
  132. if (EGL_NO_DISPLAY != fDisplay) {
  133. eglTerminate(fDisplay);
  134. }
  135. }
  136. void ANGLEGLWindowContext_win::onSwapBuffers() {
  137. if (!eglSwapBuffers(fDisplay, fEGLSurface)) {
  138. SkDebugf("Could not complete eglSwapBuffers.\n");
  139. }
  140. }
  141. } // anonymous namespace
  142. namespace sk_app {
  143. namespace window_context_factory {
  144. WindowContext* NewANGLEForWin(HWND wnd, const DisplayParams& params) {
  145. ANGLEGLWindowContext_win* ctx = new ANGLEGLWindowContext_win(wnd, params);
  146. if (!ctx->isValid()) {
  147. delete ctx;
  148. return nullptr;
  149. }
  150. return ctx;
  151. }
  152. } // namespace window_context_factory
  153. } // namespace sk_app