blob: c8fc2c70842c381b40cb3ff36270a97b3658d841 [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>
10#include <X11/keysym.h>
11#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
bsalomon@google.com8108c472012-04-03 19:33:08 +000035SkOSWindow::SkOSWindow(void* unused)
36 : INHERITED()
bsalomon@google.com11959252012-04-06 20:13:38 +000037 , fVi(NULL)
38 , fMSAASampleCount(0) {
39 fUnixWindow.fDisplay = NULL;
bsalomon@google.com8108c472012-04-03 19:33:08 +000040 fUnixWindow.fGLContext = NULL;
bsalomon@google.com11959252012-04-06 20:13:38 +000041 this->initWindow(0);
Scroggo9df214e2011-04-15 14:48:08 +000042 this->resize(WIDTH, HEIGHT);
scroggob7e9aee2011-03-15 15:15:15 +000043}
44
bsalomon@google.com8108c472012-04-03 19:33:08 +000045SkOSWindow::~SkOSWindow() {
bsalomon@google.com11959252012-04-06 20:13:38 +000046 this->closeWindow();
47}
48
49void SkOSWindow::closeWindow() {
bsalomon@google.com8108c472012-04-03 19:33:08 +000050 if (NULL != fUnixWindow.fDisplay) {
bsalomon@google.com11959252012-04-06 20:13:38 +000051 this->detach();
52 SkASSERT(NULL != fUnixWindow.fGc);
bsalomon@google.com8108c472012-04-03 19:33:08 +000053 XFreeGC(fUnixWindow.fDisplay, fUnixWindow.fGc);
bsalomon@google.com11959252012-04-06 20:13:38 +000054 fUnixWindow.fGc = NULL;
Scroggo9df214e2011-04-15 14:48:08 +000055 XDestroyWindow(fUnixWindow.fDisplay, fUnixWindow.fWin);
bsalomon@google.com11959252012-04-06 20:13:38 +000056 fVi = NULL;
Scroggo9df214e2011-04-15 14:48:08 +000057 XCloseDisplay(fUnixWindow.fDisplay);
bsalomon@google.com11959252012-04-06 20:13:38 +000058 fUnixWindow.fDisplay = NULL;
59 fMSAASampleCount = 0;
Scroggo9df214e2011-04-15 14:48:08 +000060 }
scroggob7e9aee2011-03-15 15:15:15 +000061}
62
bsalomon@google.com11959252012-04-06 20:13:38 +000063void SkOSWindow::initWindow(int requestedMSAASampleCount) {
64 if (fMSAASampleCount != requestedMSAASampleCount) {
65 this->closeWindow();
66 }
67 // presence of fDisplay means we already have a window
68 if (NULL != fUnixWindow.fDisplay) {
69 return;
70 }
71 fUnixWindow.fDisplay = XOpenDisplay(NULL);
72 Display* dsp = fUnixWindow.fDisplay;
73 if (NULL == dsp) {
74 SkDebugf("Could not open an X Display");
75 return;
76 }
77 // Attempt to create a window that supports GL
78 GLint att[] = {
79 GLX_RGBA,
80 GLX_DEPTH_SIZE, 24,
81 GLX_DOUBLEBUFFER,
82 GLX_STENCIL_SIZE, 8,
83 None
84 };
85 SkASSERT(NULL == fVi);
86 if (requestedMSAASampleCount > 0) {
87 static const GLint kAttCount = SK_ARRAY_COUNT(att);
88 GLint msaaAtt[kAttCount + 4];
89 memcpy(msaaAtt, att, sizeof(att));
90 SkASSERT(None == msaaAtt[kAttCount - 1]);
91 msaaAtt[kAttCount - 1] = GLX_SAMPLE_BUFFERS_ARB;
92 msaaAtt[kAttCount + 0] = 1;
93 msaaAtt[kAttCount + 1] = GLX_SAMPLES_ARB;
94 msaaAtt[kAttCount + 2] = requestedMSAASampleCount;
95 msaaAtt[kAttCount + 3] = None;
96 fVi = glXChooseVisual(dsp, DefaultScreen(dsp), msaaAtt);
97 fMSAASampleCount = requestedMSAASampleCount;
98 }
99 if (NULL == fVi) {
100 fVi = glXChooseVisual(dsp, DefaultScreen(dsp), att);
101 fMSAASampleCount = 0;
102 }
103
104 if (fVi) {
105 Colormap colorMap = XCreateColormap(dsp,
106 RootWindow(dsp, fVi->screen),
107 fVi->visual,
108 AllocNone);
109 XSetWindowAttributes swa;
110 swa.colormap = colorMap;
111 swa.event_mask = EVENT_MASK;
112 fUnixWindow.fWin = XCreateWindow(dsp,
113 RootWindow(dsp, fVi->screen),
114 0, 0, // x, y
115 WIDTH, HEIGHT,
116 0, // border width
117 fVi->depth,
118 InputOutput,
119 fVi->visual,
120 CWEventMask | CWColormap,
121 &swa);
122 } else {
123 // Create a simple window instead. We will not be able to show GL
124 fUnixWindow.fWin = XCreateSimpleWindow(dsp,
125 DefaultRootWindow(dsp),
126 0, 0, // x, y
127 WIDTH, HEIGHT,
128 0, // border width
129 0, // border value
130 0); // background value
131 }
132 this->mapWindowAndWait();
133 fUnixWindow.fGc = XCreateGC(dsp, fUnixWindow.fWin, 0, NULL);
134}
135
136
bsalomon@google.com8108c472012-04-03 19:33:08 +0000137void SkOSWindow::post_linuxevent() {
Scroggo9df214e2011-04-15 14:48:08 +0000138 // Put an event in the X queue to fire an SkEvent.
bsalomon@google.com11959252012-04-06 20:13:38 +0000139 if (NULL == fUnixWindow.fDisplay) {
bsalomon@google.com8108c472012-04-03 19:33:08 +0000140 return;
141 }
Scroggo9df214e2011-04-15 14:48:08 +0000142 long event_mask = NoEventMask;
143 XClientMessageEvent event;
144 event.type = ClientMessage;
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +0000145 Atom myAtom(0);
Scroggo9df214e2011-04-15 14:48:08 +0000146 event.message_type = myAtom;
147 event.format = 32;
148 event.data.l[0] = 0;
149 XSendEvent(fUnixWindow.fDisplay, fUnixWindow.fWin, false, 0,
150 (XEvent*) &event);
Scroggo5a234242011-06-13 19:17:58 +0000151 XFlush(fUnixWindow.fDisplay);
Scroggo9df214e2011-04-15 14:48:08 +0000152}
153
bsalomon@google.com8108c472012-04-03 19:33:08 +0000154void SkOSWindow::loop() {
Scroggo9df214e2011-04-15 14:48:08 +0000155 Display* dsp = fUnixWindow.fDisplay;
bsalomon@google.com11959252012-04-06 20:13:38 +0000156 if (NULL == dsp) {
157 return;
158 }
Scroggo9df214e2011-04-15 14:48:08 +0000159 XSelectInput(dsp, fUnixWindow.fWin, EVENT_MASK);
160
161 bool loop = true;
162 XEvent evt;
163 while (loop) {
Scroggo9df214e2011-04-15 14:48:08 +0000164 XNextEvent(dsp, &evt);
165 switch (evt.type) {
166 case Expose:
167 if (evt.xexpose.count == 0)
168 this->inval(NULL);
169 break;
170 case ConfigureNotify:
171 this->resize(evt.xconfigure.width, evt.xconfigure.height);
172 break;
173 case ButtonPress:
174 if (evt.xbutton.button == Button1)
175 this->handleClick(evt.xbutton.x, evt.xbutton.y, SkView::Click::kDown_State);
176 break;
177 case ButtonRelease:
178 if (evt.xbutton.button == Button1)
179 this->handleClick(evt.xbutton.x, evt.xbutton.y, SkView::Click::kUp_State);
180 break;
181 case MotionNotify:
182 this->handleClick(evt.xmotion.x, evt.xmotion.y, SkView::Click::kMoved_State);
183 break;
bsalomon@google.com8108c472012-04-03 19:33:08 +0000184 case KeyPress: {
Scroggo9df214e2011-04-15 14:48:08 +0000185 KeySym keysym = XKeycodeToKeysym(dsp, evt.xkey.keycode, 0);
186 //SkDebugf("pressed key %i!\n\tKeySym:%i\n", evt.xkey.keycode, XKeycodeToKeysym(dsp, evt.xkey.keycode, 0));
187 if (keysym == XK_Escape) {
188 loop = false;
189 break;
190 }
191 this->handleKey(XKeyToSkKey(keysym));
192 long uni = keysym2ucs(keysym);
193 if (uni != -1) {
194 this->handleChar((SkUnichar) uni);
195 }
196 break;
197 }
198 case KeyRelease:
199 //SkDebugf("released key %i\n", evt.xkey.keycode);
200 this->handleKeyUp(XKeyToSkKey(XKeycodeToKeysym(dsp, evt.xkey.keycode, 0)));
201 break;
202 case ClientMessage:
203 if (SkEvent::ProcessEvent()) {
204 this->post_linuxevent();
205 }
206 break;
207 default:
208 // Do nothing for other events
209 break;
210 }
211 }
212}
213
bsalomon@google.com8108c472012-04-03 19:33:08 +0000214void SkOSWindow::mapWindowAndWait() {
bsalomon@google.com11959252012-04-06 20:13:38 +0000215 SkASSERT(NULL != fUnixWindow.fDisplay);
Scroggo9df214e2011-04-15 14:48:08 +0000216 Display* dsp = fUnixWindow.fDisplay;
217 Window win = fUnixWindow.fWin;
218 XMapWindow(dsp, win);
219
220 long eventMask = StructureNotifyMask;
221 XSelectInput(dsp, win, eventMask);
222
223 // Wait until screen is ready.
224 XEvent evt;
225 do {
226 XNextEvent(dsp, &evt);
227 } while(evt.type != MapNotify);
228
scroggob7e9aee2011-03-15 15:15:15 +0000229}
230
bsalomon@google.com11959252012-04-06 20:13:38 +0000231bool SkOSWindow::attach(SkBackEndTypes /* attachType */, int msaaSampleCount) {
232 this->initWindow(msaaSampleCount);
233 if (NULL == fUnixWindow.fDisplay) {
234 return false;
235 }
bsalomon@google.com8108c472012-04-03 19:33:08 +0000236 if (NULL == fUnixWindow.fGLContext) {
bsalomon@google.com11959252012-04-06 20:13:38 +0000237 SkASSERT(NULL != fVi);
Scroggo9df214e2011-04-15 14:48:08 +0000238
bsalomon@google.com8108c472012-04-03 19:33:08 +0000239 fUnixWindow.fGLContext = glXCreateContext(fUnixWindow.fDisplay,
240 fVi,
241 NULL,
242 GL_TRUE);
243 if (NULL == fUnixWindow.fGLContext) {
244 return false;
245 }
Scroggo9df214e2011-04-15 14:48:08 +0000246 }
bsalomon@google.com8108c472012-04-03 19:33:08 +0000247 glXMakeCurrent(fUnixWindow.fDisplay,
248 fUnixWindow.fWin,
249 fUnixWindow.fGLContext);
250 glViewport(0, 0,
251 SkScalarRound(this->width()), SkScalarRound(this->height()));
252 glClearColor(0, 0, 0, 0);
253 glClearStencil(0);
254 glClear(GL_COLOR_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
Scroggo9df214e2011-04-15 14:48:08 +0000255 return true;
scroggob7e9aee2011-03-15 15:15:15 +0000256}
257
bsalomon@google.com8108c472012-04-03 19:33:08 +0000258void SkOSWindow::detach() {
259 if (NULL == fUnixWindow.fDisplay || NULL == fUnixWindow.fGLContext) {
260 return;
261 }
Scroggo9df214e2011-04-15 14:48:08 +0000262 glXMakeCurrent(fUnixWindow.fDisplay, None, NULL);
bsalomon@google.com8108c472012-04-03 19:33:08 +0000263 glXDestroyContext(fUnixWindow.fDisplay, fUnixWindow.fGLContext);
264 fUnixWindow.fGLContext = NULL;
scroggob7e9aee2011-03-15 15:15:15 +0000265}
266
bsalomon@google.com8108c472012-04-03 19:33:08 +0000267void SkOSWindow::present() {
268 if (NULL != fUnixWindow.fDisplay && NULL != fUnixWindow.fGLContext) {
Scroggo9df214e2011-04-15 14:48:08 +0000269 glXSwapBuffers(fUnixWindow.fDisplay, fUnixWindow.fWin);
270 }
scroggob7e9aee2011-03-15 15:15:15 +0000271}
272
bsalomon@google.com8108c472012-04-03 19:33:08 +0000273void SkOSWindow::onSetTitle(const char title[]) {
bsalomon@google.com11959252012-04-06 20:13:38 +0000274 if (NULL == fUnixWindow.fDisplay) {
bsalomon@google.com8108c472012-04-03 19:33:08 +0000275 return;
276 }
scroggob7e9aee2011-03-15 15:15:15 +0000277 XTextProperty textProp;
278 textProp.value = (unsigned char*)title;
279 textProp.format = 8;
280 textProp.nitems = strlen((char*)textProp.value);
281 textProp.encoding = XA_STRING;
282 XSetWMName(fUnixWindow.fDisplay, fUnixWindow.fWin, &textProp);
283}
284
reed@google.comf2164b22011-08-04 13:57:56 +0000285void SkOSWindow::onHandleInval(const SkIRect&) {
286 (new SkEvent("inval-imageview", this->getSinkID()))->post();
scroggob7e9aee2011-03-15 15:15:15 +0000287}
288
bsalomon@google.com8108c472012-04-03 19:33:08 +0000289bool SkOSWindow::onEvent(const SkEvent& evt) {
scroggob7e9aee2011-03-15 15:15:15 +0000290 if (evt.isType("inval-imageview")) {
291 update(NULL);
bsalomon@google.com8108c472012-04-03 19:33:08 +0000292 if (NULL == fUnixWindow.fGLContext)
293 this->doPaint();
scroggob7e9aee2011-03-15 15:15:15 +0000294 return true;
295 }
296 return INHERITED::onEvent(evt);
297}
298
bsalomon@google.com8108c472012-04-03 19:33:08 +0000299static bool convertBitmapToXImage(XImage& image, const SkBitmap& bitmap) {
scroggob66365f2011-03-18 21:43:03 +0000300 sk_bzero(&image, sizeof(image));
301
302 int bitsPerPixel = bitmap.bytesPerPixel() * 8;
303 image.width = bitmap.width();
304 image.height = bitmap.height();
305 image.format = ZPixmap;
306 image.data = (char*) bitmap.getPixels();
307 image.byte_order = LSBFirst;
308 image.bitmap_unit = bitsPerPixel;
309 image.bitmap_bit_order = LSBFirst;
310 image.bitmap_pad = bitsPerPixel;
311 image.depth = 24;
312 image.bytes_per_line = bitmap.rowBytes() - bitmap.width() * bitmap.bytesPerPixel();
313 image.bits_per_pixel = bitsPerPixel;
scroggo08526c02011-03-22 14:03:21 +0000314 return XInitImage(&image);
315}
316
scroggo08526c02011-03-22 14:03:21 +0000317void SkOSWindow::doPaint() {
bsalomon@google.com11959252012-04-06 20:13:38 +0000318 if (NULL == fUnixWindow.fDisplay) {
319 return;
320 }
scroggo08526c02011-03-22 14:03:21 +0000321 // Draw the bitmap to the screen.
322 const SkBitmap& bitmap = getBitmap();
323 int width = bitmap.width();
324 int height = bitmap.height();
325
Scroggo0f185c22011-03-24 18:35:50 +0000326 XImage image;
bsalomon@google.com11959252012-04-06 20:13:38 +0000327 if (!convertBitmapToXImage(image, bitmap)) {
328 return;
329 }
scroggo08526c02011-03-22 14:03:21 +0000330
bsalomon@google.com11959252012-04-06 20:13:38 +0000331 XPutImage(fUnixWindow.fDisplay,
332 fUnixWindow.fWin,
333 fUnixWindow.fGc,
334 &image,
335 0, 0, // src x,y
336 0, 0, // dst x,y
337 width, height);
scroggob7e9aee2011-03-15 15:15:15 +0000338}
339
bsalomon@google.com8108c472012-04-03 19:33:08 +0000340bool SkOSWindow::onHandleChar(SkUnichar) {
scroggob7e9aee2011-03-15 15:15:15 +0000341 return false;
342}
343
bsalomon@google.com8108c472012-04-03 19:33:08 +0000344bool SkOSWindow::onHandleKey(SkKey key) {
scroggob7e9aee2011-03-15 15:15:15 +0000345 return false;
346}
347
bsalomon@google.com8108c472012-04-03 19:33:08 +0000348bool SkOSWindow::onHandleKeyUp(SkKey key) {
scroggob7e9aee2011-03-15 15:15:15 +0000349 return false;
350}