summaryrefslogtreecommitdiff
path: root/src/gallium/state_trackers/egl/gdi/native_gdi.c
blob: 1791d198d50212b6d271a4374c76fc86110066f0 (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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
/*
 * Mesa 3-D graphics library
 * Version:  7.9
 *
 * Copyright (C) 2010 LunarG Inc.
 *
 * 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, sublicense,
 * 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 above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * 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 NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS 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.
 *
 * Authors:
 *    Chia-I Wu <olv@lunarg.com>
 */

#include <windows.h>

#include "pipe/p_compiler.h"
#include "util/u_memory.h"
#include "util/u_format.h"
#include "util/u_inlines.h"
#include "gdi/gdi_sw_winsys.h"

#include "common/native_helper.h"
#include "common/native.h"

struct gdi_display {
   struct native_display base;

   HDC hDC;
   struct native_event_handler *event_handler;

   struct native_config *configs;
   int num_configs;
};

struct gdi_surface {
   struct native_surface base;

   HWND hWnd;
   enum pipe_format color_format;

   struct gdi_display *gdpy;

   unsigned int server_stamp;
   unsigned int client_stamp;

   struct resource_surface *rsurf;
};

static INLINE struct gdi_display *
gdi_display(const struct native_display *ndpy)
{
   return (struct gdi_display *) ndpy;
}

static INLINE struct gdi_surface *
gdi_surface(const struct native_surface *nsurf)
{
   return (struct gdi_surface *) nsurf;
}

/**
 * Update the geometry of the surface.  This is a slow functions.
 */
static void
gdi_surface_update_geometry(struct native_surface *nsurf)
{
   struct gdi_surface *gsurf = gdi_surface(nsurf);
   RECT rect;
   uint w, h;

   GetClientRect(gsurf->hWnd, &rect);
   w = rect.right - rect.left;
   h = rect.bottom - rect.top;

   if (resource_surface_set_size(gsurf->rsurf, w, h))
      gsurf->server_stamp++;
}

/**
 * Update the buffers of the surface.
 */
static boolean
gdi_surface_update_buffers(struct native_surface *nsurf, uint buffer_mask)
{
   struct gdi_surface *gsurf = gdi_surface(nsurf);

   if (gsurf->client_stamp != gsurf->server_stamp) {
      gdi_surface_update_geometry(&gsurf->base);
      gsurf->client_stamp = gsurf->server_stamp;
   }

   return resource_surface_add_resources(gsurf->rsurf, buffer_mask);
}

/**
 * Emulate an invalidate event.
 */
static void
gdi_surface_invalidate(struct native_surface *nsurf)
{
   struct gdi_surface *gsurf = gdi_surface(nsurf);
   struct gdi_display *gdpy = gsurf->gdpy;

   gsurf->server_stamp++;
   gdpy->event_handler->invalid_surface(&gdpy->base,
         &gsurf->base, gsurf->server_stamp);
}

static boolean
gdi_surface_flush_frontbuffer(struct native_surface *nsurf)
{
   struct gdi_surface *gsurf = gdi_surface(nsurf);
   HDC hDC;
   boolean ret;

   hDC = GetDC(gsurf->hWnd);
   ret = resource_surface_present(gsurf->rsurf,
         NATIVE_ATTACHMENT_FRONT_LEFT, (void *) hDC);
   ReleaseDC(gsurf->hWnd, hDC);

   /* force buffers to be updated in next validation call */
   gdi_surface_invalidate(&gsurf->base);

   return ret;
}

static boolean
gdi_surface_swap_buffers(struct native_surface *nsurf)
{
   struct gdi_surface *gsurf = gdi_surface(nsurf);
   HDC hDC;
   boolean ret;

   hDC = GetDC(gsurf->hWnd);
   ret = resource_surface_present(gsurf->rsurf,
         NATIVE_ATTACHMENT_BACK_LEFT, (void *) hDC);
   ReleaseDC(gsurf->hWnd, hDC);

   resource_surface_swap_buffers(gsurf->rsurf,
         NATIVE_ATTACHMENT_FRONT_LEFT, NATIVE_ATTACHMENT_BACK_LEFT, TRUE);
   /* the front/back buffers have been swapped */
   gdi_surface_invalidate(&gsurf->base);

   return ret;
}

static boolean
gdi_surface_validate(struct native_surface *nsurf, uint attachment_mask,
                        unsigned int *seq_num, struct pipe_resource **textures,
                        int *width, int *height)
{
   struct gdi_surface *gsurf = gdi_surface(nsurf);
   uint w, h;

   if (!gdi_surface_update_buffers(&gsurf->base, attachment_mask))
      return FALSE;

   if (seq_num)
      *seq_num = gsurf->client_stamp;

   if (textures)
      resource_surface_get_resources(gsurf->rsurf, textures, attachment_mask);

   resource_surface_get_size(gsurf->rsurf, &w, &h);
   if (width)
      *width = w;
   if (height)
      *height = h;

   return TRUE;
}

static void
gdi_surface_wait(struct native_surface *nsurf)
{
   /* no-op */
}

static void
gdi_surface_destroy(struct native_surface *nsurf)
{
   struct gdi_surface *gsurf = gdi_surface(nsurf);

   resource_surface_destroy(gsurf->rsurf);
   FREE(gsurf);
}

static struct native_surface *
gdi_display_create_window_surface(struct native_display *ndpy,
                                  EGLNativeWindowType win,
                                  const struct native_config *nconf)
{
   struct gdi_display *gdpy = gdi_display(ndpy);
   struct gdi_surface *gsurf;

   gsurf = CALLOC_STRUCT(gdi_surface);
   if (!gsurf)
      return NULL;

   gsurf->gdpy = gdpy;
   gsurf->color_format = nconf->color_format;
   gsurf->hWnd = (HWND) win;

   gsurf->rsurf = resource_surface_create(gdpy->base.screen,
         gsurf->color_format,
         PIPE_BIND_RENDER_TARGET |
         PIPE_BIND_SAMPLER_VIEW |
         PIPE_BIND_DISPLAY_TARGET |
         PIPE_BIND_SCANOUT);
   if (!gsurf->rsurf) {
      FREE(gsurf);
      return NULL;
   }

   /* initialize the geometry */
   gdi_surface_update_geometry(&gsurf->base);

   gsurf->base.destroy = gdi_surface_destroy;
   gsurf->base.swap_buffers = gdi_surface_swap_buffers;
   gsurf->base.flush_frontbuffer = gdi_surface_flush_frontbuffer;
   gsurf->base.validate = gdi_surface_validate;
   gsurf->base.wait = gdi_surface_wait;

   return &gsurf->base;
}

static int
fill_color_formats(struct native_display *ndpy, enum pipe_format formats[8])
{
   struct pipe_screen *screen = ndpy->screen;
   int i, count = 0;

   enum pipe_format candidates[] = {
      /* 32-bit */
      PIPE_FORMAT_B8G8R8A8_UNORM,
      PIPE_FORMAT_A8R8G8B8_UNORM,
      /* 24-bit */
      PIPE_FORMAT_B8G8R8X8_UNORM,
      PIPE_FORMAT_X8R8G8B8_UNORM,
      /* 16-bit */
      PIPE_FORMAT_B5G6R5_UNORM
   };

   assert(Elements(candidates) <= 8);

   for (i = 0; i < Elements(candidates); i++) {
      if (screen->is_format_supported(screen, candidates[i],
               PIPE_TEXTURE_2D, 0, PIPE_BIND_RENDER_TARGET, 0))
         formats[count++] = candidates[i];
   }

   return count;
}

static const struct native_config **
gdi_display_get_configs(struct native_display *ndpy, int *num_configs)
{
   struct gdi_display *gdpy = gdi_display(ndpy);
   const struct native_config **configs;
   int i;

   /* first time */
   if (!gdpy->configs) {
      enum pipe_format formats[8];
      int i, count;

      count = fill_color_formats(&gdpy->base, formats);

      gdpy->configs = CALLOC(count, sizeof(*gdpy->configs));
      if (!gdpy->configs)
         return NULL;

      for (i = 0; i < count; i++) {
         struct native_config *nconf = &gdpy->configs[i];

         nconf->buffer_mask =
            (1 << NATIVE_ATTACHMENT_FRONT_LEFT) |
            (1 << NATIVE_ATTACHMENT_BACK_LEFT);
         nconf->color_format = formats[i];

         nconf->window_bit = TRUE;
         nconf->slow_config = TRUE;
      }

      gdpy->num_configs = count;
   }

   configs = MALLOC(gdpy->num_configs * sizeof(*configs));
   if (configs) {
      for (i = 0; i < gdpy->num_configs; i++)
         configs[i] = (const struct native_config *) &gdpy->configs[i];
      if (num_configs)
         *num_configs = gdpy->num_configs;
   }
   return configs;
}

static int
gdi_display_get_param(struct native_display *ndpy,
                         enum native_param_type param)
{
   int val;

   switch (param) {
   case NATIVE_PARAM_USE_NATIVE_BUFFER:
      /* private buffers are allocated */
      val = FALSE;
      break;
   default:
      val = 0;
      break;
   }

   return val;
}

static void
gdi_display_destroy(struct native_display *ndpy)
{
   struct gdi_display *gdpy = gdi_display(ndpy);

   if (gdpy->configs)
      FREE(gdpy->configs);

   gdpy->base.screen->destroy(gdpy->base.screen);

   FREE(gdpy);
}

static struct native_display *
gdi_create_display(HDC hDC, struct pipe_screen *screen,
                   struct native_event_handler *event_handler)
{
   struct gdi_display *gdpy;

   gdpy = CALLOC_STRUCT(gdi_display);
   if (!gdpy)
      return NULL;

   gdpy->hDC = hDC;
   gdpy->event_handler = event_handler;

   gdpy->base.screen = screen;

   gdpy->base.destroy = gdi_display_destroy;
   gdpy->base.get_param = gdi_display_get_param;

   gdpy->base.get_configs = gdi_display_get_configs;
   gdpy->base.create_window_surface = gdi_display_create_window_surface;

   return &gdpy->base;
}

struct native_probe *
native_create_probe(EGLNativeDisplayType dpy)
{
   return NULL;
}

enum native_probe_result
native_get_probe_result(struct native_probe *nprobe)
{
   return NATIVE_PROBE_UNKNOWN;
}

const char *
native_get_name(void)
{
   return "GDI";
}

struct native_display *
native_create_display(EGLNativeDisplayType dpy,
                      struct native_event_handler *event_handler)
{
   struct sw_winsys *winsys;
   struct pipe_screen *screen;

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

   screen = native_create_sw_screen(winsys);
   if (!screen) {
      if (winsys->destroy)
         winsys->destroy(winsys);
      return NULL;
   }

   return gdi_create_display((HDC) dpy, screen, event_handler);
}