• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2 * Copyright 2016 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7 
8 #include "include/gpu/vk/GrVkVulkan.h"
9 
10 #include "tools/sk_app/win/Window_win.h"
11 
12 #include <tchar.h>
13 #include <windows.h>
14 #include <windowsx.h>
15 
16 #include "src/core/SkUtils.h"
17 #include "tools/ModifierKey.h"
18 #include "tools/sk_app/WindowContext.h"
19 #include "tools/sk_app/win/WindowContextFactory_win.h"
20 
21 #ifdef SK_VULKAN
22 #include "tools/sk_app/VulkanWindowContext.h"
23 #endif
24 
25 namespace sk_app {
26 
27 static int gWindowX = CW_USEDEFAULT;
28 static int gWindowY = 0;
29 static int gWindowWidth = CW_USEDEFAULT;
30 static int gWindowHeight = 0;
31 
CreateNativeWindow(void * platformData)32 Window* Window::CreateNativeWindow(void* platformData) {
33     HINSTANCE hInstance = (HINSTANCE)platformData;
34 
35     Window_win* window = new Window_win();
36     if (!window->init(hInstance)) {
37         delete window;
38         return nullptr;
39     }
40 
41     return window;
42 }
43 
closeWindow()44 void Window_win::closeWindow() {
45     RECT r;
46     if (GetWindowRect(fHWnd, &r)) {
47         gWindowX = r.left;
48         gWindowY = r.top;
49         gWindowWidth = r.right - r.left;
50         gWindowHeight = r.bottom - r.top;
51     }
52     DestroyWindow(fHWnd);
53 }
54 
~Window_win()55 Window_win::~Window_win() {
56     this->closeWindow();
57 }
58 
59 LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
60 
61 
init(HINSTANCE hInstance)62 bool Window_win::init(HINSTANCE hInstance) {
63     fHInstance = hInstance ? hInstance : GetModuleHandle(nullptr);
64 
65     // The main window class name
66     static const TCHAR gSZWindowClass[] = _T("SkiaApp");
67 
68     static WNDCLASSEX wcex;
69     static bool wcexInit = false;
70     if (!wcexInit) {
71         wcex.cbSize = sizeof(WNDCLASSEX);
72 
73         wcex.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
74         wcex.lpfnWndProc = WndProc;
75         wcex.cbClsExtra = 0;
76         wcex.cbWndExtra = 0;
77         wcex.hInstance = fHInstance;
78         wcex.hIcon = LoadIcon(fHInstance, (LPCTSTR)IDI_WINLOGO);
79         wcex.hCursor = LoadCursor(nullptr, IDC_ARROW);
80         wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
81         wcex.lpszMenuName = nullptr;
82         wcex.lpszClassName = gSZWindowClass;
83         wcex.hIconSm = LoadIcon(fHInstance, (LPCTSTR)IDI_WINLOGO);
84 
85         if (!RegisterClassEx(&wcex)) {
86             return false;
87         }
88         wcexInit = true;
89     }
90 
91    /*
92     if (fullscreen)
93     {
94         DEVMODE dmScreenSettings;
95         // If full screen set the screen to maximum size of the users desktop and 32bit.
96         memset(&dmScreenSettings, 0, sizeof(dmScreenSettings));
97         dmScreenSettings.dmSize = sizeof(dmScreenSettings);
98         dmScreenSettings.dmPelsWidth = (unsigned long)width;
99         dmScreenSettings.dmPelsHeight = (unsigned long)height;
100         dmScreenSettings.dmBitsPerPel = 32;
101         dmScreenSettings.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;
102 
103         // Change the display settings to full screen.
104         ChangeDisplaySettings(&dmScreenSettings, CDS_FULLSCREEN);
105 
106         // Set the position of the window to the top left corner.
107         posX = posY = 0;
108     }
109     */
110  //   gIsFullscreen = fullscreen;
111 
112     fHWnd = CreateWindow(gSZWindowClass, nullptr, WS_OVERLAPPEDWINDOW,
113                          gWindowX, gWindowY, gWindowWidth, gWindowHeight,
114                          nullptr, nullptr, fHInstance, nullptr);
115     if (!fHWnd)
116     {
117         return false;
118     }
119 
120     SetWindowLongPtr(fHWnd, GWLP_USERDATA, (LONG_PTR)this);
121     RegisterTouchWindow(fHWnd, 0);
122 
123     return true;
124 }
125 
get_key(WPARAM vk)126 static Window::Key get_key(WPARAM vk) {
127     static const struct {
128         WPARAM      fVK;
129         Window::Key fKey;
130     } gPair[] = {
131         { VK_BACK, Window::Key::kBack },
132         { VK_CLEAR, Window::Key::kBack },
133         { VK_RETURN, Window::Key::kOK },
134         { VK_UP, Window::Key::kUp },
135         { VK_DOWN, Window::Key::kDown },
136         { VK_LEFT, Window::Key::kLeft },
137         { VK_RIGHT, Window::Key::kRight },
138         { VK_TAB, Window::Key::kTab },
139         { VK_PRIOR, Window::Key::kPageUp },
140         { VK_NEXT, Window::Key::kPageDown },
141         { VK_HOME, Window::Key::kHome },
142         { VK_END, Window::Key::kEnd },
143         { VK_DELETE, Window::Key::kDelete },
144         { VK_ESCAPE, Window::Key::kEscape },
145         { VK_SHIFT, Window::Key::kShift },
146         { VK_CONTROL, Window::Key::kCtrl },
147         { VK_MENU, Window::Key::kOption },
148         { 'A', Window::Key::kA },
149         { 'C', Window::Key::kC },
150         { 'V', Window::Key::kV },
151         { 'X', Window::Key::kX },
152         { 'Y', Window::Key::kY },
153         { 'Z', Window::Key::kZ },
154     };
155     for (size_t i = 0; i < SK_ARRAY_COUNT(gPair); i++) {
156         if (gPair[i].fVK == vk) {
157             return gPair[i].fKey;
158         }
159     }
160     return Window::Key::kNONE;
161 }
162 
get_modifiers(UINT message,WPARAM wParam,LPARAM lParam)163 static ModifierKey get_modifiers(UINT message, WPARAM wParam, LPARAM lParam) {
164     ModifierKey modifiers = ModifierKey::kNone;
165 
166     switch (message) {
167         case WM_UNICHAR:
168         case WM_CHAR:
169             if (0 == (lParam & (1 << 30))) {
170                 modifiers |= ModifierKey::kFirstPress;
171             }
172             if (lParam & (1 << 29)) {
173                 modifiers |= ModifierKey::kOption;
174             }
175             break;
176 
177         case WM_KEYDOWN:
178         case WM_SYSKEYDOWN:
179             if (0 == (lParam & (1 << 30))) {
180                 modifiers |= ModifierKey::kFirstPress;
181             }
182             if (lParam & (1 << 29)) {
183                 modifiers |= ModifierKey::kOption;
184             }
185             break;
186 
187         case WM_KEYUP:
188         case WM_SYSKEYUP:
189             if (lParam & (1 << 29)) {
190                 modifiers |= ModifierKey::kOption;
191             }
192             break;
193 
194         case WM_LBUTTONDOWN:
195         case WM_LBUTTONUP:
196         case WM_MOUSEMOVE:
197         case WM_MOUSEWHEEL:
198             if (wParam & MK_CONTROL) {
199                 modifiers |= ModifierKey::kControl;
200             }
201             if (wParam & MK_SHIFT) {
202                 modifiers |= ModifierKey::kShift;
203             }
204             break;
205     }
206 
207     return modifiers;
208 }
209 
WndProc(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam)210 LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
211 {
212     PAINTSTRUCT ps;
213     HDC hdc;
214 
215     Window_win* window = (Window_win*) GetWindowLongPtr(hWnd, GWLP_USERDATA);
216 
217     bool eventHandled = false;
218 
219     switch (message) {
220         case WM_PAINT:
221             hdc = BeginPaint(hWnd, &ps);
222             window->onPaint();
223             EndPaint(hWnd, &ps);
224             eventHandled = true;
225             break;
226 
227         case WM_CLOSE:
228             PostQuitMessage(0);
229             eventHandled = true;
230             break;
231 
232         case WM_ACTIVATE:
233             // disable/enable rendering here, depending on wParam != WA_INACTIVE
234             break;
235 
236         case WM_SIZE:
237             window->onResize(LOWORD(lParam), HIWORD(lParam));
238             eventHandled = true;
239             break;
240 
241         case WM_UNICHAR:
242             eventHandled = window->onChar((SkUnichar)wParam,
243                                           get_modifiers(message, wParam, lParam));
244             break;
245 
246         case WM_CHAR: {
247             const uint16_t* c = reinterpret_cast<uint16_t*>(&wParam);
248             eventHandled = window->onChar(SkUTF16_NextUnichar(&c),
249                                           get_modifiers(message, wParam, lParam));
250         } break;
251 
252         case WM_KEYDOWN:
253         case WM_SYSKEYDOWN:
254             eventHandled = window->onKey(get_key(wParam), InputState::kDown,
255                                          get_modifiers(message, wParam, lParam));
256             break;
257 
258         case WM_KEYUP:
259         case WM_SYSKEYUP:
260             eventHandled = window->onKey(get_key(wParam), InputState::kUp,
261                                          get_modifiers(message, wParam, lParam));
262             break;
263 
264         case WM_LBUTTONDOWN:
265         case WM_LBUTTONUP: {
266             int xPos = GET_X_LPARAM(lParam);
267             int yPos = GET_Y_LPARAM(lParam);
268 
269             //if (!gIsFullscreen)
270             //{
271             //    RECT rc = { 0, 0, 640, 480 };
272             //    AdjustWindowRect(&rc, WS_OVERLAPPEDWINDOW, FALSE);
273             //    xPos -= rc.left;
274             //    yPos -= rc.top;
275             //}
276 
277             InputState istate = ((wParam & MK_LBUTTON) != 0) ? InputState::kDown
278                                                                      : InputState::kUp;
279 
280             eventHandled = window->onMouse(xPos, yPos, istate,
281                                             get_modifiers(message, wParam, lParam));
282         } break;
283 
284         case WM_MOUSEMOVE: {
285             int xPos = GET_X_LPARAM(lParam);
286             int yPos = GET_Y_LPARAM(lParam);
287 
288             //if (!gIsFullscreen)
289             //{
290             //    RECT rc = { 0, 0, 640, 480 };
291             //    AdjustWindowRect(&rc, WS_OVERLAPPEDWINDOW, FALSE);
292             //    xPos -= rc.left;
293             //    yPos -= rc.top;
294             //}
295 
296             eventHandled = window->onMouse(xPos, yPos, InputState::kMove,
297                                            get_modifiers(message, wParam, lParam));
298         } break;
299 
300         case WM_MOUSEWHEEL:
301             eventHandled = window->onMouseWheel(GET_WHEEL_DELTA_WPARAM(wParam) > 0 ? +1.0f : -1.0f,
302                                                 get_modifiers(message, wParam, lParam));
303             break;
304 
305         case WM_TOUCH: {
306             uint16_t numInputs = LOWORD(wParam);
307             std::unique_ptr<TOUCHINPUT[]> inputs(new TOUCHINPUT[numInputs]);
308             if (GetTouchInputInfo((HTOUCHINPUT)lParam, numInputs, inputs.get(),
309                                   sizeof(TOUCHINPUT))) {
310                 POINT topLeft = {0, 0};
311                 ClientToScreen(hWnd, &topLeft);
312                 for (uint16_t i = 0; i < numInputs; ++i) {
313                     TOUCHINPUT ti = inputs[i];
314                     InputState state;
315                     if (ti.dwFlags & TOUCHEVENTF_DOWN) {
316                         state = InputState::kDown;
317                     } else if (ti.dwFlags & TOUCHEVENTF_MOVE) {
318                         state = InputState::kMove;
319                     } else if (ti.dwFlags & TOUCHEVENTF_UP) {
320                         state = InputState::kUp;
321                     } else {
322                         continue;
323                     }
324                     // TOUCHINPUT coordinates are in 100ths of pixels
325                     // Adjust for that, and make them window relative
326                     LONG tx = (ti.x / 100) - topLeft.x;
327                     LONG ty = (ti.y / 100) - topLeft.y;
328                     eventHandled = window->onTouch(ti.dwID, state, tx, ty) || eventHandled;
329                 }
330             }
331         } break;
332 
333         default:
334             return DefWindowProc(hWnd, message, wParam, lParam);
335     }
336 
337     return eventHandled ? 0 : 1;
338 }
339 
setTitle(const char * title)340 void Window_win::setTitle(const char* title) {
341     SetWindowTextA(fHWnd, title);
342 }
343 
show()344 void Window_win::show() {
345     ShowWindow(fHWnd, SW_SHOW);
346 }
347 
348 
attach(BackendType attachType)349 bool Window_win::attach(BackendType attachType) {
350     fBackend = attachType;
351 
352     switch (attachType) {
353         case kNativeGL_BackendType:
354             fWindowContext = window_context_factory::MakeGLForWin(fHWnd, fRequestedDisplayParams);
355             break;
356 #if SK_ANGLE
357         case kANGLE_BackendType:
358             fWindowContext =
359                     window_context_factory::MakeANGLEForWin(fHWnd, fRequestedDisplayParams);
360             break;
361 #endif
362 #ifdef SK_DAWN
363         case kDawn_BackendType:
364             fWindowContext =
365                     window_context_factory::MakeDawnD3D12ForWin(fHWnd, fRequestedDisplayParams);
366             break;
367 #endif
368         case kRaster_BackendType:
369             fWindowContext =
370                     window_context_factory::MakeRasterForWin(fHWnd, fRequestedDisplayParams);
371             break;
372 #ifdef SK_VULKAN
373         case kVulkan_BackendType:
374             fWindowContext =
375                     window_context_factory::MakeVulkanForWin(fHWnd, fRequestedDisplayParams);
376             break;
377 #endif
378     }
379     this->onBackendCreated();
380 
381     return (SkToBool(fWindowContext));
382 }
383 
onInval()384 void Window_win::onInval() {
385     InvalidateRect(fHWnd, nullptr, false);
386 }
387 
setRequestedDisplayParams(const DisplayParams & params,bool allowReattach)388 void Window_win::setRequestedDisplayParams(const DisplayParams& params, bool allowReattach) {
389     // GL on Windows doesn't let us change MSAA after the window is created
390     if (params.fMSAASampleCount != this->getRequestedDisplayParams().fMSAASampleCount
391             && allowReattach) {
392         // Need to change these early, so attach() creates the window context correctly
393         fRequestedDisplayParams = params;
394 
395         fWindowContext = nullptr;
396         this->closeWindow();
397         this->init(fHInstance);
398         this->attach(fBackend);
399     }
400 
401     INHERITED::setRequestedDisplayParams(params, allowReattach);
402 }
403 
404 }   // namespace sk_app
405