Get Mac viewer working with SDL
authorjvanverth <jvanverth@google.com>
Thu, 4 Aug 2016 19:30:31 +0000 (12:30 -0700)
committerCommit bot <commit-bot@chromium.org>
Thu, 4 Aug 2016 19:30:31 +0000 (12:30 -0700)
Also fixes SkiaSDLExample.

BUG=skia:
GOLD_TRYBOT_URL= https://gold.skia.org/search?issue=2210603003

Review-Url: https://codereview.chromium.org/2210603003

example/SkiaSDLExample.cpp
gyp/viewer.gyp
src/views/mac/skia_mac.mm
third_party/libsdl/mac/sdl_mac.gypi
tools/viewer/sk_app/mac/GLWindowContext_mac.cpp
tools/viewer/sk_app/mac/WindowContextFactory_mac.h
tools/viewer/sk_app/mac/Window_mac.cpp
tools/viewer/sk_app/mac/Window_mac.h
tools/viewer/sk_app/mac/main_mac.cpp

index da5f2dc..0072f0f 100644 (file)
@@ -20,7 +20,7 @@
 #elif defined(SK_BUILD_FOR_UNIX)
 #include <GL/gl.h>
 #elif defined(SK_BUILD_FOR_MAC)
-#include <gl.h>
+#include <OpenGL/gl.h>
 #endif
 
 /*
@@ -224,7 +224,7 @@ int main(int argc, char** argv) {
     SkPaint paint;
 
     // create a surface for CPU rasterization
-    SkAutoTUnref<SkSurface> cpuSurface(SkSurface::NewRaster(canvas->imageInfo()));
+    sk_sp<SkSurface> cpuSurface(SkSurface::MakeRaster(canvas->imageInfo()));
 
     SkCanvas* offscreen = cpuSurface->getCanvas();
     offscreen->save();
@@ -232,7 +232,7 @@ int main(int argc, char** argv) {
     offscreen->drawPath(create_star(), paint);
     offscreen->restore();
 
-    SkAutoTUnref<SkImage> image(cpuSurface->newImageSnapshot());
+    sk_sp<SkImage> image = cpuSurface->makeImageSnapshot();
 
     int rotation = 0;
     while (!state.fQuit) { // Our application loop
index f94c875..4317b8a 100644 (file)
@@ -32,6 +32,7 @@
       'sources': [
         '../gm/gm.cpp',
         '<!@(python find.py "*.cpp" ../tools/viewer)',
+        '<!@(python find.py "*.h" ../tools/viewer)',
 
         # views (subset of files for the Android build)
         '../src/views/SkEvent.cpp',
             ],
           },
         }],
+        [ 'skia_os == "mac"', {
+          'dependencies': [
+            'sdl.gyp:sdl',
+          ],
+        }],
         ['skia_os != "android"', {
           'sources/': [
             ['exclude', '_android.(h|cpp)$'],
index ea3b75c..2a79407 100644 (file)
@@ -90,7 +90,7 @@ int main(int argc, char *argv[]) {
     [window makeKeyAndOrderFront:NSApp];
 
     AppDelegate * appDelegate = [[[AppDelegate alloc] init] autorelease];
-    
+
     app.delegate = appDelegate;
     
     NSMenu* menu=[[NSMenu alloc] initWithTitle:@"AMainMenu"];
index 19970cc..4853dfd 100644 (file)
@@ -5,17 +5,14 @@
 # Mac specific files and settings for SDL
 
 {
- #TODO what is really necessary here
+
  'link_settings': {
    'libraries': [ 
-     '-lm',
-     '-liconv',
-     '-lobjc',
-     '-lpthread',
      '$(SDKROOT)/System/Library/Frameworks/CoreVideo.framework',
      '$(SDKROOT)/System/Library/Frameworks/Cocoa.framework',
      '$(SDKROOT)/System/Library/Frameworks/Carbon.framework',
      '$(SDKROOT)/System/Library/Frameworks/IOKit.framework',
+     '/usr/lib/libiconv.dylib',
    ],
  },
  'sources': [
index c2de0df..bc86df3 100644 (file)
@@ -9,7 +9,9 @@
 #include "../GLWindowContext.h"
 #include "WindowContextFactory_mac.h"
 
-//#include <GL/gl.h>
+#include "SDL.h"
+
+#include <OpenGL/gl.h>
 
 using sk_app::DisplayParams;
 using sk_app::window_context_factory::MacWindowInfo;
@@ -29,25 +31,16 @@ public:
     void onDestroyContext() override;
     
 private:
-  
-#if 0
-    // TODO: add Mac-specific GL display objects
-    Display*     fDisplay;
-    XWindow      fWindow;
-    XVisualInfo* fVisualInfo;
-    GLXContext   fGLContext;
-#endif
+    SDL_Window*   fWindow;
+    SDL_GLContext fGLContext;
+    
+    typedef GLWindowContext INHERITED;
 };
 
 GLWindowContext_mac::GLWindowContext_mac(const MacWindowInfo& info, const DisplayParams& params)
-    : GLWindowContext(params)
-#if 0
-    // TODO: init Mac-specific OpenGL objects
-    , fDisplay(nullptr)
-    , fWindow(0)
-    , fGLContext(0)
-#endif
-    {
+    : INHERITED(params)
+    , fWindow(info.fWindow)
+    , fGLContext(nullptr) {
 
     // any config code here (particularly for msaa)?
 
@@ -59,68 +52,57 @@ GLWindowContext_mac::~GLWindowContext_mac() {
 }
 
 void GLWindowContext_mac::onInitializeContext() {
-#if 0
-    // TODO: Init for Mac
-    SkASSERT(fDisplay);
+    SkASSERT(fWindow);
 
-    fGLContext = glXCreateContext(fDisplay, fVisualInfo, nullptr, GL_TRUE);
+    fGLContext = SDL_GL_CreateContext(fWindow);
     if (!fGLContext) {
+        SkDebugf("%s\n", SDL_GetError());
         return;
     }
 
-    if (glXMakeCurrent(fDisplay, fWindow, fGLContext)) {
+    if (0 == SDL_GL_MakeCurrent(fWindow, fGLContext)) {
         glClearStencil(0);
         glClearColor(0, 0, 0, 0);
         glStencilMask(0xffffffff);
         glClear(GL_STENCIL_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
 
         int redBits, greenBits, blueBits;
-        glXGetConfig(fDisplay, fVisualInfo, GLX_RED_SIZE, &redBits);
-        glXGetConfig(fDisplay, fVisualInfo, GLX_GREEN_SIZE, &greenBits);
-        glXGetConfig(fDisplay, fVisualInfo, GLX_BLUE_SIZE, &blueBits);
+        SDL_GL_GetAttribute(SDL_GL_RED_SIZE, &redBits);
+        SDL_GL_GetAttribute(SDL_GL_GREEN_SIZE, &greenBits);
+        SDL_GL_GetAttribute(SDL_GL_BLUE_SIZE, &blueBits);
         fColorBits = redBits + greenBits + blueBits;
-        glXGetConfig(fDisplay, fVisualInfo, GLX_STENCIL_SIZE, &fStencilBits);
-        glXGetConfig(fDisplay, fVisualInfo, GLX_SAMPLES_ARB, &fSampleCount);
-
-        XWindow root;
-        int x, y;
-        unsigned int border_width, depth;
-        XGetGeometry(fDisplay, fWindow, &root, &x, &y,
-                     (unsigned int*)&fWidth, (unsigned int*)&fHeight, &border_width, &depth); 
+
+        SDL_GL_GetAttribute(SDL_GL_STENCIL_SIZE, &fStencilBits);
+        SDL_GL_GetAttribute(SDL_GL_MULTISAMPLESAMPLES, &fSampleCount);
+
+        SDL_GetWindowSize(fWindow, &fWidth, &fHeight);
         glViewport(0, 0, fWidth, fHeight);
+    } else {
+        SkDebugf("MakeCurrent failed: %s\n", SDL_GetError());
     }
-#endif
 }
 
 void GLWindowContext_mac::onDestroyContext() {
-#if 0
-    // TODO: teardown for Mac
-    if (!fDisplay || !fGLContext) {
+    if (!fWindow || !fGLContext) {
         return;
     }
-    glXMakeCurrent(fDisplay, None, nullptr);
-    glXDestroyContext(fDisplay, fGLContext);
+    SDL_GL_DeleteContext(fGLContext);
     fGLContext = nullptr;
-#endif
 }
 
 
 void GLWindowContext_mac::onSwapBuffers() {
-#if 0
-    // TODO: swap for Mac
-    if (fDisplay && fGLContext) {
-        glXSwapBuffers(fDisplay, fWindow);
+    if (fWindow && fGLContext) {
+        SDL_GL_SwapWindow(fWindow);
     }
-#endif
 }
 
 }  // anonymous namespace
 
-
 namespace sk_app {
 namespace window_context_factory {
 
-WindowContext* MakeGLForMac(const MacWindowInfo& info, const DisplayParams& params) {
+WindowContext* NewGLForMac(const MacWindowInfo& info, const DisplayParams& params) {
     WindowContext* ctx = new GLWindowContext_mac(info, params);
     if (!ctx->isValid()) {
         delete ctx;
index a86fc73..7928ec0 100644 (file)
@@ -9,6 +9,8 @@
 #ifndef WindowContextFactory_mac_DEFINED
 #define WindowContextFactory_mac_DEFINED
 
+#include "SDL.h"
+
 namespace sk_app {
 
 class WindowContext;
@@ -17,11 +19,7 @@ struct DisplayParams;
 namespace window_context_factory {
 
 struct MacWindowInfo {
-#if 0 // TODO: use Mac-specific objects
-    Display*     fDisplay;
-    XWindow      fWindow;
-    XVisualInfo* fVisualInfo;
-#endif
+    SDL_Window*  fWindow;
 };
 
 inline WindowContext* NewVulkanForMac(const MacWindowInfo&, const DisplayParams&) {
index b1ab5c9..4aad93c 100644 (file)
@@ -5,8 +5,6 @@
 * found in the LICENSE file.
 */
 
