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 "../GLWindowContext.h"
10 #include "SkCanvas.h"
11 #include "SkColorFilter.h"
12 #include "WindowContextFactory_ios.h"
13 #include "gl/GrGLInterface.h"
14 #include "sk_tool_utils.h"
15
16 #include <OpenGLES/ES2/gl.h>
17
18 #include "SDL.h"
19
20 using sk_app::DisplayParams;
21 using sk_app::window_context_factory::IOSWindowInfo;
22 using sk_app::GLWindowContext;
23
24 namespace {
25
26 // We use SDL to support Mac windowing mainly for convenience's sake. However, it
27 // does not allow us to support a purely raster backend because we have no hooks into
28 // the NSWindow's drawRect: method. Hence we use GL to handle the update. Should we
29 // want to avoid this, we will probably need to write our own windowing backend.
30
31 class RasterWindowContext_ios : public GLWindowContext {
32 public:
33 RasterWindowContext_ios(const IOSWindowInfo&, const DisplayParams&);
34
35 ~RasterWindowContext_ios() override;
36
37 sk_sp<SkSurface> getBackbufferSurface() override;
38
39 void onSwapBuffers() override;
40
41 sk_sp<const GrGLInterface> onInitializeContext() override;
42 void onDestroyContext() override;
43
44 private:
45 SDL_Window* fWindow;
46 SDL_GLContext fGLContext;
47 sk_sp<SkSurface> fBackbufferSurface;
48
49 typedef GLWindowContext INHERITED;
50 };
51
RasterWindowContext_ios(const IOSWindowInfo & info,const DisplayParams & params)52 RasterWindowContext_ios::RasterWindowContext_ios(const IOSWindowInfo& info,
53 const DisplayParams& params)
54 : INHERITED(params)
55 , fWindow(info.fWindow)
56 , fGLContext(nullptr) {
57
58 // any config code here (particularly for msaa)?
59
60 this->initializeContext();
61 }
62
~RasterWindowContext_ios()63 RasterWindowContext_ios::~RasterWindowContext_ios() {
64 this->destroyContext();
65 }
66
onInitializeContext()67 sk_sp<const GrGLInterface> RasterWindowContext_ios::onInitializeContext() {
68 SkASSERT(fWindow);
69 SkASSERT(fGLContext);
70
71 if (0 == SDL_GL_MakeCurrent(fWindow, fGLContext)) {
72 glClearStencil(0);
73 glClearColor(0, 0, 0, 0);
74 glStencilMask(0xffffffff);
75 glClear(GL_STENCIL_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
76
77 SDL_GL_GetAttribute(SDL_GL_STENCIL_SIZE, &fStencilBits);
78 SDL_GL_GetAttribute(SDL_GL_MULTISAMPLESAMPLES, &fSampleCount);
79 fSampleCount = SkTMax(fSampleCount, 1);
80
81 SDL_GL_GetDrawableSize(fWindow, &fWidth, &fHeight);
82 glViewport(0, 0, fWidth, fHeight);
83 } else {
84 SkDebugf("MakeCurrent failed: %s\n", SDL_GetError());
85 }
86
87 // make the offscreen image
88 SkImageInfo info = SkImageInfo::Make(fWidth, fHeight, fDisplayParams.fColorType,
89 kPremul_SkAlphaType, fDisplayParams.fColorSpace);
90 fBackbufferSurface = SkSurface::MakeRaster(info);
91 return GrGLMakeNativeInterface();
92 }
93
onDestroyContext()94 void RasterWindowContext_ios::onDestroyContext() {
95 fBackbufferSurface.reset(nullptr);
96 }
97
getBackbufferSurface()98 sk_sp<SkSurface> RasterWindowContext_ios::getBackbufferSurface() { return fBackbufferSurface; }
99
onSwapBuffers()100 void RasterWindowContext_ios::onSwapBuffers() {
101 if (fWindow && fGLContext) {
102 // We made/have an off-screen surface. Get the contents as an SkImage:
103 sk_sp<SkImage> snapshot = fBackbufferSurface->makeImageSnapshot();
104
105 sk_sp<SkSurface> gpuSurface = INHERITED::getBackbufferSurface();
106 SkCanvas* gpuCanvas = gpuSurface->getCanvas();
107 gpuCanvas->drawImage(snapshot, 0, 0);
108 gpuCanvas->flush();
109
110 SDL_GL_SwapWindow(fWindow);
111 }
112 }
113
114 } // anonymous namespace
115
116 namespace sk_app {
117 namespace window_context_factory {
118
NewRasterForIOS(const IOSWindowInfo & info,const DisplayParams & params)119 WindowContext* NewRasterForIOS(const IOSWindowInfo& info, const DisplayParams& params) {
120 WindowContext* ctx = new RasterWindowContext_ios(info, params);
121 if (!ctx->isValid()) {
122 delete ctx;
123 return nullptr;
124 }
125 return ctx;
126 }
127
128 } // namespace window_context_factory
129 } // namespace sk_app
130