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 "tools/sk_app/GLWindowContext.h"
11 #include "SDL.h"
12 #include "include/gpu/gl/GrGLInterface.h"
13 #include "tools/sk_app/ios/WindowContextFactory_ios.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;
onDestroyContext()30 void onDestroyContext() override {}
31
32 private:
33 SDL_Window* fWindow;
34 SDL_GLContext fGLContext;
35
36 typedef GLWindowContext INHERITED;
37 };
38
GLWindowContext_ios(const IOSWindowInfo & info,const DisplayParams & params)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
~GLWindowContext_ios()49 GLWindowContext_ios::~GLWindowContext_ios() {
50 this->destroyContext();
51 }
52
onInitializeContext()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
onSwapBuffers()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
MakeGLForIOS(const IOSWindowInfo & info,const DisplayParams & params)86 std::unique_ptr<WindowContext> MakeGLForIOS(const IOSWindowInfo& info,
87 const DisplayParams& params) {
88 std::unique_ptr<WindowContext> ctx(new GLWindowContext_ios(info, params));
89 if (!ctx->isValid()) {
90 return nullptr;
91 }
92 return ctx;
93 }
94
95 } // namespace window_context_factory
96 } // namespace sk_app
97