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