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