• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "SkCanvas.h"
13 #include "SkColorFilter.h"
14 #include "WindowContextFactory_mac.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::MacWindowInfo;
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_mac : public GLWindowContext {
30 public:
31     RasterWindowContext_mac(const MacWindowInfo&, const DisplayParams&);
32 
33     ~RasterWindowContext_mac() 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_mac(const MacWindowInfo & info,const DisplayParams & params)50 RasterWindowContext_mac::RasterWindowContext_mac(const MacWindowInfo& 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_mac()61 RasterWindowContext_mac::~RasterWindowContext_mac() {
62     this->destroyContext();
63 }
64 
onInitializeContext()65 sk_sp<const GrGLInterface> RasterWindowContext_mac::onInitializeContext() {
66     SkASSERT(fWindow);
67 
68     fGLContext = SDL_GL_CreateContext(fWindow);
69     if (!fGLContext) {
70         SkDebugf("%s\n", SDL_GetError());
71         return nullptr;
72     }
73 
74     if (0 == SDL_GL_MakeCurrent(fWindow, fGLContext)) {
75         glClearStencil(0);
76         glClearColor(0, 0, 0, 0);
77         glStencilMask(0xffffffff);
78         glClear(GL_STENCIL_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
79 
80         SDL_GL_GetAttribute(SDL_GL_STENCIL_SIZE, &fStencilBits);
81         SDL_GL_GetAttribute(SDL_GL_MULTISAMPLESAMPLES, &fSampleCount);
82         fSampleCount = SkTMax(fSampleCount, 1);
83 
84         SDL_GetWindowSize(fWindow, &fWidth, &fHeight);
85         glViewport(0, 0, fWidth, fHeight);
86     } else {
87         SkDebugf("MakeCurrent failed: %s\n", SDL_GetError());
88     }
89 
90     // make the offscreen image
91     SkImageInfo info = SkImageInfo::Make(fWidth, fHeight, fDisplayParams.fColorType,
92                                          kPremul_SkAlphaType, fDisplayParams.fColorSpace);
93     fBackbufferSurface = SkSurface::MakeRaster(info);
94     return GrGLMakeNativeInterface();
95 }
96 
onDestroyContext()97 void RasterWindowContext_mac::onDestroyContext() {
98     if (!fWindow || !fGLContext) {
99         return;
100     }
101     fBackbufferSurface.reset(nullptr);
102     SDL_GL_DeleteContext(fGLContext);
103     fGLContext = nullptr;
104 }
105 
getBackbufferSurface()106 sk_sp<SkSurface> RasterWindowContext_mac::getBackbufferSurface() { return fBackbufferSurface; }
107 
onSwapBuffers()108 void RasterWindowContext_mac::onSwapBuffers() {
109     if (fWindow && fGLContext) {
110         // We made/have an off-screen surface. Get the contents as an SkImage:
111         sk_sp<SkImage> snapshot = fBackbufferSurface->makeImageSnapshot();
112 
113         sk_sp<SkSurface> gpuSurface = INHERITED::getBackbufferSurface();
114         SkCanvas* gpuCanvas = gpuSurface->getCanvas();
115         gpuCanvas->drawImage(snapshot, 0, 0);
116         gpuCanvas->flush();
117 
118         SDL_GL_SwapWindow(fWindow);
119     }
120 }
121 
122 }  // anonymous namespace
123 
124 namespace sk_app {
125 namespace window_context_factory {
126 
NewRasterForMac(const MacWindowInfo & info,const DisplayParams & params)127 WindowContext* NewRasterForMac(const MacWindowInfo& info, const DisplayParams& params) {
128     WindowContext* ctx = new RasterWindowContext_mac(info, params);
129     if (!ctx->isValid()) {
130         delete ctx;
131         return nullptr;
132     }
133     return ctx;
134 }
135 
136 }  // namespace window_context_factory
137 }  // namespace sk_app
138