blob: 9fa2b304b9fb576730114bca98b12dd77f613b73 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
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 */
scroggob7e9aee2011-03-15 15:15:15 +00008#include <X11/Xlib.h>
9#include <X11/Xatom.h>
bsalomon@google.com75356a72012-06-19 13:08:59 +000010#include <X11/XKBlib.h>
scroggob7e9aee2011-03-15 15:15:15 +000011#include <GL/glx.h>
Scroggo9df214e2011-04-15 14:48:08 +000012#include <GL/gl.h>
13#include <GL/glu.h>
scroggob7e9aee2011-03-15 15:15:15 +000014
15#include "SkWindow.h"
16
17#include "SkBitmap.h"
scroggo08526c02011-03-22 14:03:21 +000018#include "SkCanvas.h"
scroggob7e9aee2011-03-15 15:15:15 +000019#include "SkColor.h"
20#include "SkEvent.h"
Scroggo9df214e2011-04-15 14:48:08 +000021#include "SkKey.h"
22#include "SkWindow.h"
23#include "XkeysToSkKeys.h"
24extern "C" {
25 #include "keysym2ucs.h"
26}
27
Scroggoaed68d92011-06-08 14:26:00 +000028const int WIDTH = 500;
29const int HEIGHT = 500;
Scroggo9df214e2011-04-15 14:48:08 +000030
31// Determine which events to listen for.
32const long EVENT_MASK = StructureNotifyMask|ButtonPressMask|ButtonReleaseMask
33 |ExposureMask|PointerMotionMask|KeyPressMask|KeyReleaseMask;
scroggob7e9aee2011-03-15 15:15:15 +000034
sugoi@google.com93c7ee32013-03-12 14:36:57 +000035SkOSWindow::SkOSWindow(void*)
robertphillips@google.com4bdfb8c2012-06-12 21:23:49 +000036 : fVi(NULL)
bsalomon@google.com11959252012-04-06 20:13:38 +000037 , fMSAASampleCount(0) {
38 fUnixWindow.fDisplay = NULL;
bsalomon@google.com8108c472012-04-03 19:33:08 +000039 fUnixWindow.fGLContext = NULL;
bsalomon@google.com64cc8102013-03-05 20:06:05 +000040 this->initWindow(0, NULL);
Scroggo9df214e2011-04-15 14:48:08 +000041 this->resize(WIDTH, HEIGHT);
scroggob7e9aee2011-03-15 15:15:15 +000042}
43
bsalomon@google.com8108c472012-04-03 19:33:08 +000044SkOSWindow::~SkOSWindow() {
bsalomon@google.com11959252012-04-06 20:13:38 +000045 this->closeWindow();
46}
47
48void SkOSWindow::closeWindow() {
bsalomon@google.com8108c472012-04-03 19:33:08 +000049 if (NULL != fUnixWindow.fDisplay) {
bsalomon@google.com11959252012-04-06 20:13:38 +000050 this->detach();
51 SkASSERT(NULL != fUnixWindow.fGc);
bsalomon@google.com8108c472012-04-03 19:33:08 +000052 XFreeGC(fUnixWindow.fDisplay, fUnixWindow.fGc);
bsalomon@google.com11959252012-04-06 20:13:38 +000053 fUnixWindow.fGc = NULL;
Scroggo9df214e2011-04-15 14:48:08 +000054 XDestroyWindow(fUnixWindow.fDisplay, fUnixWindow.fWin);
bsalomon@google.com11959252012-04-06 20:13:38 +000055 fVi = NULL;
Scroggo9df214e2011-04-15 14:48:08 +000056 XCloseDisplay(fUnixWindow.fDisplay);
bsalomon@google.com11959252012-04-06 20:13:38 +000057 fUnixWindow.fDisplay = NULL;
58 fMSAASampleCount = 0;
Scroggo9df214e2011-04-15 14:48:08 +000059 }
scroggob7e9aee2011-03-15 15:15:15 +000060}
61
bsalomon@google.com64cc8102013-03-05 20:06:05 +000062void SkOSWindow::initWindow(int requestedMSAASampleCount, AttachmentInfo* info) {
bsalomon@google.com11959252012-04-06 20:13:38 +000063 if (fMSAASampleCount != requestedMSAASampleCount) {
64 this->closeWindow();
65 }
66 // presence of fDisplay means we already have a window
67 if (NULL != fUnixWindow.fDisplay) {
bsalomon@google.com64cc8102013-03-05 20:06:05 +000068 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.com11959252012-04-06 20:13:38 +000077 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.com64cc8102013-03-05 20:06:05 +0000113 if (NULL != info) {
114 glXGetConfig(dsp, fVi, GLX_SAMPLES_ARB, &info->fSampleCount);
115 glXGetConfig(dsp, fVi, GLX_STENCIL_SIZE, &info->fStencilBits);
116 }
bsalomon@google.com11959252012-04-06 20:13:38 +0000117 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.com64cc8102013-03-05 20:06:05 +0000135 if (NULL != info) {
136 info->fSampleCount = 0;
137 info->fStencilBits = 0;
138 }
bsalomon@google.com11959252012-04-06 20:13:38 +0000139 // 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.com37924182013-01-08 16:25:42 +0000152static unsigned getModi(const XEvent& evt) {
reed@google.come378d832013-01-08 16:50:52 +0000153 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.com4e73aa12013-01-09 02:01:30 +0000162
reed@google.come378d832013-01-08 16:50:52 +0000163 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.com4d5c26d2013-01-08 16:17:50 +0000170}
171
reed@google.comacb3d882013-06-17 13:42:43 +0000172static SkMSec gTimerDelay;
173
reed@google.comd1f1b672013-06-19 20:56:45 +0000174static bool MyXNextEventWithDelay(Display* dsp, XEvent* evt) {
reed@google.comacb3d882013-06-17 13:42:43 +0000175 SkMSec ms = gTimerDelay;
176 if (ms > 0) {
177 int x11_fd = ConnectionNumber(dsp);
178 fd_set input_fds;
179 FD_ZERO(&input_fds);
180 FD_SET(x11_fd, &input_fds);
181
182 timeval tv;
183 tv.tv_sec = ms / 1000; // seconds
184 tv.tv_usec = (ms % 1000) * 1000; // microseconds
skia.committer@gmail.coma48595d2013-06-18 07:00:52 +0000185
reed@google.comd1f1b672013-06-19 20:56:45 +0000186 if (!select(x11_fd + 1, &input_fds, NULL, NULL, &tv)) {
187 if (!XPending(dsp)) {
188 return false;
189 }
190 }
reed@google.comacb3d882013-06-17 13:42:43 +0000191 }
reed@google.comd1f1b672013-06-19 20:56:45 +0000192 XNextEvent(dsp, evt);
193 return true;
reed@google.comacb3d882013-06-17 13:42:43 +0000194}
195
196SkOSWindow::NextXEventResult SkOSWindow::nextXEvent() {
197 XEvent evt;
198 Display* dsp = fUnixWindow.fDisplay;
199
reed@google.comd1f1b672013-06-19 20:56:45 +0000200 if (!MyXNextEventWithDelay(fUnixWindow.fDisplay, &evt)) {
201 return kContinue_NextXEventResult;
202 }
reed@google.comacb3d882013-06-17 13:42:43 +0000203
204 switch (evt.type) {
205 case Expose:
206 if (0 == evt.xexpose.count) {
207 return kPaintRequest_NextXEventResult;
208 }
209 break;
210 case ConfigureNotify:
211 this->resize(evt.xconfigure.width, evt.xconfigure.height);
212 break;
213 case ButtonPress:
214 if (evt.xbutton.button == Button1)
215 this->handleClick(evt.xbutton.x, evt.xbutton.y,
216 SkView::Click::kDown_State, NULL, getModi(evt));
217 break;
218 case ButtonRelease:
219 if (evt.xbutton.button == Button1)
220 this->handleClick(evt.xbutton.x, evt.xbutton.y,
221 SkView::Click::kUp_State, NULL, getModi(evt));
222 break;
223 case MotionNotify:
224 this->handleClick(evt.xmotion.x, evt.xmotion.y,
225 SkView::Click::kMoved_State, NULL, getModi(evt));
226 break;
227 case KeyPress: {
bungeman@google.com5e4fe212013-06-19 23:06:00 +0000228 int shiftLevel = (evt.xkey.state & ShiftMask) ? 1 : 0;
reed@google.comacb3d882013-06-17 13:42:43 +0000229 KeySym keysym = XkbKeycodeToKeysym(dsp, evt.xkey.keycode,
230 0, shiftLevel);
231 if (keysym == XK_Escape) {
232 return kQuitRequest_NextXEventResult;
233 }
234 this->handleKey(XKeyToSkKey(keysym));
235 long uni = keysym2ucs(keysym);
236 if (uni != -1) {
237 this->handleChar((SkUnichar) uni);
238 }
239 break;
240 }
241 case KeyRelease:
242 this->handleKeyUp(XKeyToSkKey(XkbKeycodeToKeysym(dsp, evt.xkey.keycode, 0, 0)));
243 break;
244 default:
245 // Do nothing for other events
246 break;
247 }
248 return kContinue_NextXEventResult;
249}
250
bsalomon@google.com8108c472012-04-03 19:33:08 +0000251void SkOSWindow::loop() {
Scroggo9df214e2011-04-15 14:48:08 +0000252 Display* dsp = fUnixWindow.fDisplay;
bsalomon@google.com11959252012-04-06 20:13:38 +0000253 if (NULL == dsp) {
254 return;
255 }
reed@google.comd1f1b672013-06-19 20:56:45 +0000256 Window win = fUnixWindow.fWin;
Scroggo9df214e2011-04-15 14:48:08 +0000257
reed@google.comd1f1b672013-06-19 20:56:45 +0000258 XSelectInput(dsp, win, EVENT_MASK);
259
260 bool sentExposeEvent = false;
reed@google.comacb3d882013-06-17 13:42:43 +0000261
262 for (;;) {
reed@google.comd1f1b672013-06-19 20:56:45 +0000263 SkEvent::ServiceQueueTimer();
264
265 bool moreToDo = SkEvent::ProcessEvent();
266
267 if (this->isDirty() && !sentExposeEvent) {
268 sentExposeEvent = true;
269
270 XEvent evt;
271 sk_bzero(&evt, sizeof(evt));
272 evt.type = Expose;
273 evt.xexpose.display = dsp;
274 XSendEvent(dsp, win, false, ExposureMask, &evt);
reed@google.comacb3d882013-06-17 13:42:43 +0000275 }
reed@google.comd1f1b672013-06-19 20:56:45 +0000276
reed@google.comacb3d882013-06-17 13:42:43 +0000277 if (XPending(dsp) || !moreToDo) {
278 switch (this->nextXEvent()) {
279 case kContinue_NextXEventResult:
Scroggo9df214e2011-04-15 14:48:08 +0000280 break;
reed@google.comacb3d882013-06-17 13:42:43 +0000281 case kPaintRequest_NextXEventResult:
reed@google.comd1f1b672013-06-19 20:56:45 +0000282 sentExposeEvent = false;
283 if (this->isDirty()) {
284 this->update(NULL);
285 }
286 this->doPaint();
reed@google.comacb3d882013-06-17 13:42:43 +0000287 break;
288 case kQuitRequest_NextXEventResult:
289 return;
Scroggo9df214e2011-04-15 14:48:08 +0000290 }
Scroggo9df214e2011-04-15 14:48:08 +0000291 }
292 }
293}
294
bsalomon@google.com8108c472012-04-03 19:33:08 +0000295void SkOSWindow::mapWindowAndWait() {
bsalomon@google.com11959252012-04-06 20:13:38 +0000296 SkASSERT(NULL != fUnixWindow.fDisplay);
Scroggo9df214e2011-04-15 14:48:08 +0000297 Display* dsp = fUnixWindow.fDisplay;
298 Window win = fUnixWindow.fWin;
299 XMapWindow(dsp, win);
300
301 long eventMask = StructureNotifyMask;
302 XSelectInput(dsp, win, eventMask);
303
304 // Wait until screen is ready.
305 XEvent evt;
306 do {
307 XNextEvent(dsp, &evt);
308 } while(evt.type != MapNotify);
309
scroggob7e9aee2011-03-15 15:15:15 +0000310}
311
bsalomon@google.com64cc8102013-03-05 20:06:05 +0000312bool SkOSWindow::attach(SkBackEndTypes, int msaaSampleCount, AttachmentInfo* info) {
313 this->initWindow(msaaSampleCount, info);
314
bsalomon@google.com11959252012-04-06 20:13:38 +0000315 if (NULL == fUnixWindow.fDisplay) {
316 return false;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000317 }
bsalomon@google.com8108c472012-04-03 19:33:08 +0000318 if (NULL == fUnixWindow.fGLContext) {
bsalomon@google.com11959252012-04-06 20:13:38 +0000319 SkASSERT(NULL != fVi);
Scroggo9df214e2011-04-15 14:48:08 +0000320
bsalomon@google.com8108c472012-04-03 19:33:08 +0000321 fUnixWindow.fGLContext = glXCreateContext(fUnixWindow.fDisplay,
322 fVi,
323 NULL,
324 GL_TRUE);
325 if (NULL == fUnixWindow.fGLContext) {
326 return false;
327 }
Scroggo9df214e2011-04-15 14:48:08 +0000328 }
bsalomon@google.com8108c472012-04-03 19:33:08 +0000329 glXMakeCurrent(fUnixWindow.fDisplay,
330 fUnixWindow.fWin,
331 fUnixWindow.fGLContext);
332 glViewport(0, 0,
333 SkScalarRound(this->width()), SkScalarRound(this->height()));
334 glClearColor(0, 0, 0, 0);
335 glClearStencil(0);
336 glClear(GL_COLOR_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
Scroggo9df214e2011-04-15 14:48:08 +0000337 return true;
scroggob7e9aee2011-03-15 15:15:15 +0000338}
339
bsalomon@google.com8108c472012-04-03 19:33:08 +0000340void SkOSWindow::detach() {
341 if (NULL == fUnixWindow.fDisplay || NULL == fUnixWindow.fGLContext) {
342 return;
343 }
Scroggo9df214e2011-04-15 14:48:08 +0000344 glXMakeCurrent(fUnixWindow.fDisplay, None, NULL);
bsalomon@google.com8108c472012-04-03 19:33:08 +0000345 glXDestroyContext(fUnixWindow.fDisplay, fUnixWindow.fGLContext);
346 fUnixWindow.fGLContext = NULL;
scroggob7e9aee2011-03-15 15:15:15 +0000347}
348
bsalomon@google.com8108c472012-04-03 19:33:08 +0000349void SkOSWindow::present() {
350 if (NULL != fUnixWindow.fDisplay && NULL != fUnixWindow.fGLContext) {
Scroggo9df214e2011-04-15 14:48:08 +0000351 glXSwapBuffers(fUnixWindow.fDisplay, fUnixWindow.fWin);
352 }
scroggob7e9aee2011-03-15 15:15:15 +0000353}
354
bsalomon@google.com8108c472012-04-03 19:33:08 +0000355void SkOSWindow::onSetTitle(const char title[]) {
bsalomon@google.com11959252012-04-06 20:13:38 +0000356 if (NULL == fUnixWindow.fDisplay) {
bsalomon@google.com8108c472012-04-03 19:33:08 +0000357 return;
358 }
scroggob7e9aee2011-03-15 15:15:15 +0000359 XTextProperty textProp;
360 textProp.value = (unsigned char*)title;
361 textProp.format = 8;
362 textProp.nitems = strlen((char*)textProp.value);
363 textProp.encoding = XA_STRING;
364 XSetWMName(fUnixWindow.fDisplay, fUnixWindow.fWin, &textProp);
365}
366
bsalomon@google.com8108c472012-04-03 19:33:08 +0000367static bool convertBitmapToXImage(XImage& image, const SkBitmap& bitmap) {
scroggob66365f2011-03-18 21:43:03 +0000368 sk_bzero(&image, sizeof(image));
369
370 int bitsPerPixel = bitmap.bytesPerPixel() * 8;
371 image.width = bitmap.width();
372 image.height = bitmap.height();
373 image.format = ZPixmap;
374 image.data = (char*) bitmap.getPixels();
375 image.byte_order = LSBFirst;
376 image.bitmap_unit = bitsPerPixel;
377 image.bitmap_bit_order = LSBFirst;
378 image.bitmap_pad = bitsPerPixel;
379 image.depth = 24;
reed@google.comacb3d882013-06-17 13:42:43 +0000380 image.bytes_per_line = bitmap.rowBytes() - bitmap.width() * 4;
scroggob66365f2011-03-18 21:43:03 +0000381 image.bits_per_pixel = bitsPerPixel;
scroggo08526c02011-03-22 14:03:21 +0000382 return XInitImage(&image);
383}
384
scroggo08526c02011-03-22 14:03:21 +0000385void SkOSWindow::doPaint() {
bsalomon@google.com11959252012-04-06 20:13:38 +0000386 if (NULL == fUnixWindow.fDisplay) {
387 return;
388 }
commit-bot@chromium.org7fa22f32013-06-18 15:37:27 +0000389 // If we are drawing with GL, we don't need XPutImage.
390 if (NULL != fUnixWindow.fGLContext) {
391 return;
392 }
scroggo08526c02011-03-22 14:03:21 +0000393 // Draw the bitmap to the screen.
394 const SkBitmap& bitmap = getBitmap();
395 int width = bitmap.width();
396 int height = bitmap.height();
397
Scroggo0f185c22011-03-24 18:35:50 +0000398 XImage image;
bsalomon@google.com11959252012-04-06 20:13:38 +0000399 if (!convertBitmapToXImage(image, bitmap)) {
400 return;
401 }
scroggo08526c02011-03-22 14:03:21 +0000402
bsalomon@google.com11959252012-04-06 20:13:38 +0000403 XPutImage(fUnixWindow.fDisplay,
404 fUnixWindow.fWin,
405 fUnixWindow.fGc,
406 &image,
407 0, 0, // src x,y
408 0, 0, // dst x,y
409 width, height);
scroggob7e9aee2011-03-15 15:15:15 +0000410}
411
reed@google.comacb3d882013-06-17 13:42:43 +0000412///////////////////////////////////////////////////////////////////////////////
413
414void SkEvent::SignalNonEmptyQueue() {
415 // nothing to do, since we spin on our event-queue, polling for XPending
scroggob7e9aee2011-03-15 15:15:15 +0000416}
417
reed@google.comacb3d882013-06-17 13:42:43 +0000418void SkEvent::SignalQueueTimer(SkMSec delay) {
419 // just need to record the delay time. We handle waking up for it in
420 // MyXNextEventWithDelay()
421 gTimerDelay = delay;
scroggob7e9aee2011-03-15 15:15:15 +0000422}