summaryrefslogtreecommitdiff
path: root/src/gallium/targets/wgl/wgl.c
blob: 18423e9f42837b48bb84df6874a302acc01d6217 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/**************************************************************************
 *
 * Copyright 2009-2010 VMware, Inc.
 * All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sub license, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
 * USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 * The above copyright notice and this permission notice (including the
 * next paragraph) shall be included in all copies or substantial portions
 * of the Software.
 *
 *
 **************************************************************************/

/**
 * @file
 * Softpipe/LLVMpipe support.
 *
 * @author Jose Fonseca <jfonseca@vmware.com>
 */


#include <windows.h>

#include "util/u_debug.h"
#include "util/debug.h"
#include "stw_winsys.h"
#include "stw_device.h"
#include "gdi/gdi_sw_winsys.h"
#include "pipe/p_screen.h"
#include "pipe/p_context.h"

#ifdef GALLIUM_SOFTPIPE
#include "softpipe/sp_texture.h"
#include "softpipe/sp_screen.h"
#include "softpipe/sp_public.h"
#endif

#ifdef GALLIUM_LLVMPIPE
#include "llvmpipe/lp_texture.h"
#include "llvmpipe/lp_screen.h"
#include "llvmpipe/lp_public.h"
#endif

#ifdef GALLIUM_D3D12
#include "d3d12/wgl/d3d12_wgl_public.h"
#endif

#ifdef GALLIUM_ZINK
#include "zink/zink_public.h"
#endif

#ifdef GALLIUM_LLVMPIPE
static boolean use_llvmpipe = FALSE;
#endif
#ifdef GALLIUM_D3D12
static boolean use_d3d12 = FALSE;
#endif
#ifdef GALLIUM_ZINK
static boolean use_zink = FALSE;
#endif

static const char *created_driver_name = NULL;

static struct pipe_screen *
wgl_screen_create_by_name(HDC hDC, const char* driver, struct sw_winsys *winsys)
{
   struct pipe_screen* screen = NULL;

#ifdef GALLIUM_LLVMPIPE
   if (strcmp(driver, "llvmpipe") == 0) {
      screen = llvmpipe_create_screen(winsys);
      if (screen)
         use_llvmpipe = TRUE;
   }
#endif
#ifdef GALLIUM_D3D12
   if (strcmp(driver, "d3d12") == 0) {
      screen = d3d12_wgl_create_screen(winsys, hDC);
      if (screen)
         use_d3d12 = TRUE;
   }
#endif
#ifdef GALLIUM_ZINK
   if (strcmp(driver, "zink") == 0) {
      screen = zink_create_screen(winsys, NULL);
      if (screen)
         use_zink = TRUE;
   }
#endif
#ifdef GALLIUM_SOFTPIPE
   if (strcmp(driver, "softpipe") == 0) {
      screen = softpipe_create_screen(winsys);
   }
#endif

   return screen;
}

static struct pipe_screen *
wgl_screen_create(HDC hDC)
{
   struct sw_winsys *winsys;
   UNUSED bool sw_only = env_var_as_boolean("LIBGL_ALWAYS_SOFTWARE", false);

   winsys = gdi_create_sw_winsys();
   if (!winsys)
      return NULL;

   const char *const drivers[] = {
      debug_get_option("GALLIUM_DRIVER", ""),
#ifdef GALLIUM_D3D12
      sw_only ? "" : "d3d12",
#endif
#ifdef GALLIUM_ZINK
      "zink",
#endif
#if defined(GALLIUM_LLVMPIPE)
      "llvmpipe",
#endif
#if defined(GALLIUM_SOFTPIPE)
      "softpipe",
#endif
   };

   /* If the default driver screen creation fails, fall back to the next option in the
    * sorted list. Don't do this if GALLIUM_DRIVER is specified.
    */
   for (unsigned i = 0; i < ARRAY_SIZE(drivers); ++i) {
      struct pipe_screen* screen = wgl_screen_create_by_name(hDC, drivers[i], winsys);
      if (screen) {
         created_driver_name = drivers[i];
         return screen;
      }
      if (i == 0 && drivers[i][0] != '\0')
         break;
   }

   winsys->destroy(winsys);
   return NULL;
}


