graw/gdi: Initial commit.
[mesa.git] / src / gallium / targets / graw-gdi / graw_gdi.c
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 "softpipe/sp_public.h"
32 #include "state_tracker/graw.h"
33 #include "sw/sw_public.h"
34 #include "target-helpers/wrap_screen.h"
35 #include <windows.h>
36
37
38 static LRESULT CALLBACK
39 window_proc(HWND hWnd,
40 UINT uMsg,
41 WPARAM wParam,
42 LPARAM lParam)
43 {
44 switch (uMsg) {
45 case WM_DESTROY:
46 PostQuitMessage(0);
47 break;
48
49 default:
50 return DefWindowProc(hWnd, uMsg, wParam, lParam);
51 }
52
53 return 0;
54 }
55
56 static struct {
57 void (* draw)(void);
58 } graw;
59
60 struct pipe_screen *
61 graw_create_window_and_screen(int x,
62 int y,
63 unsigned width,
64 unsigned height,
65 enum pipe_format format,
66 void **handle)
67 {
68 struct sw_winsys *winsys = NULL;
69 struct pipe_screen *screen = NULL;
70 WNDCLASSEX wc = {sizeof(wc)};
71 HWND hWnd = NULL;
72 HDC hDC = NULL;
73
74 if (format != PIPE_FORMAT_R8G8B8A8_UNORM)
75 goto fail;
76
77 winsys = gdi_create_sw_winsys();
78 if (winsys == NULL)
79 goto fail;
80
81 screen = softpipe_create_screen(winsys);
82 if (screen == NULL)
83 goto fail;
84
85 wc.style = CS_OWNDC | CS_HREDRAW | CS_VREDRAW;
86 wc.lpfnWndProc = window_proc;
87 wc.lpszClassName = "graw-gdi";
88 wc.hInstance = GetModuleHandle(NULL);
89 wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
90 wc.hCursor = LoadCursor(NULL, IDC_ARROW);
91 wc.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1);
92 RegisterClassEx(&wc);
93
94 hWnd = CreateWindowEx(0,
95 wc.lpszClassName,
96 wc.lpszClassName,
97 WS_VISIBLE | WS_TILEDWINDOW,
98 x,
99 y,
100 width,
101 height,
102 NULL,
103 NULL,
104 wc.hInstance,
105 0);
106 if (hWnd == NULL)
107 goto fail;
108
109 hDC = GetDC(hWnd);
110 if (hDC == NULL)
111 goto fail;
112
113 *handle = (void *)hDC;
114
115 return gallium_wrap_screen(screen);
116
117 fail:
118 if (hWnd)
119 DestroyWindow(hWnd);
120
121 if (screen)
122 screen->destroy(screen);
123
124 return NULL;
125 }
126
127 void
128 graw_set_display_func(void (* draw)(void))
129 {
130 graw.draw = draw;
131 }
132
133 void
134 graw_main_loop(void)
135 {
136 for (;;) {
137 MSG msg;
138
139 while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
140 if (msg.message == WM_QUIT) {
141 return;
142 }
143 TranslateMessage(&msg);
144 DispatchMessage(&msg);
145 }
146
147 if (graw.draw) {
148 graw.draw();
149 }
150
151 Sleep(0);
152 }
153 }