blob: 9aeec6d16ee6c7571e0f86725980b287afed94dc [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"
borenet@google.com0dd5ceb2012-08-28 15:15:49 +000027
28#define ANGLE_GL_CALL(IFACE, X) \
29 do { \
30 (IFACE)->f##X; \
31 } while (false)
32
bsalomon@google.com2858b312012-04-02 20:44:38 +000033#endif
34
35#define INVALIDATE_DELAY_MS 200
36
37static SkOSWindow* gCurrOSWin;
38static HWND gEventTarget;
39
40#define WM_EVENT_CALLBACK (WM_USER+0)
41
42void post_skwinevent()
43{
44 PostMessage(gEventTarget, WM_EVENT_CALLBACK, 0, 0);
45}
46
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000047SkOSWindow::SkOSWindow(void* hWnd) {
48 fHWND = hWnd;
49#if SK_SUPPORT_GPU
bsalomon@google.com2858b312012-04-02 20:44:38 +000050#if SK_ANGLE
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000051 fDisplay = EGL_NO_DISPLAY;
52 fContext = EGL_NO_CONTEXT;
53 fSurface = EGL_NO_SURFACE;
bsalomon@google.com2858b312012-04-02 20:44:38 +000054#endif
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000055 fHGLRC = NULL;
56#endif
57 fAttached = kNone_BackEndType;
bsalomon@google.com2858b312012-04-02 20:44:38 +000058 gEventTarget = (HWND)hWnd;
59}
60
61SkOSWindow::~SkOSWindow() {
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000062#if SK_SUPPORT_GPU
bsalomon@google.com2858b312012-04-02 20:44:38 +000063 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.comcf8fb1f2012-08-02 14:03:32 +000081#endif // SK_ANGLE
82#endif // SK_SUPPORT_GPU
bsalomon@google.com2858b312012-04-02 20:44:38 +000083}
84
85static 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.com4d5c26d2013-01-08 16:17:50 +0000106static unsigned getModifiers(UINT message) {
107 return 0; // TODO
108}
109
bsalomon@google.com2858b312012-04-02 20:44:38 +0000110bool 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.comd6176b02012-08-23 18:14:13 +0000128 return true;
bsalomon@google.com2858b312012-04-02 20:44:38 +0000129 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.comd6176b02012-08-23 18:14:13 +0000151
152 case WM_LBUTTONDOWN:
reed@google.com4d5c26d2013-01-08 16:17:50 +0000153 this->handleClick(GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam),
reed@google.com72708fa2013-01-08 16:22:44 +0000154 Click::kDown_State, NULL, getModifiers(message));
bsalomon@google.com2858b312012-04-02 20:44:38 +0000155 return true;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000156
bsalomon@google.com2858b312012-04-02 20:44:38 +0000157 case WM_MOUSEMOVE:
reed@google.com4d5c26d2013-01-08 16:17:50 +0000158 this->handleClick(GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam),
reed@google.com72708fa2013-01-08 16:22:44 +0000159 Click::kMoved_State, NULL, getModifiers(message));
bsalomon@google.com2858b312012-04-02 20:44:38 +0000160 return true;
161
162 case WM_LBUTTONUP:
reed@google.com4d5c26d2013-01-08 16:17:50 +0000163 this->handleClick(GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam),
reed@google.com72708fa2013-01-08 16:22:44 +0000164 Click::kUp_State, NULL, getModifiers(message));
bsalomon@google.com2858b312012-04-02 20:44:38 +0000165 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
176void 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.comd6176b02012-08-23 18:14:13 +0000188 bmi.bmiHeader.biHeight = -bitmap.height(); // top-down image
bsalomon@google.com2858b312012-04-02 20:44:38 +0000189 bmi.bmiHeader.biPlanes = 1;
190 bmi.bmiHeader.biBitCount = 32;
191 bmi.bmiHeader.biCompression = BI_RGB;
192 bmi.bmiHeader.biSizeImage = 0;
193
rmistry@google.comd6176b02012-08-23 18:14:13 +0000194 //
195 // Do the SetDIBitsToDevice.
196 //
bsalomon@google.com2858b312012-04-02 20:44:38 +0000197 // 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();
bsalomon@google.comb58a6392013-03-21 20:29:05 +0000205 int ret = SetDIBitsToDevice(hdc,
bsalomon@google.com2858b312012-04-02 20:44:38 +0000206 0, 0,
207 bitmap.width(), bitmap.height(),
208 0, 0,
209 0, bitmap.height(),
210 bitmap.getPixels(),
211 &bmi,
212 DIB_RGB_COLORS);
bsalomon@google.comb58a6392013-03-21 20:29:05 +0000213 (void)ret; // we're ignoring potential failures for now.
bsalomon@google.com2858b312012-04-02 20:44:38 +0000214 bitmap.unlockPixels();
215 }
216}
217
218#if 0
219void SkOSWindow::updateSize()
220{
221 RECT r;
222 GetWindowRect((HWND)this->getHWND(), &r);
223 this->resize(r.right - r.left, r.bottom - r.top);
224}
225#endif
226
227void SkOSWindow::onHandleInval(const SkIRect& r) {
228 RECT* rect = new RECT;
229 rect->left = r.fLeft;
230 rect->top = r.fTop;
231 rect->right = r.fRight;
232 rect->bottom = r.fBottom;
233 SetTimer((HWND)fHWND, (UINT_PTR)rect, INVALIDATE_DELAY_MS, NULL);
234}
235
236void SkOSWindow::onAddMenu(const SkOSMenu* sk_menu)
237{
238}
239
240void SkOSWindow::onSetTitle(const char title[]){
241 SetWindowTextA((HWND)fHWND, title);
242}
243
244enum {
245 SK_MacReturnKey = 36,
246 SK_MacDeleteKey = 51,
247 SK_MacEndKey = 119,
248 SK_MacLeftKey = 123,
249 SK_MacRightKey = 124,
250 SK_MacDownKey = 125,
251 SK_MacUpKey = 126,
rmistry@google.comd6176b02012-08-23 18:14:13 +0000252
bsalomon@google.com2858b312012-04-02 20:44:38 +0000253 SK_Mac0Key = 0x52,
254 SK_Mac1Key = 0x53,
255 SK_Mac2Key = 0x54,
256 SK_Mac3Key = 0x55,
257 SK_Mac4Key = 0x56,
258 SK_Mac5Key = 0x57,
259 SK_Mac6Key = 0x58,
260 SK_Mac7Key = 0x59,
261 SK_Mac8Key = 0x5b,
262 SK_Mac9Key = 0x5c
263};
rmistry@google.comd6176b02012-08-23 18:14:13 +0000264
bsalomon@google.com2858b312012-04-02 20:44:38 +0000265static SkKey raw2key(uint32_t raw)
266{
267 static const struct {
268 uint32_t fRaw;
269 SkKey fKey;
270 } gKeys[] = {
271 { SK_MacUpKey, kUp_SkKey },
272 { SK_MacDownKey, kDown_SkKey },
273 { SK_MacLeftKey, kLeft_SkKey },
274 { SK_MacRightKey, kRight_SkKey },
275 { SK_MacReturnKey, kOK_SkKey },
276 { SK_MacDeleteKey, kBack_SkKey },
277 { SK_MacEndKey, kEnd_SkKey },
278 { SK_Mac0Key, k0_SkKey },
279 { SK_Mac1Key, k1_SkKey },
280 { SK_Mac2Key, k2_SkKey },
281 { SK_Mac3Key, k3_SkKey },
282 { SK_Mac4Key, k4_SkKey },
283 { SK_Mac5Key, k5_SkKey },
284 { SK_Mac6Key, k6_SkKey },
285 { SK_Mac7Key, k7_SkKey },
286 { SK_Mac8Key, k8_SkKey },
287 { SK_Mac9Key, k9_SkKey }
288 };
rmistry@google.comd6176b02012-08-23 18:14:13 +0000289
bsalomon@google.com2858b312012-04-02 20:44:38 +0000290 for (unsigned i = 0; i < SK_ARRAY_COUNT(gKeys); i++)
291 if (gKeys[i].fRaw == raw)
292 return gKeys[i].fKey;
293 return kNONE_SkKey;
294}
295
296///////////////////////////////////////////////////////////////////////////////////////
297
298void SkEvent::SignalNonEmptyQueue()
299{
300 post_skwinevent();
301 //SkDebugf("signal nonempty\n");
302}
303
304static UINT_PTR gTimer;
305
306VOID CALLBACK sk_timer_proc(HWND hwnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime)
307{
308 SkEvent::ServiceQueueTimer();
309 //SkDebugf("timer task fired\n");
310}
311
312void SkEvent::SignalQueueTimer(SkMSec delay)
313{
314 if (gTimer)
315 {
316 KillTimer(NULL, gTimer);
317 gTimer = NULL;
318 }
319 if (delay)
rmistry@google.comd6176b02012-08-23 18:14:13 +0000320 {
bsalomon@google.com2858b312012-04-02 20:44:38 +0000321 gTimer = SetTimer(NULL, 0, delay, sk_timer_proc);
322 //SkDebugf("SetTimer of %d returned %d\n", delay, gTimer);
323 }
324}
325
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000326#if SK_SUPPORT_GPU
bsalomon@google.com2858b312012-04-02 20:44:38 +0000327
bsalomon@google.com64cc8102013-03-05 20:06:05 +0000328bool SkOSWindow::attachGL(int msaaSampleCount, AttachmentInfo* info) {
bsalomon@google.comb7f20f22013-03-05 19:13:09 +0000329 HDC dc = GetDC((HWND)fHWND);
bsalomon@google.com2858b312012-04-02 20:44:38 +0000330 if (NULL == fHGLRC) {
bsalomon@google.comb7f20f22013-03-05 19:13:09 +0000331 fHGLRC = SkCreateWGLContext(dc, msaaSampleCount, false);
bsalomon@google.com2858b312012-04-02 20:44:38 +0000332 if (NULL == fHGLRC) {
333 return false;
334 }
335 glClearStencil(0);
336 glClearColor(0, 0, 0, 0);
337 glStencilMask(0xffffffff);
338 glClear(GL_STENCIL_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
339 }
bsalomon@google.comb7f20f22013-03-05 19:13:09 +0000340 if (wglMakeCurrent(dc, (HGLRC)fHGLRC)) {
bsalomon@google.com64cc8102013-03-05 20:06:05 +0000341 // use DescribePixelFormat to get the stencil bit depth.
342 int pixelFormat = GetPixelFormat(dc);
343 PIXELFORMATDESCRIPTOR pfd;
344 DescribePixelFormat(dc, pixelFormat, sizeof(pfd), &pfd);
345 info->fStencilBits = pfd.cStencilBits;
346
347 // Get sample count if the MSAA WGL extension is present
348 SkWGLExtensions extensions;
349 if (extensions.hasExtension(dc, "WGL_ARB_multisample")) {
350 static const int kSampleCountAttr = SK_WGL_SAMPLES;
351 extensions.getPixelFormatAttribiv(dc,
352 pixelFormat,
353 0,
354 1,
355 &kSampleCountAttr,
356 &info->fSampleCount);
357 } else {
358 info->fSampleCount = 0;
359 }
360
361 glViewport(0, 0, SkScalarRound(this->width()), SkScalarRound(this->height()));
bsalomon@google.com2858b312012-04-02 20:44:38 +0000362 return true;
363 }
364 return false;
365}
366
367void SkOSWindow::detachGL() {
368 wglMakeCurrent(GetDC((HWND)fHWND), 0);
369 wglDeleteContext((HGLRC)fHGLRC);
370 fHGLRC = NULL;
371}
372
373void SkOSWindow::presentGL() {
374 glFlush();
bungeman@google.comf44957e2013-03-12 17:27:10 +0000375 HDC dc = GetDC((HWND)fHWND);
376 SwapBuffers(dc);
377 ReleaseDC((HWND)fHWND, dc);
bsalomon@google.com2858b312012-04-02 20:44:38 +0000378}
379
380#if SK_ANGLE
bsalomon@google.com11959252012-04-06 20:13:38 +0000381bool create_ANGLE(EGLNativeWindowType hWnd,
382 int msaaSampleCount,
383 EGLDisplay* eglDisplay,
384 EGLContext* eglContext,
bsalomon@google.com64cc8102013-03-05 20:06:05 +0000385 EGLSurface* eglSurface,
386 EGLConfig* eglConfig) {
bsalomon@google.com11959252012-04-06 20:13:38 +0000387 static const EGLint contextAttribs[] = {
388 EGL_CONTEXT_CLIENT_VERSION, 2,
rmistry@google.comd6176b02012-08-23 18:14:13 +0000389 EGL_NONE, EGL_NONE
bsalomon@google.com2858b312012-04-02 20:44:38 +0000390 };
bsalomon@google.com11959252012-04-06 20:13:38 +0000391 static const EGLint configAttribList[] = {
bsalomon@google.com2858b312012-04-02 20:44:38 +0000392 EGL_RED_SIZE, 8,
393 EGL_GREEN_SIZE, 8,
394 EGL_BLUE_SIZE, 8,
395 EGL_ALPHA_SIZE, 8,
396 EGL_DEPTH_SIZE, 8,
397 EGL_STENCIL_SIZE, 8,
398 EGL_NONE
399 };
bsalomon@google.com11959252012-04-06 20:13:38 +0000400 static const EGLint surfaceAttribList[] = {
bsalomon@google.com2858b312012-04-02 20:44:38 +0000401 EGL_NONE, EGL_NONE
402 };
403
404 EGLDisplay display = eglGetDisplay(GetDC(hWnd));
405 if (display == EGL_NO_DISPLAY ) {
406 return false;
407 }
408
409 // Initialize EGL
410 EGLint majorVersion, minorVersion;
411 if (!eglInitialize(display, &majorVersion, &minorVersion)) {
412 return false;
413 }
414
415 EGLint numConfigs;
416 if (!eglGetConfigs(display, NULL, 0, &numConfigs)) {
417 return false;
418 }
419
420 // Choose config
bsalomon@google.com11959252012-04-06 20:13:38 +0000421 bool foundConfig = false;
422 if (msaaSampleCount) {
423 static const int kConfigAttribListCnt =
424 SK_ARRAY_COUNT(configAttribList);
425 EGLint msaaConfigAttribList[kConfigAttribListCnt + 4];
426 memcpy(msaaConfigAttribList,
427 configAttribList,
428 sizeof(configAttribList));
429 SkASSERT(EGL_NONE == msaaConfigAttribList[kConfigAttribListCnt - 1]);
430 msaaConfigAttribList[kConfigAttribListCnt - 1] = EGL_SAMPLE_BUFFERS;
431 msaaConfigAttribList[kConfigAttribListCnt + 0] = 1;
432 msaaConfigAttribList[kConfigAttribListCnt + 1] = EGL_SAMPLES;
433 msaaConfigAttribList[kConfigAttribListCnt + 2] = msaaSampleCount;
434 msaaConfigAttribList[kConfigAttribListCnt + 3] = EGL_NONE;
bsalomon@google.com64cc8102013-03-05 20:06:05 +0000435 if (eglChooseConfig(display, configAttribList, eglConfig, 1, &numConfigs)) {
bsalomon@google.com11959252012-04-06 20:13:38 +0000436 SkASSERT(numConfigs > 0);
437 foundConfig = true;
438 }
439 }
440 if (!foundConfig) {
bsalomon@google.com64cc8102013-03-05 20:06:05 +0000441 if (!eglChooseConfig(display, configAttribList, eglConfig, 1, &numConfigs)) {
bsalomon@google.com11959252012-04-06 20:13:38 +0000442 return false;
443 }
bsalomon@google.com2858b312012-04-02 20:44:38 +0000444 }
445
446 // Create a surface
bsalomon@google.com64cc8102013-03-05 20:06:05 +0000447 EGLSurface surface = eglCreateWindowSurface(display, *eglConfig,
rmistry@google.comd6176b02012-08-23 18:14:13 +0000448 (EGLNativeWindowType)hWnd,
bsalomon@google.com2858b312012-04-02 20:44:38 +0000449 surfaceAttribList);
450 if (surface == EGL_NO_SURFACE) {
451 return false;
452 }
453
454 // Create a GL context
bsalomon@google.com64cc8102013-03-05 20:06:05 +0000455 EGLContext context = eglCreateContext(display, *eglConfig,
bsalomon@google.com2858b312012-04-02 20:44:38 +0000456 EGL_NO_CONTEXT,
457 contextAttribs );
458 if (context == EGL_NO_CONTEXT ) {
459 return false;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000460 }
461
bsalomon@google.com2858b312012-04-02 20:44:38 +0000462 // Make the context current
463 if (!eglMakeCurrent(display, surface, surface, context)) {
464 return false;
465 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000466
bsalomon@google.com2858b312012-04-02 20:44:38 +0000467 *eglDisplay = display;
468 *eglContext = context;
469 *eglSurface = surface;
470 return true;
471}
472
bsalomon@google.com64cc8102013-03-05 20:06:05 +0000473bool SkOSWindow::attachANGLE(int msaaSampleCount, AttachmentInfo* info) {
bsalomon@google.com2858b312012-04-02 20:44:38 +0000474 if (EGL_NO_DISPLAY == fDisplay) {
bsalomon@google.com11959252012-04-06 20:13:38 +0000475 bool bResult = create_ANGLE((HWND)fHWND,
476 msaaSampleCount,
477 &fDisplay,
478 &fContext,
bsalomon@google.com64cc8102013-03-05 20:06:05 +0000479 &fSurface,
480 &fConfig);
bsalomon@google.com2858b312012-04-02 20:44:38 +0000481 if (false == bResult) {
482 return false;
483 }
bsalomon@google.com82502e22013-01-24 20:47:18 +0000484 SkAutoTUnref<const GrGLInterface> intf(GrGLCreateANGLEInterface());
bsalomon@google.com2858b312012-04-02 20:44:38 +0000485
486 if (intf) {
borenet@google.com0dd5ceb2012-08-28 15:15:49 +0000487 ANGLE_GL_CALL(intf, ClearStencil(0));
488 ANGLE_GL_CALL(intf, ClearColor(0, 0, 0, 0));
489 ANGLE_GL_CALL(intf, StencilMask(0xffffffff));
490 ANGLE_GL_CALL(intf, Clear(GL_STENCIL_BUFFER_BIT |GL_COLOR_BUFFER_BIT));
bsalomon@google.com2858b312012-04-02 20:44:38 +0000491 }
492 }
493 if (eglMakeCurrent(fDisplay, fSurface, fSurface, fContext)) {
bsalomon@google.com64cc8102013-03-05 20:06:05 +0000494 eglGetConfigAttrib(fDisplay, fConfig, EGL_STENCIL_SIZE, &info->fStencilBits);
495 eglGetConfigAttrib(fDisplay, fConfig, EGL_SAMPLES, &info->fSampleCount);
496
bsalomon@google.com82502e22013-01-24 20:47:18 +0000497 SkAutoTUnref<const GrGLInterface> intf(GrGLCreateANGLEInterface());
bsalomon@google.com2858b312012-04-02 20:44:38 +0000498
499 if (intf ) {
borenet@google.com0dd5ceb2012-08-28 15:15:49 +0000500 ANGLE_GL_CALL(intf, Viewport(0, 0, SkScalarRound(this->width()),
bsalomon@google.com2858b312012-04-02 20:44:38 +0000501 SkScalarRound(this->height())));
502 }
503 return true;
504 }
505 return false;
506}
507
508void SkOSWindow::detachANGLE() {
509 eglMakeCurrent(fDisplay, EGL_NO_SURFACE , EGL_NO_SURFACE , EGL_NO_CONTEXT);
510
511 eglDestroyContext(fDisplay, fContext);
512 fContext = EGL_NO_CONTEXT;
513
514 eglDestroySurface(fDisplay, fSurface);
515 fSurface = EGL_NO_SURFACE;
516
517 eglTerminate(fDisplay);
518 fDisplay = EGL_NO_DISPLAY;
519}
520
521void SkOSWindow::presentANGLE() {
bsalomon@google.com82502e22013-01-24 20:47:18 +0000522 SkAutoTUnref<const GrGLInterface> intf(GrGLCreateANGLEInterface());
bsalomon@google.com2858b312012-04-02 20:44:38 +0000523
524 if (intf) {
borenet@google.com0dd5ceb2012-08-28 15:15:49 +0000525 ANGLE_GL_CALL(intf, Flush());
bsalomon@google.com2858b312012-04-02 20:44:38 +0000526 }
527
528 eglSwapBuffers(fDisplay, fSurface);
529}
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000530#endif // SK_ANGLE
531#endif // SK_SUPPORT_GPU
bsalomon@google.com2858b312012-04-02 20:44:38 +0000532
bsalomon@google.com2858b312012-04-02 20:44:38 +0000533// return true on success
bsalomon@google.com64cc8102013-03-05 20:06:05 +0000534bool SkOSWindow::attach(SkBackEndTypes attachType, int msaaSampleCount, AttachmentInfo* info) {
bsalomon@google.com2858b312012-04-02 20:44:38 +0000535
536 // attach doubles as "windowResize" so we need to allo
537 // already bound states to pass through again
538 // TODO: split out the resize functionality
539// SkASSERT(kNone_BackEndType == fAttached);
540 bool result = true;
541
542 switch (attachType) {
543 case kNone_BackEndType:
544 // nothing to do
rmistry@google.comd6176b02012-08-23 18:14:13 +0000545 break;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000546#if SK_SUPPORT_GPU
bsalomon@google.com2858b312012-04-02 20:44:38 +0000547 case kNativeGL_BackEndType:
bsalomon@google.com64cc8102013-03-05 20:06:05 +0000548 result = attachGL(msaaSampleCount, info);
bsalomon@google.com2858b312012-04-02 20:44:38 +0000549 break;
550#if SK_ANGLE
551 case kANGLE_BackEndType:
bsalomon@google.com64cc8102013-03-05 20:06:05 +0000552 result = attachANGLE(msaaSampleCount, info);
bsalomon@google.com2858b312012-04-02 20:44:38 +0000553 break;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000554#endif // SK_ANGLE
555#endif // SK_SUPPORT_GPU
bsalomon@google.com2858b312012-04-02 20:44:38 +0000556 default:
557 SkASSERT(false);
558 result = false;
559 break;
560 }
561
562 if (result) {
563 fAttached = attachType;
564 }
565
566 return result;
567}
568
569void SkOSWindow::detach() {
570 switch (fAttached) {
571 case kNone_BackEndType:
572 // nothing to do
rmistry@google.comd6176b02012-08-23 18:14:13 +0000573 break;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000574#if SK_SUPPORT_GPU
bsalomon@google.com2858b312012-04-02 20:44:38 +0000575 case kNativeGL_BackEndType:
576 detachGL();
577 break;
578#if SK_ANGLE
579 case kANGLE_BackEndType:
580 detachANGLE();
581 break;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000582#endif // SK_ANGLE
583#endif // SK_SUPPORT_GPU
bsalomon@google.com2858b312012-04-02 20:44:38 +0000584 default:
585 SkASSERT(false);
586 break;
587 }
588 fAttached = kNone_BackEndType;
589}
590
591void SkOSWindow::present() {
592 switch (fAttached) {
593 case kNone_BackEndType:
594 // nothing to do
rmistry@google.comd6176b02012-08-23 18:14:13 +0000595 return;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000596#if SK_SUPPORT_GPU
bsalomon@google.com2858b312012-04-02 20:44:38 +0000597 case kNativeGL_BackEndType:
598 presentGL();
599 break;
600#if SK_ANGLE
601 case kANGLE_BackEndType:
602 presentANGLE();
603 break;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000604#endif // SK_ANGLE
605#endif // SK_SUPPORT_GPU
bsalomon@google.com2858b312012-04-02 20:44:38 +0000606 default:
607 SkASSERT(false);
608 break;
609 }
610}
611
612#endif