GLWindowContext_ios.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*
  2. * Copyright 2017 Google Inc.
  3. *
  4. * Use of this source code is governed by a BSD-style license that can be
  5. * found in the LICENSE file.
  6. */
  7. #include <OpenGLES/ES2/gl.h>
  8. #include "tools/sk_app/GLWindowContext.h"
  9. #include "SDL.h"
  10. #include "include/gpu/gl/GrGLInterface.h"
  11. #include "tools/sk_app/ios/WindowContextFactory_ios.h"
  12. using sk_app::DisplayParams;
  13. using sk_app::window_context_factory::IOSWindowInfo;
  14. using sk_app::GLWindowContext;
  15. namespace {
  16. class GLWindowContext_ios : public GLWindowContext {
  17. public:
  18. GLWindowContext_ios(const IOSWindowInfo&, const DisplayParams&);
  19. ~GLWindowContext_ios() override;
  20. void onSwapBuffers() override;
  21. sk_sp<const GrGLInterface> onInitializeContext() override;
  22. void onDestroyContext() override {}
  23. private:
  24. SDL_Window* fWindow;
  25. SDL_GLContext fGLContext;
  26. typedef GLWindowContext INHERITED;
  27. };
  28. GLWindowContext_ios::GLWindowContext_ios(const IOSWindowInfo& info, const DisplayParams& params)
  29. : INHERITED(params)
  30. , fWindow(info.fWindow)
  31. , fGLContext(info.fGLContext) {
  32. // any config code here (particularly for msaa)?
  33. this->initializeContext();
  34. }
  35. GLWindowContext_ios::~GLWindowContext_ios() {
  36. this->destroyContext();
  37. }
  38. sk_sp<const GrGLInterface> GLWindowContext_ios::onInitializeContext() {
  39. SkASSERT(fWindow);
  40. SkASSERT(fGLContext);
  41. if (0 == SDL_GL_MakeCurrent(fWindow, fGLContext)) {
  42. glClearStencil(0);
  43. glClearColor(0, 0, 0, 0);
  44. glStencilMask(0xffffffff);
  45. glClear(GL_STENCIL_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
  46. SDL_GL_GetAttribute(SDL_GL_STENCIL_SIZE, &fStencilBits);
  47. SDL_GL_GetAttribute(SDL_GL_MULTISAMPLESAMPLES, &fSampleCount);
  48. fSampleCount = SkTMax(fSampleCount, 1);
  49. SDL_GL_GetDrawableSize(fWindow, &fWidth, &fHeight);
  50. glViewport(0, 0, fWidth, fHeight);
  51. } else {
  52. SkDebugf("MakeCurrent failed: %s\n", SDL_GetError());
  53. }
  54. return GrGLMakeNativeInterface();
  55. }
  56. void GLWindowContext_ios::onSwapBuffers() {
  57. if (fWindow && fGLContext) {
  58. SDL_GL_SwapWindow(fWindow);
  59. }
  60. }
  61. } // anonymous namespace
  62. namespace sk_app {
  63. namespace window_context_factory {
  64. WindowContext* NewGLForIOS(const IOSWindowInfo& info, const DisplayParams& params) {
  65. WindowContext* ctx = new GLWindowContext_ios(info, params);
  66. if (!ctx->isValid()) {
  67. delete ctx;
  68. return nullptr;
  69. }
  70. return ctx;
  71. }
  72. } // namespace window_context_factory
  73. } // namespace sk_app