epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright 2011 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 | */ |
scroggo | b7e9aee | 2011-03-15 15:15:15 +0000 | [diff] [blame] | 8 | #include <X11/Xlib.h> |
| 9 | #include <X11/Xatom.h> |
bsalomon@google.com | 75356a7 | 2012-06-19 13:08:59 +0000 | [diff] [blame] | 10 | #include <X11/XKBlib.h> |
scroggo | b7e9aee | 2011-03-15 15:15:15 +0000 | [diff] [blame] | 11 | #include <GL/glx.h> |
Scroggo | 9df214e | 2011-04-15 14:48:08 +0000 | [diff] [blame] | 12 | #include <GL/gl.h> |
| 13 | #include <GL/glu.h> |
scroggo | b7e9aee | 2011-03-15 15:15:15 +0000 | [diff] [blame] | 14 | |
| 15 | #include "SkWindow.h" |
| 16 | |
| 17 | #include "SkBitmap.h" |
scroggo | 08526c0 | 2011-03-22 14:03:21 +0000 | [diff] [blame] | 18 | #include "SkCanvas.h" |
scroggo | b7e9aee | 2011-03-15 15:15:15 +0000 | [diff] [blame] | 19 | #include "SkColor.h" |
| 20 | #include "SkEvent.h" |
Scroggo | 9df214e | 2011-04-15 14:48:08 +0000 | [diff] [blame] | 21 | #include "SkKey.h" |
| 22 | #include "SkWindow.h" |
| 23 | #include "XkeysToSkKeys.h" |
| 24 | extern "C" { |
| 25 | #include "keysym2ucs.h" |
| 26 | } |
| 27 | |
Scroggo | aed68d9 | 2011-06-08 14:26:00 +0000 | [diff] [blame] | 28 | const int WIDTH = 500; |
| 29 | const int HEIGHT = 500; |
Scroggo | 9df214e | 2011-04-15 14:48:08 +0000 | [diff] [blame] | 30 | |
| 31 | // Determine which events to listen for. |
| 32 | const long EVENT_MASK = StructureNotifyMask|ButtonPressMask|ButtonReleaseMask |
| 33 | |ExposureMask|PointerMotionMask|KeyPressMask|KeyReleaseMask; |
scroggo | b7e9aee | 2011-03-15 15:15:15 +0000 | [diff] [blame] | 34 | |
sugoi@google.com | 93c7ee3 | 2013-03-12 14:36:57 +0000 | [diff] [blame] | 35 | SkOSWindow::SkOSWindow(void*) |
robertphillips@google.com | 4bdfb8c | 2012-06-12 21:23:49 +0000 | [diff] [blame] | 36 | : fVi(NULL) |
bsalomon@google.com | 1195925 | 2012-04-06 20:13:38 +0000 | [diff] [blame] | 37 | , fMSAASampleCount(0) { |
| 38 | fUnixWindow.fDisplay = NULL; |
bsalomon@google.com | 8108c47 | 2012-04-03 19:33:08 +0000 | [diff] [blame] | 39 | fUnixWindow.fGLContext = NULL; |
bsalomon@google.com | 64cc810 | 2013-03-05 20:06:05 +0000 | [diff] [blame] | 40 | this->initWindow(0, NULL); |
Scroggo | 9df214e | 2011-04-15 14:48:08 +0000 | [diff] [blame] | 41 | this->resize(WIDTH, HEIGHT); |
scroggo | b7e9aee | 2011-03-15 15:15:15 +0000 | [diff] [blame] | 42 | } |
| 43 | |
bsalomon@google.com | 8108c47 | 2012-04-03 19:33:08 +0000 | [diff] [blame] | 44 | SkOSWindow::~SkOSWindow() { |
bsalomon@google.com | 1195925 | 2012-04-06 20:13:38 +0000 | [diff] [blame] | 45 | this->closeWindow(); |
| 46 | } |
| 47 | |
| 48 | void SkOSWindow::closeWindow() { |
bsalomon@google.com | 8108c47 | 2012-04-03 19:33:08 +0000 | [diff] [blame] | 49 | if (NULL != fUnixWindow.fDisplay) { |
bsalomon@google.com | 1195925 | 2012-04-06 20:13:38 +0000 | [diff] [blame] | 50 | this->detach(); |
| 51 | SkASSERT(NULL != fUnixWindow.fGc); |
bsalomon@google.com | 8108c47 | 2012-04-03 19:33:08 +0000 | [diff] [blame] | 52 | XFreeGC(fUnixWindow.fDisplay, fUnixWindow.fGc); |
bsalomon@google.com | 1195925 | 2012-04-06 20:13:38 +0000 | [diff] [blame] | 53 | fUnixWindow.fGc = NULL; |
Scroggo | 9df214e | 2011-04-15 14:48:08 +0000 | [diff] [blame] | 54 | XDestroyWindow(fUnixWindow.fDisplay, fUnixWindow.fWin); |
bsalomon@google.com | 1195925 | 2012-04-06 20:13:38 +0000 | [diff] [blame] | 55 | fVi = NULL; |
Scroggo | 9df214e | 2011-04-15 14:48:08 +0000 | [diff] [blame] | 56 | XCloseDisplay(fUnixWindow.fDisplay); |
bsalomon@google.com | 1195925 | 2012-04-06 20:13:38 +0000 | [diff] [blame] | 57 | fUnixWindow.fDisplay = NULL; |
| 58 | fMSAASampleCount = 0; |
Scroggo | 9df214e | 2011-04-15 14:48:08 +0000 | [diff] [blame] | 59 | } |
scroggo | b7e9aee | 2011-03-15 15:15:15 +0000 | [diff] [blame] | 60 | } |
| 61 | |
bsalomon@google.com | 64cc810 | 2013-03-05 20:06:05 +0000 | [diff] [blame] | 62 | void SkOSWindow::initWindow(int requestedMSAASampleCount, AttachmentInfo* info) { |
bsalomon@google.com | 1195925 | 2012-04-06 20:13:38 +0000 | [diff] [blame] | 63 | if (fMSAASampleCount != requestedMSAASampleCount) { |
| 64 | this->closeWindow(); |
| 65 | } |
| 66 | // presence of fDisplay means we already have a window |
| 67 | if (NULL != fUnixWindow.fDisplay) { |
bsalomon@google.com | 64cc810 | 2013-03-05 20:06:05 +0000 | [diff] [blame] | 68 | if (NULL != info) { |
| 69 | if (NULL != fVi) { |
| 70 | glXGetConfig(fUnixWindow.fDisplay, fVi, GLX_SAMPLES_ARB, &info->fSampleCount); |
| 71 | glXGetConfig(fUnixWindow.fDisplay, fVi, GLX_STENCIL_SIZE, &info->fStencilBits); |
| 72 | } else { |
| 73 | info->fSampleCount = 0; |
| 74 | info->fStencilBits = 0; |
| 75 | } |
| 76 | } |
bsalomon@google.com | 1195925 | 2012-04-06 20:13:38 +0000 | [diff] [blame] | 77 | return; |
| 78 | } |
| 79 | fUnixWindow.fDisplay = XOpenDisplay(NULL); |
| 80 | Display* dsp = fUnixWindow.fDisplay; |
| 81 | if (NULL == dsp) { |
| 82 | SkDebugf("Could not open an X Display"); |
| 83 | return; |
| 84 | } |
| 85 | // Attempt to create a window that supports GL |
| 86 | GLint att[] = { |
| 87 | GLX_RGBA, |
| 88 | GLX_DEPTH_SIZE, 24, |
| 89 | GLX_DOUBLEBUFFER, |
| 90 | GLX_STENCIL_SIZE, 8, |
| 91 | None |
| 92 | }; |
| 93 | SkASSERT(NULL == fVi); |
| 94 | if (requestedMSAASampleCount > 0) { |
| 95 | static const GLint kAttCount = SK_ARRAY_COUNT(att); |
| 96 | GLint msaaAtt[kAttCount + 4]; |
| 97 | memcpy(msaaAtt, att, sizeof(att)); |
| 98 | SkASSERT(None == msaaAtt[kAttCount - 1]); |
| 99 | msaaAtt[kAttCount - 1] = GLX_SAMPLE_BUFFERS_ARB; |
| 100 | msaaAtt[kAttCount + 0] = 1; |
| 101 | msaaAtt[kAttCount + 1] = GLX_SAMPLES_ARB; |
| 102 | msaaAtt[kAttCount + 2] = requestedMSAASampleCount; |
| 103 | msaaAtt[kAttCount + 3] = None; |
| 104 | fVi = glXChooseVisual(dsp, DefaultScreen(dsp), msaaAtt); |
| 105 | fMSAASampleCount = requestedMSAASampleCount; |
| 106 | } |
| 107 | if (NULL == fVi) { |
| 108 | fVi = glXChooseVisual(dsp, DefaultScreen(dsp), att); |
| 109 | fMSAASampleCount = 0; |
| 110 | } |
| 111 | |
| 112 | if (fVi) { |
bsalomon@google.com | 64cc810 | 2013-03-05 20:06:05 +0000 | [diff] [blame] | 113 | if (NULL != info) { |
| 114 | glXGetConfig(dsp, fVi, GLX_SAMPLES_ARB, &info->fSampleCount); |
| 115 | glXGetConfig(dsp, fVi, GLX_STENCIL_SIZE, &info->fStencilBits); |
| 116 | } |
bsalomon@google.com | 1195925 | 2012-04-06 20:13:38 +0000 | [diff] [blame] | 117 | Colormap colorMap = XCreateColormap(dsp, |
| 118 | RootWindow(dsp, fVi->screen), |
| 119 | fVi->visual, |
| 120 | AllocNone); |
| 121 | XSetWindowAttributes swa; |
| 122 | swa.colormap = colorMap; |
| 123 | swa.event_mask = EVENT_MASK; |
| 124 | fUnixWindow.fWin = XCreateWindow(dsp, |
| 125 | RootWindow(dsp, fVi->screen), |
| 126 | 0, 0, // x, y |
| 127 | WIDTH, HEIGHT, |
| 128 | 0, // border width |
| 129 | fVi->depth, |
| 130 | InputOutput, |
| 131 | fVi->visual, |
| 132 | CWEventMask | CWColormap, |
| 133 | &swa); |
| 134 | } else { |
bsalomon@google.com | 64cc810 | 2013-03-05 20:06:05 +0000 | [diff] [blame] | 135 | if (NULL != info) { |
| 136 | info->fSampleCount = 0; |
| 137 | info->fStencilBits = 0; |
| 138 | } |
bsalomon@google.com | 1195925 | 2012-04-06 20:13:38 +0000 | [diff] [blame] | 139 | // Create a simple window instead. We will not be able to show GL |
| 140 | fUnixWindow.fWin = XCreateSimpleWindow(dsp, |
| 141 | DefaultRootWindow(dsp), |
| 142 | 0, 0, // x, y |
| 143 | WIDTH, HEIGHT, |
| 144 | 0, // border width |
| 145 | 0, // border value |
| 146 | 0); // background value |
| 147 | } |
| 148 | this->mapWindowAndWait(); |
| 149 | fUnixWindow.fGc = XCreateGC(dsp, fUnixWindow.fWin, 0, NULL); |
| 150 | } |
| 151 | |
reed@google.com | 3792418 | 2013-01-08 16:25:42 +0000 | [diff] [blame] | 152 | static unsigned getModi(const XEvent& evt) { |
reed@google.com | e378d83 | 2013-01-08 16:50:52 +0000 | [diff] [blame] | 153 | static const struct { |
| 154 | unsigned fXMask; |
| 155 | unsigned fSkMask; |
| 156 | } gModi[] = { |
| 157 | // X values found by experiment. Is there a better way? |
| 158 | { 1, kShift_SkModifierKey }, |
| 159 | { 4, kControl_SkModifierKey }, |
| 160 | { 8, kOption_SkModifierKey }, |
| 161 | }; |
skia.committer@gmail.com | 4e73aa1 | 2013-01-09 02:01:30 +0000 | [diff] [blame] | 162 | |
reed@google.com | e378d83 | 2013-01-08 16:50:52 +0000 | [diff] [blame] | 163 | unsigned modi = 0; |
| 164 | for (size_t i = 0; i < SK_ARRAY_COUNT(gModi); ++i) { |
| 165 | if (evt.xkey.state & gModi[i].fXMask) { |
| 166 | modi |= gModi[i].fSkMask; |
| 167 | } |
| 168 | } |
| 169 | return modi; |
reed@google.com | 4d5c26d | 2013-01-08 16:17:50 +0000 | [diff] [blame] | 170 | } |
| 171 | |
reed@google.com | acb3d88 | 2013-06-17 13:42:43 +0000 | [diff] [blame] | 172 | static SkMSec gTimerDelay; |
| 173 | |
reed@google.com | d1f1b67 | 2013-06-19 20:56:45 +0000 | [diff] [blame] | 174 | static bool MyXNextEventWithDelay(Display* dsp, XEvent* evt) { |
commit-bot@chromium.org | 04018f3 | 2014-01-02 22:21:47 +0000 | [diff] [blame] | 175 | // Check for pending events before entering the select loop. There might |
| 176 | // be events in the in-memory queue but not processed yet. |
| 177 | if (XPending(dsp)) { |
| 178 | XNextEvent(dsp, evt); |
| 179 | return true; |
| 180 | } |
| 181 | |
reed@google.com | acb3d88 | 2013-06-17 13:42:43 +0000 | [diff] [blame] | 182 | SkMSec ms = gTimerDelay; |
| 183 | if (ms > 0) { |
| 184 | int x11_fd = ConnectionNumber(dsp); |
| 185 | fd_set input_fds; |
| 186 | FD_ZERO(&input_fds); |
| 187 | FD_SET(x11_fd, &input_fds); |
| 188 | |
| 189 | timeval tv; |
| 190 | tv.tv_sec = ms / 1000; // seconds |
| 191 | tv.tv_usec = (ms % 1000) * 1000; // microseconds |
skia.committer@gmail.com | a48595d | 2013-06-18 07:00:52 +0000 | [diff] [blame] | 192 | |
reed@google.com | d1f1b67 | 2013-06-19 20:56:45 +0000 | [diff] [blame] | 193 | if (!select(x11_fd + 1, &input_fds, NULL, NULL, &tv)) { |
| 194 | if (!XPending(dsp)) { |
| 195 | return false; |
| 196 | } |
| 197 | } |
reed@google.com | acb3d88 | 2013-06-17 13:42:43 +0000 | [diff] [blame] | 198 | } |
reed@google.com | d1f1b67 | 2013-06-19 20:56:45 +0000 | [diff] [blame] | 199 | XNextEvent(dsp, evt); |
| 200 | return true; |
reed@google.com | acb3d88 | 2013-06-17 13:42:43 +0000 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | SkOSWindow::NextXEventResult SkOSWindow::nextXEvent() { |
| 204 | XEvent evt; |
| 205 | Display* dsp = fUnixWindow.fDisplay; |
| 206 | |
reed@google.com | d1f1b67 | 2013-06-19 20:56:45 +0000 | [diff] [blame] | 207 | if (!MyXNextEventWithDelay(fUnixWindow.fDisplay, &evt)) { |
| 208 | return kContinue_NextXEventResult; |
| 209 | } |
reed@google.com | acb3d88 | 2013-06-17 13:42:43 +0000 | [diff] [blame] | 210 | |
| 211 | switch (evt.type) { |
| 212 | case Expose: |
| 213 | if (0 == evt.xexpose.count) { |
| 214 | return kPaintRequest_NextXEventResult; |
| 215 | } |
| 216 | break; |
| 217 | case ConfigureNotify: |
| 218 | this->resize(evt.xconfigure.width, evt.xconfigure.height); |
| 219 | break; |
| 220 | case ButtonPress: |
| 221 | if (evt.xbutton.button == Button1) |
| 222 | this->handleClick(evt.xbutton.x, evt.xbutton.y, |
| 223 | SkView::Click::kDown_State, NULL, getModi(evt)); |
| 224 | break; |
| 225 | case ButtonRelease: |
| 226 | if (evt.xbutton.button == Button1) |
| 227 | this->handleClick(evt.xbutton.x, evt.xbutton.y, |
| 228 | SkView::Click::kUp_State, NULL, getModi(evt)); |
| 229 | break; |
| 230 | case MotionNotify: |
| 231 | this->handleClick(evt.xmotion.x, evt.xmotion.y, |
| 232 | SkView::Click::kMoved_State, NULL, getModi(evt)); |
| 233 | break; |
| 234 | case KeyPress: { |
bungeman@google.com | 5e4fe21 | 2013-06-19 23:06:00 +0000 | [diff] [blame] | 235 | int shiftLevel = (evt.xkey.state & ShiftMask) ? 1 : 0; |
reed@google.com | acb3d88 | 2013-06-17 13:42:43 +0000 | [diff] [blame] | 236 | KeySym keysym = XkbKeycodeToKeysym(dsp, evt.xkey.keycode, |
| 237 | 0, shiftLevel); |
| 238 | if (keysym == XK_Escape) { |
| 239 | return kQuitRequest_NextXEventResult; |
| 240 | } |
| 241 | this->handleKey(XKeyToSkKey(keysym)); |
| 242 | long uni = keysym2ucs(keysym); |
| 243 | if (uni != -1) { |
| 244 | this->handleChar((SkUnichar) uni); |
| 245 | } |
| 246 | break; |
| 247 | } |
| 248 | case KeyRelease: |
| 249 | this->handleKeyUp(XKeyToSkKey(XkbKeycodeToKeysym(dsp, evt.xkey.keycode, 0, 0))); |
| 250 | break; |
| 251 | default: |
| 252 | // Do nothing for other events |
| 253 | break; |
| 254 | } |
| 255 | return kContinue_NextXEventResult; |
| 256 | } |
| 257 | |
bsalomon@google.com | 8108c47 | 2012-04-03 19:33:08 +0000 | [diff] [blame] | 258 | void SkOSWindow::loop() { |
Scroggo | 9df214e | 2011-04-15 14:48:08 +0000 | [diff] [blame] | 259 | Display* dsp = fUnixWindow.fDisplay; |
bsalomon@google.com | 1195925 | 2012-04-06 20:13:38 +0000 | [diff] [blame] | 260 | if (NULL == dsp) { |
| 261 | return; |
| 262 | } |
reed@google.com | d1f1b67 | 2013-06-19 20:56:45 +0000 | [diff] [blame] | 263 | Window win = fUnixWindow.fWin; |
Scroggo | 9df214e | 2011-04-15 14:48:08 +0000 | [diff] [blame] | 264 | |
reed@google.com | d1f1b67 | 2013-06-19 20:56:45 +0000 | [diff] [blame] | 265 | XSelectInput(dsp, win, EVENT_MASK); |
| 266 | |
| 267 | bool sentExposeEvent = false; |
reed@google.com | acb3d88 | 2013-06-17 13:42:43 +0000 | [diff] [blame] | 268 | |
| 269 | for (;;) { |
reed@google.com | d1f1b67 | 2013-06-19 20:56:45 +0000 | [diff] [blame] | 270 | SkEvent::ServiceQueueTimer(); |
| 271 | |
| 272 | bool moreToDo = SkEvent::ProcessEvent(); |
| 273 | |
| 274 | if (this->isDirty() && !sentExposeEvent) { |
| 275 | sentExposeEvent = true; |
| 276 | |
| 277 | XEvent evt; |
| 278 | sk_bzero(&evt, sizeof(evt)); |
| 279 | evt.type = Expose; |
| 280 | evt.xexpose.display = dsp; |
| 281 | XSendEvent(dsp, win, false, ExposureMask, &evt); |
reed@google.com | acb3d88 | 2013-06-17 13:42:43 +0000 | [diff] [blame] | 282 | } |
reed@google.com | d1f1b67 | 2013-06-19 20:56:45 +0000 | [diff] [blame] | 283 | |
reed@google.com | acb3d88 | 2013-06-17 13:42:43 +0000 | [diff] [blame] | 284 | if (XPending(dsp) || !moreToDo) { |
| 285 | switch (this->nextXEvent()) { |
| 286 | case kContinue_NextXEventResult: |
Scroggo | 9df214e | 2011-04-15 14:48:08 +0000 | [diff] [blame] | 287 | break; |
reed@google.com | acb3d88 | 2013-06-17 13:42:43 +0000 | [diff] [blame] | 288 | case kPaintRequest_NextXEventResult: |
reed@google.com | d1f1b67 | 2013-06-19 20:56:45 +0000 | [diff] [blame] | 289 | sentExposeEvent = false; |
| 290 | if (this->isDirty()) { |
| 291 | this->update(NULL); |
| 292 | } |
| 293 | this->doPaint(); |
reed@google.com | acb3d88 | 2013-06-17 13:42:43 +0000 | [diff] [blame] | 294 | break; |
| 295 | case kQuitRequest_NextXEventResult: |
| 296 | return; |
Scroggo | 9df214e | 2011-04-15 14:48:08 +0000 | [diff] [blame] | 297 | } |
Scroggo | 9df214e | 2011-04-15 14:48:08 +0000 | [diff] [blame] | 298 | } |
| 299 | } |
| 300 | } |
| 301 | |
bsalomon@google.com | 8108c47 | 2012-04-03 19:33:08 +0000 | [diff] [blame] | 302 | void SkOSWindow::mapWindowAndWait() { |
bsalomon@google.com | 1195925 | 2012-04-06 20:13:38 +0000 | [diff] [blame] | 303 | SkASSERT(NULL != fUnixWindow.fDisplay); |
Scroggo | 9df214e | 2011-04-15 14:48:08 +0000 | [diff] [blame] | 304 | Display* dsp = fUnixWindow.fDisplay; |
| 305 | Window win = fUnixWindow.fWin; |
| 306 | XMapWindow(dsp, win); |
| 307 | |
| 308 | long eventMask = StructureNotifyMask; |
| 309 | XSelectInput(dsp, win, eventMask); |
| 310 | |
| 311 | // Wait until screen is ready. |
| 312 | XEvent evt; |
| 313 | do { |
| 314 | XNextEvent(dsp, &evt); |
| 315 | } while(evt.type != MapNotify); |
| 316 | |
scroggo | b7e9aee | 2011-03-15 15:15:15 +0000 | [diff] [blame] | 317 | } |
| 318 | |
bsalomon@google.com | 64cc810 | 2013-03-05 20:06:05 +0000 | [diff] [blame] | 319 | bool SkOSWindow::attach(SkBackEndTypes, int msaaSampleCount, AttachmentInfo* info) { |
| 320 | this->initWindow(msaaSampleCount, info); |
| 321 | |
bsalomon@google.com | 1195925 | 2012-04-06 20:13:38 +0000 | [diff] [blame] | 322 | if (NULL == fUnixWindow.fDisplay) { |
| 323 | return false; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 324 | } |
bsalomon@google.com | 8108c47 | 2012-04-03 19:33:08 +0000 | [diff] [blame] | 325 | if (NULL == fUnixWindow.fGLContext) { |
bsalomon@google.com | 1195925 | 2012-04-06 20:13:38 +0000 | [diff] [blame] | 326 | SkASSERT(NULL != fVi); |
Scroggo | 9df214e | 2011-04-15 14:48:08 +0000 | [diff] [blame] | 327 | |
bsalomon@google.com | 8108c47 | 2012-04-03 19:33:08 +0000 | [diff] [blame] | 328 | fUnixWindow.fGLContext = glXCreateContext(fUnixWindow.fDisplay, |
| 329 | fVi, |
| 330 | NULL, |
| 331 | GL_TRUE); |
| 332 | if (NULL == fUnixWindow.fGLContext) { |
| 333 | return false; |
| 334 | } |
Scroggo | 9df214e | 2011-04-15 14:48:08 +0000 | [diff] [blame] | 335 | } |
bsalomon@google.com | 8108c47 | 2012-04-03 19:33:08 +0000 | [diff] [blame] | 336 | glXMakeCurrent(fUnixWindow.fDisplay, |
| 337 | fUnixWindow.fWin, |
| 338 | fUnixWindow.fGLContext); |
| 339 | glViewport(0, 0, |
reed@google.com | e1ca705 | 2013-12-17 19:22:07 +0000 | [diff] [blame] | 340 | SkScalarRoundToInt(this->width()), |
| 341 | SkScalarRoundToInt(this->height())); |
bsalomon@google.com | 8108c47 | 2012-04-03 19:33:08 +0000 | [diff] [blame] | 342 | glClearColor(0, 0, 0, 0); |
| 343 | glClearStencil(0); |
| 344 | glClear(GL_COLOR_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); |
Scroggo | 9df214e | 2011-04-15 14:48:08 +0000 | [diff] [blame] | 345 | return true; |
scroggo | b7e9aee | 2011-03-15 15:15:15 +0000 | [diff] [blame] | 346 | } |
| 347 | |
bsalomon@google.com | 8108c47 | 2012-04-03 19:33:08 +0000 | [diff] [blame] | 348 | void SkOSWindow::detach() { |
| 349 | if (NULL == fUnixWindow.fDisplay || NULL == fUnixWindow.fGLContext) { |
| 350 | return; |
| 351 | } |
Scroggo | 9df214e | 2011-04-15 14:48:08 +0000 | [diff] [blame] | 352 | glXMakeCurrent(fUnixWindow.fDisplay, None, NULL); |
bsalomon@google.com | 8108c47 | 2012-04-03 19:33:08 +0000 | [diff] [blame] | 353 | glXDestroyContext(fUnixWindow.fDisplay, fUnixWindow.fGLContext); |
| 354 | fUnixWindow.fGLContext = NULL; |
scroggo | b7e9aee | 2011-03-15 15:15:15 +0000 | [diff] [blame] | 355 | } |
| 356 | |
bsalomon@google.com | 8108c47 | 2012-04-03 19:33:08 +0000 | [diff] [blame] | 357 | void SkOSWindow::present() { |
| 358 | if (NULL != fUnixWindow.fDisplay && NULL != fUnixWindow.fGLContext) { |
Scroggo | 9df214e | 2011-04-15 14:48:08 +0000 | [diff] [blame] | 359 | glXSwapBuffers(fUnixWindow.fDisplay, fUnixWindow.fWin); |
| 360 | } |
scroggo | b7e9aee | 2011-03-15 15:15:15 +0000 | [diff] [blame] | 361 | } |
| 362 | |
bsalomon@google.com | 8108c47 | 2012-04-03 19:33:08 +0000 | [diff] [blame] | 363 | void SkOSWindow::onSetTitle(const char title[]) { |
bsalomon@google.com | 1195925 | 2012-04-06 20:13:38 +0000 | [diff] [blame] | 364 | if (NULL == fUnixWindow.fDisplay) { |
bsalomon@google.com | 8108c47 | 2012-04-03 19:33:08 +0000 | [diff] [blame] | 365 | return; |
| 366 | } |
scroggo | b7e9aee | 2011-03-15 15:15:15 +0000 | [diff] [blame] | 367 | XTextProperty textProp; |
| 368 | textProp.value = (unsigned char*)title; |
| 369 | textProp.format = 8; |
| 370 | textProp.nitems = strlen((char*)textProp.value); |
| 371 | textProp.encoding = XA_STRING; |
| 372 | XSetWMName(fUnixWindow.fDisplay, fUnixWindow.fWin, &textProp); |
| 373 | } |
| 374 | |
bsalomon@google.com | 8108c47 | 2012-04-03 19:33:08 +0000 | [diff] [blame] | 375 | static bool convertBitmapToXImage(XImage& image, const SkBitmap& bitmap) { |
scroggo | b66365f | 2011-03-18 21:43:03 +0000 | [diff] [blame] | 376 | sk_bzero(&image, sizeof(image)); |
| 377 | |
| 378 | int bitsPerPixel = bitmap.bytesPerPixel() * 8; |
| 379 | image.width = bitmap.width(); |
| 380 | image.height = bitmap.height(); |
| 381 | image.format = ZPixmap; |
| 382 | image.data = (char*) bitmap.getPixels(); |
| 383 | image.byte_order = LSBFirst; |
| 384 | image.bitmap_unit = bitsPerPixel; |
| 385 | image.bitmap_bit_order = LSBFirst; |
| 386 | image.bitmap_pad = bitsPerPixel; |
| 387 | image.depth = 24; |
reed@google.com | acb3d88 | 2013-06-17 13:42:43 +0000 | [diff] [blame] | 388 | image.bytes_per_line = bitmap.rowBytes() - bitmap.width() * 4; |
scroggo | b66365f | 2011-03-18 21:43:03 +0000 | [diff] [blame] | 389 | image.bits_per_pixel = bitsPerPixel; |
scroggo | 08526c0 | 2011-03-22 14:03:21 +0000 | [diff] [blame] | 390 | return XInitImage(&image); |
| 391 | } |
| 392 | |
scroggo | 08526c0 | 2011-03-22 14:03:21 +0000 | [diff] [blame] | 393 | void SkOSWindow::doPaint() { |
bsalomon@google.com | 1195925 | 2012-04-06 20:13:38 +0000 | [diff] [blame] | 394 | if (NULL == fUnixWindow.fDisplay) { |
| 395 | return; |
| 396 | } |
commit-bot@chromium.org | 7fa22f3 | 2013-06-18 15:37:27 +0000 | [diff] [blame] | 397 | // If we are drawing with GL, we don't need XPutImage. |
| 398 | if (NULL != fUnixWindow.fGLContext) { |
| 399 | return; |
| 400 | } |
scroggo | 08526c0 | 2011-03-22 14:03:21 +0000 | [diff] [blame] | 401 | // Draw the bitmap to the screen. |
| 402 | const SkBitmap& bitmap = getBitmap(); |
| 403 | int width = bitmap.width(); |
| 404 | int height = bitmap.height(); |
| 405 | |
Scroggo | 0f185c2 | 2011-03-24 18:35:50 +0000 | [diff] [blame] | 406 | XImage image; |
bsalomon@google.com | 1195925 | 2012-04-06 20:13:38 +0000 | [diff] [blame] | 407 | if (!convertBitmapToXImage(image, bitmap)) { |
| 408 | return; |
| 409 | } |
scroggo | 08526c0 | 2011-03-22 14:03:21 +0000 | [diff] [blame] | 410 | |
bsalomon@google.com | 1195925 | 2012-04-06 20:13:38 +0000 | [diff] [blame] | 411 | XPutImage(fUnixWindow.fDisplay, |
| 412 | fUnixWindow.fWin, |
| 413 | fUnixWindow.fGc, |
| 414 | &image, |
| 415 | 0, 0, // src x,y |
| 416 | 0, 0, // dst x,y |
| 417 | width, height); |
scroggo | b7e9aee | 2011-03-15 15:15:15 +0000 | [diff] [blame] | 418 | } |
| 419 | |
reed@google.com | acb3d88 | 2013-06-17 13:42:43 +0000 | [diff] [blame] | 420 | /////////////////////////////////////////////////////////////////////////////// |
| 421 | |
| 422 | void SkEvent::SignalNonEmptyQueue() { |
| 423 | // nothing to do, since we spin on our event-queue, polling for XPending |
scroggo | b7e9aee | 2011-03-15 15:15:15 +0000 | [diff] [blame] | 424 | } |
| 425 | |
reed@google.com | acb3d88 | 2013-06-17 13:42:43 +0000 | [diff] [blame] | 426 | void SkEvent::SignalQueueTimer(SkMSec delay) { |
| 427 | // just need to record the delay time. We handle waking up for it in |
| 428 | // MyXNextEventWithDelay() |
| 429 | gTimerDelay = delay; |
scroggo | b7e9aee | 2011-03-15 15:15:15 +0000 | [diff] [blame] | 430 | } |