| OLD | NEW |
| (Empty) |
| 1 /* | |
| 2 * Copyright 2015 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 "SkOSWindow_Android.h" | |
| 9 | |
| 10 #include <GLES/gl.h> | |
| 11 | |
| 12 SkOSWindow::SkOSWindow(void* hwnd) { | |
| 13 fWindow.fDisplay = EGL_NO_DISPLAY; | |
| 14 fWindow.fContext = EGL_NO_CONTEXT; | |
| 15 fWindow.fSurface = EGL_NO_SURFACE; | |
| 16 fNativeWindow = (ANativeWindow*)hwnd; | |
| 17 fDestroyRequested = false; | |
| 18 } | |
| 19 | |
| 20 SkOSWindow::~SkOSWindow() { | |
| 21 this->release(); | |
| 22 } | |
| 23 | |
| 24 bool SkOSWindow::attach(SkBackEndTypes attachType, | |
| 25 int /*msaaSampleCount*/, | |
| 26 bool /*deepColor*/, | |
| 27 AttachmentInfo* info) { | |
| 28 static const EGLint kEGLContextAttribsForOpenGL[] = { | |
| 29 EGL_NONE | |
| 30 }; | |
| 31 | |
| 32 static const EGLint kEGLContextAttribsForOpenGLES[] = { | |
| 33 EGL_CONTEXT_CLIENT_VERSION, 2, | |
| 34 EGL_NONE | |
| 35 }; | |
| 36 | |
| 37 static const struct { | |
| 38 const EGLint* fContextAttribs; | |
| 39 EGLenum fAPI; | |
| 40 EGLint fRenderableTypeBit; | |
| 41 } kAPIs[] = { | |
| 42 { // OpenGL | |
| 43 kEGLContextAttribsForOpenGL, | |
| 44 EGL_OPENGL_API, | |
| 45 EGL_OPENGL_BIT, | |
| 46 }, | |
| 47 { // OpenGL ES. This seems to work for both ES2 and 3 (when available)
. | |
| 48 kEGLContextAttribsForOpenGLES, | |
| 49 EGL_OPENGL_ES_API, | |
| 50 EGL_OPENGL_ES2_BIT, | |
| 51 }, | |
| 52 }; | |
| 53 | |
| 54 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY); | |
| 55 if (EGL_NO_DISPLAY == display) { | |
| 56 return false; | |
| 57 } | |
| 58 | |
| 59 EGLint majorVersion; | |
| 60 EGLint minorVersion; | |
| 61 if (!eglInitialize(display, &majorVersion, &minorVersion)) { | |
| 62 return false; | |
| 63 } | |
| 64 | |
| 65 for (size_t api = 0; api < SK_ARRAY_COUNT(kAPIs); ++api) { | |
| 66 if (!eglBindAPI(kAPIs[api].fAPI)) { | |
| 67 continue; | |
| 68 } | |
| 69 #if 0 | |
| 70 SkDebugf("VENDOR: %s\n", eglQueryString(fDisplay, EGL_VENDOR)); | |
| 71 SkDebugf("APIS: %s\n", eglQueryString(fDisplay, EGL_CLIENT_APIS)); | |
| 72 SkDebugf("VERSION: %s\n", eglQueryString(fDisplay, EGL_VERSION)); | |
| 73 SkDebugf("EXTENSIONS %s\n", eglQueryString(fDisplay, EGL_EXTENSIONS)); | |
| 74 #endif | |
| 75 | |
| 76 const EGLint configAttribs[] = { | |
| 77 EGL_SURFACE_TYPE, EGL_PBUFFER_BIT, | |
| 78 EGL_RENDERABLE_TYPE, kAPIs[api].fRenderableTypeBit, | |
| 79 EGL_RED_SIZE, 8, | |
| 80 EGL_GREEN_SIZE, 8, | |
| 81 EGL_BLUE_SIZE, 8, | |
| 82 EGL_ALPHA_SIZE, 8, | |
| 83 EGL_NONE | |
| 84 }; | |
| 85 | |
| 86 EGLint format; | |
| 87 EGLint numConfigs; | |
| 88 EGLConfig config; | |
| 89 EGLSurface surface; | |
| 90 EGLContext context; | |
| 91 | |
| 92 /* Here, the application chooses the configuration it desires. In this | |
| 93 * sample, we have a very simplified selection process, where we pick | |
| 94 * the first EGLConfig that matches our criteria */ | |
| 95 if (!eglChooseConfig(display, configAttribs, &config, 1, &numConfigs) || | |
| 96 numConfigs != 1) { | |
| 97 continue; | |
| 98 } | |
| 99 | |
| 100 /* EGL_NATIVE_VISUAL_ID is an attribute of the EGLConfig that is | |
| 101 * guaranteed to be accepted by ANativeWindow_setBuffersGeometry(). | |
| 102 * As soon as we picked a EGLConfig, we can safely reconfigure the | |
| 103 * ANativeWindow buffers to match, using EGL_NATIVE_VISUAL_ID. */ | |
| 104 if (!eglGetConfigAttrib(display, config, EGL_NATIVE_VISUAL_ID, &format))
{ | |
| 105 continue; | |
| 106 } | |
| 107 | |
| 108 ANativeWindow_setBuffersGeometry(fNativeWindow, 0, 0, format); | |
| 109 | |
| 110 surface = eglCreateWindowSurface(display, config, fNativeWindow, nullptr
); | |
| 111 if (EGL_NO_SURFACE == surface) { | |
| 112 SkDebugf("eglCreateWindowSurface failed. EGL Error: 0x%08x\n", eglG
etError()); | |
| 113 continue; | |
| 114 } | |
| 115 context = eglCreateContext(display, config, nullptr, kAPIs[api].fContext
Attribs); | |
| 116 if (EGL_NO_CONTEXT == context) { | |
| 117 SkDebugf("eglCreateContext failed. EGL Error: 0x%08x\n", eglGetErro
r()); | |
| 118 eglDestroySurface(display, surface); | |
| 119 continue; | |
| 120 } | |
| 121 | |
| 122 if (!eglMakeCurrent(display, surface, surface, context)) { | |
| 123 SkDebugf("eglMakeCurrent failed. EGL Error: 0x%08x\n", eglGetError(
)); | |
| 124 eglDestroyContext(display, context); | |
| 125 eglDestroySurface(display, surface); | |
| 126 continue; | |
| 127 } | |
| 128 | |
| 129 fWindow.fDisplay = display; | |
| 130 fWindow.fContext = context; | |
| 131 fWindow.fSurface = surface; | |
| 132 break; | |
| 133 } | |
| 134 | |
| 135 if (fWindow.fDisplay && fWindow.fContext && fWindow.fSurface) { | |
| 136 EGLint w, h; | |
| 137 eglQuerySurface(fWindow.fDisplay, fWindow.fSurface, EGL_WIDTH, &w); | |
| 138 eglQuerySurface(fWindow.fDisplay, fWindow.fSurface, EGL_HEIGHT, &h); | |
| 139 | |
| 140 glViewport(0, 0, w, h); | |
| 141 glClearColor(0.0, 0, 0, 0.0); | |
| 142 glClearStencil(0); | |
| 143 glClear(GL_COLOR_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); | |
| 144 | |
| 145 // We retrieve the fullscreen width and height | |
| 146 this->setSize((SkScalar)w, (SkScalar)h); | |
| 147 return true; | |
| 148 } else { | |
| 149 return false; | |
| 150 } | |
| 151 } | |
| 152 | |
| 153 void SkOSWindow::release() { | |
| 154 if (fWindow.fDisplay != EGL_NO_DISPLAY) { | |
| 155 eglMakeCurrent(fWindow.fDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_
CONTEXT); | |
| 156 if (fWindow.fContext != EGL_NO_CONTEXT) { | |
| 157 eglDestroyContext(fWindow.fDisplay, fWindow.fContext); | |
| 158 } | |
| 159 if (fWindow.fSurface != EGL_NO_SURFACE) { | |
| 160 eglDestroySurface(fWindow.fDisplay, fWindow.fSurface); | |
| 161 } | |
| 162 eglTerminate(fWindow.fDisplay); | |
| 163 } | |
| 164 fWindow.fDisplay = EGL_NO_DISPLAY; | |
| 165 fWindow.fContext = EGL_NO_CONTEXT; | |
| 166 fWindow.fSurface = EGL_NO_SURFACE; | |
| 167 } | |
| 168 | |
| 169 void SkOSWindow::present() { | |
| 170 if (fWindow.fDisplay != EGL_NO_DISPLAY && fWindow.fContext != EGL_NO_CONTEXT
) { | |
| 171 eglSwapBuffers(fWindow.fDisplay, fWindow.fSurface); | |
| 172 } | |
| 173 } | |
| 174 | |
| 175 void SkOSWindow::closeWindow() { | |
| 176 fDestroyRequested = true; | |
| 177 } | |
| 178 | |
| 179 void SkOSWindow::setVsync(bool vsync) { | |
| 180 if (fWindow.fDisplay != EGL_NO_DISPLAY) { | |
| 181 int swapInterval = vsync ? 1 : 0; | |
| 182 eglSwapInterval(fWindow.fDisplay, swapInterval); | |
| 183 } | |
| 184 } | |
| 185 | |
| 186 void SkOSWindow::onSetTitle(const char title[]) { | |
| 187 } | |
| 188 | |
| 189 void SkOSWindow::onHandleInval(const SkIRect& rect) { | |
| 190 } | |
| 191 | |
| 192 /////////////////////////////////////////// | |
| 193 /////////////// SkEvent impl ////////////// | |
| 194 /////////////////////////////////////////// | |
| 195 | |
| 196 void SkEvent::SignalQueueTimer(SkMSec ms) { | |
| 197 } | |
| 198 | |
| 199 void SkEvent::SignalNonEmptyQueue() { | |
| 200 } | |
| OLD | NEW |