1 2 /* 3 * Copyright 2017 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 <OpenGLES/ES2/gl.h> 10 #include "../GLWindowContext.h" 11 #include "SDL.h" 12 #include "WindowContextFactory_ios.h" 13 #include "gl/GrGLInterface.h" 14 15 using sk_app::DisplayParams; 16 using sk_app::window_context_factory::IOSWindowInfo; 17 using sk_app::GLWindowContext; 18 19 namespace { 20 21 class GLWindowContext_ios : public GLWindowContext { 22 public: 23 GLWindowContext_ios(const IOSWindowInfo&, const DisplayParams&); 24 25 ~GLWindowContext_ios() override; 26 27 void onSwapBuffers() override; 28 29 sk_sp<const GrGLInterface> onInitializeContext() override; 30 void onDestroyContext() override {} 31 32 private: 33 SDL_Window* fWindow; 34 SDL_GLContext fGLContext; 35 36 typedef GLWindowContext INHERITED; 37 }; 38 39 GLWindowContext_ios::GLWindowContext_ios(const IOSWindowInfo& info, const DisplayParams& params) 40 : INHERITED(params) 41 , fWindow(info.fWindow) 42 , fGLContext(info.fGLContext) { 43 44 // any config code here (particularly for msaa)? 45 46 this->initializeContext(); 47 } 48 49 GLWindowContext_ios::~GLWindowContext_ios() { 50 this->destroyContext(); 51 } 52 53 sk_sp<const GrGLInterface> GLWindowContext_ios::onInitializeContext() { 54 SkASSERT(fWindow); 55 SkASSERT(fGLContext); 56 57 if (0 == SDL_GL_MakeCurrent(fWindow, fGLContext)) { 58 glClearStencil(0); 59 glClearColor(0, 0, 0, 0); 60 glStencilMask(0xffffffff); 61 glClear(GL_STENCIL_BUFFER_BIT | GL_COLOR_BUFFER_BIT); 62 63 SDL_GL_GetAttribute(SDL_GL_STENCIL_SIZE, &fStencilBits); 64 SDL_GL_GetAttribute(SDL_GL_MULTISAMPLESAMPLES, &fSampleCount); 65 fSampleCount = SkTMax(fSampleCount, 1); 66 67 SDL_GL_GetDrawableSize(fWindow, &fWidth, &fHeight); 68 glViewport(0, 0, fWidth, fHeight); 69 } else { 70 SkDebugf("MakeCurrent failed: %s\n", SDL_GetError()); 71 } 72 return GrGLMakeNativeInterface(); 73 } 74 75 void GLWindowContext_ios::onSwapBuffers() { 76 if (fWindow && fGLContext) { 77 SDL_GL_SwapWindow(fWindow); 78 } 79 } 80 81 } // anonymous namespace 82 83 namespace sk_app { 84 namespace window_context_factory { 85 86 WindowContext* NewGLForIOS(const IOSWindowInfo& info, const DisplayParams& params) { 87 WindowContext* ctx = new GLWindowContext_ios(info, params); 88 if (!ctx->isValid()) { 89 delete ctx; 90 return nullptr; 91 } 92 return ctx; 93 } 94 95 } // namespace window_context_factory 96 } // namespace sk_app 97