native_gdi.c revision a27690931be9479353dd8ae0d52186f5c72f4380
1/*
2 * Mesa 3-D graphics library
3 * Version:  7.9
4 *
5 * Copyright (C) 2010 LunarG Inc.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23 * DEALINGS IN THE SOFTWARE.
24 *
25 * Authors:
26 *    Chia-I Wu <olv@lunarg.com>
27 */
28
29#include <windows.h>
30
31#include "pipe/p_compiler.h"
32#include "util/u_memory.h"
33#include "util/u_format.h"
34#include "util/u_inlines.h"
35#include "target-helpers/wrap_screen.h"
36#include "llvmpipe/lp_public.h"
37#include "softpipe/sp_public.h"
38#include "gdi/gdi_sw_winsys.h"
39
40#include "common/native_helper.h"
41#include "common/native.h"
42
43struct gdi_display {
44   struct native_display base;
45
46   HDC hDC;
47   struct native_event_handler *event_handler;
48
49   struct native_config *configs;
50   int num_configs;
51};
52
53struct gdi_surface {
54   struct native_surface base;
55
56   HWND hWnd;
57   enum pipe_format color_format;
58
59   struct gdi_display *gdpy;
60
61   unsigned int server_stamp;
62   unsigned int client_stamp;
63
64   struct resource_surface *rsurf;
65};
66
67static INLINE struct gdi_display *
68gdi_display(const struct native_display *ndpy)
69{
70   return (struct gdi_display *) ndpy;
71}
72
73static INLINE struct gdi_surface *
74gdi_surface(const struct native_surface *nsurf)
75{
76   return (struct gdi_surface *) nsurf;
77}
78
79/**
80 * Update the geometry of the surface.  This is a slow functions.
81 */
82static void
83gdi_surface_update_geometry(struct native_surface *nsurf)
84{
85   struct gdi_surface *gsurf = gdi_surface(nsurf);
86   RECT rect;
87   uint w, h;
88
89   GetClientRect(gsurf->hWnd, &rect);
90   w = rect.right - rect.left;
91   h = rect.bottom - rect.top;
92
93   if (resource_surface_set_size(gsurf->rsurf, w, h))
94      gsurf->server_stamp++;
95}
96
97/**
98 * Update the buffers of the surface.
99 */
100static boolean
101gdi_surface_update_buffers(struct native_surface *nsurf, uint buffer_mask)
102{
103   struct gdi_surface *gsurf = gdi_surface(nsurf);
104
105   if (gsurf->client_stamp != gsurf->server_stamp) {
106      gdi_surface_update_geometry(&gsurf->base);
107      gsurf->client_stamp = gsurf->server_stamp;
108   }
109
110   return resource_surface_add_resources(gsurf->rsurf, buffer_mask);
111}
112
113/**
114 * Emulate an invalidate event.
115 */
116static void
117gdi_surface_invalidate(struct native_surface *nsurf)
118{
119   struct gdi_surface *gsurf = gdi_surface(nsurf);
120   struct gdi_display *gdpy = gsurf->gdpy;
121
122   gsurf->server_stamp++;
123   gdpy->event_handler->invalid_surface(&gdpy->base,
124         &gsurf->base, gsurf->server_stamp);
125}
126
127static boolean
128gdi_surface_flush_frontbuffer(struct native_surface *nsurf)
129{
130   struct gdi_surface *gsurf = gdi_surface(nsurf);
131   HDC hDC;
132   boolean ret;
133
134   hDC = GetDC(gsurf->hWnd);
135   ret = resource_surface_present(gsurf->rsurf,
136         NATIVE_ATTACHMENT_FRONT_LEFT, (void *) hDC);
137   ReleaseDC(gsurf->hWnd, hDC);
138
139   /* force buffers to be updated in next validation call */
140   gdi_surface_invalidate(&gsurf->base);
141
142   return ret;
143}
144
145static boolean
146gdi_surface_swap_buffers(struct native_surface *nsurf)
147{
148   struct gdi_surface *gsurf = gdi_surface(nsurf);
149   HDC hDC;
150   boolean ret;
151
152   hDC = GetDC(gsurf->hWnd);
153   ret = resource_surface_present(gsurf->rsurf,
154         NATIVE_ATTACHMENT_BACK_LEFT, (void *) hDC);
155   ReleaseDC(gsurf->hWnd, hDC);
156
157   resource_surface_swap_buffers(gsurf->rsurf,
158         NATIVE_ATTACHMENT_FRONT_LEFT, NATIVE_ATTACHMENT_BACK_LEFT, TRUE);
159   /* the front/back buffers have been swapped */
160   gdi_surface_invalidate(&gsurf->base);
161
162   return ret;
163}
164
165static boolean
166gdi_surface_validate(struct native_surface *nsurf, uint attachment_mask,
167                        unsigned int *seq_num, struct pipe_resource **textures,
168                        int *width, int *height)
169{
170   struct gdi_surface *gsurf = gdi_surface(nsurf);
171   uint w, h;
172
173   if (!gdi_surface_update_buffers(&gsurf->base, attachment_mask))
174      return FALSE;
175
176   if (seq_num)
177      *seq_num = gsurf->client_stamp;
178
179   if (textures)
180      resource_surface_get_resources(gsurf->rsurf, textures, attachment_mask);
181
182   resource_surface_get_size(gsurf->rsurf, &w, &h);
183   if (width)
184      *width = w;
185   if (height)
186      *height = h;
187
188   return TRUE;
189}
190
191static void
192gdi_surface_wait(struct native_surface *nsurf)
193{
194   /* no-op */
195}
196
197static void
198gdi_surface_destroy(struct native_surface *nsurf)
199{
200   struct gdi_surface *gsurf = gdi_surface(nsurf);
201
202   resource_surface_destroy(gsurf->rsurf);
203   FREE(gsurf);
204}
205
206static struct native_surface *
207gdi_display_create_window_surface(struct native_display *ndpy,
208                                  EGLNativeWindowType win,
209                                  const struct native_config *nconf)
210{
211   struct gdi_display *gdpy = gdi_display(ndpy);
212   struct gdi_surface *gsurf;
213
214   gsurf = CALLOC_STRUCT(gdi_surface);
215   if (!gsurf)
216      return NULL;
217
218   gsurf->gdpy = gdpy;
219   gsurf->color_format = nconf->color_format;
220   gsurf->hWnd = (HWND) win;
221
222   gsurf->rsurf = resource_surface_create(gdpy->base.screen,
223         gsurf->color_format,
224         PIPE_BIND_RENDER_TARGET |
225         PIPE_BIND_SAMPLER_VIEW |
226         PIPE_BIND_DISPLAY_TARGET |
227         PIPE_BIND_SCANOUT);
228   if (!gsurf->rsurf) {
229      FREE(gsurf);
230      return NULL;
231   }
232
233   /* initialize the geometry */
234   gdi_surface_update_geometry(&gsurf->base);
235
236   gsurf->base.destroy = gdi_surface_destroy;
237   gsurf->base.swap_buffers = gdi_surface_swap_buffers;
238   gsurf->base.flush_frontbuffer = gdi_surface_flush_frontbuffer;
239   gsurf->base.validate = gdi_surface_validate;
240   gsurf->base.wait = gdi_surface_wait;
241
242   return &gsurf->base;
243}
244
245static int
246fill_color_formats(struct native_display *ndpy, enum pipe_format formats[8])
247{
248   struct pipe_screen *screen = ndpy->screen;
249   int i, count = 0;
250
251   enum pipe_format candidates[] = {
252      /* 32-bit */
253      PIPE_FORMAT_B8G8R8A8_UNORM,
254      PIPE_FORMAT_A8R8G8B8_UNORM,
255      /* 24-bit */
256      PIPE_FORMAT_B8G8R8X8_UNORM,
257      PIPE_FORMAT_X8R8G8B8_UNORM,
258      /* 16-bit */
259      PIPE_FORMAT_B5G6R5_UNORM
260   };
261
262   assert(Elements(candidates) <= 8);
263
264   for (i = 0; i < Elements(candidates); i++) {
265      if (screen->is_format_supported(screen, candidates[i],
266               PIPE_TEXTURE_2D, 0, PIPE_BIND_RENDER_TARGET, 0))
267         formats[count++] = candidates[i];
268   }
269
270   return count;
271}
272
273static const struct native_config **
274gdi_display_get_configs(struct native_display *ndpy, int *num_configs)
275{
276   struct gdi_display *gdpy = gdi_display(ndpy);
277   const struct native_config **configs;
278   int i;
279
280   /* first time */
281   if (!gdpy->configs) {
282      enum pipe_format formats[8];
283      int i, count;
284
285      count = fill_color_formats(&gdpy->base, formats);
286
287      gdpy->configs = CALLOC(count, sizeof(*gdpy->configs));
288      if (!gdpy->configs)
289         return NULL;
290
291      for (i = 0; i < count; i++) {
292         struct native_config *nconf = &gdpy->configs[i];
293
294         nconf->buffer_mask =
295            (1 << NATIVE_ATTACHMENT_FRONT_LEFT) |
296            (1 << NATIVE_ATTACHMENT_BACK_LEFT);
297         nconf->color_format = formats[i];
298
299         nconf->window_bit = TRUE;
300         nconf->slow_config = TRUE;
301      }
302
303      gdpy->num_configs = count;
304   }
305
306   configs = MALLOC(gdpy->num_configs * sizeof(*configs));
307   if (configs) {
308      for (i = 0; i < gdpy->num_configs; i++)
309         configs[i] = (const struct native_config *) &gdpy->configs[i];
310      if (num_configs)
311         *num_configs = gdpy->num_configs;
312   }
313   return configs;
314}
315
316static int
317gdi_display_get_param(struct native_display *ndpy,
318                         enum native_param_type param)
319{
320   int val;
321
322   switch (param) {
323   case NATIVE_PARAM_USE_NATIVE_BUFFER:
324      /* private buffers are allocated */
325      val = FALSE;
326      break;
327   default:
328      val = 0;
329      break;
330   }
331
332   return val;
333}
334
335static void
336gdi_display_destroy(struct native_display *ndpy)
337{
338   struct gdi_display *gdpy = gdi_display(ndpy);
339
340   if (gdpy->configs)
341      FREE(gdpy->configs);
342
343   gdpy->base.screen->destroy(gdpy->base.screen);
344
345   FREE(gdpy);
346}
347
348static struct native_display *
349gdi_create_display(HDC hDC, struct pipe_screen *screen,
350                   struct native_event_handler *event_handler)
351{
352   struct gdi_display *gdpy;
353
354   gdpy = CALLOC_STRUCT(gdi_display);
355   if (!gdpy)
356      return NULL;
357
358   gdpy->hDC = hDC;
359   gdpy->event_handler = event_handler;
360
361   gdpy->base.screen = screen;
362
363   gdpy->base.destroy = gdi_display_destroy;
364   gdpy->base.get_param = gdi_display_get_param;
365
366   gdpy->base.get_configs = gdi_display_get_configs;
367   gdpy->base.create_window_surface = gdi_display_create_window_surface;
368
369   return &gdpy->base;
370}
371
372static struct pipe_screen *
373gdi_create_screen(void)
374{
375   struct sw_winsys *winsys;
376   struct pipe_screen *screen = NULL;
377
378   winsys = gdi_create_sw_winsys();
379   if (!winsys)
380      return NULL;
381
382#if defined(GALLIUM_LLVMPIPE)
383   if (!screen && !debug_get_bool_option("GALLIUM_NO_LLVM", FALSE))
384      screen = llvmpipe_create_screen(winsys);
385#endif
386   if (!screen)
387      screen = softpipe_create_screen(winsys);
388
389   if (!screen) {
390      if (winsys->destroy)
391         winsys->destroy(winsys);
392      return NULL;
393   }
394
395   return gallium_wrap_screen(screen);
396}
397
398struct native_probe *
399native_create_probe(EGLNativeDisplayType dpy)
400{
401   return NULL;
402}
403
404enum native_probe_result
405native_get_probe_result(struct native_probe *nprobe)
406{
407   return NATIVE_PROBE_UNKNOWN;
408}
409
410const char *
411native_get_name(void)
412{
413   return "GDI";
414}
415
416struct native_display *
417native_create_display(EGLNativeDisplayType dpy,
418                      struct native_event_handler *event_handler)
419{
420   struct pipe_screen *screen;
421
422   screen = gdi_create_screen();
423   if (!screen)
424      return NULL;
425
426   return gdi_create_display((HDC) dpy, screen, event_handler);
427}
428