native_gdi.c revision e968975cb57eb854769292f7c6ff773c64a386c3
144d362409d5469aed47d19e7908d19bd194493aThomas Graf/*
244d362409d5469aed47d19e7908d19bd194493aThomas Graf * Mesa 3-D graphics library
344d362409d5469aed47d19e7908d19bd194493aThomas Graf * Version:  7.9
444d362409d5469aed47d19e7908d19bd194493aThomas Graf *
544d362409d5469aed47d19e7908d19bd194493aThomas Graf * Copyright (C) 2010 LunarG Inc.
644d362409d5469aed47d19e7908d19bd194493aThomas Graf *
744d362409d5469aed47d19e7908d19bd194493aThomas Graf * Permission is hereby granted, free of charge, to any person obtaining a
844d362409d5469aed47d19e7908d19bd194493aThomas Graf * copy of this software and associated documentation files (the "Software"),
944d362409d5469aed47d19e7908d19bd194493aThomas Graf * to deal in the Software without restriction, including without limitation
1044d362409d5469aed47d19e7908d19bd194493aThomas Graf * the rights to use, copy, modify, merge, publish, distribute, sublicense,
1144d362409d5469aed47d19e7908d19bd194493aThomas Graf * and/or sell copies of the Software, and to permit persons to whom the
1244d362409d5469aed47d19e7908d19bd194493aThomas Graf * Software is furnished to do so, subject to the following conditions:
1344d362409d5469aed47d19e7908d19bd194493aThomas Graf *
1444d362409d5469aed47d19e7908d19bd194493aThomas Graf * The above copyright notice and this permission notice shall be included
1544d362409d5469aed47d19e7908d19bd194493aThomas Graf * in all copies or substantial portions of the Software.
1644d362409d5469aed47d19e7908d19bd194493aThomas Graf *
1744d362409d5469aed47d19e7908d19bd194493aThomas Graf * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1844d362409d5469aed47d19e7908d19bd194493aThomas Graf * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1944d362409d5469aed47d19e7908d19bd194493aThomas Graf * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
2044d362409d5469aed47d19e7908d19bd194493aThomas Graf * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
2144d362409d5469aed47d19e7908d19bd194493aThomas Graf * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
2244d362409d5469aed47d19e7908d19bd194493aThomas Graf * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
2344d362409d5469aed47d19e7908d19bd194493aThomas Graf * DEALINGS IN THE SOFTWARE.
2444d362409d5469aed47d19e7908d19bd194493aThomas Graf *
2544d362409d5469aed47d19e7908d19bd194493aThomas Graf * Authors:
2644d362409d5469aed47d19e7908d19bd194493aThomas Graf *    Chia-I Wu <olv@lunarg.com>
2744d362409d5469aed47d19e7908d19bd194493aThomas Graf */
2844d362409d5469aed47d19e7908d19bd194493aThomas Graf
2944d362409d5469aed47d19e7908d19bd194493aThomas Graf#include <windows.h>
3044d362409d5469aed47d19e7908d19bd194493aThomas Graf
3144d362409d5469aed47d19e7908d19bd194493aThomas Graf#include "pipe/p_compiler.h"
3244d362409d5469aed47d19e7908d19bd194493aThomas Graf#include "util/u_memory.h"
3344d362409d5469aed47d19e7908d19bd194493aThomas Graf#include "util/u_format.h"
3444d362409d5469aed47d19e7908d19bd194493aThomas Graf#include "util/u_inlines.h"
3544d362409d5469aed47d19e7908d19bd194493aThomas Graf#include "gdi/gdi_sw_winsys.h"
3644d362409d5469aed47d19e7908d19bd194493aThomas Graf
3744d362409d5469aed47d19e7908d19bd194493aThomas Graf#include "common/native_helper.h"
3844d362409d5469aed47d19e7908d19bd194493aThomas Graf#include "common/native.h"
3944d362409d5469aed47d19e7908d19bd194493aThomas Graf
4044d362409d5469aed47d19e7908d19bd194493aThomas Grafstruct gdi_display {
4144d362409d5469aed47d19e7908d19bd194493aThomas Graf   struct native_display base;
4244d362409d5469aed47d19e7908d19bd194493aThomas Graf
4344d362409d5469aed47d19e7908d19bd194493aThomas Graf   HDC hDC;
4444d362409d5469aed47d19e7908d19bd194493aThomas Graf   struct native_event_handler *event_handler;
4544d362409d5469aed47d19e7908d19bd194493aThomas Graf
4644d362409d5469aed47d19e7908d19bd194493aThomas Graf   struct native_config *configs;
4744d362409d5469aed47d19e7908d19bd194493aThomas Graf   int num_configs;
4844d362409d5469aed47d19e7908d19bd194493aThomas Graf};
4944d362409d5469aed47d19e7908d19bd194493aThomas Graf
5044d362409d5469aed47d19e7908d19bd194493aThomas Grafstruct gdi_surface {
5144d362409d5469aed47d19e7908d19bd194493aThomas Graf   struct native_surface base;
5244d362409d5469aed47d19e7908d19bd194493aThomas Graf
5344d362409d5469aed47d19e7908d19bd194493aThomas Graf   HWND hWnd;
5444d362409d5469aed47d19e7908d19bd194493aThomas Graf   enum pipe_format color_format;
5544d362409d5469aed47d19e7908d19bd194493aThomas Graf
5644d362409d5469aed47d19e7908d19bd194493aThomas Graf   struct gdi_display *gdpy;
5744d362409d5469aed47d19e7908d19bd194493aThomas Graf
5844d362409d5469aed47d19e7908d19bd194493aThomas Graf   unsigned int server_stamp;
5944d362409d5469aed47d19e7908d19bd194493aThomas Graf   unsigned int client_stamp;
6044d362409d5469aed47d19e7908d19bd194493aThomas Graf
6144d362409d5469aed47d19e7908d19bd194493aThomas Graf   struct resource_surface *rsurf;
6244d362409d5469aed47d19e7908d19bd194493aThomas Graf};
6344d362409d5469aed47d19e7908d19bd194493aThomas Graf
6444d362409d5469aed47d19e7908d19bd194493aThomas Grafstatic INLINE struct gdi_display *
6544d362409d5469aed47d19e7908d19bd194493aThomas Grafgdi_display(const struct native_display *ndpy)
6644d362409d5469aed47d19e7908d19bd194493aThomas Graf{
6744d362409d5469aed47d19e7908d19bd194493aThomas Graf   return (struct gdi_display *) ndpy;
6844d362409d5469aed47d19e7908d19bd194493aThomas Graf}
6944d362409d5469aed47d19e7908d19bd194493aThomas Graf
7044d362409d5469aed47d19e7908d19bd194493aThomas Grafstatic INLINE struct gdi_surface *
7144d362409d5469aed47d19e7908d19bd194493aThomas Grafgdi_surface(const struct native_surface *nsurf)
7244d362409d5469aed47d19e7908d19bd194493aThomas Graf{
7344d362409d5469aed47d19e7908d19bd194493aThomas Graf   return (struct gdi_surface *) nsurf;
7444d362409d5469aed47d19e7908d19bd194493aThomas Graf}
7544d362409d5469aed47d19e7908d19bd194493aThomas Graf
7644d362409d5469aed47d19e7908d19bd194493aThomas Graf/**
7744d362409d5469aed47d19e7908d19bd194493aThomas Graf * Update the geometry of the surface.  This is a slow functions.
7844d362409d5469aed47d19e7908d19bd194493aThomas Graf */
7944d362409d5469aed47d19e7908d19bd194493aThomas Grafstatic void
8044d362409d5469aed47d19e7908d19bd194493aThomas Grafgdi_surface_update_geometry(struct native_surface *nsurf)
8144d362409d5469aed47d19e7908d19bd194493aThomas Graf{
8244d362409d5469aed47d19e7908d19bd194493aThomas Graf   struct gdi_surface *gsurf = gdi_surface(nsurf);
8344d362409d5469aed47d19e7908d19bd194493aThomas Graf   RECT rect;
8444d362409d5469aed47d19e7908d19bd194493aThomas Graf   uint w, h;
8544d362409d5469aed47d19e7908d19bd194493aThomas Graf
8644d362409d5469aed47d19e7908d19bd194493aThomas Graf   GetClientRect(gsurf->hWnd, &rect);
8744d362409d5469aed47d19e7908d19bd194493aThomas Graf   w = rect.right - rect.left;
8844d362409d5469aed47d19e7908d19bd194493aThomas Graf   h = rect.bottom - rect.top;
8944d362409d5469aed47d19e7908d19bd194493aThomas Graf
9044d362409d5469aed47d19e7908d19bd194493aThomas Graf   if (resource_surface_set_size(gsurf->rsurf, w, h))
9144d362409d5469aed47d19e7908d19bd194493aThomas Graf      gsurf->server_stamp++;
9244d362409d5469aed47d19e7908d19bd194493aThomas Graf}
9344d362409d5469aed47d19e7908d19bd194493aThomas Graf
9444d362409d5469aed47d19e7908d19bd194493aThomas Graf/**
9544d362409d5469aed47d19e7908d19bd194493aThomas Graf * Update the buffers of the surface.
9644d362409d5469aed47d19e7908d19bd194493aThomas Graf */
9744d362409d5469aed47d19e7908d19bd194493aThomas Grafstatic boolean
9844d362409d5469aed47d19e7908d19bd194493aThomas Grafgdi_surface_update_buffers(struct native_surface *nsurf, uint buffer_mask)
9944d362409d5469aed47d19e7908d19bd194493aThomas Graf{
10044d362409d5469aed47d19e7908d19bd194493aThomas Graf   struct gdi_surface *gsurf = gdi_surface(nsurf);
10144d362409d5469aed47d19e7908d19bd194493aThomas Graf
10244d362409d5469aed47d19e7908d19bd194493aThomas Graf   if (gsurf->client_stamp != gsurf->server_stamp) {
10344d362409d5469aed47d19e7908d19bd194493aThomas Graf      gdi_surface_update_geometry(&gsurf->base);
10444d362409d5469aed47d19e7908d19bd194493aThomas Graf      gsurf->client_stamp = gsurf->server_stamp;
10544d362409d5469aed47d19e7908d19bd194493aThomas Graf   }
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_present(struct native_surface *nsurf,
164                    enum native_attachment natt,
165                    boolean preserve,
166                    uint swap_interval)
167{
168   boolean ret;
169
170   if (preserve || swap_interval)
171      return FALSE;
172
173   switch (natt) {
174   case NATIVE_ATTACHMENT_FRONT_LEFT:
175      ret = gdi_surface_flush_frontbuffer(nsurf);
176      break;
177   case NATIVE_ATTACHMENT_BACK_LEFT:
178      ret = gdi_surface_swap_buffers(nsurf);
179      break;
180   default:
181      ret = FALSE;
182      break;
183   }
184
185   return ret;
186}
187
188static boolean
189gdi_surface_validate(struct native_surface *nsurf, uint attachment_mask,
190                        unsigned int *seq_num, struct pipe_resource **textures,
191                        int *width, int *height)
192{
193   struct gdi_surface *gsurf = gdi_surface(nsurf);
194   uint w, h;
195
196   if (!gdi_surface_update_buffers(&gsurf->base, attachment_mask))
197      return FALSE;
198
199   if (seq_num)
200      *seq_num = gsurf->client_stamp;
201
202   if (textures)
203      resource_surface_get_resources(gsurf->rsurf, textures, attachment_mask);
204
205   resource_surface_get_size(gsurf->rsurf, &w, &h);
206   if (width)
207      *width = w;
208   if (height)
209      *height = h;
210
211   return TRUE;
212}
213
214static void
215gdi_surface_wait(struct native_surface *nsurf)
216{
217   /* no-op */
218}
219
220static void
221gdi_surface_destroy(struct native_surface *nsurf)
222{
223   struct gdi_surface *gsurf = gdi_surface(nsurf);
224
225   resource_surface_destroy(gsurf->rsurf);
226   FREE(gsurf);
227}
228
229static struct native_surface *
230gdi_display_create_window_surface(struct native_display *ndpy,
231                                  EGLNativeWindowType win,
232                                  const struct native_config *nconf)
233{
234   struct gdi_display *gdpy = gdi_display(ndpy);
235   struct gdi_surface *gsurf;
236
237   gsurf = CALLOC_STRUCT(gdi_surface);
238   if (!gsurf)
239      return NULL;
240
241   gsurf->gdpy = gdpy;
242   gsurf->color_format = nconf->color_format;
243   gsurf->hWnd = (HWND) win;
244
245   gsurf->rsurf = resource_surface_create(gdpy->base.screen,
246         gsurf->color_format,
247         PIPE_BIND_RENDER_TARGET |
248         PIPE_BIND_SAMPLER_VIEW |
249         PIPE_BIND_DISPLAY_TARGET |
250         PIPE_BIND_SCANOUT);
251   if (!gsurf->rsurf) {
252      FREE(gsurf);
253      return NULL;
254   }
255
256   /* initialize the geometry */
257   gdi_surface_update_geometry(&gsurf->base);
258
259   gsurf->base.destroy = gdi_surface_destroy;
260   gsurf->base.present = gdi_surface_present;
261   gsurf->base.validate = gdi_surface_validate;
262   gsurf->base.wait = gdi_surface_wait;
263
264   return &gsurf->base;
265}
266
267static int
268fill_color_formats(struct native_display *ndpy, enum pipe_format formats[8])
269{
270   struct pipe_screen *screen = ndpy->screen;
271   int i, count = 0;
272
273   enum pipe_format candidates[] = {
274      /* 32-bit */
275      PIPE_FORMAT_B8G8R8A8_UNORM,
276      PIPE_FORMAT_A8R8G8B8_UNORM,
277      /* 24-bit */
278      PIPE_FORMAT_B8G8R8X8_UNORM,
279      PIPE_FORMAT_X8R8G8B8_UNORM,
280      /* 16-bit */
281      PIPE_FORMAT_B5G6R5_UNORM
282   };
283
284   assert(Elements(candidates) <= 8);
285
286   for (i = 0; i < Elements(candidates); i++) {
287      if (screen->is_format_supported(screen, candidates[i],
288               PIPE_TEXTURE_2D, 0, PIPE_BIND_RENDER_TARGET))
289         formats[count++] = candidates[i];
290   }
291
292   return count;
293}
294
295static const struct native_config **
296gdi_display_get_configs(struct native_display *ndpy, int *num_configs)
297{
298   struct gdi_display *gdpy = gdi_display(ndpy);
299   const struct native_config **configs;
300   int i;
301
302   /* first time */
303   if (!gdpy->configs) {
304      enum pipe_format formats[8];
305      int i, count;
306
307      count = fill_color_formats(&gdpy->base, formats);
308
309      gdpy->configs = CALLOC(count, sizeof(*gdpy->configs));
310      if (!gdpy->configs)
311         return NULL;
312
313      for (i = 0; i < count; i++) {
314         struct native_config *nconf = &gdpy->configs[i];
315
316         nconf->buffer_mask =
317            (1 << NATIVE_ATTACHMENT_FRONT_LEFT) |
318            (1 << NATIVE_ATTACHMENT_BACK_LEFT);
319         nconf->color_format = formats[i];
320
321         nconf->window_bit = TRUE;
322      }
323
324      gdpy->num_configs = count;
325   }
326
327   configs = MALLOC(gdpy->num_configs * sizeof(*configs));
328   if (configs) {
329      for (i = 0; i < gdpy->num_configs; i++)
330         configs[i] = (const struct native_config *) &gdpy->configs[i];
331      if (num_configs)
332         *num_configs = gdpy->num_configs;
333   }
334   return configs;
335}
336
337static int
338gdi_display_get_param(struct native_display *ndpy,
339                         enum native_param_type param)
340{
341   int val;
342
343   switch (param) {
344   case NATIVE_PARAM_USE_NATIVE_BUFFER:
345      /* private buffers are allocated */
346      val = FALSE;
347      break;
348   case NATIVE_PARAM_PRESERVE_BUFFER:
349   case NATIVE_PARAM_MAX_SWAP_INTERVAL:
350   default:
351      val = 0;
352      break;
353   }
354
355   return val;
356}
357
358static void
359gdi_display_destroy(struct native_display *ndpy)
360{
361   struct gdi_display *gdpy = gdi_display(ndpy);
362
363   if (gdpy->configs)
364      FREE(gdpy->configs);
365
366   ndpy_uninit(ndpy);
367
368   FREE(gdpy);
369}
370
371static struct native_display *
372gdi_create_display(HDC hDC, struct native_event_handler *event_handler,
373                   void *user_data)
374{
375   struct gdi_display *gdpy;
376   struct sw_winsys *winsys;
377
378   gdpy = CALLOC_STRUCT(gdi_display);
379   if (!gdpy)
380      return NULL;
381
382   gdpy->hDC = hDC;
383   gdpy->event_handler = event_handler;
384   gdpy->base.user_data = user_data;
385
386   winsys = gdi_create_sw_winsys();
387   if (!winsys) {
388      FREE(gdpy);
389      return NULL;
390   }
391
392   gdpy->base.screen = gdpy->event_handler->new_sw_screen(&gdpy->base, winsys);
393   if (!gdpy->base.screen) {
394      if (winsys->destroy)
395         winsys->destroy(winsys);
396      FREE(gdpy);
397      return NULL;
398   }
399
400   gdpy->base.destroy = gdi_display_destroy;
401   gdpy->base.get_param = gdi_display_get_param;
402
403   gdpy->base.get_configs = gdi_display_get_configs;
404   gdpy->base.create_window_surface = gdi_display_create_window_surface;
405
406   return &gdpy->base;
407}
408
409static struct native_event_handler *gdi_event_handler;
410
411static void
412native_set_event_handler(struct native_event_handler *event_handler)
413{
414   gdi_event_handler = event_handler;
415}
416
417static struct native_display *
418native_create_display(void *dpy, boolean use_sw, void *user_data)
419{
420   return gdi_create_display((HDC) dpy, gdi_event_handler, user_data);
421}
422
423static const struct native_platform gdi_platform = {
424   "GDI", /* name */
425   native_set_event_handler,
426   native_create_display
427};
428
429const struct native_platform *
430native_get_gdi_platform(void)
431{
432   return &gdi_platform;
433}
434