• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 
2 /*
3  * Copyright 2011 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 #include "gl/SkNativeGLContext.h"
9 
AutoContextRestore()10 SkNativeGLContext::AutoContextRestore::AutoContextRestore() {
11     fOldEGLContext = eglGetCurrentContext();
12     fOldDisplay = eglGetCurrentDisplay();
13     fOldSurface = eglGetCurrentSurface(EGL_DRAW);
14 
15 }
16 
~AutoContextRestore()17 SkNativeGLContext::AutoContextRestore::~AutoContextRestore() {
18     if (NULL != fOldDisplay) {
19         eglMakeCurrent(fOldDisplay, fOldSurface, fOldSurface, fOldEGLContext);
20     }
21 }
22 
23 ///////////////////////////////////////////////////////////////////////////////
24 
SkNativeGLContext()25 SkNativeGLContext::SkNativeGLContext()
26     : fContext(EGL_NO_CONTEXT)
27     , fDisplay(EGL_NO_DISPLAY)
28     , fSurface(EGL_NO_SURFACE) {
29 }
30 
~SkNativeGLContext()31 SkNativeGLContext::~SkNativeGLContext() {
32     this->destroyGLContext();
33 }
34 
destroyGLContext()35 void SkNativeGLContext::destroyGLContext() {
36     if (fDisplay) {
37         eglMakeCurrent(fDisplay, 0, 0, 0);
38 
39         if (fContext) {
40             eglDestroyContext(fDisplay, fContext);
41             fContext = EGL_NO_CONTEXT;
42         }
43 
44         if (fSurface) {
45             eglDestroySurface(fDisplay, fSurface);
46             fSurface = EGL_NO_SURFACE;
47         }
48 
49         //TODO should we close the display?
50         fDisplay = EGL_NO_DISPLAY;
51     }
52 }
53 
createGLContext()54 const GrGLInterface* SkNativeGLContext::createGLContext() {
55     fDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
56 
57     EGLint majorVersion;
58     EGLint minorVersion;
59     eglInitialize(fDisplay, &majorVersion, &minorVersion);
60 
61     EGLint numConfigs;
62     static const EGLint configAttribs[] = {
63         EGL_SURFACE_TYPE, EGL_PBUFFER_BIT,
64         EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
65         EGL_RED_SIZE, 8,
66         EGL_GREEN_SIZE, 8,
67         EGL_BLUE_SIZE, 8,
68         EGL_ALPHA_SIZE, 8,
69         EGL_NONE
70     };
71 
72     EGLConfig surfaceConfig;
73     eglChooseConfig(fDisplay, configAttribs, &surfaceConfig, 1, &numConfigs);
74 
75     static const EGLint contextAttribs[] = {
76         EGL_CONTEXT_CLIENT_VERSION, 2,
77         EGL_NONE
78     };
79     fContext = eglCreateContext(fDisplay, surfaceConfig, NULL, contextAttribs);
80 
81 
82     static const EGLint surfaceAttribs[] = {
83             EGL_WIDTH, 1,
84             EGL_HEIGHT, 1,
85             EGL_NONE
86         };
87     fSurface = eglCreatePbufferSurface(fDisplay, surfaceConfig, surfaceAttribs);
88 
89     eglMakeCurrent(fDisplay, fSurface, fSurface, fContext);
90 
91     const GrGLInterface* interface = GrGLCreateNativeInterface();
92     if (!interface) {
93         SkDebugf("Failed to create gl interface");
94         this->destroyGLContext();
95         return NULL;
96     }
97     return interface;
98 }
99 
makeCurrent() const100 void SkNativeGLContext::makeCurrent() const {
101     if (!eglMakeCurrent(fDisplay, fSurface, fSurface, fContext)) {
102         SkDebugf("Could not set the context.\n");
103     }
104 }
105