| OLD | NEW |
| 1 | 1 |
| 2 /* | 2 /* |
| 3 * Copyright 2016 Google Inc. | 3 * Copyright 2016 Google Inc. |
| 4 * | 4 * |
| 5 * Use of this source code is governed by a BSD-style license that can be | 5 * Use of this source code is governed by a BSD-style license that can be |
| 6 * found in the LICENSE file. | 6 * found in the LICENSE file. |
| 7 */ | 7 */ |
| 8 | 8 |
| 9 #include <GLES/gl.h> | 9 #include <GLES/gl.h> |
| 10 | 10 |
| 11 #include "GLWindowContext_android.h" | 11 #include "WindowContextFactory_android.h" |
| 12 #include "../GLWindowContext.h" |
| 13 #include <EGL/egl.h> |
| 12 | 14 |
| 13 #include <android/native_window_jni.h> | 15 using sk_app::GLWindowContext; |
| 16 using sk_app::DisplayParams; |
| 14 | 17 |
| 15 namespace sk_app { | 18 namespace { |
| 19 class GLWindowContext_android : public GLWindowContext { |
| 20 public: |
| 16 | 21 |
| 17 // Most of the following 3 functions (GLWindowContext::Create, constructor, desc
tructor) | 22 GLWindowContext_android(ANativeWindow*, const DisplayParams&); |
| 18 // are copied from Unix/Win platform with unix/win changed to android | |
| 19 | 23 |
| 20 // platform-dependent create | 24 ~GLWindowContext_android() override; |
| 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 | 25 |
| 30 GLWindowContext_android::GLWindowContext_android(void* platformData, const Displ
ayParams& params) | 26 void onSwapBuffers() override; |
| 31 : GLWindowContext(platformData, params) | 27 |
| 28 void onInitializeContext() override; |
| 29 void onDestroyContext() override; |
| 30 |
| 31 private: |
| 32 |
| 33 EGLDisplay fDisplay; |
| 34 EGLContext fEGLContext; |
| 35 EGLSurface fSurface; |
| 36 |
| 37 // For setDisplayParams and resize which call onInitializeContext with null
platformData |
| 38 ANativeWindow* fNativeWindow = nullptr; |
| 39 }; |
| 40 |
| 41 GLWindowContext_android::GLWindowContext_android(ANativeWindow* window, const Di
splayParams& params) |
| 42 : GLWindowContext(params) |
| 32 , fDisplay(EGL_NO_DISPLAY) | 43 , fDisplay(EGL_NO_DISPLAY) |
| 33 , fEGLContext(EGL_NO_CONTEXT) | 44 , fEGLContext(EGL_NO_CONTEXT) |
| 34 , fSurface(EGL_NO_SURFACE) { | 45 , fSurface(EGL_NO_SURFACE) |
| 46 , fNativeWindow(window) { |
| 35 | 47 |
| 36 // any config code here (particularly for msaa)? | 48 // any config code here (particularly for msaa)? |
| 37 | 49 |
| 38 this->initializeContext(platformData, params); | 50 this->initializeContext(); |
| 39 } | 51 } |
| 40 | 52 |
| 41 GLWindowContext_android::~GLWindowContext_android() { | 53 GLWindowContext_android::~GLWindowContext_android() { |
| 42 this->destroyContext(); | 54 this->destroyContext(); |
| 43 } | 55 } |
| 44 | 56 |
| 45 void GLWindowContext_android::onInitializeContext(void* platformData, const Disp
layParams& params) { | 57 void GLWindowContext_android::onInitializeContext() { |
| 46 if (platformData != nullptr) { | |
| 47 ContextPlatformData_android* androidPlatformData = | |
| 48 reinterpret_cast<ContextPlatformData_android*>(platformData); | |
| 49 fNativeWindow = androidPlatformData->fNativeWindow; | |
| 50 } else { | |
| 51 SkASSERT(fNativeWindow); | |
| 52 } | |
| 53 | |
| 54 | |
| 55 fWidth = ANativeWindow_getWidth(fNativeWindow); | 58 fWidth = ANativeWindow_getWidth(fNativeWindow); |
| 56 fHeight = ANativeWindow_getHeight(fNativeWindow); | 59 fHeight = ANativeWindow_getHeight(fNativeWindow); |
| 57 | 60 |
| 58 fDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY); | 61 fDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY); |
| 59 | 62 |
| 60 EGLint majorVersion; | 63 EGLint majorVersion; |
| 61 EGLint minorVersion; | 64 EGLint minorVersion; |
| 62 eglInitialize(fDisplay, &majorVersion, &minorVersion); | 65 eglInitialize(fDisplay, &majorVersion, &minorVersion); |
| 63 | 66 |
| 64 SkAssertResult(eglBindAPI(EGL_OPENGL_ES_API)); | 67 SkAssertResult(eglBindAPI(EGL_OPENGL_ES_API)); |
| (...skipping 28 matching lines...) Expand all Loading... |
| 93 // These values are the same as the corresponding VG colorspace attributes, | 96 // These values are the same as the corresponding VG colorspace attributes, |
| 94 // which were accepted starting in EGL 1.2. For some reason in 1.4, sRGB | 97 // which were accepted starting in EGL 1.2. For some reason in 1.4, sRGB |
| 95 // became hidden behind an extension, but it looks like devices aren't | 98 // became hidden behind an extension, but it looks like devices aren't |
| 96 // advertising that extension (including Nexus 5X). So just check version? | 99 // advertising that extension (including Nexus 5X). So just check version? |
| 97 const EGLint srgbWindowAttribs[] = { | 100 const EGLint srgbWindowAttribs[] = { |
| 98 /*EGL_GL_COLORSPACE_KHR*/ 0x309D, /*EGL_GL_COLORSPACE_SRGB_KHR*/ 0x3089, | 101 /*EGL_GL_COLORSPACE_KHR*/ 0x309D, /*EGL_GL_COLORSPACE_SRGB_KHR*/ 0x3089, |
| 99 EGL_NONE, | 102 EGL_NONE, |
| 100 }; | 103 }; |
| 101 const EGLint* windowAttribs = nullptr; | 104 const EGLint* windowAttribs = nullptr; |
| 102 auto srgbColorSpace = SkColorSpace::NewNamed(SkColorSpace::kSRGB_Named); | 105 auto srgbColorSpace = SkColorSpace::NewNamed(SkColorSpace::kSRGB_Named); |
| 103 if (srgbColorSpace == params.fColorSpace && majorVersion == 1 && minorVersio
n >= 2) { | 106 if (srgbColorSpace == fDisplayParams.fColorSpace && majorVersion == 1 && min
orVersion >= 2) { |
| 104 windowAttribs = srgbWindowAttribs; | 107 windowAttribs = srgbWindowAttribs; |
| 105 } | 108 } |
| 106 | 109 |
| 107 fSurface = eglCreateWindowSurface(fDisplay, surfaceConfig, fNativeWindow, wi
ndowAttribs); | 110 fSurface = eglCreateWindowSurface(fDisplay, surfaceConfig, fNativeWindow, wi
ndowAttribs); |
| 108 if (EGL_NO_SURFACE == fSurface && windowAttribs) { | 111 if (EGL_NO_SURFACE == fSurface && windowAttribs) { |
| 109 // Try again without sRGB | 112 // Try again without sRGB |
| 110 fSurface = eglCreateWindowSurface(fDisplay, surfaceConfig, fNativeWindow
, nullptr); | 113 fSurface = eglCreateWindowSurface(fDisplay, surfaceConfig, fNativeWindow
, nullptr); |
| 111 } | 114 } |
| 112 SkASSERT(EGL_NO_SURFACE != fSurface); | 115 SkASSERT(EGL_NO_SURFACE != fSurface); |
| 113 | 116 |
| (...skipping 25 matching lines...) Expand all Loading... |
| 139 fEGLContext = EGL_NO_CONTEXT; | 142 fEGLContext = EGL_NO_CONTEXT; |
| 140 fSurface = EGL_NO_SURFACE; | 143 fSurface = EGL_NO_SURFACE; |
| 141 } | 144 } |
| 142 | 145 |
| 143 void GLWindowContext_android::onSwapBuffers() { | 146 void GLWindowContext_android::onSwapBuffers() { |
| 144 if (fDisplay && fEGLContext && fSurface) { | 147 if (fDisplay && fEGLContext && fSurface) { |
| 145 eglSwapBuffers(fDisplay, fSurface); | 148 eglSwapBuffers(fDisplay, fSurface); |
| 146 } | 149 } |
| 147 } | 150 } |
| 148 | 151 |
| 152 } // anonymous namespace |
| 153 |
| 154 namespace sk_app { |
| 155 namespace window_context_factory { |
| 156 |
| 157 WindowContext* NewGLForAndroid(ANativeWindow* window, const DisplayParams& param
s) { |
| 158 WindowContext* ctx = new GLWindowContext_android(window, params); |
| 159 if (!ctx->isValid()) { |
| 160 delete ctx; |
| 161 return nullptr; |
| 162 } |
| 163 return ctx; |
| 149 } | 164 } |
| 165 |
| 166 } // namespace window_context_factory |
| 167 } // namespace sk_app |
| OLD | NEW |