blob: d5aebeaffd40b47f05f5de2ac33e54a3ba3d3b84 [file] [log] [blame]
bsalomon@google.com2858b312012-04-02 20:44:38 +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 */
8#include "SkTypes.h"
9
10#if defined(SK_BUILD_FOR_WIN)
11
12#include <GL/gl.h>
bsalomon@google.com2858b312012-04-02 20:44:38 +000013#include <WindowsX.h>
14#include "SkWGL.h"
15#include "SkWindow.h"
16#include "SkCanvas.h"
17#include "SkOSMenu.h"
18#include "SkTime.h"
19#include "SkUtils.h"
20
21#include "SkGraphics.h"
22
23#if SK_ANGLE
24#include "gl/GrGLInterface.h"
25
26#include "GLES2/gl2.h"
27#endif
28
29#define INVALIDATE_DELAY_MS 200
30
31static SkOSWindow* gCurrOSWin;
32static HWND gEventTarget;
33
34#define WM_EVENT_CALLBACK (WM_USER+0)
35
36void post_skwinevent()
37{
38 PostMessage(gEventTarget, WM_EVENT_CALLBACK, 0, 0);
39}
40
41SkOSWindow::SkOSWindow(void* hWnd)
42 : fHWND(hWnd)
43#if SK_ANGLE
44 , fDisplay(EGL_NO_DISPLAY)
45 , fContext(EGL_NO_CONTEXT)
46 , fSurface(EGL_NO_SURFACE)
47#endif
48 , fHGLRC(NULL)
bsalomon@google.com2858b312012-04-02 20:44:38 +000049 , fAttached(kNone_BackEndType) {
50 gEventTarget = (HWND)hWnd;
51}
52
53SkOSWindow::~SkOSWindow() {
bsalomon@google.com2858b312012-04-02 20:44:38 +000054 if (NULL != fHGLRC) {
55 wglDeleteContext((HGLRC)fHGLRC);
56 }
57#if SK_ANGLE
58 if (EGL_NO_CONTEXT != fContext) {
59 eglDestroyContext(fDisplay, fContext);
60 fContext = EGL_NO_CONTEXT;
61 }
62
63 if (EGL_NO_SURFACE != fSurface) {
64 eglDestroySurface(fDisplay, fSurface);
65 fSurface = EGL_NO_SURFACE;
66 }
67
68 if (EGL_NO_DISPLAY != fDisplay) {
69 eglTerminate(fDisplay);
70 fDisplay = EGL_NO_DISPLAY;
71 }
72#endif
73}
74
75static SkKey winToskKey(WPARAM vk) {
76 static const struct {
77 WPARAM fVK;
78 SkKey fKey;
79 } gPair[] = {
80 { VK_BACK, kBack_SkKey },
81 { VK_CLEAR, kBack_SkKey },
82 { VK_RETURN, kOK_SkKey },
83 { VK_UP, kUp_SkKey },
84 { VK_DOWN, kDown_SkKey },
85 { VK_LEFT, kLeft_SkKey },
86 { VK_RIGHT, kRight_SkKey }
87 };
88 for (size_t i = 0; i < SK_ARRAY_COUNT(gPair); i++) {
89 if (gPair[i].fVK == vk) {
90 return gPair[i].fKey;
91 }
92 }
93 return kNONE_SkKey;
94}
95
96bool SkOSWindow::wndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) {
97 switch (message) {
98 case WM_KEYDOWN: {
99 SkKey key = winToskKey(wParam);
100 if (kNONE_SkKey != key) {
101 this->handleKey(key);
102 return true;
103 }
104 } break;
105 case WM_KEYUP: {
106 SkKey key = winToskKey(wParam);
107 if (kNONE_SkKey != key) {
108 this->handleKeyUp(key);
109 return true;
110 }
111 } break;
112 case WM_UNICHAR:
113 this->handleChar(wParam);
114 return true;
115 case WM_CHAR: {
116 this->handleChar(SkUTF8_ToUnichar((char*)&wParam));
117 return true;
118 } break;
119 case WM_SIZE:
120 this->resize(lParam & 0xFFFF, lParam >> 16);
121 break;
122 case WM_PAINT: {
123 PAINTSTRUCT ps;
124 HDC hdc = BeginPaint(hWnd, &ps);
125 this->doPaint(hdc);
126 EndPaint(hWnd, &ps);
127 return true;
128 } break;
129
130 case WM_TIMER: {
131 RECT* rect = (RECT*)wParam;
132 InvalidateRect(hWnd, rect, FALSE);
133 KillTimer(hWnd, (UINT_PTR)rect);
134 delete rect;
135 return true;
136 } break;
137
138 case WM_LBUTTONDOWN:
139 this->handleClick(GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam), Click::kDown_State);
140 return true;
141
142 case WM_MOUSEMOVE:
143 this->handleClick(GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam), Click::kMoved_State);
144 return true;
145
146 case WM_LBUTTONUP:
147 this->handleClick(GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam), Click::kUp_State);
148 return true;
149
150 case WM_EVENT_CALLBACK:
151 if (SkEvent::ProcessEvent()) {
152 post_skwinevent();
153 }
154 return true;
155 }
156 return false;
157}
158
159void SkOSWindow::doPaint(void* ctx) {
160 this->update(NULL);
161
162 if (kNone_BackEndType == fAttached)
163 {
164 HDC hdc = (HDC)ctx;
165 const SkBitmap& bitmap = this->getBitmap();
166
167 BITMAPINFO bmi;
168 memset(&bmi, 0, sizeof(bmi));
169 bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
170 bmi.bmiHeader.biWidth = bitmap.width();
171 bmi.bmiHeader.biHeight = -bitmap.height(); // top-down image
172 bmi.bmiHeader.biPlanes = 1;
173 bmi.bmiHeader.biBitCount = 32;
174 bmi.bmiHeader.biCompression = BI_RGB;
175 bmi.bmiHeader.biSizeImage = 0;
176
177 //
178 // Do the SetDIBitsToDevice.
179 //
180 // TODO(wjmaclean):
181 // Fix this call to handle SkBitmaps that have rowBytes != width,
182 // i.e. may have padding at the end of lines. The SkASSERT below
183 // may be ignored by builds, and the only obviously safe option
184 // seems to be to copy the bitmap to a temporary (contiguous)
185 // buffer before passing to SetDIBitsToDevice().
186 SkASSERT(bitmap.width() * bitmap.bytesPerPixel() == bitmap.rowBytes());
187 bitmap.lockPixels();
188 int iRet = SetDIBitsToDevice(hdc,
189 0, 0,
190 bitmap.width(), bitmap.height(),
191 0, 0,
192 0, bitmap.height(),
193 bitmap.getPixels(),
194 &bmi,
195 DIB_RGB_COLORS);
196 bitmap.unlockPixels();
197 }
198}
199
200#if 0
201void SkOSWindow::updateSize()
202{
203 RECT r;
204 GetWindowRect((HWND)this->getHWND(), &r);
205 this->resize(r.right - r.left, r.bottom - r.top);
206}
207#endif
208
209void SkOSWindow::onHandleInval(const SkIRect& r) {
210 RECT* rect = new RECT;
211 rect->left = r.fLeft;
212 rect->top = r.fTop;
213 rect->right = r.fRight;
214 rect->bottom = r.fBottom;
215 SetTimer((HWND)fHWND, (UINT_PTR)rect, INVALIDATE_DELAY_MS, NULL);
216}
217
218void SkOSWindow::onAddMenu(const SkOSMenu* sk_menu)
219{
220}
221
222void SkOSWindow::onSetTitle(const char title[]){
223 SetWindowTextA((HWND)fHWND, title);
224}
225
226enum {
227 SK_MacReturnKey = 36,
228 SK_MacDeleteKey = 51,
229 SK_MacEndKey = 119,
230 SK_MacLeftKey = 123,
231 SK_MacRightKey = 124,
232 SK_MacDownKey = 125,
233 SK_MacUpKey = 126,
234
235 SK_Mac0Key = 0x52,
236 SK_Mac1Key = 0x53,
237 SK_Mac2Key = 0x54,
238 SK_Mac3Key = 0x55,
239 SK_Mac4Key = 0x56,
240 SK_Mac5Key = 0x57,
241 SK_Mac6Key = 0x58,
242 SK_Mac7Key = 0x59,
243 SK_Mac8Key = 0x5b,
244 SK_Mac9Key = 0x5c
245};
246
247static SkKey raw2key(uint32_t raw)
248{
249 static const struct {
250 uint32_t fRaw;
251 SkKey fKey;
252 } gKeys[] = {
253 { SK_MacUpKey, kUp_SkKey },
254 { SK_MacDownKey, kDown_SkKey },
255 { SK_MacLeftKey, kLeft_SkKey },
256 { SK_MacRightKey, kRight_SkKey },
257 { SK_MacReturnKey, kOK_SkKey },
258 { SK_MacDeleteKey, kBack_SkKey },
259 { SK_MacEndKey, kEnd_SkKey },
260 { SK_Mac0Key, k0_SkKey },
261 { SK_Mac1Key, k1_SkKey },
262 { SK_Mac2Key, k2_SkKey },
263 { SK_Mac3Key, k3_SkKey },
264 { SK_Mac4Key, k4_SkKey },
265 { SK_Mac5Key, k5_SkKey },
266 { SK_Mac6Key, k6_SkKey },
267 { SK_Mac7Key, k7_SkKey },
268 { SK_Mac8Key, k8_SkKey },
269 { SK_Mac9Key, k9_SkKey }
270 };
271
272 for (unsigned i = 0; i < SK_ARRAY_COUNT(gKeys); i++)
273 if (gKeys[i].fRaw == raw)
274 return gKeys[i].fKey;
275 return kNONE_SkKey;
276}
277
278///////////////////////////////////////////////////////////////////////////////////////
279
280void SkEvent::SignalNonEmptyQueue()
281{
282 post_skwinevent();
283 //SkDebugf("signal nonempty\n");
284}
285
286static UINT_PTR gTimer;
287
288VOID CALLBACK sk_timer_proc(HWND hwnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime)
289{
290 SkEvent::ServiceQueueTimer();
291 //SkDebugf("timer task fired\n");
292}
293
294void SkEvent::SignalQueueTimer(SkMSec delay)
295{
296 if (gTimer)
297 {
298 KillTimer(NULL, gTimer);
299 gTimer = NULL;
300 }
301 if (delay)
302 {
303 gTimer = SetTimer(NULL, 0, delay, sk_timer_proc);
304 //SkDebugf("SetTimer of %d returned %d\n", delay, gTimer);
305 }
306}
307
308
309#define USE_MSAA 0
310
bsalomon@google.com3c4d0322012-04-03 18:04:51 +0000311HGLRC create_gl(HWND hwnd) {
bsalomon@google.com2858b312012-04-02 20:44:38 +0000312
313 HDC dc = GetDC(hwnd);
314
315 SkWGLExtensions extensions;
316 if (!extensions.hasExtension(dc, "WGL_ARB_pixel_format")) {
317 return NULL;
318 }
319
320 HDC prevDC = wglGetCurrentDC();
321 HGLRC prevGLRC = wglGetCurrentContext();
322 PIXELFORMATDESCRIPTOR pfd;
323
324 int format = 0;
325
bsalomon@google.com3c4d0322012-04-03 18:04:51 +0000326 GLint iattrs[] = {
bsalomon@google.com2858b312012-04-02 20:44:38 +0000327 SK_WGL_DRAW_TO_WINDOW, TRUE,
328 SK_WGL_DOUBLE_BUFFER, TRUE,
329 SK_WGL_ACCELERATION, SK_WGL_FULL_ACCELERATION,
330 SK_WGL_SUPPORT_OPENGL, TRUE,
331 SK_WGL_COLOR_BITS, 24,
332 SK_WGL_ALPHA_BITS, 8,
333 SK_WGL_STENCIL_BITS, 8,
bsalomon@google.com3c4d0322012-04-03 18:04:51 +0000334
335 // these must be kept last
336 SK_WGL_SAMPLE_BUFFERS, TRUE,
337 SK_WGL_SAMPLES, 0,
338 0,0
bsalomon@google.com2858b312012-04-02 20:44:38 +0000339 };
340
bsalomon@google.com3c4d0322012-04-03 18:04:51 +0000341 static const int kSampleBuffersValueIdx = SK_ARRAY_COUNT(iattrs) - 5;
342 static const int kSamplesValueIdx = SK_ARRAY_COUNT(iattrs) - 3;
343 if (USE_MSAA && extensions.hasExtension(dc, "WGL_ARB_multisample")) {
344 for (int samples = 16; samples > 1; --samples) {
345
346 iattrs[kSamplesValueIdx] = samples;
347 GLfloat fattrs[] = {0,0};
348 GLuint num;
349 int formats[64];
350 extensions.choosePixelFormat(dc, iattrs, fattrs, 64, formats, &num);
351 num = min(num,64);
352 for (GLuint i = 0; i < num; ++i) {
353 DescribePixelFormat(dc, formats[i], sizeof(pfd), &pfd);
354 if (SetPixelFormat(dc, formats[i], &pfd)) {
355 format = formats[i];
356 break;
357 }
bsalomon@google.com2858b312012-04-02 20:44:38 +0000358 }
359 }
360 }
361
362 if (0 == format) {
bsalomon@google.com3c4d0322012-04-03 18:04:51 +0000363 iattrs[kSampleBuffersValueIdx-1] = iattrs[kSampleBuffersValueIdx] = 0;
364 iattrs[kSamplesValueIdx-1] = iattrs[kSamplesValueIdx] = 0;
365 GLfloat fattrs[] = {0,0};
bsalomon@google.com2858b312012-04-02 20:44:38 +0000366 GLuint num;
bsalomon@google.com3c4d0322012-04-03 18:04:51 +0000367 extensions.choosePixelFormat(dc, iattrs, fattrs, 1, &format, &num);
bsalomon@google.com2858b312012-04-02 20:44:38 +0000368 DescribePixelFormat(dc, format, sizeof(pfd), &pfd);
369 BOOL set = SetPixelFormat(dc, format, &pfd);
370 SkASSERT(TRUE == set);
371 }
372
373 HGLRC glrc = wglCreateContext(dc);
374 SkASSERT(glrc);
375
376 wglMakeCurrent(prevDC, prevGLRC);
377 return glrc;
378}
379
bsalomon@google.com3c4d0322012-04-03 18:04:51 +0000380bool SkOSWindow::attachGL() {
bsalomon@google.com2858b312012-04-02 20:44:38 +0000381 if (NULL == fHGLRC) {
bsalomon@google.com3c4d0322012-04-03 18:04:51 +0000382 fHGLRC = create_gl((HWND)fHWND);
bsalomon@google.com2858b312012-04-02 20:44:38 +0000383 if (NULL == fHGLRC) {
384 return false;
385 }
386 glClearStencil(0);
387 glClearColor(0, 0, 0, 0);
388 glStencilMask(0xffffffff);
389 glClear(GL_STENCIL_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
390 }
391 if (wglMakeCurrent(GetDC((HWND)fHWND), (HGLRC)fHGLRC)) {
392 glViewport(0, 0, SkScalarRound(this->width()),
393 SkScalarRound(this->height()));
394 return true;
395 }
396 return false;
397}
398
399void SkOSWindow::detachGL() {
400 wglMakeCurrent(GetDC((HWND)fHWND), 0);
401 wglDeleteContext((HGLRC)fHGLRC);
402 fHGLRC = NULL;
403}
404
405void SkOSWindow::presentGL() {
406 glFlush();
407 SwapBuffers(GetDC((HWND)fHWND));
408}
409
410#if SK_ANGLE
bsalomon@google.com3c4d0322012-04-03 18:04:51 +0000411bool create_ANGLE(EGLNativeWindowType hWnd, EGLDisplay* eglDisplay,
412 EGLContext* eglContext, EGLSurface* eglSurface) {
413 EGLint contextAttribs[] = {
414 EGL_CONTEXT_CLIENT_VERSION, 2,
bsalomon@google.com2858b312012-04-02 20:44:38 +0000415 EGL_NONE, EGL_NONE
416 };
bsalomon@google.com3c4d0322012-04-03 18:04:51 +0000417 EGLint configAttribList[] = {
bsalomon@google.com2858b312012-04-02 20:44:38 +0000418 EGL_RED_SIZE, 8,
419 EGL_GREEN_SIZE, 8,
420 EGL_BLUE_SIZE, 8,
421 EGL_ALPHA_SIZE, 8,
422 EGL_DEPTH_SIZE, 8,
423 EGL_STENCIL_SIZE, 8,
424 EGL_NONE
425 };
bsalomon@google.com3c4d0322012-04-03 18:04:51 +0000426 EGLint surfaceAttribList[] = {
bsalomon@google.com2858b312012-04-02 20:44:38 +0000427 EGL_NONE, EGL_NONE
428 };
429
430 EGLDisplay display = eglGetDisplay(GetDC(hWnd));
431 if (display == EGL_NO_DISPLAY ) {
432 return false;
433 }
434
435 // Initialize EGL
436 EGLint majorVersion, minorVersion;
437 if (!eglInitialize(display, &majorVersion, &minorVersion)) {
438 return false;
439 }
440
441 EGLint numConfigs;
442 if (!eglGetConfigs(display, NULL, 0, &numConfigs)) {
443 return false;
444 }
445
446 // Choose config
447 EGLConfig config;
bsalomon@google.com3c4d0322012-04-03 18:04:51 +0000448 if (!eglChooseConfig(display, configAttribList,
449 &config, 1, &numConfigs)) {
450 return false;
bsalomon@google.com2858b312012-04-02 20:44:38 +0000451 }
452
453 // Create a surface
454 EGLSurface surface = eglCreateWindowSurface(display, config,
455 (EGLNativeWindowType)hWnd,
456 surfaceAttribList);
457 if (surface == EGL_NO_SURFACE) {
458 return false;
459 }
460
461 // Create a GL context
462 EGLContext context = eglCreateContext(display, config,
463 EGL_NO_CONTEXT,
464 contextAttribs );
465 if (context == EGL_NO_CONTEXT ) {
466 return false;
467 }
468
469 // Make the context current
470 if (!eglMakeCurrent(display, surface, surface, context)) {
471 return false;
472 }
473
474 *eglDisplay = display;
475 *eglContext = context;
476 *eglSurface = surface;
477 return true;
478}
479
bsalomon@google.com3c4d0322012-04-03 18:04:51 +0000480bool SkOSWindow::attachANGLE() {
bsalomon@google.com2858b312012-04-02 20:44:38 +0000481 if (EGL_NO_DISPLAY == fDisplay) {
482 bool bResult = create_ANGLE((HWND)fHWND, &fDisplay, &fContext, &fSurface);
483 if (false == bResult) {
484 return false;
485 }
486 const GrGLInterface* intf = GrGLCreateANGLEInterface();
487
488 if (intf) {
489 GR_GL_CALL(intf, ClearStencil(0));
490 GR_GL_CALL(intf, ClearColor(0, 0, 0, 0));
491 GR_GL_CALL(intf, StencilMask(0xffffffff));
492 GR_GL_CALL(intf, Clear(GL_STENCIL_BUFFER_BIT | GL_COLOR_BUFFER_BIT));
493 }
494 }
495 if (eglMakeCurrent(fDisplay, fSurface, fSurface, fContext)) {
496 const GrGLInterface* intf = GrGLCreateANGLEInterface();
497
498 if (intf ) {
499 GR_GL_CALL(intf, Viewport(0, 0, SkScalarRound(this->width()),
500 SkScalarRound(this->height())));
501 }
502 return true;
503 }
504 return false;
505}
506
507void SkOSWindow::detachANGLE() {
508 eglMakeCurrent(fDisplay, EGL_NO_SURFACE , EGL_NO_SURFACE , EGL_NO_CONTEXT);
509
510 eglDestroyContext(fDisplay, fContext);
511 fContext = EGL_NO_CONTEXT;
512
513 eglDestroySurface(fDisplay, fSurface);
514 fSurface = EGL_NO_SURFACE;
515
516 eglTerminate(fDisplay);
517 fDisplay = EGL_NO_DISPLAY;
518}
519
520void SkOSWindow::presentANGLE() {
521 const GrGLInterface* intf = GrGLCreateANGLEInterface();
522
523 if (intf) {
524 GR_GL_CALL(intf, Flush());
525 }
526
527 eglSwapBuffers(fDisplay, fSurface);
528}
529#endif
530
bsalomon@google.com2858b312012-04-02 20:44:38 +0000531// return true on success
bsalomon@google.com3c4d0322012-04-03 18:04:51 +0000532bool SkOSWindow::attach(SkBackEndTypes attachType) {
bsalomon@google.com2858b312012-04-02 20:44:38 +0000533
534 // attach doubles as "windowResize" so we need to allo
535 // already bound states to pass through again
536 // TODO: split out the resize functionality
537// SkASSERT(kNone_BackEndType == fAttached);
538 bool result = true;
539
540 switch (attachType) {
541 case kNone_BackEndType:
542 // nothing to do
543 break;
544 case kNativeGL_BackEndType:
bsalomon@google.com3c4d0322012-04-03 18:04:51 +0000545 result = attachGL();
bsalomon@google.com2858b312012-04-02 20:44:38 +0000546 break;
547#if SK_ANGLE
548 case kANGLE_BackEndType:
bsalomon@google.com3c4d0322012-04-03 18:04:51 +0000549 result = attachANGLE();
bsalomon@google.com2858b312012-04-02 20:44:38 +0000550 break;
551#endif
bsalomon@google.com2858b312012-04-02 20:44:38 +0000552 default:
553 SkASSERT(false);
554 result = false;
555 break;
556 }
557
558 if (result) {
559 fAttached = attachType;
560 }
561
562 return result;
563}
564
565void SkOSWindow::detach() {
566 switch (fAttached) {
567 case kNone_BackEndType:
568 // nothing to do
569 break;
570 case kNativeGL_BackEndType:
571 detachGL();
572 break;
573#if SK_ANGLE
574 case kANGLE_BackEndType:
575 detachANGLE();
576 break;
577#endif
bsalomon@google.com2858b312012-04-02 20:44:38 +0000578 default:
579 SkASSERT(false);
580 break;
581 }
582 fAttached = kNone_BackEndType;
583}
584
585void SkOSWindow::present() {
586 switch (fAttached) {
587 case kNone_BackEndType:
588 // nothing to do
589 return;
590 case kNativeGL_BackEndType:
591 presentGL();
592 break;
593#if SK_ANGLE
594 case kANGLE_BackEndType:
595 presentANGLE();
596 break;
597#endif
bsalomon@google.com2858b312012-04-02 20:44:38 +0000598 default:
599 SkASSERT(false);
600 break;
601 }
602}
603
604#endif