Joe Gregorio | a8fabd3 | 2017-05-31 09:45:19 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017 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 | |
Ben Wagner | 5ec237d | 2018-05-22 16:44:53 -0400 | [diff] [blame] | 8 | #include "GrContext.h" |
| 9 | #include "SkRefCnt.h" |
| 10 | #include "gl/GrGLFunctions.h" |
| 11 | #include "gl/GrGLInterface.h" |
Joe Gregorio | a8fabd3 | 2017-05-31 09:45:19 -0400 | [diff] [blame] | 12 | |
| 13 | #include <EGL/egl.h> |
Joe Gregorio | 97b10ac | 2017-06-01 13:24:11 -0400 | [diff] [blame] | 14 | #include <GLES2/gl2.h> |
Joe Gregorio | a8fabd3 | 2017-05-31 09:45:19 -0400 | [diff] [blame] | 15 | |
Ben Wagner | 5ec237d | 2018-05-22 16:44:53 -0400 | [diff] [blame] | 16 | #include <sstream> |
| 17 | |
Joe Gregorio | a8fabd3 | 2017-05-31 09:45:19 -0400 | [diff] [blame] | 18 | static const EGLint configAttribs[] = { |
| 19 | EGL_SURFACE_TYPE, EGL_PBUFFER_BIT, |
| 20 | EGL_BLUE_SIZE, 8, |
| 21 | EGL_GREEN_SIZE, 8, |
| 22 | EGL_RED_SIZE, 8, |
| 23 | EGL_DEPTH_SIZE, 8, |
| 24 | EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT, |
| 25 | EGL_NONE |
| 26 | }; |
| 27 | |
| 28 | static const int pbufferWidth = 9; |
| 29 | static const int pbufferHeight = 9; |
| 30 | |
| 31 | static const EGLint pbufferAttribs[] = { |
| 32 | EGL_WIDTH, pbufferWidth, |
| 33 | EGL_HEIGHT, pbufferHeight, |
| 34 | EGL_NONE, |
| 35 | }; |
| 36 | |
| 37 | // create_grcontext implementation for EGL. |
Joe Gregorio | 97b10ac | 2017-06-01 13:24:11 -0400 | [diff] [blame] | 38 | sk_sp<GrContext> create_grcontext(std::ostringstream &driverinfo) { |
Joe Gregorio | a8fabd3 | 2017-05-31 09:45:19 -0400 | [diff] [blame] | 39 | EGLDisplay eglDpy = eglGetDisplay(EGL_DEFAULT_DISPLAY); |
| 40 | if (EGL_NO_DISPLAY == eglDpy) { |
| 41 | return nullptr; |
| 42 | } |
| 43 | |
| 44 | EGLint major, minor; |
| 45 | if (EGL_TRUE != eglInitialize(eglDpy, &major, &minor)) { |
| 46 | return nullptr; |
| 47 | } |
| 48 | |
| 49 | EGLint numConfigs; |
| 50 | EGLConfig eglCfg; |
| 51 | if (EGL_TRUE != eglChooseConfig(eglDpy, configAttribs, &eglCfg, 1, &numConfigs)) { |
| 52 | return nullptr; |
| 53 | } |
| 54 | |
| 55 | EGLSurface eglSurf = eglCreatePbufferSurface(eglDpy, eglCfg, pbufferAttribs); |
| 56 | if (EGL_NO_SURFACE == eglSurf) { |
| 57 | return nullptr; |
| 58 | } |
| 59 | |
| 60 | if (EGL_TRUE != eglBindAPI(EGL_OPENGL_API)) { |
| 61 | return nullptr; |
| 62 | } |
| 63 | |
Ben Wagner | 5ec237d | 2018-05-22 16:44:53 -0400 | [diff] [blame] | 64 | EGLContext eglCtx = eglCreateContext(eglDpy, eglCfg, EGL_NO_CONTEXT, nullptr); |
Joe Gregorio | a8fabd3 | 2017-05-31 09:45:19 -0400 | [diff] [blame] | 65 | if (EGL_NO_CONTEXT == eglCtx) { |
| 66 | return nullptr; |
| 67 | } |
| 68 | if (EGL_FALSE == eglMakeCurrent(eglDpy, eglSurf, eglSurf, eglCtx)) { |
| 69 | return nullptr; |
| 70 | } |
| 71 | |
Joe Gregorio | 97b10ac | 2017-06-01 13:24:11 -0400 | [diff] [blame] | 72 | driverinfo << "EGL " << major << "." << minor << "\n"; |
| 73 | GrGLGetStringProc getString = (GrGLGetStringProc )eglGetProcAddress("glGetString"); |
| 74 | driverinfo << "GL Versionr: " << getString(GL_VERSION) << "\n"; |
| 75 | driverinfo << "GL Vendor: " << getString(GL_VENDOR) << "\n"; |
| 76 | driverinfo << "GL Renderer: " << getString(GL_RENDERER) << "\n"; |
| 77 | driverinfo << "GL Extensions: " << getString(GL_EXTENSIONS) << "\n"; |
| 78 | |
Brian Salomon | 3d6801e | 2017-12-11 10:06:31 -0500 | [diff] [blame] | 79 | auto interface = GrGLMakeNativeInterface(); |
Joe Gregorio | a8fabd3 | 2017-05-31 09:45:19 -0400 | [diff] [blame] | 80 | if (!interface) { |
| 81 | return nullptr; |
| 82 | } |
| 83 | eglTerminate(eglDpy); |
| 84 | |
Greg Daniel | 02611d9 | 2017-07-25 10:05:01 -0400 | [diff] [blame] | 85 | return sk_sp<GrContext>(GrContext::MakeGL(interface)); |
Joe Gregorio | a8fabd3 | 2017-05-31 09:45:19 -0400 | [diff] [blame] | 86 | } |