OLD | NEW |
---|---|
(Empty) | |
1 | |
2 /* | |
3 * Copyright 2016 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 <GLES/gl.h> | |
10 | |
11 #include "GLWindowContext_android.h" | |
12 | |
13 #include <android/native_window_jni.h> | |
14 | |
15 namespace sk_app { | |
16 | |
17 // Most of the following 3 functions (GLWindowContext::Create, constructor, desc tructor) | |
18 // are copied from Unix/Win platform with unix/win changed to android | |
19 | |
20 // platform-dependent create | |
21 GLWindowContext* GLWindowContext::Create(void* platformData, const DisplayParams & params) { | |
22 GLWindowContext_android* ctx = new GLWindowContext_android(platformData, par ams); | |
23 if (!ctx->isValid()) { | |
24 delete ctx; | |
25 return nullptr; | |
26 } | |
27 return ctx; | |
28 } | |
29 | |
30 GLWindowContext_android::GLWindowContext_android(void* platformData, const Displ ayParams& params) | |
31 : GLWindowContext(platformData, params) | |
32 , fDisplay(EGL_NO_DISPLAY) | |
33 , fEGLContext(EGL_NO_CONTEXT) | |
34 , fSurface(EGL_NO_SURFACE) { | |
35 | |
36 // any config code here (particularly for msaa)? | |
37 | |
38 this->initializeContext(platformData, params); | |
39 } | |
40 | |
41 GLWindowContext_android::~GLWindowContext_android() { | |
42 this->destroyContext(); | |
43 } | |
44 | |
45 void GLWindowContext_android::onInitializeContext(void* platformData, const Disp layParams& params) { | |
46 ContextPlatformData_android* androidPlatformData = | |
47 reinterpret_cast<ContextPlatformData_android*>(platformData); | |
48 | |
49 fWidth = ANativeWindow_getWidth(androidPlatformData->fNativeWindow); | |
50 fHeight = ANativeWindow_getHeight(androidPlatformData->fNativeWindow); | |
51 | |
52 // The following initialization is mainly copied from CreatePlatformGLTestCo ntext_egl.cpp | |
jvanverth1
2016/06/02 17:17:36
I don't think you need to note this. It's all a ve
liyuqian
2016/06/02 18:47:13
Done.
| |
53 fDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY); | |
54 | |
55 EGLint majorVersion; | |
56 EGLint minorVersion; | |
57 eglInitialize(fDisplay, &majorVersion, &minorVersion); | |
58 | |
59 SkAssertResult(eglBindAPI(EGL_OPENGL_ES_API)); | |
60 | |
61 EGLint numConfigs = 0; | |
62 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 SkAssertResult(eglChooseConfig(fDisplay, configAttribs, &surfaceConfig, 1, & numConfigs)); | |
74 SkASSERT(numConfigs > 0); | |
75 | |
76 static const EGLint kEGLContextAttribsForOpenGLES[] = { | |
77 EGL_CONTEXT_CLIENT_VERSION, 2, | |
78 EGL_NONE | |
79 }; | |
80 fEGLContext = eglCreateContext( | |
81 fDisplay, surfaceConfig, nullptr, kEGLContextAttribsForOpenGLES); | |
82 SkASSERT(EGL_NO_CONTEXT != fEGLContext); | |
83 | |
84 fSurface = eglCreateWindowSurface( | |
85 fDisplay, surfaceConfig, androidPlatformData->fNativeWindow, nullptr ); | |
86 SkASSERT(EGL_NO_SURFACE != fSurface); | |
87 | |
88 SkAssertResult(eglMakeCurrent(fDisplay, fSurface, fSurface, fEGLContext)); | |
89 // Initialization copied from CreatePlatformGLTestContext_egl.cpp ends. | |
jvanverth1
2016/06/02 17:17:36
Same here, I don't think this comment is needed.
liyuqian
2016/06/02 18:47:13
Done.
| |
90 // GLWindowContext::initializeContext will call GrGLCreateNativeInterface so we | |
91 // won't call it here. | |
92 | |
93 glClearStencil(0); | |
94 glClearColor(0, 0, 0, 0); | |
95 glStencilMask(0xffffffff); | |
96 glClear(GL_STENCIL_BUFFER_BIT | GL_COLOR_BUFFER_BIT); | |
97 | |
98 int redBits, greenBits, blueBits; | |
99 eglGetConfigAttrib(fDisplay, surfaceConfig, EGL_RED_SIZE, &redBits); | |
100 eglGetConfigAttrib(fDisplay, surfaceConfig, EGL_GREEN_SIZE, &greenBits); | |
101 eglGetConfigAttrib(fDisplay, surfaceConfig, EGL_BLUE_SIZE, &blueBits); | |
102 fColorBits = redBits + greenBits + blueBits; | |
103 eglGetConfigAttrib(fDisplay, surfaceConfig, EGL_STENCIL_SIZE, &fStencilBits) ; | |
104 eglGetConfigAttrib(fDisplay, surfaceConfig, EGL_SAMPLES, &fSampleCount); | |
105 } | |
106 | |
107 void GLWindowContext_android::onDestroyContext() { | |
108 if (!fDisplay || !fEGLContext || !fSurface) { | |
109 return; | |
110 } | |
111 eglMakeCurrent(fDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT); | |
112 SkAssertResult(eglDestroySurface(fDisplay, fSurface)); | |
113 SkAssertResult(eglDestroyContext(fDisplay, fEGLContext)); | |
114 fEGLContext = EGL_NO_CONTEXT; | |
115 fSurface = EGL_NO_SURFACE; | |
116 } | |
117 | |
118 void GLWindowContext_android::onSwapBuffers() { | |
119 if (fDisplay && fEGLContext && fSurface) { | |
120 eglSwapBuffers(fDisplay, fSurface); | |
121 } | |
122 } | |
123 | |
124 } | |
OLD | NEW |