native_gdi.c revision 8977879ec91b21572abd9bb95dcd0e72ba49f753
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 "gdi/gdi_sw_winsys.h"
36
37#include "common/native_helper.h"
38#include "common/native.h"
39
40struct gdi_display {
41   struct native_display base;
42
43   HDC hDC;
44   struct native_event_handler *event_handler;
45
46   struct native_config *configs;
47   int num_configs;
48};
49
50struct gdi_surface {
51   struct native_surface base;
52
53   HWND hWnd;
54   enum pipe_format color_format;
55
56   struct gdi_display *gdpy;
57
58   unsigned int server_stamp;
59   unsigned int client_stamp;
60
61   struct resource_surface *rsurf;
62};
63
64static INLINE struct gdi_display *
65gdi_display(const struct native_display *ndpy)
66{
67   return (struct gdi_display *) ndpy;
68}
69
70static INLINE struct gdi_surface *
71gdi_surface(const struct native_surface *nsurf)
72{
73   return (struct gdi_surface *) nsurf;
74}
75
76/**
77 * Update the geometry of the surface.  This is a slow functions.
78 */
79static void
80gdi_surface_update_geometry(struct native_surface *nsurf)
81{
82   struct gdi_surface *gsurf = gdi_surface(nsurf);
83   RECT rect;
84   uint w, h;
85
86   GetClientRect(gsurf->hWnd, &rect);
87   w = rect.right - rect.left;
88   h = rect.bottom - rect.top;
89
90   if (resource_surface_set_size(gsurf->rsurf, w, h))
91      gsurf->server_stamp++;
92}
93
94/**
95 * Update the buffers of the surface.
96 */
97static boolean
98gdi_surface_update_buffers(struct native_surface *nsurf, uint buffer_mask)
99{
100   struct gdi_surface *gsurf = gdi_surface(nsurf);
101
102   if (gsurf->client_stamp != gsurf->server_stamp) {
103      gdi_surface_update_geometry(&gsurf->base);
104      gsurf->client_stamp = gsurf->server_stamp;
105   }
106
107   return resource_surface_add_resources(gsurf->rsurf, buffer_mask);
108}
109
110/**
111 * Emulate an invalidate event.
112 */
113static void
114gdi_surface_invalidate(struct native_surface *nsurf)
115{
116   struct gdi_surface *gsurf = gdi_surface(nsurf);
117   struct gdi_display *gdpy = gsurf->gdpy;
118
119   gsurf->server_stamp++;
120   gdpy->event_handler->invalid_surface(&gdpy->base,
121         &gsurf->base, gsurf->server_stamp);
122}
123
124static boolean
125gdi_surface_flush_frontbuffer(struct native_surface *nsurf)
126{
127   struct gdi_surface *gsurf = gdi_surface(nsurf);
128   HDC hDC;
129   boolean ret;
130
131   hDC = GetDC(gsurf->hWnd);
132   ret = resource_surface_present(gsurf->rsurf,
133         NATIVE_ATTACHMENT_FRONT_LEFT, (void *) hDC);
134   ReleaseDC(gsurf->hWnd, hDC);
135
136   /* force buffers to be updated in next validation call */
137   gdi_surface_invalidate(&gsurf->base);
138
139   return ret;
140}
141
142static boolean
143gdi_surface_swap_buffers(struct native_surface *nsurf)
144{
145   struct gdi_surface *gsurf = gdi_surface(nsurf);
146   HDC hDC;
147   boolean ret;
148
149   hDC = GetDC(gsurf->hWnd);
150   ret = resource_surface_present(gsurf->rsurf,
151         NATIVE_ATTACHMENT_BACK_LEFT, (void *) hDC);
152   ReleaseDC(gsurf->hWnd, hDC);
153
154   resource_surface_swap_buffers(gsurf->rsurf,
155         NATIVE_ATTACHMENT_FRONT_LEFT, NATIVE_ATTACHMENT_BACK_LEFT, TRUE);
156   /* the front/back buffers have been swapped */
157   gdi_surface_invalidate(&gsurf->base);
158
159   return ret;
160}
161
162static boolean
163gdi_surface_validate(struct native_surface *nsurf, uint attachment_mask,
164                        unsigned int *seq_num, struct pipe_resource **textures,
165                        int *width, int *height)
166{
167   struct gdi_surface *gsurf = gdi_surface(nsurf);
168   uint w, h;
169
170   if (!gdi_surface_update_buffers(&gsurf->base, attachment_mask))
171      return FALSE;
172
173   if (seq_num)
174      *seq_num = gsurf->client_stamp;
175
176   if (textures)
177      resource_surface_get_resources(gsurf->rsurf, textures, attachment_mask);
178
179   resource_surface_get_size(gsurf->rsurf, &w, &h);
180   if (width)
181      *width = w;
182   if (height)
183      *height = h;
184
185   return TRUE;
186}
187
188static void
189gdi_surface_wait(struct native_surface *nsurf)
190{
191   /* no-op */
192}
193
194static void
195gdi_surface_destroy(struct native_surface *nsurf)
196{
197   struct gdi_surface *gsurf = gdi_surface(nsurf);
198
199   resource_surface_destroy(gsurf->rsurf);
200   FREE(gsurf);
201}
202
203static struct native_surface *
204gdi_display_create_window_surface(struct native_display *ndpy,
205                                  EGLNativeWindowType win,
206                                  const struct native_config *nconf)
207{
208   struct gdi_display *gdpy = gdi_display(ndpy);
209   struct gdi_surface *gsurf;
210
211   gsurf = CALLOC_STRUCT(gdi_surface);
212   if (!gsurf)
213      return NULL;
214
215   gsurf->gdpy = gdpy;
216   gsurf->color_format = nconf->color_format;
217   gsurf->hWnd = (HWND) win;
218
219   gsurf->rsurf = resource_surface_create(gdpy->base.screen,
220         gsurf->color_format,
221         PIPE_BIND_RENDER_TARGET |
222         PIPE_BIND_SAMPLER_VIEW |
223         PIPE_BIND_DISPLAY_TARGET |
224         PIPE_BIND_SCANOUT);
225   if (!gsurf->rsurf) {
226      FREE(gsurf);
227      return NULL;
228   }
229
230   /* initialize the geometry */
231   gdi_surface_update_geometry(&gsurf->base);
232
233   gsurf->base.destroy = gdi_surface_destroy;
234   gsurf->base.swap_buffers = gdi_surface_swap_buffers;
235   gsurf->base.flush_frontbuffer = gdi_surface_flush_frontbuffer;
236   gsurf->base.validate = gdi_surface_validate;
237   gsurf->base.wait = gdi_surface_wait;
238
239   return &gsurf->base;
240}
241
242static int
243fill_color_formats(struct native_display *ndpy, enum pipe_format formats[8])
244{
245   struct pipe_screen *screen = ndpy->screen;
246   int i, count = 0;
247
248   enum pipe_format candidates[] = {
249      /* 32-bit */
250      PIPE_FORMAT_B8G8R8A8_UNORM,
251      PIPE_FORMAT_A8R8G8B8_UNORM,
252      /* 24-bit */
253      PIPE_FORMAT_B8G8R8X8_UNORM,
254      PIPE_FORMAT_X8R8G8B8_UNORM,
255      /* 16-bit */
256      PIPE_FORMAT_B5G6R5_UNORM
257   };
258
259   assert(Elements(candidates) <= 8);
260
261   for (i = 0; i < Elements(candidates); i++) {
262      if (screen->is_format_supported(screen, candidates[i],
263               PIPE_TEXTURE_2D, 0, PIPE_BIND_RENDER_TARGET, 0))
264         formats[count++] = candidates[i];
265   }
266
267   return count;
268}
269
270static const struct native_config **
271gdi_display_get_configs(struct native_display *ndpy, int *num_configs)
272{
273   struct gdi_display *gdpy = gdi_display(ndpy);
274   const struct native_config **configs;
275   int i;
276
277   /* first time */
278   if (!gdpy->configs) {
279      enum pipe_format formats[8];
280      int i, count;
281
282      count = fill_color_formats(&gdpy->base, formats);
283
284      gdpy->configs = CALLOC(count, sizeof(*gdpy->configs));
285      if (!gdpy->configs)
286         return NULL;
287
288      for (i = 0; i < count; i++) {
289         struct native_config *nconf = &gdpy->configs[i];
290
291         nconf->buffer_mask =
292            (1 << NATIVE_ATTACHMENT_FRONT_LEFT) |
293            (1 << NATIVE_ATTACHMENT_BACK_LEFT);
294         nconf->color_format = formats[i];
295
296         nconf->window_bit = TRUE;
297         nconf->slow_config = TRUE;
298      }
299
300      gdpy->num_configs = count;
301   }
302
303   configs = MALLOC(gdpy->num_configs * sizeof(*configs));
304   if (configs) {
305      for (i = 0; i < gdpy->num_configs; i++)
306         configs[i] = (const struct native_config *) &gdpy->configs[i];
307      if (num_configs)
308         *num_configs = gdpy->num_configs;
309   }
310   return configs;
311}
312
313static int
314gdi_display_get_param(struct native_display *ndpy,
315                         enum native_param_type param)
316{
317   int val;
318
319   switch (param) {
320   case NATIVE_PARAM_USE_NATIVE_BUFFER:
321      /* private buffers are allocated */
322      val = FALSE;
323      break;
324   default:
325      val = 0;
326      break;
327   }
328
329   return val;
330}
331
332static void
333gdi_display_destroy(struct native_display *ndpy)
334{
335   struct gdi_display *gdpy = gdi_display(ndpy);
336
337   if (gdpy->configs)
338      FREE(gdpy->configs);
339
340   gdpy->base.screen->destroy(gdpy->base.screen);
341
342   FREE(gdpy);
343}
344
345static struct native_display *
346gdi_create_display(HDC hDC, struct native_event_handler *event_handler,
347                   void *user_data)
348{
349   struct gdi_display *gdpy;
350   struct sw_winsys *winsys;
351
352   gdpy = CALLOC_STRUCT(gdi_display);
353   if (!gdpy)
354      return NULL;
355
356   gdpy->hDC = hDC;
357   gdpy->event_handler = event_handler;
358   gdpy->base.user_data = user_data;
359
360   winsys = gdi_create_sw_winsys();
361   if (!winsys) {
362      FREE(gdpy);
363      return NULL;
364   }
365
366   gdpy->base.screen = gdpy->event_handler->new_sw_screen(&gdpy->base, winsys);
367   if (!gdpy->base.screen) {
368      if (winsys->destroy)
369         winsys->destroy(winsys);
370      FREE(gdpy);
371      return NULL;
372   }
373
374   gdpy->base.destroy = gdi_display_destroy;
375   gdpy->base.get_param = gdi_display_get_param;
376
377   gdpy->base.get_configs = gdi_display_get_configs;
378   gdpy->base.create_window_surface = gdi_display_create_window_surface;
379
380   return &gdpy->base;
381}
382
383static struct native_display *
384native_create_display(void *dpy, struct native_event_handler *event_handler,
385                      void *user_data)
386{
387   return gdi_create_display((HDC) dpy, event_handler, user_data);
388}
389
390static const struct native_platform gdi_platform = {
391   "GDI", /* name */
392   native_create_display
393};
394
395const struct native_platform *
396native_get_gdi_platform(void)
397{
398   return &gdi_platform;
399}
400