1a3f3caccdfa6e5e044cc1c76e256e55b8a6004adjvanverth 2a3f3caccdfa6e5e044cc1c76e256e55b8a6004adjvanverth/* 3a3f3caccdfa6e5e044cc1c76e256e55b8a6004adjvanverth * Copyright 2016 Google Inc. 4a3f3caccdfa6e5e044cc1c76e256e55b8a6004adjvanverth * 5a3f3caccdfa6e5e044cc1c76e256e55b8a6004adjvanverth * Use of this source code is governed by a BSD-style license that can be 6a3f3caccdfa6e5e044cc1c76e256e55b8a6004adjvanverth * found in the LICENSE file. 7a3f3caccdfa6e5e044cc1c76e256e55b8a6004adjvanverth */ 8a3f3caccdfa6e5e044cc1c76e256e55b8a6004adjvanverth 9d1bdd1fcbd308afb9903f39d231742f5c951cf07bsalomon#include "../GLWindowContext.h" 10d1bdd1fcbd308afb9903f39d231742f5c951cf07bsalomon#include "WindowContextFactory_mac.h" 11a3f3caccdfa6e5e044cc1c76e256e55b8a6004adjvanverth 121ba1d372c2727db56021d0687825320b12d10d4ejvanverth#include "SDL.h" 131ba1d372c2727db56021d0687825320b12d10d4ejvanverth 141ba1d372c2727db56021d0687825320b12d10d4ejvanverth#include <OpenGL/gl.h> 15a3f3caccdfa6e5e044cc1c76e256e55b8a6004adjvanverth 16d1bdd1fcbd308afb9903f39d231742f5c951cf07bsalomonusing sk_app::DisplayParams; 17d1bdd1fcbd308afb9903f39d231742f5c951cf07bsalomonusing sk_app::window_context_factory::MacWindowInfo; 18d1bdd1fcbd308afb9903f39d231742f5c951cf07bsalomonusing sk_app::GLWindowContext; 19d1bdd1fcbd308afb9903f39d231742f5c951cf07bsalomon 20d1bdd1fcbd308afb9903f39d231742f5c951cf07bsalomonnamespace { 21d1bdd1fcbd308afb9903f39d231742f5c951cf07bsalomon 22d1bdd1fcbd308afb9903f39d231742f5c951cf07bsalomonclass GLWindowContext_mac : public GLWindowContext { 23d1bdd1fcbd308afb9903f39d231742f5c951cf07bsalomonpublic: 24d1bdd1fcbd308afb9903f39d231742f5c951cf07bsalomon GLWindowContext_mac(const MacWindowInfo&, const DisplayParams&); 25d1bdd1fcbd308afb9903f39d231742f5c951cf07bsalomon 26d1bdd1fcbd308afb9903f39d231742f5c951cf07bsalomon ~GLWindowContext_mac() override; 27d1bdd1fcbd308afb9903f39d231742f5c951cf07bsalomon 28d1bdd1fcbd308afb9903f39d231742f5c951cf07bsalomon void onSwapBuffers() override; 29d1bdd1fcbd308afb9903f39d231742f5c951cf07bsalomon 30d1bdd1fcbd308afb9903f39d231742f5c951cf07bsalomon void onInitializeContext() override; 31d1bdd1fcbd308afb9903f39d231742f5c951cf07bsalomon void onDestroyContext() override; 32d1bdd1fcbd308afb9903f39d231742f5c951cf07bsalomon 33d1bdd1fcbd308afb9903f39d231742f5c951cf07bsalomonprivate: 341ba1d372c2727db56021d0687825320b12d10d4ejvanverth SDL_Window* fWindow; 351ba1d372c2727db56021d0687825320b12d10d4ejvanverth SDL_GLContext fGLContext; 361ba1d372c2727db56021d0687825320b12d10d4ejvanverth 371ba1d372c2727db56021d0687825320b12d10d4ejvanverth typedef GLWindowContext INHERITED; 38d1bdd1fcbd308afb9903f39d231742f5c951cf07bsalomon}; 39a3f3caccdfa6e5e044cc1c76e256e55b8a6004adjvanverth 40d1bdd1fcbd308afb9903f39d231742f5c951cf07bsalomonGLWindowContext_mac::GLWindowContext_mac(const MacWindowInfo& info, const DisplayParams& params) 411ba1d372c2727db56021d0687825320b12d10d4ejvanverth : INHERITED(params) 421ba1d372c2727db56021d0687825320b12d10d4ejvanverth , fWindow(info.fWindow) 431ba1d372c2727db56021d0687825320b12d10d4ejvanverth , fGLContext(nullptr) { 44a3f3caccdfa6e5e044cc1c76e256e55b8a6004adjvanverth 45a3f3caccdfa6e5e044cc1c76e256e55b8a6004adjvanverth // any config code here (particularly for msaa)? 46a3f3caccdfa6e5e044cc1c76e256e55b8a6004adjvanverth 47d1bdd1fcbd308afb9903f39d231742f5c951cf07bsalomon this->initializeContext(); 48a3f3caccdfa6e5e044cc1c76e256e55b8a6004adjvanverth} 49a3f3caccdfa6e5e044cc1c76e256e55b8a6004adjvanverth 50a3f3caccdfa6e5e044cc1c76e256e55b8a6004adjvanverthGLWindowContext_mac::~GLWindowContext_mac() { 51a3f3caccdfa6e5e044cc1c76e256e55b8a6004adjvanverth this->destroyContext(); 52a3f3caccdfa6e5e044cc1c76e256e55b8a6004adjvanverth} 53a3f3caccdfa6e5e044cc1c76e256e55b8a6004adjvanverth 54d1bdd1fcbd308afb9903f39d231742f5c951cf07bsalomonvoid GLWindowContext_mac::onInitializeContext() { 551ba1d372c2727db56021d0687825320b12d10d4ejvanverth SkASSERT(fWindow); 56a3f3caccdfa6e5e044cc1c76e256e55b8a6004adjvanverth 571ba1d372c2727db56021d0687825320b12d10d4ejvanverth fGLContext = SDL_GL_CreateContext(fWindow); 58a3f3caccdfa6e5e044cc1c76e256e55b8a6004adjvanverth if (!fGLContext) { 591ba1d372c2727db56021d0687825320b12d10d4ejvanverth SkDebugf("%s\n", SDL_GetError()); 60a3f3caccdfa6e5e044cc1c76e256e55b8a6004adjvanverth return; 61a3f3caccdfa6e5e044cc1c76e256e55b8a6004adjvanverth } 62a3f3caccdfa6e5e044cc1c76e256e55b8a6004adjvanverth 631ba1d372c2727db56021d0687825320b12d10d4ejvanverth if (0 == SDL_GL_MakeCurrent(fWindow, fGLContext)) { 64a3f3caccdfa6e5e044cc1c76e256e55b8a6004adjvanverth glClearStencil(0); 65a3f3caccdfa6e5e044cc1c76e256e55b8a6004adjvanverth glClearColor(0, 0, 0, 0); 66a3f3caccdfa6e5e044cc1c76e256e55b8a6004adjvanverth glStencilMask(0xffffffff); 67a3f3caccdfa6e5e044cc1c76e256e55b8a6004adjvanverth glClear(GL_STENCIL_BUFFER_BIT | GL_COLOR_BUFFER_BIT); 68a3f3caccdfa6e5e044cc1c76e256e55b8a6004adjvanverth 691ba1d372c2727db56021d0687825320b12d10d4ejvanverth SDL_GL_GetAttribute(SDL_GL_STENCIL_SIZE, &fStencilBits); 701ba1d372c2727db56021d0687825320b12d10d4ejvanverth SDL_GL_GetAttribute(SDL_GL_MULTISAMPLESAMPLES, &fSampleCount); 711ba1d372c2727db56021d0687825320b12d10d4ejvanverth 721ba1d372c2727db56021d0687825320b12d10d4ejvanverth SDL_GetWindowSize(fWindow, &fWidth, &fHeight); 73a3f3caccdfa6e5e044cc1c76e256e55b8a6004adjvanverth glViewport(0, 0, fWidth, fHeight); 741ba1d372c2727db56021d0687825320b12d10d4ejvanverth } else { 751ba1d372c2727db56021d0687825320b12d10d4ejvanverth SkDebugf("MakeCurrent failed: %s\n", SDL_GetError()); 76a3f3caccdfa6e5e044cc1c76e256e55b8a6004adjvanverth } 77a3f3caccdfa6e5e044cc1c76e256e55b8a6004adjvanverth} 78a3f3caccdfa6e5e044cc1c76e256e55b8a6004adjvanverth 79a3f3caccdfa6e5e044cc1c76e256e55b8a6004adjvanverthvoid GLWindowContext_mac::onDestroyContext() { 801ba1d372c2727db56021d0687825320b12d10d4ejvanverth if (!fWindow || !fGLContext) { 81a3f3caccdfa6e5e044cc1c76e256e55b8a6004adjvanverth return; 82a3f3caccdfa6e5e044cc1c76e256e55b8a6004adjvanverth } 831ba1d372c2727db56021d0687825320b12d10d4ejvanverth SDL_GL_DeleteContext(fGLContext); 84a3f3caccdfa6e5e044cc1c76e256e55b8a6004adjvanverth fGLContext = nullptr; 85a3f3caccdfa6e5e044cc1c76e256e55b8a6004adjvanverth} 86a3f3caccdfa6e5e044cc1c76e256e55b8a6004adjvanverth 87a3f3caccdfa6e5e044cc1c76e256e55b8a6004adjvanverth 88a3f3caccdfa6e5e044cc1c76e256e55b8a6004adjvanverthvoid GLWindowContext_mac::onSwapBuffers() { 891ba1d372c2727db56021d0687825320b12d10d4ejvanverth if (fWindow && fGLContext) { 901ba1d372c2727db56021d0687825320b12d10d4ejvanverth SDL_GL_SwapWindow(fWindow); 91a3f3caccdfa6e5e044cc1c76e256e55b8a6004adjvanverth } 92a3f3caccdfa6e5e044cc1c76e256e55b8a6004adjvanverth} 93a3f3caccdfa6e5e044cc1c76e256e55b8a6004adjvanverth 94d1bdd1fcbd308afb9903f39d231742f5c951cf07bsalomon} // anonymous namespace 95d1bdd1fcbd308afb9903f39d231742f5c951cf07bsalomon 96d1bdd1fcbd308afb9903f39d231742f5c951cf07bsalomonnamespace sk_app { 97d1bdd1fcbd308afb9903f39d231742f5c951cf07bsalomonnamespace window_context_factory { 98d1bdd1fcbd308afb9903f39d231742f5c951cf07bsalomon 991ba1d372c2727db56021d0687825320b12d10d4ejvanverthWindowContext* NewGLForMac(const MacWindowInfo& info, const DisplayParams& params) { 100d1bdd1fcbd308afb9903f39d231742f5c951cf07bsalomon WindowContext* ctx = new GLWindowContext_mac(info, params); 101d1bdd1fcbd308afb9903f39d231742f5c951cf07bsalomon if (!ctx->isValid()) { 102d1bdd1fcbd308afb9903f39d231742f5c951cf07bsalomon delete ctx; 103d1bdd1fcbd308afb9903f39d231742f5c951cf07bsalomon return nullptr; 104d1bdd1fcbd308afb9903f39d231742f5c951cf07bsalomon } 105d1bdd1fcbd308afb9903f39d231742f5c951cf07bsalomon return ctx; 106d1bdd1fcbd308afb9903f39d231742f5c951cf07bsalomon} 107a3f3caccdfa6e5e044cc1c76e256e55b8a6004adjvanverth 108d1bdd1fcbd308afb9903f39d231742f5c951cf07bsalomon} // namespace window_context_factory 109d1bdd1fcbd308afb9903f39d231742f5c951cf07bsalomon} // namespace sk_app 110