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