graw_gdi.c revision 5d28d2f9d4eb783dbf738e36bf60c6c5275d1db9
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
38static LRESULT CALLBACK
39window_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
56static struct {
57   void (* draw)(void);
58} graw;
59
60struct pipe_screen *
61graw_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   UINT style = WS_VISIBLE | WS_TILEDWINDOW;
72   RECT rect;
73   HWND hWnd = NULL;
74   HDC hDC = NULL;
75
76   if (format != PIPE_FORMAT_R8G8B8A8_UNORM)
77      goto fail;
78
79   winsys = gdi_create_sw_winsys();
80   if (winsys == NULL)
81      goto fail;
82
83   screen = softpipe_create_screen(winsys);
84   if (screen == NULL)
85      goto fail;
86
87   wc.style = CS_OWNDC | CS_HREDRAW | CS_VREDRAW;
88   wc.lpfnWndProc = window_proc;
89   wc.lpszClassName = "graw-gdi";
90   wc.hInstance = GetModuleHandle(NULL);
91   wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
92   wc.hCursor = LoadCursor(NULL, IDC_ARROW);
93   wc.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1);
94   RegisterClassEx(&wc);
95
96   SetRect(&rect, 0, 0, width, height);
97   AdjustWindowRectEx(&rect, style, FALSE, 0);
98
99   hWnd = CreateWindowEx(0,
100                         wc.lpszClassName,
101                         wc.lpszClassName,
102                         style,
103                         x,
104                         y,
105                         rect.right - rect.left,
106                         rect.bottom - rect.top,
107                         NULL,
108                         NULL,
109                         wc.hInstance,
110                         0);
111   if (hWnd == NULL)
112      goto fail;
113
114   hDC = GetDC(hWnd);
115   if (hDC == NULL)
116      goto fail;
117
118   *handle = (void *)hDC;
119
120   return gallium_wrap_screen(screen);
121
122fail:
123   if (hWnd)
124      DestroyWindow(hWnd);
125
126   if (screen)
127      screen->destroy(screen);
128
129   return NULL;
130}
131
132void
133graw_set_display_func(void (* draw)(void))
134{
135   graw.draw = draw;
136}
137
138void
139graw_main_loop(void)
140{
141   for (;;) {
142      MSG msg;
143
144      while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
145         if (msg.message == WM_QUIT) {
146            return;
147         }
148         TranslateMessage(&msg);
149         DispatchMessage(&msg);
150      }
151
152      if (graw.draw) {
153         graw.draw();
154      }
155
156      Sleep(0);
157   }
158}
159