1 /**************************************************************************
2 *
3 * Copyright 2010 VMware, Inc.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
17 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
18 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20 * USE OR OTHER DEALINGS IN THE SOFTWARE.
21 *
22 * The above copyright notice and this permission notice (including the
23 * next paragraph) shall be included in all copies or substantial portions
24 * of the Software.
25 *
26 *
27 **************************************************************************/
28
29 #include "gdi/gdi_sw_winsys.h"
30 #include "pipe/p_screen.h"
31 #include "state_tracker/graw.h"
32 #include "target-helpers/inline_debug_helper.h"
33 #include "target-helpers/inline_sw_helper.h"
34 #include <windows.h>
35
36
37 static LRESULT CALLBACK
window_proc(HWND hWnd,UINT uMsg,WPARAM wParam,LPARAM lParam)38 window_proc(HWND hWnd,
39 UINT uMsg,
40 WPARAM wParam,
41 LPARAM lParam)
42 {
43 switch (uMsg) {
44 case WM_DESTROY:
45 PostQuitMessage(0);
46 break;
47
48 default:
49 return DefWindowProc(hWnd, uMsg, wParam, lParam);
50 }
51
52 return 0;
53 }
54
55 static struct {
56 void (* draw)(void);
57 } graw;
58
59 struct pipe_screen *
graw_create_window_and_screen(int x,int y,unsigned width,unsigned height,enum pipe_format format,void ** handle)60 graw_create_window_and_screen(int x,
61 int y,
62 unsigned width,
63 unsigned height,
64 enum pipe_format format,
65 void **handle)
66 {
67 struct sw_winsys *winsys = NULL;
68 struct pipe_screen *screen = NULL;
69 WNDCLASSEX wc;
70 UINT style = WS_VISIBLE | WS_TILEDWINDOW;
71 RECT rect;
72 HWND hWnd = NULL;
73 HDC hDC = NULL;
74
75 if (format != PIPE_FORMAT_R8G8B8A8_UNORM)
76 goto fail;
77
78 winsys = gdi_create_sw_winsys();
79 if (winsys == NULL)
80 goto fail;
81
82 screen = sw_screen_create(winsys);
83 if (screen == NULL)
84 goto fail;
85
86 memset(&wc, 0, sizeof wc);
87 wc.cbSize = sizeof wc;
88 wc.style = CS_OWNDC | CS_HREDRAW | CS_VREDRAW;
89 wc.lpfnWndProc = window_proc;
90 wc.lpszClassName = "graw-gdi";
91 wc.hInstance = GetModuleHandle(NULL);
92 wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
93 wc.hCursor = LoadCursor(NULL, IDC_ARROW);
94 wc.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1);
95 RegisterClassEx(&wc);
96
97 SetRect(&rect, 0, 0, width, height);
98 AdjustWindowRectEx(&rect, style, FALSE, 0);
99
100 hWnd = CreateWindowEx(0,
101 wc.lpszClassName,
102 wc.lpszClassName,
103 style,
104 x,
105 y,
106 rect.right - rect.left,
107 rect.bottom - rect.top,
108 NULL,
109 NULL,
110 wc.hInstance,
111 0);
112 if (hWnd == NULL)
113 goto fail;
114
115 hDC = GetDC(hWnd);
116 if (hDC == NULL)
117 goto fail;
118
119 *handle = (void *)hDC;
120
121 return debug_screen_wrap(screen);
122
123 fail:
124 if (hWnd)
125 DestroyWindow(hWnd);
126
127 if (screen)
128 screen->destroy(screen);
129
130 return NULL;
131 }
132
133 void
graw_set_display_func(void (* draw)(void))134 graw_set_display_func(void (* draw)(void))
135 {
136 graw.draw = draw;
137 }
138
139 void
graw_main_loop(void)140 graw_main_loop(void)
141 {
142 for (;;) {
143 MSG msg;
144
145 while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
146 if (msg.message == WM_QUIT) {
147 return;
148 }
149 TranslateMessage(&msg);
150 DispatchMessage(&msg);
151 }
152
153 if (graw.draw) {
154 graw.draw();
155 }
156
157 Sleep(0);
158 }
159 }
160