-//#include <tchar.h>
-
 #include "SkUtils.h"
 #include "Timer.h"
 #include "WindowContextFactory_mac.h"
 
 namespace sk_app {
 
-Window* Window::CreateNativeWindow(void* platformData) {
-#if 0
-    // TODO: platform-specific window creation
-    Display* display = (Display*)platformData;
+SkTDynamicHash<Window_mac, Uint32> Window_mac::gWindowMap;
 
+Window* Window::CreateNativeWindow(void*) {
     Window_mac* window = new Window_mac();
-    if (!window->initWindow(display, nullptr)) {
+    if (!window->initWindow(nullptr)) {
         delete window;
         return nullptr;
     }
 
     return window;
-#else
-    return nullptr;
-#endif
 }
-    
-#if 0
-    // TODO: Implement Mac window code
-
-const long kEventMask = ExposureMask | StructureNotifyMask | 
-                        KeyPressMask | KeyReleaseMask | 
-                        PointerMotionMask | ButtonPressMask | ButtonReleaseMask;
 
-bool Window_mac::initWindow(Display* display, const DisplayParams* params) {
+bool Window_mac::initWindow(const DisplayParams* params) {
     if (params && params->fMSAASampleCount != fMSAASampleCount) {
         this->closeWindow();
     }
     // we already have a window
-    if (fDisplay) {
+    if (fWindow) {
         return true;
     } 
-    fDisplay = display;
 
     fWidth = 1280;
     fHeight = 960;
 
-    // Attempt to create a window that supports GL
-    GLint att[] = {
-        GLX_RGBA,
-        GLX_DEPTH_SIZE, 24,
-        GLX_DOUBLEBUFFER,
-        GLX_STENCIL_SIZE, 8,
-        None
-    };
-    SkASSERT(nullptr == fVisualInfo);
-    if (params && params->fMSAASampleCount > 0) {
-        static const GLint kAttCount = SK_ARRAY_COUNT(att);
-        GLint msaaAtt[kAttCount + 4];
-        memcpy(msaaAtt, att, sizeof(att));
-        SkASSERT(None == msaaAtt[kAttCount - 1]);
-        msaaAtt[kAttCount - 1] = GLX_SAMPLE_BUFFERS_ARB;
-        msaaAtt[kAttCount + 0] = 1;
-        msaaAtt[kAttCount + 1] = GLX_SAMPLES_ARB;
-        msaaAtt[kAttCount + 2] = params->fMSAASampleCount;
-        msaaAtt[kAttCount + 3] = None;
-        fVisualInfo = glXChooseVisual(display, DefaultScreen(display), msaaAtt);
-        fMSAASampleCount = params->fMSAASampleCount;
-    }
-    if (nullptr == fVisualInfo) {
-        fVisualInfo = glXChooseVisual(display, DefaultScreen(display), att);
-        fMSAASampleCount = 0;
-    }
+    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
+    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0);
+    SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
+
+    SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
+    SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
+    SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
+    SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
+    SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 0);
+    SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8);
+
+    SDL_GL_SetAttribute(SDL_GL_ACCELERATED_VISUAL, 1);
 
-    if (fVisualInfo) {
-        Colormap colorMap = XCreateColormap(display,
-                                            RootWindow(display, fVisualInfo->screen),
-                                            fVisualInfo->visual,
-                                            AllocNone);
-        XSetWindowAttributes swa;
-        swa.colormap = colorMap;
-        swa.event_mask = kEventMask;
-        fWindow = XCreateWindow(display,
-                                RootWindow(display, fVisualInfo->screen),
-                                0, 0, // x, y
-                                fWidth, fHeight,
-                                0, // border width
-                                fVisualInfo->depth,
-                                InputOutput,
-                                fVisualInfo->visual,
-                                CWEventMask | CWColormap,
-                                &swa);
+    if (params && params->fMSAASampleCount > 0) {
+        SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1);
+        SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, params->fMSAASampleCount);
     } else {
-        // Create a simple window instead.  We will not be able to show GL
-        fWindow = XCreateSimpleWindow(display,
-                                      DefaultRootWindow(display),
-                                      0, 0,  // x, y
-                                      fWidth, fHeight,
-                                      0,     // border width
-                                      0,     // border value
-                                      0);    // background value
-        XSelectInput(display, fWindow, kEventMask);
+        SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 0);
     }
+    // TODO: handle other display params
+
+    uint32_t windowFlags = SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE;
+    fWindow = SDL_CreateWindow("SDL Window", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
+                               fWidth, fHeight, windowFlags);
 
     if (!fWindow) {
         return false;
     }
 
-    // set up to catch window delete message
-    fWmDeleteMessage = XInternAtom(display, "WM_DELETE_WINDOW", False);
-    XSetWMProtocols(display, fWindow, &fWmDeleteMessage, 1);
-
     // add to hashtable of windows
+    fWindowID = SDL_GetWindowID(fWindow);
     gWindowMap.add(this);
 
-    // init event variables
-    fPendingPaint = false;
-    fPendingResize = false;
-
     return true;
 }
 
 void Window_mac::closeWindow() {
-    if (fDisplay) {
-        this->detach();
-        SkASSERT(fGC);
-        XFreeGC(fDisplay, fGC);
-        fGC = nullptr;
-        gWindowMap.remove(fWindow);
-        XDestroyWindow(fDisplay, fWindow);
-        fWindow = 0;
-        fVisualInfo = nullptr;
-        fDisplay = nullptr;
-        fMSAASampleCount = 0;
+    if (fWindow) {
+        gWindowMap.remove(fWindowID);
+        SDL_DestroyWindow(fWindow);
+        fWindowID = 0;
+        fWindow = nullptr;
     }
 }
 
-static Window::Key get_key(KeySym keysym) {
+static Window::Key get_key(const SDL_Keysym& keysym) {
     static const struct {
-        KeySym      fXK;
+        SDL_Keycode fSDLK;
         Window::Key fKey;
     } gPair[] = {
-        { XK_BackSpace, Window::Key::kBack },
-        { XK_Clear, Window::Key::kBack },
-        { XK_Return, Window::Key::kOK },
-        { XK_Up, Window::Key::kUp },
-        { XK_Down, Window::Key::kDown },
-        { XK_Left, Window::Key::kLeft },
-        { XK_Right, Window::Key::kRight }
+        { SDLK_BACKSPACE, Window::Key::kBack },
+        { SDLK_CLEAR, Window::Key::kBack },
+        { SDLK_RETURN, Window::Key::kOK },
+        { SDLK_UP, Window::Key::kUp },
+        { SDLK_DOWN, Window::Key::kDown },
+        { SDLK_LEFT, Window::Key::kLeft },
+        { SDLK_RIGHT, Window::Key::kRight }
     };
     for (size_t i = 0; i < SK_ARRAY_COUNT(gPair); i++) {
-        if (gPair[i].fXK == keysym) {
+        if (gPair[i].fSDLK == keysym.sym) {
             return gPair[i].fKey;
         }
     }
     return Window::Key::kNONE;
 }
 
-static uint32_t get_modifiers(const XEvent& event) {
+static uint32_t get_modifiers(const SDL_Event& event) {
     static const struct {
-        unsigned    fXMask;
+        unsigned    fSDLMask;
         unsigned    fSkMask;
     } gModifiers[] = {
-        { ShiftMask,   Window::kShift_ModifierKey },
-        { ControlMask, Window::kControl_ModifierKey },
-        { Mod1Mask,    Window::kOption_ModifierKey },
+        { KMOD_SHIFT, Window::kShift_ModifierKey },
+        { KMOD_CTRL,  Window::kControl_ModifierKey },
+        { KMOD_ALT,   Window::kOption_ModifierKey },
     };
 
     auto modifiers = 0;
-    for (size_t i = 0; i < SK_ARRAY_COUNT(gModifiers); ++i) {
-        if (event.xkey.state & gModifiers[i].fXMask) {
-            modifiers |= gModifiers[i].fSkMask;
+
+    switch (event.type) {
+        case SDL_KEYDOWN:
+            // fall through
+        case SDL_KEYUP: {
+            for (size_t i = 0; i < SK_ARRAY_COUNT(gModifiers); ++i) {
+                if (event.key.keysym.mod & gModifiers[i].fSDLMask) {
+                    modifiers |= gModifiers[i].fSkMask;
+                }
+            }
+            if (0 == event.key.repeat) {
+                modifiers |= Window::kFirstPress_ModifierKey;
+            }
+            break;
+        }
+
+        default: {
+            SDL_Keymod mod = SDL_GetModState();
+            for (size_t i = 0; i < SK_ARRAY_COUNT(gModifiers); ++i) {
+                if (mod & gModifiers[i].fSDLMask) {
+                    modifiers |= gModifiers[i].fSkMask;
+                }
+            }
+            break;
         }
     }
     return modifiers;
 }
 
-bool Window_mac::handleEvent(const XEvent& event) {
-    switch (event.type) {
-        case MapNotify:
-            if (!fGC) {
-                fGC = XCreateGC(fDisplay, fWindow, 0, nullptr);
-            }
-            break;
+bool Window_mac::HandleWindowEvent(const SDL_Event& event) {
+    Window_mac* win = gWindowMap.find(event.window.windowID);
+    if (win && win->handleEvent(event)) {
+        return true;
+    }
 
-        case ClientMessage:
-            if ((Atom)event.xclient.data.l[0] == fWmDeleteMessage &&
-                gWindowMap.count() == 1) {
-                return true;
+    return false;
+}
+
+bool Window_mac::handleEvent(const SDL_Event& event) {
+    switch (event.type) {
+        case SDL_WINDOWEVENT:
+            if (SDL_WINDOWEVENT_EXPOSED == event.window.event) {
+                this->onPaint();
+            } else if (SDL_WINDOWEVENT_RESIZED == event.window.event) {
+                this->onResize(event.window.data1, event.window.data2);
             }
             break;
 
-        case ButtonPress:
-            if (event.xbutton.button == Button1) {
-                this->onMouse(event.xbutton.x, event.xbutton.y,
+        case SDL_MOUSEBUTTONDOWN:
+            if (event.button.button == SDL_BUTTON_LEFT) {
+                this->onMouse(event.button.x, event.button.y,
                               Window::kDown_InputState, get_modifiers(event));
             }
             break;
 
-        case ButtonRelease:
-            if (event.xbutton.button == Button1) {
-                this->onMouse(event.xbutton.x, event.xbutton.y,
+        case SDL_MOUSEBUTTONUP:
+            if (event.button.button == SDL_BUTTON_LEFT) {
+                this->onMouse(event.button.x, event.button.y,
                               Window::kUp_InputState, get_modifiers(event));
             }
             break;
 
-        case MotionNotify:
+        case SDL_MOUSEMOTION:
             // only track if left button is down
-            if (event.xmotion.state & Button1Mask) {
-                this->onMouse(event.xmotion.x, event.xmotion.y, 
+            if (event.motion.state & SDL_BUTTON_LMASK) {
+                this->onMouse(event.motion.x, event.motion.y,
                               Window::kMove_InputState, get_modifiers(event));
             }
             break;
 
-        case KeyPress: {
-            int shiftLevel = (event.xkey.state & ShiftMask) ? 1 : 0;
-            KeySym keysym = XkbKeycodeToKeysym(fDisplay, event.xkey.keycode,
-                                               0, shiftLevel);
-            if (keysym == XK_Escape) {
+        case SDL_KEYDOWN: {
+            if (event.key.keysym.sym == SDLK_ESCAPE) {
                 return true;
             }
-            Window::Key key = get_key(keysym);
+            Window::Key key = get_key(event.key.keysym);
             if (key != Window::Key::kNONE) {
-                (void) this->onKey(key, Window::kDown_InputState, 
+                (void) this->onKey(key, Window::kDown_InputState,
                                    get_modifiers(event));
             } else {
-                long uni = keysym2ucs(keysym);
-                if (uni != -1) {
-                    (void) this->onChar((SkUnichar) uni, 
-                                        get_modifiers(event));
-                }
+                (void) this->onChar((SkUnichar) event.key.keysym.sym,
+                                    get_modifiers(event));
             }
         } break;
 
-        case KeyRelease: {
-            int shiftLevel = (event.xkey.state & ShiftMask) ? 1 : 0;
-            KeySym keysym = XkbKeycodeToKeysym(fDisplay, event.xkey.keycode,
-                                               0, shiftLevel);
-            Window::Key key = get_key(keysym);
-            (void) this->onKey(key, Window::kUp_InputState, 
-                               get_modifiers(event));
+        case SDL_KEYUP: {
+            Window::Key key = get_key(event.key.keysym);
+            if (key != Window::Key::kNONE) {
+                (void) this->onKey(key, Window::kUp_InputState,
+                                   get_modifiers(event));
+            }
         } break;
-        
 
         default:
-            // these events should be handled in the main event loop
-            SkASSERT(event.type != Expose && event.type != ConfigureNotify);
             break;
     }
 
@@ -258,29 +213,19 @@ bool Window_mac::handleEvent(const XEvent& event) {
 }
 
 void Window_mac::setTitle(const char* title) {
-    XTextProperty textproperty;
-    XStringListToTextProperty(const_cast<char**>(&title), 1, &textproperty);
-    XSetWMName(fDisplay, fWindow, &textproperty);    
+    SDL_SetWindowTitle(fWindow, title);
 }
 
 void Window_mac::show() {
-    XMapWindow(fDisplay, fWindow);
+    SDL_ShowWindow(fWindow);
 }
 
 bool Window_mac::attach(BackendType attachType, const DisplayParams& params) {
-    this->initWindow(fDisplay, &params);
+    this->initWindow(&params);
 
-    MacWindowInfo info;
-#if 0
-    // Init Mac window info here
-    info.foo = foo;
-#endif
+    window_context_factory::MacWindowInfo info;
+    info.fWindow = fWindow;
     switch (attachType) {
-#ifdef SK_VULKAN
-        case kVulkan_BackendType:
-            fWindowContext = NewVulkanForMac(info, params);
-            break;
-#endif
         case kNativeGL_BackendType:
         default:
             fWindowContext = NewGLForMac(info, params);
@@ -291,19 +236,11 @@ bool Window_mac::attach(BackendType attachType, const DisplayParams& params) {
 }
 
 void Window_mac::onInval() {
-    XEvent event;
-    event.type = Expose;
-    event.xexpose.send_event = True;
-    event.xexpose.display = fDisplay;
-    event.xexpose.window = fWindow;
-    event.xexpose.x = 0;
-    event.xexpose.y = 0;
-    event.xexpose.width = fWidth;
-    event.xexpose.height = fHeight;
-    event.xexpose.count = 0;
-    
-    XSendEvent(fDisplay, fWindow, False, 0, &event);
+    SDL_Event sdlevent;
+    sdlevent.type = SDL_WINDOWEVENT;
+    sdlevent.window.windowID = fWindowID;
+    sdlevent.window.event = SDL_WINDOWEVENT_EXPOSED;
+    SDL_PushEvent(&sdlevent);
 }
-#endif
-    
+
 }   // namespace sk_app
index a5f2aa1..19211ae 100644 (file)
 #include "SkChecksum.h"
 #include "SkTDynamicHash.h"
 
+#include "SDL.h"
+
 namespace sk_app {
 
 class Window_mac : public Window {
 public:
-    Window_mac() : Window()
-#if 0
-                  // TODO: use Mac-specific objects
-                  , fDisplay(nullptr)
-                  , fWindow(0)
-                  , fGC(nullptr)
-                  , fVisualInfo(nullptr)
-#endif
-                  , fMSAASampleCount(0) {}
+    Window_mac()
+        : INHERITED()
+        , fWindow(nullptr)
+        , fWindowID(0)
+        , fMSAASampleCount(0) {}
     ~Window_mac() override { this->closeWindow(); }
 
-#if 0
-    // TODO: need to init with Mac-specific data
-    bool initWindow(Display* display, const DisplayParams* params);
-#endif
+    bool initWindow(const DisplayParams* params);
 
     void setTitle(const char*) override;
     void show() override;
@@ -39,18 +34,29 @@ public:
 
     void onInval() override;
 
+    static bool HandleWindowEvent(const SDL_Event& event);
+
+    static const Uint32& GetKey(const Window_mac& w) {
+        return w.fWindowID;
+    }
+
+    static uint32_t Hash(const Uint32& winID) {
+        return winID;
+    }
+
 private:
+    bool handleEvent(const SDL_Event& event);
+
     void closeWindow();
 
-#if 0
-    // TODO: use Mac-specific window data
-    Display*     fDisplay;
-    XWindow      fWindow;
-    GC           fGC;
-    XVisualInfo* fVisualInfo;
-#endif
+    static SkTDynamicHash<Window_mac, Uint32> gWindowMap;
+
+    SDL_Window*  fWindow;
+    Uint32       fWindowID;
     
     int          fMSAASampleCount;
+    
+    typedef Window INHERITED;
 };
 
 }   // namespace sk_app
index 94bc99e..c7040b5 100644 (file)
@@ -7,69 +7,50 @@
 
 #include "SkTypes.h"
 #include "SkTHash.h"
+#include "SDL.h"
 #include "Timer.h"
 #include "Window_mac.h"
 #include "../Application.h"
 
 using sk_app::Application;
 
-int main(int argc, char**argv) {
-#if 0 
-    // TODO: use Mac main loop
-
-    Display* display = XOpenDisplay(nullptr);
-
-    Application* app = Application::Create(argc, argv, (void*)display);
+int main(int argc, char* argv[]) {
+    if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS) != 0) {
+        SkDebugf("Could not initialize SDL!\n");
+        return 1;
+    }
 
-    // Get the file descriptor for the X display
-    int x11_fd = ConnectionNumber(display);
-    fd_set in_fds;
+    Application* app = Application::Create(argc, argv, nullptr);
 
-    SkTHashSet<sk_app::Window_mac*> pendingWindows;
+    SDL_Event event;
     bool done = false;
     while (!done) {
-        // Create a file description set containing x11_fd
-        FD_ZERO(&in_fds);
-        FD_SET(x11_fd, &in_fds);
-
-        // Set a sleep timer
-        struct timeval tv;
-        tv.tv_usec = 100;
-        tv.tv_sec = 0;
-
-        // Wait for an event on the file descriptor or for timer expiration
-        (void) select(1, &in_fds, NULL, NULL, &tv);
-
-        // Handle XEvents (if any) and flush the input 
-        XEvent event;
-        while (XPending(display) && !done) {
-            XNextEvent(display, &event);
-
-            sk_app::Window_mac* win = sk_app::Window_mac::gWindowMap.find(event.xany.window);
-            // paint and resize events get collapsed
+        while (SDL_PollEvent(&event)) {
             switch (event.type) {
-            case Expose: 
-                win->markPendingPaint();
-                pendingWindows.add(win);
-                break;
-            case ConfigureNotify:
-                win->markPendingResize(event.xconfigurerequest.width,
-                                       event.xconfigurerequest.height);
-                pendingWindows.add(win);
-                break;
-            default:
-                if (win->handleEvent(event)) {
+                // events handled by the windows
+                case SDL_WINDOWEVENT:
+                case SDL_MOUSEMOTION:
+                case SDL_MOUSEBUTTONDOWN:
+                case SDL_MOUSEBUTTONUP:
+                case SDL_KEYDOWN:
+                case SDL_KEYUP:
+                    done = sk_app::Window_mac::HandleWindowEvent(event);
+                    break;
+                    
+                case SDL_QUIT:
                     done = true;
-                }
-                break;
-            } 
+                    break;
+                    
+                default:
+                    break;
+            }
         }
-    }
 
+        app->onIdle();
+    }
     delete app;
 
-    XCloseDisplay(display);
-#endif
-    
+    SDL_Quit();
+
     return 0;
 }