gfx/skia/trunk/src/views/unix/SkOSWindow_Unix.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/gfx/skia/trunk/src/views/unix/SkOSWindow_Unix.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,430 @@
     1.4 +
     1.5 +/*
     1.6 + * Copyright 2011 Google Inc.
     1.7 + *
     1.8 + * Use of this source code is governed by a BSD-style license that can be
     1.9 + * found in the LICENSE file.
    1.10 + */
    1.11 +#include <X11/Xlib.h>
    1.12 +#include <X11/Xatom.h>
    1.13 +#include <X11/XKBlib.h>
    1.14 +#include <GL/glx.h>
    1.15 +#include <GL/gl.h>
    1.16 +#include <GL/glu.h>
    1.17 +
    1.18 +#include "SkWindow.h"
    1.19 +
    1.20 +#include "SkBitmap.h"
    1.21 +#include "SkCanvas.h"
    1.22 +#include "SkColor.h"
    1.23 +#include "SkEvent.h"
    1.24 +#include "SkKey.h"
    1.25 +#include "SkWindow.h"
    1.26 +#include "XkeysToSkKeys.h"
    1.27 +extern "C" {
    1.28 +    #include "keysym2ucs.h"
    1.29 +}
    1.30 +
    1.31 +const int WIDTH = 500;
    1.32 +const int HEIGHT = 500;
    1.33 +
    1.34 +// Determine which events to listen for.
    1.35 +const long EVENT_MASK = StructureNotifyMask|ButtonPressMask|ButtonReleaseMask
    1.36 +        |ExposureMask|PointerMotionMask|KeyPressMask|KeyReleaseMask;
    1.37 +
    1.38 +SkOSWindow::SkOSWindow(void*)
    1.39 +    : fVi(NULL)
    1.40 +    , fMSAASampleCount(0) {
    1.41 +    fUnixWindow.fDisplay = NULL;
    1.42 +    fUnixWindow.fGLContext = NULL;
    1.43 +    this->initWindow(0, NULL);
    1.44 +    this->resize(WIDTH, HEIGHT);
    1.45 +}
    1.46 +
    1.47 +SkOSWindow::~SkOSWindow() {
    1.48 +    this->closeWindow();
    1.49 +}
    1.50 +
    1.51 +void SkOSWindow::closeWindow() {
    1.52 +    if (NULL != fUnixWindow.fDisplay) {
    1.53 +        this->detach();
    1.54 +        SkASSERT(NULL != fUnixWindow.fGc);
    1.55 +        XFreeGC(fUnixWindow.fDisplay, fUnixWindow.fGc);
    1.56 +        fUnixWindow.fGc = NULL;
    1.57 +        XDestroyWindow(fUnixWindow.fDisplay, fUnixWindow.fWin);
    1.58 +        fVi = NULL;
    1.59 +        XCloseDisplay(fUnixWindow.fDisplay);
    1.60 +        fUnixWindow.fDisplay = NULL;
    1.61 +        fMSAASampleCount = 0;
    1.62 +    }
    1.63 +}
    1.64 +
    1.65 +void SkOSWindow::initWindow(int requestedMSAASampleCount, AttachmentInfo* info) {
    1.66 +    if (fMSAASampleCount != requestedMSAASampleCount) {
    1.67 +        this->closeWindow();
    1.68 +    }
    1.69 +    // presence of fDisplay means we already have a window
    1.70 +    if (NULL != fUnixWindow.fDisplay) {
    1.71 +        if (NULL != info) {
    1.72 +            if (NULL != fVi) {
    1.73 +                glXGetConfig(fUnixWindow.fDisplay, fVi, GLX_SAMPLES_ARB, &info->fSampleCount);
    1.74 +                glXGetConfig(fUnixWindow.fDisplay, fVi, GLX_STENCIL_SIZE, &info->fStencilBits);
    1.75 +            } else {
    1.76 +                info->fSampleCount = 0;
    1.77 +                info->fStencilBits = 0;
    1.78 +            }
    1.79 +        }
    1.80 +        return;
    1.81 +    }
    1.82 +    fUnixWindow.fDisplay = XOpenDisplay(NULL);
    1.83 +    Display* dsp = fUnixWindow.fDisplay;
    1.84 +    if (NULL == dsp) {
    1.85 +        SkDebugf("Could not open an X Display");
    1.86 +        return;
    1.87 +    }
    1.88 +    // Attempt to create a window that supports GL
    1.89 +    GLint att[] = {
    1.90 +        GLX_RGBA,
    1.91 +        GLX_DEPTH_SIZE, 24,
    1.92 +        GLX_DOUBLEBUFFER,
    1.93 +        GLX_STENCIL_SIZE, 8,
    1.94 +        None
    1.95 +    };
    1.96 +    SkASSERT(NULL == fVi);
    1.97 +    if (requestedMSAASampleCount > 0) {
    1.98 +        static const GLint kAttCount = SK_ARRAY_COUNT(att);
    1.99 +        GLint msaaAtt[kAttCount + 4];
   1.100 +        memcpy(msaaAtt, att, sizeof(att));
   1.101 +        SkASSERT(None == msaaAtt[kAttCount - 1]);
   1.102 +        msaaAtt[kAttCount - 1] = GLX_SAMPLE_BUFFERS_ARB;
   1.103 +        msaaAtt[kAttCount + 0] = 1;
   1.104 +        msaaAtt[kAttCount + 1] = GLX_SAMPLES_ARB;
   1.105 +        msaaAtt[kAttCount + 2] = requestedMSAASampleCount;
   1.106 +        msaaAtt[kAttCount + 3] = None;
   1.107 +        fVi = glXChooseVisual(dsp, DefaultScreen(dsp), msaaAtt);
   1.108 +        fMSAASampleCount = requestedMSAASampleCount;
   1.109 +    }
   1.110 +    if (NULL == fVi) {
   1.111 +        fVi = glXChooseVisual(dsp, DefaultScreen(dsp), att);
   1.112 +        fMSAASampleCount = 0;
   1.113 +    }
   1.114 +
   1.115 +    if (fVi) {
   1.116 +        if (NULL != info) {
   1.117 +            glXGetConfig(dsp, fVi, GLX_SAMPLES_ARB, &info->fSampleCount);
   1.118 +            glXGetConfig(dsp, fVi, GLX_STENCIL_SIZE, &info->fStencilBits);
   1.119 +        }
   1.120 +        Colormap colorMap = XCreateColormap(dsp,
   1.121 +                                            RootWindow(dsp, fVi->screen),
   1.122 +                                            fVi->visual,
   1.123 +                                             AllocNone);
   1.124 +        XSetWindowAttributes swa;
   1.125 +        swa.colormap = colorMap;
   1.126 +        swa.event_mask = EVENT_MASK;
   1.127 +        fUnixWindow.fWin = XCreateWindow(dsp,
   1.128 +                                         RootWindow(dsp, fVi->screen),
   1.129 +                                         0, 0, // x, y
   1.130 +                                         WIDTH, HEIGHT,
   1.131 +                                         0, // border width
   1.132 +                                         fVi->depth,
   1.133 +                                         InputOutput,
   1.134 +                                         fVi->visual,
   1.135 +                                         CWEventMask | CWColormap,
   1.136 +                                         &swa);
   1.137 +    } else {
   1.138 +        if (NULL != info) {
   1.139 +            info->fSampleCount = 0;
   1.140 +            info->fStencilBits = 0;
   1.141 +        }
   1.142 +        // Create a simple window instead.  We will not be able to show GL
   1.143 +        fUnixWindow.fWin = XCreateSimpleWindow(dsp,
   1.144 +                                               DefaultRootWindow(dsp),
   1.145 +                                               0, 0,  // x, y
   1.146 +                                               WIDTH, HEIGHT,
   1.147 +                                               0,     // border width
   1.148 +                                               0,     // border value
   1.149 +                                               0);    // background value
   1.150 +    }
   1.151 +    this->mapWindowAndWait();
   1.152 +    fUnixWindow.fGc = XCreateGC(dsp, fUnixWindow.fWin, 0, NULL);
   1.153 +}
   1.154 +
   1.155 +static unsigned getModi(const XEvent& evt) {
   1.156 +    static const struct {
   1.157 +        unsigned    fXMask;
   1.158 +        unsigned    fSkMask;
   1.159 +    } gModi[] = {
   1.160 +        // X values found by experiment. Is there a better way?
   1.161 +        { 1,    kShift_SkModifierKey },
   1.162 +        { 4,    kControl_SkModifierKey },
   1.163 +        { 8,    kOption_SkModifierKey },
   1.164 +    };
   1.165 +
   1.166 +    unsigned modi = 0;
   1.167 +    for (size_t i = 0; i < SK_ARRAY_COUNT(gModi); ++i) {
   1.168 +        if (evt.xkey.state & gModi[i].fXMask) {
   1.169 +            modi |= gModi[i].fSkMask;
   1.170 +        }
   1.171 +    }
   1.172 +    return modi;
   1.173 +}
   1.174 +
   1.175 +static SkMSec gTimerDelay;
   1.176 +
   1.177 +static bool MyXNextEventWithDelay(Display* dsp, XEvent* evt) {
   1.178 +    // Check for pending events before entering the select loop. There might
   1.179 +    // be events in the in-memory queue but not processed yet.
   1.180 +    if (XPending(dsp)) {
   1.181 +        XNextEvent(dsp, evt);
   1.182 +        return true;
   1.183 +    }
   1.184 +
   1.185 +    SkMSec ms = gTimerDelay;
   1.186 +    if (ms > 0) {
   1.187 +        int x11_fd = ConnectionNumber(dsp);
   1.188 +        fd_set input_fds;
   1.189 +        FD_ZERO(&input_fds);
   1.190 +        FD_SET(x11_fd, &input_fds);
   1.191 +
   1.192 +        timeval tv;
   1.193 +        tv.tv_sec = ms / 1000;              // seconds
   1.194 +        tv.tv_usec = (ms % 1000) * 1000;    // microseconds
   1.195 +
   1.196 +        if (!select(x11_fd + 1, &input_fds, NULL, NULL, &tv)) {
   1.197 +            if (!XPending(dsp)) {
   1.198 +                return false;
   1.199 +            }
   1.200 +        }
   1.201 +    }
   1.202 +    XNextEvent(dsp, evt);
   1.203 +    return true;
   1.204 +}
   1.205 +
   1.206 +SkOSWindow::NextXEventResult SkOSWindow::nextXEvent() {
   1.207 +    XEvent evt;
   1.208 +    Display* dsp = fUnixWindow.fDisplay;
   1.209 +
   1.210 +    if (!MyXNextEventWithDelay(fUnixWindow.fDisplay, &evt)) {
   1.211 +        return kContinue_NextXEventResult;
   1.212 +    }
   1.213 +
   1.214 +    switch (evt.type) {
   1.215 +        case Expose:
   1.216 +            if (0 == evt.xexpose.count) {
   1.217 +                return kPaintRequest_NextXEventResult;
   1.218 +            }
   1.219 +            break;
   1.220 +        case ConfigureNotify:
   1.221 +            this->resize(evt.xconfigure.width, evt.xconfigure.height);
   1.222 +            break;
   1.223 +        case ButtonPress:
   1.224 +            if (evt.xbutton.button == Button1)
   1.225 +                this->handleClick(evt.xbutton.x, evt.xbutton.y,
   1.226 +                            SkView::Click::kDown_State, NULL, getModi(evt));
   1.227 +            break;
   1.228 +        case ButtonRelease:
   1.229 +            if (evt.xbutton.button == Button1)
   1.230 +                this->handleClick(evt.xbutton.x, evt.xbutton.y,
   1.231 +                              SkView::Click::kUp_State, NULL, getModi(evt));
   1.232 +            break;
   1.233 +        case MotionNotify:
   1.234 +            this->handleClick(evt.xmotion.x, evt.xmotion.y,
   1.235 +                           SkView::Click::kMoved_State, NULL, getModi(evt));
   1.236 +            break;
   1.237 +        case KeyPress: {
   1.238 +            int shiftLevel = (evt.xkey.state & ShiftMask) ? 1 : 0;
   1.239 +            KeySym keysym = XkbKeycodeToKeysym(dsp, evt.xkey.keycode,
   1.240 +                                               0, shiftLevel);
   1.241 +            if (keysym == XK_Escape) {
   1.242 +                return kQuitRequest_NextXEventResult;
   1.243 +            }
   1.244 +            this->handleKey(XKeyToSkKey(keysym));
   1.245 +            long uni = keysym2ucs(keysym);
   1.246 +            if (uni != -1) {
   1.247 +                this->handleChar((SkUnichar) uni);
   1.248 +            }
   1.249 +            break;
   1.250 +        }
   1.251 +        case KeyRelease:
   1.252 +            this->handleKeyUp(XKeyToSkKey(XkbKeycodeToKeysym(dsp, evt.xkey.keycode, 0, 0)));
   1.253 +            break;
   1.254 +        default:
   1.255 +            // Do nothing for other events
   1.256 +            break;
   1.257 +    }
   1.258 +    return kContinue_NextXEventResult;
   1.259 +}
   1.260 +
   1.261 +void SkOSWindow::loop() {
   1.262 +    Display* dsp = fUnixWindow.fDisplay;
   1.263 +    if (NULL == dsp) {
   1.264 +        return;
   1.265 +    }
   1.266 +    Window win = fUnixWindow.fWin;
   1.267 +
   1.268 +    XSelectInput(dsp, win, EVENT_MASK);
   1.269 +
   1.270 +    bool sentExposeEvent = false;
   1.271 +
   1.272 +    for (;;) {
   1.273 +        SkEvent::ServiceQueueTimer();
   1.274 +
   1.275 +        bool moreToDo = SkEvent::ProcessEvent();
   1.276 +
   1.277 +        if (this->isDirty() && !sentExposeEvent) {
   1.278 +            sentExposeEvent = true;
   1.279 +
   1.280 +            XEvent evt;
   1.281 +            sk_bzero(&evt, sizeof(evt));
   1.282 +            evt.type = Expose;
   1.283 +            evt.xexpose.display = dsp;
   1.284 +            XSendEvent(dsp, win, false, ExposureMask, &evt);
   1.285 +        }
   1.286 +
   1.287 +        if (XPending(dsp) || !moreToDo) {
   1.288 +            switch (this->nextXEvent()) {
   1.289 +                case kContinue_NextXEventResult:
   1.290 +                    break;
   1.291 +                case kPaintRequest_NextXEventResult:
   1.292 +                    sentExposeEvent = false;
   1.293 +                    if (this->isDirty()) {
   1.294 +                        this->update(NULL);
   1.295 +                    }
   1.296 +                    this->doPaint();
   1.297 +                    break;
   1.298 +                case kQuitRequest_NextXEventResult:
   1.299 +                    return;
   1.300 +            }
   1.301 +        }
   1.302 +    }
   1.303 +}
   1.304 +
   1.305 +void SkOSWindow::mapWindowAndWait() {
   1.306 +    SkASSERT(NULL != fUnixWindow.fDisplay);
   1.307 +    Display* dsp = fUnixWindow.fDisplay;
   1.308 +    Window win = fUnixWindow.fWin;
   1.309 +    XMapWindow(dsp, win);
   1.310 +
   1.311 +    long eventMask = StructureNotifyMask;
   1.312 +    XSelectInput(dsp, win, eventMask);
   1.313 +
   1.314 +    // Wait until screen is ready.
   1.315 +    XEvent evt;
   1.316 +    do {
   1.317 +        XNextEvent(dsp, &evt);
   1.318 +    } while(evt.type != MapNotify);
   1.319 +
   1.320 +}
   1.321 +
   1.322 +bool SkOSWindow::attach(SkBackEndTypes, int msaaSampleCount, AttachmentInfo* info) {
   1.323 +    this->initWindow(msaaSampleCount, info);
   1.324 +
   1.325 +    if (NULL == fUnixWindow.fDisplay) {
   1.326 +        return false;
   1.327 +    }
   1.328 +    if (NULL == fUnixWindow.fGLContext) {
   1.329 +        SkASSERT(NULL != fVi);
   1.330 +
   1.331 +        fUnixWindow.fGLContext = glXCreateContext(fUnixWindow.fDisplay,
   1.332 +                                                  fVi,
   1.333 +                                                  NULL,
   1.334 +                                                  GL_TRUE);
   1.335 +        if (NULL == fUnixWindow.fGLContext) {
   1.336 +            return false;
   1.337 +        }
   1.338 +    }
   1.339 +    glXMakeCurrent(fUnixWindow.fDisplay,
   1.340 +                   fUnixWindow.fWin,
   1.341 +                   fUnixWindow.fGLContext);
   1.342 +    glViewport(0, 0,
   1.343 +               SkScalarRoundToInt(this->width()),
   1.344 +               SkScalarRoundToInt(this->height()));
   1.345 +    glClearColor(0, 0, 0, 0);
   1.346 +    glClearStencil(0);
   1.347 +    glClear(GL_COLOR_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
   1.348 +    return true;
   1.349 +}
   1.350 +
   1.351 +void SkOSWindow::detach() {
   1.352 +    if (NULL == fUnixWindow.fDisplay || NULL == fUnixWindow.fGLContext) {
   1.353 +        return;
   1.354 +    }
   1.355 +    glXMakeCurrent(fUnixWindow.fDisplay, None, NULL);
   1.356 +    glXDestroyContext(fUnixWindow.fDisplay, fUnixWindow.fGLContext);
   1.357 +    fUnixWindow.fGLContext = NULL;
   1.358 +}
   1.359 +
   1.360 +void SkOSWindow::present() {
   1.361 +    if (NULL != fUnixWindow.fDisplay && NULL != fUnixWindow.fGLContext) {
   1.362 +        glXSwapBuffers(fUnixWindow.fDisplay, fUnixWindow.fWin);
   1.363 +    }
   1.364 +}
   1.365 +
   1.366 +void SkOSWindow::onSetTitle(const char title[]) {
   1.367 +    if (NULL == fUnixWindow.fDisplay) {
   1.368 +        return;
   1.369 +    }
   1.370 +    XTextProperty textProp;
   1.371 +    textProp.value = (unsigned char*)title;
   1.372 +    textProp.format = 8;
   1.373 +    textProp.nitems = strlen((char*)textProp.value);
   1.374 +    textProp.encoding = XA_STRING;
   1.375 +    XSetWMName(fUnixWindow.fDisplay, fUnixWindow.fWin, &textProp);
   1.376 +}
   1.377 +
   1.378 +static bool convertBitmapToXImage(XImage& image, const SkBitmap& bitmap) {
   1.379 +    sk_bzero(&image, sizeof(image));
   1.380 +
   1.381 +    int bitsPerPixel = bitmap.bytesPerPixel() * 8;
   1.382 +    image.width = bitmap.width();
   1.383 +    image.height = bitmap.height();
   1.384 +    image.format = ZPixmap;
   1.385 +    image.data = (char*) bitmap.getPixels();
   1.386 +    image.byte_order = LSBFirst;
   1.387 +    image.bitmap_unit = bitsPerPixel;
   1.388 +    image.bitmap_bit_order = LSBFirst;
   1.389 +    image.bitmap_pad = bitsPerPixel;
   1.390 +    image.depth = 24;
   1.391 +    image.bytes_per_line = bitmap.rowBytes() - bitmap.width() * 4;
   1.392 +    image.bits_per_pixel = bitsPerPixel;
   1.393 +    return XInitImage(&image);
   1.394 +}
   1.395 +
   1.396 +void SkOSWindow::doPaint() {
   1.397 +    if (NULL == fUnixWindow.fDisplay) {
   1.398 +        return;
   1.399 +    }
   1.400 +    // If we are drawing with GL, we don't need XPutImage.
   1.401 +    if (NULL != fUnixWindow.fGLContext) {
   1.402 +        return;
   1.403 +    }
   1.404 +    // Draw the bitmap to the screen.
   1.405 +    const SkBitmap& bitmap = getBitmap();
   1.406 +    int width = bitmap.width();
   1.407 +    int height = bitmap.height();
   1.408 +
   1.409 +    XImage image;
   1.410 +    if (!convertBitmapToXImage(image, bitmap)) {
   1.411 +        return;
   1.412 +    }
   1.413 +
   1.414 +    XPutImage(fUnixWindow.fDisplay,
   1.415 +              fUnixWindow.fWin,
   1.416 +              fUnixWindow.fGc,
   1.417 +              &image,
   1.418 +              0, 0,     // src x,y
   1.419 +              0, 0,     // dst x,y
   1.420 +              width, height);
   1.421 +}
   1.422 +
   1.423 +///////////////////////////////////////////////////////////////////////////////
   1.424 +
   1.425 +void SkEvent::SignalNonEmptyQueue() {
   1.426 +    // nothing to do, since we spin on our event-queue, polling for XPending
   1.427 +}
   1.428 +
   1.429 +void SkEvent::SignalQueueTimer(SkMSec delay) {
   1.430 +    // just need to record the delay time. We handle waking up for it in
   1.431 +    // MyXNextEventWithDelay()
   1.432 +    gTimerDelay = delay;
   1.433 +}

mercurial