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