232c8664239c505a7bbbb42d7dd8c3563269e371
[platform/framework/web/crosswalk.git] / src / third_party / skia / src / gpu / gl / angle / SkANGLEGLContext.cpp
1
2 /*
3  * Copyright 2012 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 "gl/SkANGLEGLContext.h"
10
11 SkANGLEGLContext::SkANGLEGLContext()
12     : fContext(EGL_NO_CONTEXT)
13     , fDisplay(EGL_NO_DISPLAY)
14     , fSurface(EGL_NO_SURFACE) {
15     fDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
16
17     EGLint majorVersion;
18     EGLint minorVersion;
19     eglInitialize(fDisplay, &majorVersion, &minorVersion);
20
21     EGLint numConfigs;
22     static const EGLint configAttribs[] = {
23         EGL_SURFACE_TYPE, EGL_PBUFFER_BIT,
24         EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
25         EGL_RED_SIZE, 8,
26         EGL_GREEN_SIZE, 8,
27         EGL_BLUE_SIZE, 8,
28         EGL_ALPHA_SIZE, 8,
29         EGL_NONE
30     };
31
32     EGLConfig surfaceConfig;
33     eglChooseConfig(fDisplay, configAttribs, &surfaceConfig, 1, &numConfigs);
34
35     static const EGLint contextAttribs[] = {
36         EGL_CONTEXT_CLIENT_VERSION, 2,
37         EGL_NONE
38     };
39     fContext = eglCreateContext(fDisplay, surfaceConfig, NULL, contextAttribs);
40
41
42     static const EGLint surfaceAttribs[] = {
43             EGL_WIDTH, 1,
44             EGL_HEIGHT, 1,
45             EGL_NONE
46         };
47     fSurface = eglCreatePbufferSurface(fDisplay, surfaceConfig, surfaceAttribs);
48
49     eglMakeCurrent(fDisplay, fSurface, fSurface, fContext);
50
51     fGL.reset(GrGLCreateANGLEInterface());
52     if (NULL == fGL.get()) {
53         SkDebugf("Could not create ANGLE GL interface!\n");
54         this->destroyGLContext();
55         return;
56     }
57     if (!fGL->validate()) {
58         SkDebugf("Could not validate ANGLE GL interface!\n");
59         this->destroyGLContext();
60         return;
61     }
62 }
63
64 SkANGLEGLContext::~SkANGLEGLContext() {
65     this->destroyGLContext();
66 }
67
68 void SkANGLEGLContext::destroyGLContext() {
69     fGL.reset(NULL);
70     if (fDisplay) {
71         eglMakeCurrent(fDisplay, 0, 0, 0);
72
73         if (fContext) {
74             eglDestroyContext(fDisplay, fContext);
75             fContext = EGL_NO_CONTEXT;
76         }
77
78         if (fSurface) {
79             eglDestroySurface(fDisplay, fSurface);
80             fSurface = EGL_NO_SURFACE;
81         }
82
83         //TODO should we close the display?
84         fDisplay = EGL_NO_DISPLAY;
85     }
86 }
87
88 void SkANGLEGLContext::makeCurrent() const {
89     if (!eglMakeCurrent(fDisplay, fSurface, fSurface, fContext)) {
90         SkDebugf("Could not set the context.\n");
91     }
92 }
93
94 void SkANGLEGLContext::swapBuffers() const {
95     if (!eglSwapBuffers(fDisplay, fSurface)) {
96         SkDebugf("Could not complete eglSwapBuffers.\n");
97     }
98 }