Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(244)

Side by Side Diff: tools/viewer/sk_app/android/GLWindowContext_android.cpp

Issue 2032623002: Implement OpenGL backend in Android viewer app (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Comments Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 fDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
53
54 EGLint majorVersion;
55 EGLint minorVersion;
56 eglInitialize(fDisplay, &majorVersion, &minorVersion);
57
58 SkAssertResult(eglBindAPI(EGL_OPENGL_ES_API));
59
60 EGLint numConfigs = 0;
61 const EGLint configAttribs[] = {
62 EGL_SURFACE_TYPE, EGL_PBUFFER_BIT,
63 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
64 EGL_RED_SIZE, 8,
65 EGL_GREEN_SIZE, 8,
66 EGL_BLUE_SIZE, 8,
67 EGL_ALPHA_SIZE, 8,
68 EGL_NONE
69 };
70
71 EGLConfig surfaceConfig;
72 SkAssertResult(eglChooseConfig(fDisplay, configAttribs, &surfaceConfig, 1, & numConfigs));
73 SkASSERT(numConfigs > 0);
74
75 static const EGLint kEGLContextAttribsForOpenGLES[] = {
76 EGL_CONTEXT_CLIENT_VERSION, 2,
77 EGL_NONE
78 };
79 fEGLContext = eglCreateContext(
80 fDisplay, surfaceConfig, nullptr, kEGLContextAttribsForOpenGLES);
81 SkASSERT(EGL_NO_CONTEXT != fEGLContext);
82
83 fSurface = eglCreateWindowSurface(
84 fDisplay, surfaceConfig, androidPlatformData->fNativeWindow, nullptr );
85 SkASSERT(EGL_NO_SURFACE != fSurface);
86
87 SkAssertResult(eglMakeCurrent(fDisplay, fSurface, fSurface, fEGLContext));
88 // GLWindowContext::initializeContext will call GrGLCreateNativeInterface so we
89 // won't call it here.
90
91 glClearStencil(0);
92 glClearColor(0, 0, 0, 0);
93 glStencilMask(0xffffffff);
94 glClear(GL_STENCIL_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
95
96 int redBits, greenBits, blueBits;
97 eglGetConfigAttrib(fDisplay, surfaceConfig, EGL_RED_SIZE, &redBits);
98 eglGetConfigAttrib(fDisplay, surfaceConfig, EGL_GREEN_SIZE, &greenBits);
99 eglGetConfigAttrib(fDisplay, surfaceConfig, EGL_BLUE_SIZE, &blueBits);
100 fColorBits = redBits + greenBits + blueBits;
101 eglGetConfigAttrib(fDisplay, surfaceConfig, EGL_STENCIL_SIZE, &fStencilBits) ;
102 eglGetConfigAttrib(fDisplay, surfaceConfig, EGL_SAMPLES, &fSampleCount);
103 }
104
105 void GLWindowContext_android::onDestroyContext() {
106 if (!fDisplay || !fEGLContext || !fSurface) {
107 return;
108 }
109 eglMakeCurrent(fDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
110 SkAssertResult(eglDestroySurface(fDisplay, fSurface));
111 SkAssertResult(eglDestroyContext(fDisplay, fEGLContext));
112 fEGLContext = EGL_NO_CONTEXT;
113 fSurface = EGL_NO_SURFACE;
114 }
115
116 void GLWindowContext_android::onSwapBuffers() {
117 if (fDisplay && fEGLContext && fSurface) {
118 eglSwapBuffers(fDisplay, fSurface);
119 }
120 }
121
122 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698