static void
wgl_present(struct pipe_screen *screen,
            struct pipe_context *ctx,
            struct pipe_resource *res,
            HDC hDC)
{
   /* This will fail if any interposing layer (trace, debug, etc) has
    * been introduced between the gallium frontends and the pipe driver.
    *
    * Ideally this would get replaced with a call to
    * pipe_screen::flush_frontbuffer().
    *
    * Failing that, it may be necessary for intervening layers to wrap
    * other structs such as this stw_winsys as well...
    */

   struct sw_winsys *winsys = NULL;
   struct sw_displaytarget *dt = NULL;

#ifdef GALLIUM_LLVMPIPE
   if (use_llvmpipe) {
      winsys = llvmpipe_screen(screen)->winsys;
      dt = llvmpipe_resource(res)->dt;
      gdi_sw_display(winsys, dt, hDC);
      return;
   }
#endif

#ifdef GALLIUM_D3D12
   if (use_d3d12) {
      d3d12_wgl_present(screen, ctx, res, hDC);
      return;
   }
#endif

#ifdef GALLIUM_ZINK
   if (use_zink) {
      screen->flush_frontbuffer(screen, ctx, res, 0, 0, hDC, NULL);
      return;
   }
#endif

#ifdef GALLIUM_SOFTPIPE
   winsys = softpipe_screen(screen)->winsys,
   dt = softpipe_resource(res)->dt,
   gdi_sw_display(winsys, dt, hDC);
#endif
}


#if WINVER >= 0xA00
static boolean
wgl_get_adapter_luid(struct pipe_screen* screen,
   HDC hDC,
   LUID* adapter_luid)
{
   if (!stw_dev || !stw_dev->callbacks.pfnGetAdapterLuid)
      return false;

   stw_dev->callbacks.pfnGetAdapterLuid(hDC, adapter_luid);
   return true;
}
#endif


static unsigned
wgl_get_pfd_flags(struct pipe_screen *screen)
{
#ifdef GALLIUM_D3D12
   if (use_d3d12)
      return d3d12_wgl_get_pfd_flags(screen);
#endif
   return stw_pfd_gdi_support;
}


static struct stw_winsys_framebuffer *
wgl_create_framebuffer(struct pipe_screen *screen,
                       HWND hWnd,
                       int iPixelFormat)
{
#ifdef GALLIUM_D3D12
   if (use_d3d12)
      return d3d12_wgl_create_framebuffer(screen, hWnd, iPixelFormat);
#endif
   return NULL;
}

static const char *
wgl_get_name(void)
{
   return created_driver_name;
}


static const struct stw_winsys stw_winsys = {
   &wgl_screen_create,
   &wgl_present,
#if WINVER >= 0xA00
   &wgl_get_adapter_luid,
#else
   NULL, /* get_adapter_luid */
#endif
   NULL, /* shared_surface_open */
   NULL, /* shared_surface_close */
   NULL, /* compose */
   &wgl_get_pfd_flags,
   &wgl_create_framebuffer,
   &wgl_get_name,
};


EXTERN_C BOOL WINAPI
DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved);


BOOL WINAPI
DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
{
   switch (fdwReason) {
   case DLL_PROCESS_ATTACH:
      stw_init(&stw_winsys);
      stw_init_thread();
      break;

   case DLL_THREAD_ATTACH:
      stw_init_thread();
      break;

   case DLL_THREAD_DETACH:
      stw_cleanup_thread();
      break;

   case DLL_PROCESS_DETACH:
      if (lpvReserved == NULL) {
         // We're being unloaded from the process.
         stw_cleanup_thread();
         stw_cleanup();
      } else {
         // Process itself is terminating, and all threads and modules are
         // being detached.
         //
         // The order threads (including llvmpipe rasterizer threads) are
         // destroyed can not be relied up, so it's not safe to cleanup.
         //
         // However global destructors (e.g., LLVM's) will still be called, and
         // if Microsoft OPENGL32.DLL's DllMain is called after us, it will
         // still try to invoke DrvDeleteContext to destroys all outstanding,
         // so set stw_dev to NULL to return immediately if that happens.
         stw_dev = NULL;
      }
      break;
   }
   return TRUE;
}