blob: 7994b01a672b36377dfddc72326b2a3527ba6f57 [file] [log] [blame]
Joe Gregorioa8fabd32017-05-31 09:45:19 -04001/*
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 Wagner5ec237d2018-05-22 16:44:53 -04008#include "GrContext.h"
9#include "SkRefCnt.h"
10#include "gl/GrGLFunctions.h"
11#include "gl/GrGLInterface.h"
Joe Gregorioa8fabd32017-05-31 09:45:19 -040012
13#include <EGL/egl.h>
Joe Gregorio97b10ac2017-06-01 13:24:11 -040014#include <GLES2/gl2.h>
Joe Gregorioa8fabd32017-05-31 09:45:19 -040015
Ben Wagner5ec237d2018-05-22 16:44:53 -040016#include <sstream>
17
Joe Gregorioa8fabd32017-05-31 09:45:19 -040018static 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
28static const int pbufferWidth = 9;
29static const int pbufferHeight = 9;
30
31static const EGLint pbufferAttribs[] = {
32 EGL_WIDTH, pbufferWidth,
33 EGL_HEIGHT, pbufferHeight,
34 EGL_NONE,
35};
36
37// create_grcontext implementation for EGL.
Joe Gregorio97b10ac2017-06-01 13:24:11 -040038sk_sp<GrContext> create_grcontext(std::ostringstream &driverinfo) {
Joe Gregorioa8fabd32017-05-31 09:45:19 -040039 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 Wagner5ec237d2018-05-22 16:44:53 -040064 EGLContext eglCtx = eglCreateContext(eglDpy, eglCfg, EGL_NO_CONTEXT, nullptr);
Joe Gregorioa8fabd32017-05-31 09:45:19 -040065 if (EGL_NO_CONTEXT == eglCtx) {
66 return nullptr;
67 }
68 if (EGL_FALSE == eglMakeCurrent(eglDpy, eglSurf, eglSurf, eglCtx)) {
69 return nullptr;
70 }
71
Joe Gregorio97b10ac2017-06-01 13:24:11 -040072 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 Salomon3d6801e2017-12-11 10:06:31 -050079 auto interface = GrGLMakeNativeInterface();
Joe Gregorioa8fabd32017-05-31 09:45:19 -040080 if (!interface) {
81 return nullptr;
82 }
83 eglTerminate(eglDpy);
84
Greg Daniel02611d92017-07-25 10:05:01 -040085 return sk_sp<GrContext>(GrContext::MakeGL(interface));
Joe Gregorioa8fabd32017-05-31 09:45:19 -040086}