1
2 /*
3 * Copyright 2016 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 <OpenGL/gl.h>
10 #include "../GLWindowContext.h"
11 #include "SDL.h"
12 #include "WindowContextFactory_mac.h"
13 #include "gl/GrGLInterface.h"
14
15 using sk_app::DisplayParams;
16 using sk_app::window_context_factory::MacWindowInfo;
17 using sk_app::GLWindowContext;
18
19 namespace {
20
21 class GLWindowContext_mac : public GLWindowContext {
22 public:
23 GLWindowContext_mac(const MacWindowInfo&, const DisplayParams&);
24
25 ~GLWindowContext_mac() 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
GLWindowContext_mac(const MacWindowInfo & info,const DisplayParams & params)39 GLWindowContext_mac::GLWindowContext_mac(const MacWindowInfo& info, const DisplayParams& params)
40 : INHERITED(params)
41 , fWindow(info.fWindow)
42 , fGLContext(nullptr) {
43
44 // any config code here (particularly for msaa)?
45
46 this->initializeContext();
47 }
48
~GLWindowContext_mac()49 GLWindowContext_mac::~GLWindowContext_mac() {
50 this->destroyContext();
51 }
52
onInitializeContext()53 sk_sp<const GrGLInterface> GLWindowContext_mac::onInitializeContext() {
54 SkASSERT(fWindow);
55
56 fGLContext = SDL_GL_CreateContext(fWindow);
57 if (!fGLContext) {
58 SkDebugf("%s\n", SDL_GetError());
59 return nullptr;
60 }
61
62 if (0 == SDL_GL_MakeCurrent(fWindow, fGLContext)) {
63 glClearStencil(0);
64 glClearColor(0, 0, 0, 0);
65 glStencilMask(0xffffffff);
66 glClear(GL_STENCIL_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
67
68 SDL_GL_GetAttribute(SDL_GL_STENCIL_SIZE, &fStencilBits);
69 SDL_GL_GetAttribute(SDL_GL_MULTISAMPLESAMPLES, &fSampleCount);
70 fSampleCount = SkTMax(fSampleCount, 1);
71
72 SDL_GetWindowSize(fWindow, &fWidth, &fHeight);
73 glViewport(0, 0, fWidth, fHeight);
74 } else {
75 SkDebugf("MakeCurrent failed: %s\n", SDL_GetError());
76 }
77 return GrGLMakeNativeInterface();
78 }
79
onDestroyContext()80 void GLWindowContext_mac::onDestroyContext() {
81 if (!fWindow || !fGLContext) {
82 return;
83 }
84 SDL_GL_DeleteContext(fGLContext);
85 fGLContext = nullptr;
86 }
87
88
onSwapBuffers()89 void GLWindowContext_mac::onSwapBuffers() {
90 if (fWindow && fGLContext) {
91 SDL_GL_SwapWindow(fWindow);
92 }
93 }
94
95 } // anonymous namespace
96
97 namespace sk_app {
98 namespace window_context_factory {
99
NewGLForMac(const MacWindowInfo & info,const DisplayParams & params)100 WindowContext* NewGLForMac(const MacWindowInfo& info, const DisplayParams& params) {
101 WindowContext* ctx = new GLWindowContext_mac(info, params);
102 if (!ctx->isValid()) {
103 delete ctx;
104 return nullptr;
105 }
106 return ctx;
107 }
108
109 } // namespace window_context_factory
110 } // namespace sk_app
111