blob: 0fdeba61562f6c77f98bdccaf23a7b8099d77513 [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();
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
218void 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
226void 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
235void SkOSWindow::onAddMenu(const SkOSMenu* sk_menu)
236{
237}
238
239void SkOSWindow::onSetTitle(const char title[]){
240 SetWindowTextA((HWND)fHWND, title);
241}
242
243enum {
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.comd6176b02012-08-23 18:14:13 +0000251
bsalomon@google.com2858b312012-04-02 20:44:38 +0000252 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.comd6176b02012-08-23 18:14:13 +0000263
bsalomon@google.com2858b312012-04-02 20:44:38 +0000264static 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.comd6176b02012-08-23 18:14:13 +0000288
bsalomon@google.com2858b312012-04-02 20:44:38 +0000289 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
297void SkEvent::SignalNonEmptyQueue()
298{
299 post_skwinevent();
300 //SkDebugf("signal nonempty\n");
301}
302
303static UINT_PTR gTimer;
304
305VOID 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
311void SkEvent::SignalQueueTimer(SkMSec delay)
312{
313 if (gTimer)
314 {
315 KillTimer(NULL, gTimer);
316 gTimer = NULL;
317 }
318 if (delay)
rmistry@google.comd6176b02012-08-23 18:14:13 +0000319 {
bsalomon@google.com2858b312012-04-02 20:44:38 +0000320 gTimer = SetTimer(NULL, 0, delay, sk_timer_proc);
321 //SkDebugf("SetTimer of %d returned %d\n", delay, gTimer);
322 }
323}
324
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000325#if SK_SUPPORT_GPU
bsalomon@google.com2858b312012-04-02 20:44:38 +0000326
bsalomon@google.com64cc8102013-03-05 20:06:05 +0000327bool SkOSWindow::attachGL(int msaaSampleCount, AttachmentInfo* info) {
bsalomon@google.comb7f20f22013-03-05 19:13:09 +0000328 HDC dc = GetDC((HWND)fHWND);
bsalomon@google.com2858b312012-04-02 20:44:38 +0000329 if (NULL == fHGLRC) {
bsalomon@google.comb7f20f22013-03-05 19:13:09 +0000330 fHGLRC = SkCreateWGLContext(dc, msaaSampleCount, false);
bsalomon@google.com2858b312012-04-02 20:44:38 +0000331 if (NULL == fHGLRC) {
332 return false;
333 }
334 glClearStencil(0);
335 glClearColor(0, 0, 0, 0);
336 glStencilMask(0xffffffff);
337 glClear(GL_STENCIL_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
338 }
bsalomon@google.comb7f20f22013-03-05 19:13:09 +0000339 if (wglMakeCurrent(dc, (HGLRC)fHGLRC)) {
bsalomon@google.com64cc8102013-03-05 20:06:05 +0000340 // use DescribePixelFormat to get the stencil bit depth.
341 int pixelFormat = GetPixelFormat(dc);
342 PIXELFORMATDESCRIPTOR pfd;
343 DescribePixelFormat(dc, pixelFormat, sizeof(pfd), &pfd);
344 info->fStencilBits = pfd.cStencilBits;
345
346 // Get sample count if the MSAA WGL extension is present
347 SkWGLExtensions extensions;
348 if (extensions.hasExtension(dc, "WGL_ARB_multisample")) {
349 static const int kSampleCountAttr = SK_WGL_SAMPLES;
350 extensions.getPixelFormatAttribiv(dc,
351 pixelFormat,
352 0,
353 1,
354 &kSampleCountAttr,
355 &info->fSampleCount);
356 } else {
357 info->fSampleCount = 0;
358 }
359
360 glViewport(0, 0, SkScalarRound(this->width()), SkScalarRound(this->height()));
bsalomon@google.com2858b312012-04-02 20:44:38 +0000361 return true;
362 }
363 return false;
364}
365
366void SkOSWindow::detachGL() {
367 wglMakeCurrent(GetDC((HWND)fHWND), 0);
368 wglDeleteContext((HGLRC)fHGLRC);
369 fHGLRC = NULL;
370}
371
372void SkOSWindow::presentGL() {
373 glFlush();
bungeman@google.comf44957e2013-03-12 17:27:10 +0000374 HDC dc = GetDC((HWND)fHWND);
375 SwapBuffers(dc);
376 ReleaseDC((HWND)fHWND, dc);
bsalomon@google.com2858b312012-04-02 20:44:38 +0000377}
378
379#if SK_ANGLE
bsalomon@google.com11959252012-04-06 20:13:38 +0000380bool create_ANGLE(EGLNativeWindowType hWnd,
381 int msaaSampleCount,
382 EGLDisplay* eglDisplay,
383 EGLContext* eglContext,
bsalomon@google.com64cc8102013-03-05 20:06:05 +0000384 EGLSurface* eglSurface,
385 EGLConfig* eglConfig) {
bsalomon@google.com11959252012-04-06 20:13:38 +0000386 static const EGLint contextAttribs[] = {
387 EGL_CONTEXT_CLIENT_VERSION, 2,
rmistry@google.comd6176b02012-08-23 18:14:13 +0000388 EGL_NONE, EGL_NONE
bsalomon@google.com2858b312012-04-02 20:44:38 +0000389 };
bsalomon@google.com11959252012-04-06 20:13:38 +0000390 static const EGLint configAttribList[] = {
bsalomon@google.com2858b312012-04-02 20:44:38 +0000391 EGL_RED_SIZE, 8,
392 EGL_GREEN_SIZE, 8,
393 EGL_BLUE_SIZE, 8,
394 EGL_ALPHA_SIZE, 8,
395 EGL_DEPTH_SIZE, 8,
396 EGL_STENCIL_SIZE, 8,
397 EGL_NONE
398 };
bsalomon@google.com11959252012-04-06 20:13:38 +0000399 static const EGLint surfaceAttribList[] = {
bsalomon@google.com2858b312012-04-02 20:44:38 +0000400 EGL_NONE, EGL_NONE
401 };
402
403 EGLDisplay display = eglGetDisplay(GetDC(hWnd));
404 if (display == EGL_NO_DISPLAY ) {
405 return false;
406 }
407
408 // Initialize EGL
409 EGLint majorVersion, minorVersion;
410 if (!eglInitialize(display, &majorVersion, &minorVersion)) {
411 return false;
412 }
413
414 EGLint numConfigs;
415 if (!eglGetConfigs(display, NULL, 0, &numConfigs)) {
416 return false;
417 }
418
419 // Choose config
bsalomon@google.com11959252012-04-06 20:13:38 +0000420 bool foundConfig = false;
421 if (msaaSampleCount) {
422 static const int kConfigAttribListCnt =
423 SK_ARRAY_COUNT(configAttribList);
424 EGLint msaaConfigAttribList[kConfigAttribListCnt + 4];
425 memcpy(msaaConfigAttribList,
426 configAttribList,
427 sizeof(configAttribList));
428 SkASSERT(EGL_NONE == msaaConfigAttribList[kConfigAttribListCnt - 1]);
429 msaaConfigAttribList[kConfigAttribListCnt - 1] = EGL_SAMPLE_BUFFERS;
430 msaaConfigAttribList[kConfigAttribListCnt + 0] = 1;
431 msaaConfigAttribList[kConfigAttribListCnt + 1] = EGL_SAMPLES;
432 msaaConfigAttribList[kConfigAttribListCnt + 2] = msaaSampleCount;
433 msaaConfigAttribList[kConfigAttribListCnt + 3] = EGL_NONE;
bsalomon@google.com64cc8102013-03-05 20:06:05 +0000434 if (eglChooseConfig(display, configAttribList, eglConfig, 1, &numConfigs)) {
bsalomon@google.com11959252012-04-06 20:13:38 +0000435 SkASSERT(numConfigs > 0);
436 foundConfig = true;
437 }
438 }
439 if (!foundConfig) {
bsalomon@google.com64cc8102013-03-05 20:06:05 +0000440 if (!eglChooseConfig(display, configAttribList, eglConfig, 1, &numConfigs)) {
bsalomon@google.com11959252012-04-06 20:13:38 +0000441 return false;
442 }
bsalomon@google.com2858b312012-04-02 20:44:38 +0000443 }
444
445 // Create a surface
bsalomon@google.com64cc8102013-03-05 20:06:05 +0000446 EGLSurface surface = eglCreateWindowSurface(display, *eglConfig,
rmistry@google.comd6176b02012-08-23 18:14:13 +0000447 (EGLNativeWindowType)hWnd,
bsalomon@google.com2858b312012-04-02 20:44:38 +0000448 surfaceAttribList);
449 if (surface == EGL_NO_SURFACE) {
450 return false;
451 }
452
453 // Create a GL context
bsalomon@google.com64cc8102013-03-05 20:06:05 +0000454 EGLContext context = eglCreateContext(display, *eglConfig,
bsalomon@google.com2858b312012-04-02 20:44:38 +0000455 EGL_NO_CONTEXT,
456 contextAttribs );
457 if (context == EGL_NO_CONTEXT ) {
458 return false;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000459 }
460
bsalomon@google.com2858b312012-04-02 20:44:38 +0000461 // Make the context current
462 if (!eglMakeCurrent(display, surface, surface, context)) {
463 return false;
464 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000465
bsalomon@google.com2858b312012-04-02 20:44:38 +0000466 *eglDisplay = display;
467 *eglContext = context;
468 *eglSurface = surface;
469 return true;
470}
471
bsalomon@google.com64cc8102013-03-05 20:06:05 +0000472bool SkOSWindow::attachANGLE(int msaaSampleCount, AttachmentInfo* info) {
bsalomon@google.com2858b312012-04-02 20:44:38 +0000473 if (EGL_NO_DISPLAY == fDisplay) {
bsalomon@google.com11959252012-04-06 20:13:38 +0000474 bool bResult = create_ANGLE((HWND)fHWND,
475 msaaSampleCount,
476 &fDisplay,
477 &fContext,
bsalomon@google.com64cc8102013-03-05 20:06:05 +0000478 &fSurface,
479 &fConfig);
bsalomon@google.com2858b312012-04-02 20:44:38 +0000480 if (false == bResult) {
481 return false;
482 }
bsalomon@google.com82502e22013-01-24 20:47:18 +0000483 SkAutoTUnref<const GrGLInterface> intf(GrGLCreateANGLEInterface());
bsalomon@google.com2858b312012-04-02 20:44:38 +0000484
485 if (intf) {
borenet@google.com0dd5ceb2012-08-28 15:15:49 +0000486 ANGLE_GL_CALL(intf, ClearStencil(0));
487 ANGLE_GL_CALL(intf, ClearColor(0, 0, 0, 0));
488 ANGLE_GL_CALL(intf, StencilMask(0xffffffff));
489 ANGLE_GL_CALL(intf, Clear(GL_STENCIL_BUFFER_BIT |GL_COLOR_BUFFER_BIT));
bsalomon@google.com2858b312012-04-02 20:44:38 +0000490 }
491 }
492 if (eglMakeCurrent(fDisplay, fSurface, fSurface, fContext)) {
bsalomon@google.com64cc8102013-03-05 20:06:05 +0000493 eglGetConfigAttrib(fDisplay, fConfig, EGL_STENCIL_SIZE, &info->fStencilBits);
494 eglGetConfigAttrib(fDisplay, fConfig, EGL_SAMPLES, &info->fSampleCount);
495
bsalomon@google.com82502e22013-01-24 20:47:18 +0000496 SkAutoTUnref<const GrGLInterface> intf(GrGLCreateANGLEInterface());
bsalomon@google.com2858b312012-04-02 20:44:38 +0000497
498 if (intf ) {
borenet@google.com0dd5ceb2012-08-28 15:15:49 +0000499 ANGLE_GL_CALL(intf, Viewport(0, 0, SkScalarRound(this->width()),
bsalomon@google.com2858b312012-04-02 20:44:38 +0000500 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() {
bsalomon@google.com82502e22013-01-24 20:47:18 +0000521 SkAutoTUnref<const GrGLInterface> intf(GrGLCreateANGLEInterface());
bsalomon@google.com2858b312012-04-02 20:44:38 +0000522
523 if (intf) {
borenet@google.com0dd5ceb2012-08-28 15:15:49 +0000524 ANGLE_GL_CALL(intf, Flush());
bsalomon@google.com2858b312012-04-02 20:44:38 +0000525 }
526
527 eglSwapBuffers(fDisplay, fSurface);
528}
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000529#endif // SK_ANGLE
530#endif // SK_SUPPORT_GPU
bsalomon@google.com2858b312012-04-02 20:44:38 +0000531
bsalomon@google.com2858b312012-04-02 20:44:38 +0000532// return true on success
bsalomon@google.com64cc8102013-03-05 20:06:05 +0000533bool SkOSWindow::attach(SkBackEndTypes attachType, int msaaSampleCount, AttachmentInfo* info) {
bsalomon@google.com2858b312012-04-02 20:44:38 +0000534
535 // attach doubles as "windowResize" so we need to allo
536 // already bound states to pass through again
537 // TODO: split out the resize functionality
538// SkASSERT(kNone_BackEndType == fAttached);
539 bool result = true;
540
541 switch (attachType) {
542 case kNone_BackEndType:
543 // nothing to do
rmistry@google.comd6176b02012-08-23 18:14:13 +0000544 break;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000545#if SK_SUPPORT_GPU
bsalomon@google.com2858b312012-04-02 20:44:38 +0000546 case kNativeGL_BackEndType:
bsalomon@google.com64cc8102013-03-05 20:06:05 +0000547 result = attachGL(msaaSampleCount, info);
bsalomon@google.com2858b312012-04-02 20:44:38 +0000548 break;
549#if SK_ANGLE
550 case kANGLE_BackEndType:
bsalomon@google.com64cc8102013-03-05 20:06:05 +0000551 result = attachANGLE(msaaSampleCount, info);
bsalomon@google.com2858b312012-04-02 20:44:38 +0000552 break;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000553#endif // SK_ANGLE
554#endif // SK_SUPPORT_GPU
bsalomon@google.com2858b312012-04-02 20:44:38 +0000555 default:
556 SkASSERT(false);
557 result = false;
558 break;
559 }
560
561 if (result) {
562 fAttached = attachType;
563 }
564
565 return result;
566}
567
568void SkOSWindow::detach() {
569 switch (fAttached) {
570 case kNone_BackEndType:
571 // nothing to do
rmistry@google.comd6176b02012-08-23 18:14:13 +0000572 break;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000573#if SK_SUPPORT_GPU
bsalomon@google.com2858b312012-04-02 20:44:38 +0000574 case kNativeGL_BackEndType:
575 detachGL();
576 break;
577#if SK_ANGLE
578 case kANGLE_BackEndType:
579 detachANGLE();
580 break;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000581#endif // SK_ANGLE
582#endif // SK_SUPPORT_GPU
bsalomon@google.com2858b312012-04-02 20:44:38 +0000583 default:
584 SkASSERT(false);
585 break;
586 }
587 fAttached = kNone_BackEndType;
588}
589
590void SkOSWindow::present() {
591 switch (fAttached) {
592 case kNone_BackEndType:
593 // nothing to do
rmistry@google.comd6176b02012-08-23 18:14:13 +0000594 return;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000595#if SK_SUPPORT_GPU
bsalomon@google.com2858b312012-04-02 20:44:38 +0000596 case kNativeGL_BackEndType:
597 presentGL();
598 break;
599#if SK_ANGLE
600 case kANGLE_BackEndType:
601 presentANGLE();
602 break;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000603#endif // SK_ANGLE
604#endif // SK_SUPPORT_GPU
bsalomon@google.com2858b312012-04-02 20:44:38 +0000605 default:
606 SkASSERT(false);
607 break;
608 }
609}
610
611#endif