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