1f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org/**************************************************************************
2f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org *
3f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * Copyright 2009-2010 VMware, Inc.
4f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * All Rights Reserved.
5f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org *
6f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * Permission is hereby granted, free of charge, to any person obtaining a
7f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * copy of this software and associated documentation files (the
8f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * "Software"), to deal in the Software without restriction, including
9f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * without limitation the rights to use, copy, modify, merge, publish,
10f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * distribute, sub license, and/or sell copies of the Software, and to
11f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * permit persons to whom the Software is furnished to do so, subject to
12f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * the following conditions:
13f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org *
14f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
17f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
18f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * USE OR OTHER DEALINGS IN THE SOFTWARE.
21f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org *
22f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * The above copyright notice and this permission notice (including the
23f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * next paragraph) shall be included in all copies or substantial portions
24f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * of the Software.
25f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org *
26f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org *
27f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org **************************************************************************/
28f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
29f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org/**
30f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * @file
31f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * Softpipe/LLVMpipe support.
32f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org *
33f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * @author Jose Fonseca <jfonseca@vmware.com>
34f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org */
35f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
36f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
37f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include <windows.h>
38f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
39f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include "util/u_debug.h"
40f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include "stw_winsys.h"
41f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include "gdi/gdi_sw_winsys.h"
42f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
43f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include "softpipe/sp_texture.h"
44f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include "softpipe/sp_screen.h"
45f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include "softpipe/sp_public.h"
46f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
47f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#ifdef HAVE_LLVMPIPE
48f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include "llvmpipe/lp_texture.h"
49f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include "llvmpipe/lp_screen.h"
50f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include "llvmpipe/lp_public.h"
51f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#endif
52f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
53f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
54f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgstatic boolean use_llvmpipe = FALSE;
55f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
56f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
57f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgstatic struct pipe_screen *
58f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orggdi_screen_create(void)
59f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org{
60f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   const char *default_driver;
61f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   const char *driver;
62f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   struct pipe_screen *screen = NULL;
63f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   struct sw_winsys *winsys;
64f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
65f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   winsys = gdi_create_sw_winsys();
66f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   if(!winsys)
67f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      goto no_winsys;
68f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
69f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#ifdef HAVE_LLVMPIPE
70f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   default_driver = "llvmpipe";
71f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#else
72f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   default_driver = "softpipe";
73f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#endif
74f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
75f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   driver = debug_get_option("GALLIUM_DRIVER", default_driver);
76f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
77f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#ifdef HAVE_LLVMPIPE
78f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   if (strcmp(driver, "llvmpipe") == 0) {
79f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      screen = llvmpipe_create_screen( winsys );
80f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   }
81f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#endif
82f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
83f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   if (screen == NULL) {
84f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      screen = softpipe_create_screen( winsys );
85f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   } else {
86f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      use_llvmpipe = TRUE;
87f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   }
88f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
89f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   if(!screen)
90f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      goto no_screen;
91f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
92f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   return screen;
93f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
94f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgno_screen:
95f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   winsys->destroy(winsys);
96f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgno_winsys:
97f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   return NULL;
98f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org}
99f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
100f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
101f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgstatic void
102f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orggdi_present(struct pipe_screen *screen,
103f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org            struct pipe_resource *res,
104f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org            HDC hDC)
105f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org{
106f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   /* This will fail if any interposing layer (trace, debug, etc) has
107f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    * been introduced between the state-trackers and the pipe driver.
108f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    *
109f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    * Ideally this would get replaced with a call to
110f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    * pipe_screen::flush_frontbuffer().
111f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    *
112f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    * Failing that, it may be necessary for intervening layers to wrap
113f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    * other structs such as this stw_winsys as well...
114f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    */
115f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
116f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   struct sw_winsys *winsys = NULL;
117f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   struct sw_displaytarget *dt = NULL;
118f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
119f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#ifdef HAVE_LLVMPIPE
120f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   if (use_llvmpipe) {
121f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      winsys = llvmpipe_screen(screen)->winsys;
122f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      dt = llvmpipe_resource(res)->dt;
123f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      gdi_sw_display(winsys, dt, hDC);
124f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      return;
125f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   }
126f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#endif
127f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
128f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   winsys = softpipe_screen(screen)->winsys,
129f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   dt = softpipe_resource(res)->dt,
130f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   gdi_sw_display(winsys, dt, hDC);
131f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org}
132f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
133f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
134f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgstatic const struct stw_winsys stw_winsys = {
135f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   &gdi_screen_create,
136f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   &gdi_present,
137f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   NULL, /* get_adapter_luid */
138f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   NULL, /* shared_surface_open */
139f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   NULL, /* shared_surface_close */
140f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   NULL  /* compose */
141f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org};
142f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
143f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
144f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgBOOL WINAPI
145f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgDllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpReserved)
146f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org{
147f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   switch (fdwReason) {
148f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   case DLL_PROCESS_ATTACH:
149f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      stw_init(&stw_winsys);
150f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      stw_init_thread();
151f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      break;
152f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
153f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   case DLL_THREAD_ATTACH:
154f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      stw_init_thread();
155f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      break;
156f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
157f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   case DLL_THREAD_DETACH:
158f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      stw_cleanup_thread();
159f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      break;
160f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
161f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   case DLL_PROCESS_DETACH:
162f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      stw_cleanup_thread();
163f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      stw_cleanup();
164f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      break;
165f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   }
166f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   return TRUE;
167f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org}
168