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