1
2 /*
3 * Copyright 2016 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
8
9 #include <GLES/gl.h>
10
11 #include "WindowContextFactory_android.h"
12 #include "../GLWindowContext.h"
13 #include <EGL/egl.h>
14
15 using sk_app::GLWindowContext;
16 using sk_app::DisplayParams;
17
18 namespace {
19 class GLWindowContext_android : public GLWindowContext {
20 public:
21
22 GLWindowContext_android(ANativeWindow*, const DisplayParams&);
23
24 ~GLWindowContext_android() override;
25
26 void onSwapBuffers() override;
27
28 void onInitializeContext() override;
29 void onDestroyContext() override;
30
31 private:
32
33 EGLDisplay fDisplay;
34 EGLContext fEGLContext;
35 EGLSurface fSurface;
36
37 // For setDisplayParams and resize which call onInitializeContext with null platformData
38 ANativeWindow* fNativeWindow = nullptr;
39 };
40
GLWindowContext_android(ANativeWindow * window,const DisplayParams & params)41 GLWindowContext_android::GLWindowContext_android(ANativeWindow* window, const DisplayParams& params)
42 : GLWindowContext(params)
43 , fDisplay(EGL_NO_DISPLAY)
44 , fEGLContext(EGL_NO_CONTEXT)
45 , fSurface(EGL_NO_SURFACE)
46 , fNativeWindow(window) {
47
48 // any config code here (particularly for msaa)?
49
50 this->initializeContext();
51 }
52
~GLWindowContext_android()53 GLWindowContext_android::~GLWindowContext_android() {
54 this->destroyContext();
55 }
56
onInitializeContext()57 void GLWindowContext_android::onInitializeContext() {
58 fWidth = ANativeWindow_getWidth(fNativeWindow);
59 fHeight = ANativeWindow_getHeight(fNativeWindow);
60
61 fDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
62
63 EGLint majorVersion;
64 EGLint minorVersion;
65 eglInitialize(fDisplay, &majorVersion, &minorVersion);
66
67 SkAssertResult(eglBindAPI(EGL_OPENGL_ES_API));
68
69 EGLint numConfigs = 0;
70 const EGLint configAttribs[] = {
71 EGL_SURFACE_TYPE, EGL_PBUFFER_BIT,
72 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
73 EGL_RED_SIZE, 8,
74 EGL_GREEN_SIZE, 8,
75 EGL_BLUE_SIZE, 8,
76 EGL_ALPHA_SIZE, 8,
77 EGL_STENCIL_SIZE, 8,
78 EGL_SAMPLE_BUFFERS, fDisplayParams.fMSAASampleCount ? 1 : 0,
79 EGL_SAMPLES, fDisplayParams.fMSAASampleCount,
80 EGL_NONE
81 };
82
83 EGLConfig surfaceConfig;
84 SkAssertResult(eglChooseConfig(fDisplay, configAttribs, &surfaceConfig, 1, &numConfigs));
85 SkASSERT(numConfigs > 0);
86
87 static const EGLint kEGLContextAttribsForOpenGLES[] = {
88 EGL_CONTEXT_CLIENT_VERSION, 2,
89 EGL_NONE
90 };
91 fEGLContext = eglCreateContext(
92 fDisplay, surfaceConfig, nullptr, kEGLContextAttribsForOpenGLES);
93 SkASSERT(EGL_NO_CONTEXT != fEGLContext);
94
95 // SkDebugf("EGL: %d.%d", majorVersion, minorVersion);
96 // SkDebugf("Vendor: %s", eglQueryString(fDisplay, EGL_VENDOR));
97 // SkDebugf("Extensions: %s", eglQueryString(fDisplay, EGL_EXTENSIONS));
98
99 // These values are the same as the corresponding VG colorspace attributes,
100 // which were accepted starting in EGL 1.2. For some reason in 1.4, sRGB
101 // became hidden behind an extension, but it looks like devices aren't
102 // advertising that extension (including Nexus 5X). So just check version?
103 const EGLint srgbWindowAttribs[] = {
104 /*EGL_GL_COLORSPACE_KHR*/ 0x309D, /*EGL_GL_COLORSPACE_SRGB_KHR*/ 0x3089,
105 EGL_NONE,
106 };
107 const EGLint* windowAttribs = nullptr;
108 auto srgbColorSpace = SkColorSpace::MakeSRGB();
109 if (srgbColorSpace == fDisplayParams.fColorSpace && majorVersion == 1 && minorVersion >= 2) {
110 windowAttribs = srgbWindowAttribs;
111 }
112
113 fSurface = eglCreateWindowSurface(fDisplay, surfaceConfig, fNativeWindow, windowAttribs);
114 if (EGL_NO_SURFACE == fSurface && windowAttribs) {
115 // Try again without sRGB
116 fSurface = eglCreateWindowSurface(fDisplay, surfaceConfig, fNativeWindow, nullptr);
117 }
118 SkASSERT(EGL_NO_SURFACE != fSurface);
119
120 SkAssertResult(eglMakeCurrent(fDisplay, fSurface, fSurface, fEGLContext));
121 // GLWindowContext::initializeContext will call GrGLCreateNativeInterface so we
122 // won't call it here.
123
124 glClearStencil(0);
125 glClearColor(0, 0, 0, 0);
126 glStencilMask(0xffffffff);
127 glClear(GL_STENCIL_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
128
129 eglGetConfigAttrib(fDisplay, surfaceConfig, EGL_STENCIL_SIZE, &fStencilBits);
130 eglGetConfigAttrib(fDisplay, surfaceConfig, EGL_SAMPLES, &fSampleCount);
131 }
132
onDestroyContext()133 void GLWindowContext_android::onDestroyContext() {
134 if (!fDisplay || !fEGLContext || !fSurface) {
135 return;
136 }
137 eglMakeCurrent(fDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
138 SkAssertResult(eglDestroySurface(fDisplay, fSurface));
139 SkAssertResult(eglDestroyContext(fDisplay, fEGLContext));
140 fEGLContext = EGL_NO_CONTEXT;
141 fSurface = EGL_NO_SURFACE;
142 }
143
onSwapBuffers()144 void GLWindowContext_android::onSwapBuffers() {
145 if (fDisplay && fEGLContext && fSurface) {
146 eglSwapBuffers(fDisplay, fSurface);
147 }
148 }
149
150 } // anonymous namespace
151
152 namespace sk_app {
153 namespace window_context_factory {
154
NewGLForAndroid(ANativeWindow * window,const DisplayParams & params)155 WindowContext* NewGLForAndroid(ANativeWindow* window, const DisplayParams& params) {
156 WindowContext* ctx = new GLWindowContext_android(window, params);
157 if (!ctx->isValid()) {
158 delete ctx;
159 return nullptr;
160 }
161 return ctx;
162 }
163
164 } // namespace window_context_factory
165 } // namespace sk_app
166