• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2023 Google LLC
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 "tools/window/ANGLEWindowContext.h"
9 
10 #include "include/gpu/gl/GrGLAssembleInterface.h"
11 #include "src/gpu/ganesh/gl/GrGLDefines.h"
12 
13 namespace skwindow::internal {
14 
~ANGLEWindowContext()15 ANGLEWindowContext::~ANGLEWindowContext() { this->destroyContext(); }
16 
onInitializeContext()17 sk_sp<const GrGLInterface> ANGLEWindowContext::onInitializeContext() {
18     PFNEGLGETPLATFORMDISPLAYEXTPROC eglGetPlatformDisplayEXT =
19             (PFNEGLGETPLATFORMDISPLAYEXTPROC)eglGetProcAddress("eglGetPlatformDisplayEXT");
20 
21     // We expect ANGLE to support this extension
22     if (!eglGetPlatformDisplayEXT) {
23         return nullptr;
24     }
25 
26     fDisplay = this->onGetEGLDisplay(eglGetPlatformDisplayEXT);
27     if (EGL_NO_DISPLAY == fDisplay) {
28         return nullptr;
29     }
30 
31     EGLint majorVersion;
32     EGLint minorVersion;
33     if (!eglInitialize(fDisplay, &majorVersion, &minorVersion)) {
34         SkDebugf("Could not initialize display!\n");
35         return nullptr;
36     }
37     EGLint numConfigs;
38     fSampleCount = this->getDisplayParams().fMSAASampleCount;
39     const int sampleBuffers = fSampleCount > 1 ? 1 : 0;
40     const int eglSampleCnt = fSampleCount > 1 ? fSampleCount : 0;
41     const EGLint configAttribs[] = {EGL_RENDERABLE_TYPE,
42                                     // We currently only support ES3.
43                                     EGL_OPENGL_ES3_BIT,
44                                     EGL_RED_SIZE,
45                                     8,
46                                     EGL_GREEN_SIZE,
47                                     8,
48                                     EGL_BLUE_SIZE,
49                                     8,
50                                     EGL_ALPHA_SIZE,
51                                     8,
52                                     EGL_SAMPLE_BUFFERS,
53                                     sampleBuffers,
54                                     EGL_SAMPLES,
55                                     eglSampleCnt,
56                                     EGL_NONE};
57 
58     EGLConfig surfaceConfig;
59     if (!eglChooseConfig(fDisplay, configAttribs, &surfaceConfig, 1, &numConfigs)) {
60         SkDebugf("Could not create choose config!\n");
61         return nullptr;
62     }
63     // We currently only support ES3.
64     const EGLint contextAttribs[] = {EGL_CONTEXT_CLIENT_VERSION, 3, EGL_NONE};
65     fEGLContext = eglCreateContext(fDisplay, surfaceConfig, nullptr, contextAttribs);
66     if (EGL_NO_CONTEXT == fEGLContext) {
67         SkDebugf("Could not create context!\n");
68         return nullptr;
69     }
70     fEGLSurface =
71             eglCreateWindowSurface(fDisplay, surfaceConfig, this->onGetNativeWindow(), nullptr);
72     if (EGL_NO_SURFACE == fEGLSurface) {
73         SkDebugf("Could not create surface!\n");
74         return nullptr;
75     }
76     if (!eglMakeCurrent(fDisplay, fEGLSurface, fEGLSurface, fEGLContext)) {
77         SkDebugf("Could not make context current!\n");
78         return nullptr;
79     }
80 
81     sk_sp<const GrGLInterface> interface(GrGLMakeAssembledInterface(
82             nullptr,
83             [](void* ctx, const char name[]) -> GrGLFuncPtr { return eglGetProcAddress(name); }));
84     if (interface) {
85         interface->fFunctions.fClearStencil(0);
86         interface->fFunctions.fClearColor(0, 0, 0, 0);
87         interface->fFunctions.fStencilMask(0xffffffff);
88         interface->fFunctions.fClear(GR_GL_STENCIL_BUFFER_BIT | GR_GL_COLOR_BUFFER_BIT);
89 
90         fStencilBits = this->onGetStencilBits();
91 
92         SkISize size = this->onGetSize();
93         fWidth = size.width();
94         fHeight = size.height();
95         interface->fFunctions.fViewport(0, 0, fWidth, fHeight);
96     }
97     return interface;
98 }
99 
onDestroyContext()100 void ANGLEWindowContext::onDestroyContext() {
101     eglMakeCurrent(fDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
102     if (EGL_NO_CONTEXT != fEGLContext) {
103         eglDestroyContext(fDisplay, fEGLContext);
104     }
105     if (EGL_NO_SURFACE != fEGLSurface) {
106         eglDestroySurface(fDisplay, fEGLSurface);
107     }
108     if (EGL_NO_DISPLAY != fDisplay) {
109         eglTerminate(fDisplay);
110     }
111 }
112 
onSwapBuffers()113 void ANGLEWindowContext::onSwapBuffers() {
114     if (!eglSwapBuffers(fDisplay, fEGLSurface)) {
115         SkDebugf("Could not complete eglSwapBuffers.\n");
116     }
117 }
118 
119 }  // namespace skwindow::internal
120